navdata-service.ts 9.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
export interface NavDataElement {

    /**
     * 主键标识
     *
     * @memberof NavDataElement
     */
    id: string;

    /**
     * 视图标题
     *
     * @memberof NavDataElement
     */
    title: any;

    /**
     * 上下文
     *
     * @memberof NavDataElement
     */
    context:any;

    /**
     * 视图参数
     *
     * @memberof NavDataElement
     */
    viewparams:any;

    /**
     * 视图数据
     *
     * @memberof NavDataElement
     */   
    data:any;

    /**
     * 视图路径
     *
     * @memberof NavDataElement
     */ 
    path:string;

    /**
     * 元素主键
     *
     * @memberof NavDataElement
     */ 
    srfkey:string|null;

    /**
     * 视图标识
     *
     * @memberof NavDataElement
     */ 
    tag:string|null;

}

export interface ServiceState {

    /**
     * 行为
     *
     * @memberof ServiceState
     */
    action: string;

    /**
     * 名称
     *
     * @memberof ServiceState
     */
    name: any;

    /**
     * 数据
     *
     * @memberof ServiceState
     */
    data: any;

}

import { Subject } from 'rxjs';

/**
 * 导航数据服务
 * 
 * @export
 * @class CodeListService
 */
export default class NavDataService {

    /**
     * 单例变量声明
     *
     * @private
     * @static
     * @type {NavDataService}
     * @memberof NavDataService
     */
    private static navDataService: NavDataService;

    /**
     * 缓存对象
     *
     * @private
     * @type {(any)}
     * @memberof NavDataService
     */
    private static store: any;

    /**
     * 服务状态
     * 
     * @memberof NavDataService
     */
    public serviceState: Subject<ServiceState> = new Subject();

    /**
     * 导航数据栈
     * 
     * @memberof NavDataService
     */
    public navDataStack:Array<NavDataElement> = [];

    /**
     * 导航数据存储仓库
     * 
     * @memberof NavDataService
     */
    public sessionStore:any;

    /**
     * 初始化实例
     * 
     * @memberof NavDataService
     */
    constructor(opts:any = {}){
        this.sessionStore = window.sessionStorage;
        this.initNavData();
    }

    /**
     * 获取 NavDataService 单例对象
     *
     * @static
     * @returns {NavDataService}
     * @memberof NavDataService
     */
    public static getInstance(store: any): NavDataService {
        if (!NavDataService.navDataService) {
            NavDataService.navDataService = new NavDataService();
        }
        this.store = store;
        return this.navDataService;
    }

    /**
     * 初始化基础导航数据
     * 
     * @memberof NavDataService
     */
    public initNavData(){
        if(this.sessionStore && this.sessionStore.getItem('srfnavdata')){
            this.navDataStack = JSON.parse(this.sessionStore.getItem('srfnavdata'));
        }
    }

    /**
     * 添加基础导航数据到栈中
     * 
     * @memberof NavDataService
     */
    public addNavData(curNavData:NavDataElement){
        if(this.navDataStack.length >0){
            let tempIndex:number = this.navDataStack.findIndex((element:NavDataElement) =>{
                return Object.is(element.id,curNavData.id);
            })
            if(tempIndex === -1){
                this.navDataStack.push(curNavData);
                this.sessionStore.setItem('srfnavdata',JSON.stringify(this.navDataStack));
                return curNavData;
            }else{
                this.setNavDataById(curNavData.id,curNavData);
            }
        }else{
            this.navDataStack.push(curNavData);
            this.sessionStore.setItem('srfnavdata',JSON.stringify(this.navDataStack));
            return curNavData;
        }
        this.serviceState.next({action:'datarefresh',name:null,data:this.navDataStack});
    }

    /**
     * 设置指定数据到基础导航数据栈中
     * 
     * @memberof NavDataService
     */
    public setNavDataById(id:string,curNavData:NavDataElement){
        if(this.navDataStack.length >0){
            let tempIndex:number = this.navDataStack.findIndex((element:NavDataElement) =>{
                return Object.is(element.id,id);
            })
            this.navDataStack[tempIndex] = curNavData;
            this.sessionStore.setItem('srfnavdata',JSON.stringify(this.navDataStack));
            return curNavData;
        }else{
            return null;
        }
    }

    /**
     * 从导航数据栈中删除指定数据上层数据
     * 
     * @memberof NavDataService
     */
    public removeNavData(id:string){
        if(this.navDataStack.length >0){
            let tempIndex:number = this.navDataStack.findIndex((element:NavDataElement) =>{
                return Object.is(element.id,id);
            })
            let removeNavData = this.navDataStack.splice(tempIndex+1);
            if(removeNavData && removeNavData.length >0){
                removeNavData.forEach((navData:any) =>{
                    NavDataService.store.commit("deletePage", navData.path);
                })
            }
            this.sessionStore.setItem('srfnavdata',JSON.stringify(this.navDataStack));
            return removeNavData;
        }else{
            return null;
        }
    }

