import { Injectable } from '@angular/core' ; import * as _ from 'lodash' ; import { TreeviewComponent } from './treeview.component' ; import { TreeviewItem } from './treeview-item' ; @ Injectable ( ) export abstract class TreeviewEventParser { abstract getSelectedChange ( component ) ; } @ Injectable ( ) export class DefaultTreeviewEventParser extends TreeviewEventParser { getSelectedChange ( component ) { const checkedItems = component . checkedItems ; if ( ! _ . isNil ( checkedItems ) ) { return checkedItems . map ( item => item . value ) ; } return [ ] ; } } export interface DownlineTreeviewItem { item : TreeviewItem ; parent : DownlineTreeviewItem ; } @ Injectable ( ) export class DownlineTreeviewEventParser extends TreeviewEventParser { getSelectedChange ( component ) { const items = component . items ; if ( ! _ . isNil ( items ) ) { let result = [ ] ; items . forEach ( item => { const links = this . getLinks ( item , null ) ; if ( ! _ . isNil ( links ) ) { result = result . concat ( links ) ; } } ) ; return result ; } return [ ] ; } getLinks ( item , parent ) { if ( ! _ . isNil ( item . children ) ) { const link = { item : item , parent : parent } ; let result = [ ] ; item . children . forEach ( child => { const links = this . getLinks ( child , link ) ; if ( ! _ . isNil ( links ) ) { result = result . concat ( links ) ; } } ) ; return result ; } if ( item . checked ) { return [ { item : item , parent : parent } ] ; } return null ; } } @ Injectable ( ) export class OrderDownlineTreeviewEventParser extends TreeviewEventParser { private currentDownlines : DownlineTreeviewItem [ ] = [ ] ; private parser = new DownlineTreeviewEventParser ( ) ; getSelectedChange ( component ) { const newDownlines = this . parser . getSelectedChange ( component ) ; if ( this . currentDownlines . length === 0 ) { this . currentDownlines = newDownlines ; } else { const intersectDownlines = [ ] ; this . currentDownlines . forEach ( downline => { let foundIndex = - 1 ; const length = newDownlines . length ; for ( let i = 0 ; i < length ; i ++ ) { if ( downline . item . value === newDownlines [ i ] . item . value ) { foundIndex = i ; } } if ( foundIndex !== - 1 ) { intersectDownlines . push ( newDownlines [ foundIndex ] ) ; newDownlines . splice ( foundIndex , 1 ) ; } } ) ; this . currentDownlines = intersectDownlines . concat ( newDownlines ) ; } return this . currentDownlines ; } }