Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 19 | import {IXosModelRelation} from './crud'; |
| 20 | import {IXosModelStoreService} from '../../datasources/stores/model.store'; |
| 21 | import {IXosModelDiscovererService} from '../../datasources/helpers/model-discoverer.service'; |
| 22 | import * as _ from 'lodash'; |
| 23 | import {IXosFormCfg} from '../../core/form/form'; |
| 24 | import {IXosTableCfg} from '../../core/table/table'; |
| 25 | import {IXosConfigHelpersService} from '../../core/services/helpers/config.helpers'; |
| 26 | |
| 27 | interface IXosCrudRelationBaseTabData { |
| 28 | model: any; |
| 29 | class?: 'full' | 'empty'; |
| 30 | } |
| 31 | |
| 32 | export interface IXosCrudRelationFormTabData extends IXosCrudRelationBaseTabData { |
| 33 | formConfig: IXosFormCfg; |
| 34 | } |
| 35 | |
| 36 | export interface IXosCrudRelationTableTabData extends IXosCrudRelationBaseTabData { |
| 37 | tableConfig: IXosTableCfg; |
| 38 | } |
| 39 | |
| 40 | export interface IXosCrudRelationService { |
| 41 | getModel(r: IXosModelRelation, id: string | number): Promise<IXosCrudRelationFormTabData>; |
| 42 | getModels(r: IXosModelRelation, source_id: string | number): Promise<IXosCrudRelationTableTabData>; |
| 43 | existsRelatedItem(r: IXosModelRelation, item: any): boolean; |
| 44 | getHumanReadableOnField(r: IXosModelRelation, baseModel: string): string; |
| 45 | } |
| 46 | |
| 47 | export class XosCrudRelationService implements IXosCrudRelationService { |
| 48 | |
| 49 | static $inject = [ |
| 50 | '$log', |
| 51 | '$q', |
| 52 | 'XosModelStore', |
| 53 | 'XosModelDiscoverer', |
| 54 | 'ConfigHelpers' |
| 55 | ]; |
| 56 | |
| 57 | constructor ( |
| 58 | private $log: ng.ILogService, |
| 59 | private $q: ng.IQService, |
| 60 | private XosModelStore: IXosModelStoreService, |
| 61 | private XosModelDiscovererService: IXosModelDiscovererService, |
| 62 | private ConfigHelpers: IXosConfigHelpersService |
| 63 | ) {} |
| 64 | |
| 65 | public getModel (r: IXosModelRelation, id: string | number): Promise<IXosCrudRelationFormTabData> { |
| 66 | const d = this.$q.defer(); |
| 67 | this.XosModelStore.get(r.model, id) |
| 68 | .subscribe( |
| 69 | item => { |
| 70 | this.$log.debug(`[XosCrud] Loaded manytoone relation with ${r.model} on ${r.on_field}`, item); |
| 71 | |
| 72 | const data: IXosCrudRelationFormTabData = { |
| 73 | model: item, |
| 74 | formConfig: this.XosModelDiscovererService.get(r.model).formCfg, |
| 75 | class: angular ? 'full' : 'empty' |
| 76 | }; |
| 77 | |
| 78 | d.resolve(data); |
| 79 | }, |
| 80 | err => d.reject |
| 81 | ); |
| 82 | return d.promise; |
| 83 | }; |
| 84 | |
| 85 | public getModels(r: IXosModelRelation, source_id: string | number): Promise<IXosCrudRelationTableTabData> { |
| 86 | const d = this.$q.defer(); |
| 87 | |
| 88 | this.XosModelStore.query(r.model) |
| 89 | .subscribe( |
| 90 | items => { |
| 91 | this.$log.debug(`[XosCrud] Loaded onetomany relation with ${r.model} on ${r.on_field}`, items); |
| 92 | // building the filter parameters |
| 93 | const match = {}; |
| 94 | match[`${r.on_field.toLowerCase()}_id`] = source_id; |
| 95 | const filtered = _.filter(items, match); |
| 96 | // removing search bar from table |
| 97 | const tableCfg = this.XosModelDiscovererService.get(r.model).tableCfg; |
| 98 | tableCfg.filter = null; |
| 99 | |
| 100 | const data: IXosCrudRelationTableTabData = { |
| 101 | model: filtered, |
| 102 | tableConfig: tableCfg, |
| 103 | class: filtered.length > 0 ? 'full' : 'empty' |
| 104 | }; |
| 105 | |
| 106 | d.resolve(data); |
| 107 | }, |
| 108 | err => d.reject |
| 109 | ); |
| 110 | |
| 111 | return d.promise; |
| 112 | } |
| 113 | |
| 114 | public existsRelatedItem(r: IXosModelRelation, item: any): boolean { |
| 115 | return item && angular.isDefined(item[`${r.on_field.toLowerCase()}_id`]); |
| 116 | } |
| 117 | |
| 118 | public getHumanReadableOnField(r: IXosModelRelation, baseModel: string): string { |
| 119 | if (r.on_field.toLowerCase() === baseModel.toLowerCase()) { |
| 120 | return ''; |
| 121 | } |
| 122 | if (r.model.toLowerCase() === r.on_field.toLowerCase()) { |
| 123 | return ''; |
| 124 | } |
| 125 | return `[${this.ConfigHelpers.toLabel(r.on_field, false)}]`; |
| 126 | } |
| 127 | } |