import { Injectable } from '@angular/core'; import { IdService } from '@farris/ui-common'; import { Subject } from 'rxjs'; import { PersonalConfig } from './lookup-grid-options'; @Injectable() export class PersonalConfigService { // 个性化配置 personalConf: PersonalConfig; selectItemObser$ = new Subject(); displayType = 'LIST'; singleSelect = true; idField: string; textField: string; mapFields: any; // 个性化配置KEY private _key = ''; _newKey = ''; get personalConfigKey() { return this._key; } set personalConfigKey(val) { if (val) { this._key = this.buildKey(val); if (this.controlID) { this._newKey = this.buildKey(this.controlID + '_' + val); } else { this._newKey = this._key; } } else { this._newKey = this.buildKey(this.controlID); } } // 组件ID controlID = ''; constructor(private idService: IdService) {} private buildKey(str) { let prefix = ''; if (location.hash) { const pathArr = location.hash.split('?'); prefix = pathArr ? pathArr[0] : ''; } else { const pathArr = location.pathname.split('/'); prefix = pathArr ? pathArr[pathArr.length - 1] : ''; } return this.idService.encrypt(prefix + str); } initPersonalConf(obj) { Object.assign(this, obj); } getPersonalData(key?: string): PersonalConfig { if (key) { this._key = key; } if (this.personalConfigKey) { const conf = localStorage.getItem(this.personalConfigKey); if (conf && conf !== 'undefined' && conf !== 'null') { this.personalConf = conf ? JSON.parse(conf) : {}; this._updatePersonalConfig(this.personalConf); if (this.controlID) { if (this._key !== this._newKey) { localStorage.removeItem(this._key); } this.savePersonalConfig(this.personalConf); } return this.personalConf; } else { return null; } } return null; } getQuickSelected(localeId?: string): any { const d = this.getPersonalData(); const qs = d ? d.quickSelected : null; if (localeId) { if (qs) { const items = qs[localeId]; if (items && items.length) { return items; } } return null; } return qs; } getDialogSize() { const d = this.getPersonalData(); return d ? d.size : null; } updatePersonalConfig(cfg: Partial) { const data = Object.assign(this.personalConf || {}, cfg); this.savePersonalConfig(data); } savePersonalConfig(data: PersonalConfig) { if (this._newKey) { localStorage.setItem(this._newKey, JSON.stringify(data)); this.personalConf = data; return true; } return false; } getActiveTabIndex(tabName?: string) { const d = this.getPersonalData(); if (!tabName) { return d && d.tabIndex ? d.tabIndex : 'datalist'; } return tabName; } updateQueckSelected(selectedRow: any, localeId: string) { const quickItems = this.getQuickSelected(localeId); if (quickItems && quickItems.length) { const selectedIndex = []; quickItems.forEach((element, index) => { if (this.singleSelect) { if (element && selectedRow && element[this.idField] === selectedRow[this.idField]) { selectedIndex.push(index); } } else { if (selectedRow) { selectedRow.forEach(item => { if (element && element[this.idField] === item[this.idField]) { selectedIndex.push(index); } }); } } }); selectedIndex.forEach(index => { quickItems[index] = null; }); this.personalConf.quickSelected[localeId] = this.personalConf.quickSelected[localeId].filter(v => v !== null); if (this.singleSelect) { this.personalConf.quickSelected[localeId].unshift(selectedRow); } else { if (selectedRow) { selectedRow.forEach(element => { this.personalConf.quickSelected[localeId].unshift(element); }); } } this.personalConf.quickSelected[localeId].length = this.personalConf.quickSelected[localeId].length > 5 ? 5 : this.personalConf.quickSelected[localeId].length; } else { const _qs = this.personalConf.quickSelected || {}; let newData; if (this.singleSelect) { newData = Object.assign(_qs, { [localeId]: [selectedRow] }); } else { selectedRow.length = selectedRow.length > 5 ? 5 : selectedRow.length; newData = Object.assign(_qs, { [localeId]: selectedRow }); } this.personalConf.quickSelected = newData; } this.savePersonalConfig(this.personalConf); } /** * 更新数据结构,现有个性化数据均转为 中文环境下数据; */ _updatePersonalConfig(per: PersonalConfig) { if (per) { let flag = false; // 更新收藏数据 if (per.favorite && Array.isArray(per.favorite)) { per.favorite = { 'zh-CHS': [...per.favorite] }; delete per.favorite; flag = true; } // 更新快捷录入数据 if (per.selected) { if (Array.isArray(per.selected)) { per.quickSelected = { 'zh-CHS': [...per.selected] }; } delete per.selected; flag = true; } if (flag) { this.savePersonalConfig(per); } } } }