From 9e8acecadc4d617d0291ac264b278bdfc5f27bb9 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Mon, 4 Dec 2017 23:37:24 -0500 Subject: [PATCH] LP#626157 Ang2 experiments Signed-off-by: Bill Erickson --- .../src/app/share/catalog/catalog-url.service.ts | 98 ++++++++++++++++++++++ .../src/app/share/catalog/catalog.service.ts | 32 +++---- .../src/app/share/catalog/search-context.ts | 19 ----- .../src/app/staff/catalog/catalog.component.ts | 21 +++-- .../src/app/staff/catalog/catalog.module.ts | 7 +- .../app/staff/catalog/result/results.component.ts | 2 +- .../src/app/staff/catalog/search-form.component.ts | 12 +-- 7 files changed, 137 insertions(+), 54 deletions(-) create mode 100644 Open-ILS/webby-src/src/app/share/catalog/catalog-url.service.ts diff --git a/Open-ILS/webby-src/src/app/share/catalog/catalog-url.service.ts b/Open-ILS/webby-src/src/app/share/catalog/catalog-url.service.ts new file mode 100644 index 0000000000..97741621ea --- /dev/null +++ b/Open-ILS/webby-src/src/app/share/catalog/catalog-url.service.ts @@ -0,0 +1,98 @@ +import {Injectable} from '@angular/core'; +import {Params} from '@angular/router'; +import {CatalogSearchContext} from './search-context'; + +@Injectable() +export class EgCatalogUrlService { + + // consider supporting a param name prefix/namespace + + constructor() { } + + /** + * Returns a URL query structure suitable for using with + * router.navigate(..., {queryParams:...}). + * No navigation is performed within. + */ + toUrlParams(context: CatalogSearchContext): any { + let params = {}; + + params.query = []; + params.searchClass = []; + params.joiner = []; + params.match = []; + + context.query.forEach((q, idx) => { + ['query', 'searchClass','joiner','match'].forEach(field => { + params[field][idx] = context[field][idx]; + }); + }); + + return params; + } + + /** + * Creates a new search context from the active route params. + */ + fromUrlParams(params: Params): CatalogSearchContext { + let context = new CatalogSearchContext(); + + this.applyUrlParams(context, params); + + return context; + } + + applyUrlParams(context: CatalogSearchContext, params: Params): void { + + // These fields can be copied directly into place + ['limit','offset','format','sort','available','global'] + .forEach(field => { + if (params[field] != undefined) { + context[field] = params[field]; + } + }); + + if (typeof params.query == 'string') { + // Only one query set is encoded in the URL + context.query = [params.query]; + if (params.searchClass) context.searchClass = [params.searchClass]; + if (params.joiner) context.joiner = [params.joiner]; + if (params.match) context.match = [params.match]; + + } else if (Array.isArray(params.query)) { + // Multiple query sets encoded in the URL. + params.query.forEach((q, idx) => { + context.query[idx] = params.query[idx]; + context.searchClass[idx] = params.searchClass[idx]; + context.joiner[idx] = params.joiner[idx]; + context.match[idx] = params.match[idx]; + }); + } + + /* + + // Decode and propagate array-based values + angular.forEach(scs.ccvm_list_keys, function(field) { + if (url_search[field]) { + ctx.search_args[field] = url_search[field].split(/,/); + } + }); + + // if there's only a single value it will be a string. + if (typeof url_search.facet == 'string') + url_search.facet = [url_search.facet]; + angular.forEach(url_search.facet, function(facet) { + console.log('parsing: ' + facet); + ctx.search_args.facets.push(JSON.parse(facet)); + }); + + // Handle special-case values + if (url_search.org) + ctx.search_args.context_org = egCore.org.get(url_search.org); + + */ + + } + + +} diff --git a/Open-ILS/webby-src/src/app/share/catalog/catalog.service.ts b/Open-ILS/webby-src/src/app/share/catalog/catalog.service.ts index 9e7adac326..40dc7cac15 100644 --- a/Open-ILS/webby-src/src/app/share/catalog/catalog.service.ts +++ b/Open-ILS/webby-src/src/app/share/catalog/catalog.service.ts @@ -21,7 +21,9 @@ const CCVM_FILTER_TYPES = [ @Injectable() export class EgCatalogService { + // Search context is set by the application. searchContext: CatalogSearchContext; + ccvmMap: {[ccvm:string] : EgIdlObject[]} = {}; cmfMap: {[cmf:string] : EgIdlObject[]} = {}; @@ -30,14 +32,12 @@ export class EgCatalogService { private org: EgOrgService, private unapi: EgUnapiService, private pcrud: EgPcrudService - ) { - this.searchContext = new CatalogSearchContext(); - this.searchContext.org = this.org; - } + ) {} - search(context: CatalogSearchContext): Promise { + search(): Promise { + let ctx = this.searchContext; - var fullQuery = context.compileSearch(); + var fullQuery = ctx.compileSearch(); console.debug(`search query: ${fullQuery}`); @@ -46,24 +46,24 @@ export class EgCatalogService { this.net.request( 'open-ils.search', 'open-ils.search.biblio.multiclass.query.staff', { - limit : context.pager.limit, - offset : context.pager.offset + limit : ctx.pager.limit, + offset : ctx.pager.offset }, fullQuery, true ).subscribe(result => { - context.result = result; - context.result.records = []; - context.pager.resultCount = result.count; + ctx.result = result; + ctx.result.records = []; + ctx.pager.resultCount = result.count; let promises = []; result.ids.forEach(blob => { promises.push( this.getBibSummary(blob[0], - context.searchOrg.id(), - context.global ? - context.org.root().ou_typ().depth() : - context.searchOrg.ou_type().depth() + ctx.searchOrg.id(), + ctx.global ? + ctx.org.root().ou_typ().depth() : + ctx.searchOrg.ou_type().depth() ).then( - summary => context.result.records.push(summary) + summary => ctx.result.records.push(summary) ) ); }); diff --git a/Open-ILS/webby-src/src/app/share/catalog/search-context.ts b/Open-ILS/webby-src/src/app/share/catalog/search-context.ts index 1ac63a39d0..896ab3345a 100644 --- a/Open-ILS/webby-src/src/app/share/catalog/search-context.ts +++ b/Open-ILS/webby-src/src/app/share/catalog/search-context.ts @@ -149,25 +149,6 @@ export class CatalogSearchContext { return str + query + ')'; } - - /** - * Returns a URL query structure suitable for using with - * router.navigate(..., {queryParams:...}). - * No navigation is performed within. - */ - toUrlParams(): any { - let query = {query : ['piano']}; // TODO - - return query; - } - - /** - * Absorbs URL values from the active route and applies them to - * this search context instance. - */ - fromUrlParams(params: Params): void { - console.log('checking params with query ' + params.query); - } } diff --git a/Open-ILS/webby-src/src/app/staff/catalog/catalog.component.ts b/Open-ILS/webby-src/src/app/staff/catalog/catalog.component.ts index 67aa0723bd..f842350913 100644 --- a/Open-ILS/webby-src/src/app/staff/catalog/catalog.component.ts +++ b/Open-ILS/webby-src/src/app/staff/catalog/catalog.component.ts @@ -1,31 +1,34 @@ import {Component, OnInit} from '@angular/core'; -import {Observable} from 'rxjs/Rx'; import {ActivatedRoute} from '@angular/router'; +import {EgOrgService} from '@eg/core/org'; import {EgCatalogService} from '@eg/share/catalog/catalog.service'; import {CatalogSearchContext} from '@eg/share/catalog/search-context'; +import {EgCatalogUrlService} from '@eg/share/catalog/catalog-url.service'; @Component({ templateUrl: 'catalog.component.html' }) export class EgCatalogComponent implements OnInit { + // Main search context that lives for the duration of the catalog. searchContext: CatalogSearchContext; constructor( private route: ActivatedRoute, - private cat: EgCatalogService + private org: EgOrgService, + private cat: EgCatalogService, + private catUrl: EgCatalogUrlService ) {} ngOnInit() { - // TODO: MOVE THIS OUT OF THE CAT SERVICE - // and pass it down to each child component that needs - // it via @Input. - this.searchContext = this.cat.searchContext; + // Initialize the search context from the load-time URL params. + this.cat.searchContext = this.searchContext = + this.catUrl.fromUrlParams(this.route.snapshot.queryParams); - // Sync the context with the load-time URL params. - this.searchContext.fromUrlParams(this.route.snapshot.queryParams); - } + this.searchContext.org = this.org; // offer a copy of the service. + console.debug(this.searchContext); + } } diff --git a/Open-ILS/webby-src/src/app/staff/catalog/catalog.module.ts b/Open-ILS/webby-src/src/app/staff/catalog/catalog.module.ts index 347a277c27..8986b3ee37 100644 --- a/Open-ILS/webby-src/src/app/staff/catalog/catalog.module.ts +++ b/Open-ILS/webby-src/src/app/staff/catalog/catalog.module.ts @@ -4,13 +4,13 @@ import {EgStaffModule} from '../staff.module'; import {EgUnapiService} from '@eg/share/unapi'; import {EgCatalogRoutingModule} from './routing.module'; import {EgCatalogService} from '@eg/share/catalog/catalog.service'; +import {EgCatalogUrlService} from '@eg/share/catalog/catalog-url.service'; import {EgCatalogComponent} from './catalog.component'; import {SearchFormComponent} from './search-form.component'; import {ResultsComponent} from './result/results.component'; import {ResultPaginationComponent} from './result/pagination.component'; import {ResultFacetsComponent} from './result/facets.component'; -import {ResultRecordComponent} - from './result/record.component'; +import {ResultRecordComponent} from './result/record.component'; @NgModule({ declarations: [ @@ -28,7 +28,8 @@ import {ResultRecordComponent} ], providers: [ EgUnapiService, - EgCatalogService + EgCatalogService, + EgCatalogUrlService ] }) diff --git a/Open-ILS/webby-src/src/app/staff/catalog/result/results.component.ts b/Open-ILS/webby-src/src/app/staff/catalog/result/results.component.ts index 2c5db57a1f..7e6bc66c8c 100644 --- a/Open-ILS/webby-src/src/app/staff/catalog/result/results.component.ts +++ b/Open-ILS/webby-src/src/app/staff/catalog/result/results.component.ts @@ -31,7 +31,7 @@ export class ResultsComponent implements OnInit { if (!this.searchContext.query[0]) return; - this.cat.search(this.searchContext).then(ok => { + this.cat.search().then(ok => { console.debug('search complete'); }); diff --git a/Open-ILS/webby-src/src/app/staff/catalog/search-form.component.ts b/Open-ILS/webby-src/src/app/staff/catalog/search-form.component.ts index 53b2c96baa..95643d4a67 100644 --- a/Open-ILS/webby-src/src/app/staff/catalog/search-form.component.ts +++ b/Open-ILS/webby-src/src/app/staff/catalog/search-form.component.ts @@ -4,6 +4,7 @@ import {EgIdlObject} from '@eg/core/idl'; import {EgOrgService} from '@eg/core/org'; import {EgCatalogService} from '@eg/share/catalog/catalog.service'; import {CatalogSearchContext} from '@eg/share/catalog/search-context'; +import {EgCatalogUrlService} from '@eg/share/catalog/catalog-url.service'; @Component({ selector: 'eg-catalog-search-form', @@ -12,16 +13,17 @@ import {CatalogSearchContext} from '@eg/share/catalog/search-context'; }) export class SearchFormComponent implements OnInit { + searchContext: CatalogSearchContext; ccvmMap: {[ccvm:string] : EgIdlObject[]} = {}; cmfMap: {[cmf:string] : EgIdlObject[]} = {}; - searchContext: CatalogSearchContext; showAdvancedSearch: boolean = false; constructor( private router: Router, private route: ActivatedRoute, private org: EgOrgService, - private cat: EgCatalogService + private cat: EgCatalogService, + private catUrl: EgCatalogUrlService ) {} ngOnInit() { @@ -63,12 +65,10 @@ export class SearchFormComponent implements OnInit { */ searchByForm(): void { this.searchContext.searchOrg = this.org.get(4); // TODO: TEST BR1 + let params = this.catUrl.toUrlParams(this.searchContext); this.router.navigate( - ['/staff/catalog/search'], - {queryParams: this.searchContext.toUrlParams()} - ) + ['/staff/catalog/search'], {queryParams: params}); } - } -- 2.11.0