    /**
     * 从导航数据栈中删除指定数据上层数据(不清除页面缓存)
     * 
     * @memberof NavDataService
     */
    public removeNavDataWithoutCache(id:string){
        if(this.navDataStack.length >0){
            let tempIndex:number = this.navDataStack.findIndex((element:NavDataElement) =>{
                return Object.is(element.id,id);
            })
            let removeNavData = this.navDataStack.splice(tempIndex+1);
            this.sessionStore.setItem('srfnavdata',JSON.stringify(this.navDataStack));
            return removeNavData;
        }else{
            return null;
        }
    }

    /**
     * 从导航数据栈中获取指定数据
     * 
     * @memberof NavDataService
     */
    public getNavDataById(id:string){
        if(this.navDataStack.length >0){
            let tempIndex:number = this.navDataStack.findIndex((element:NavDataElement) =>{
                return Object.is(element.id,id);
            })
            return this.navDataStack[tempIndex];
        }else{
            return null;
        }
    }

    /**
     * 从导航数据栈中获取指定数据的前一条数据
     * 
     * @memberof NavDataService
     */
    public getPreNavDataById(id:string){
        if(this.navDataStack.length >0){
            let tempIndex:number = this.navDataStack.findIndex((element:NavDataElement) =>{
                return Object.is(element.id,id);
            })
            return this.navDataStack[tempIndex-1];
        }else{
            return null;
        }
    }

    /**
     * 清空导航数据栈数据
     * 
     * @memberof NavDataService
     */
    public clearNavData(id:string){
        this.navDataStack = [];
        this.sessionStore.setItem('srfnavdata',JSON.stringify(this.navDataStack));
    }

    /**
     * 从导航数据栈中获取数据
     * 
     * @memberof NavDataService
     */
    public getNavData(){
        if(this.navDataStack && this.navDataStack.length >0){
            this.navDataStack.forEach((item:any,index) =>{
                if(item && item.data && this.navDataStack[index+1]){
                    this.navDataStack[index+1] = Object.assign(this.navDataStack[index+1],{isselected:true});
                }
            })
            return this.navDataStack;
        }else{
            return [];
        }
    }

    /**
     * 从导航数据栈中直接添加数据
     * 
     * @memberof NavDataService
     */    
    public addNavDataByOnly(curNavData:NavDataElement,isOnlyAdd:boolean){
        if(isOnlyAdd){
            this.navDataStack.push(curNavData);
        }else{
            if(this.navDataStack.length >0){
                let tempIndex:number = this.navDataStack.findIndex((element:NavDataElement) =>{
                    return Object.is(element.tag,curNavData.tag);
                })
                if(tempIndex === -1){
                    this.navDataStack.push(curNavData);
                }else{
                    this.navDataStack[tempIndex] = curNavData;
                }
            }else{
                this.navDataStack.push(curNavData);
            }
        }
        this.sessionStore.setItem('srfnavdata',JSON.stringify(this.navDataStack));
        return curNavData;
    }

    /**
     * 从导航数据栈中直接删除数据
     * 
     * @memberof NavDataService
     */    
    public removeNavDataByTag(viewtag:string){
        if(this.navDataStack.length >0){
            let tempIndex:number = this.navDataStack.findIndex((element:NavDataElement) =>{
                return Object.is(element.tag,viewtag);
            })
            let removeNavData = this.navDataStack.splice(tempIndex,1);
            this.sessionStore.setItem('srfnavdata',JSON.stringify(this.navDataStack));
            return removeNavData;
        }else{
            return null;
        }
    }

    /**
     * 从导航数据栈中获取指定元素前一个元素
     * 
     * @memberof NavDataService
     */    
    public getPreNavDataByTag(viewtag:string){
        if(this.navDataStack.length >0){
            let tempIndex:number = this.navDataStack.findIndex((element:NavDataElement) =>{
                return Object.is(element.tag,viewtag);
            })
            return this.navDataStack[tempIndex - 1]?this.navDataStack[tempIndex - 1]:null;
        }else{
            return null;
        }
    }

    /**
     * 从导航数据栈中删除所有数据
     * 
     * @memberof NavDataService
     */    
    public removeAllNavData(){
        if(this.navDataStack.length >0){
            this.navDataStack = [];
            this.sessionStore.setItem('srfnavdata',JSON.stringify(this.navDataStack));
        }
    }
}