1- import { Component , Optional , ElementRef , Renderer , IterableDiffers } from "@angular/core" ;
1+ import { Component , Optional , ElementRef , Renderer , IterableDiffers , KeyValueDiffers , ChangeDetectorRef , SimpleChanges , Input } from "@angular/core" ;
22import { IgControlBase } from "../igcontrolbase/igcontrolbase" ;
33import { ControlValueAccessor , NgModel } from "@angular/forms" ;
44
@@ -11,12 +11,15 @@ declare var jQuery: any;
1111 outputs : [ "rendered" , "dataBinding" , "dataBound" , "filtering" , "filtered" , "itemsRendering" , "itemsRendered" , "dropDownOpening" , "dropDownOpened" , "dropDownClosing" , "dropDownClosed" , "selectionChanging" , "selectionChanged" ]
1212} )
1313export class IgComboComponent extends IgControlBase < IgCombo > implements ControlValueAccessor {
14+
15+ @Input ( )
16+ public dataSource ;
17+
1418 protected _model : any ;
15- private _dataSource : any ;
1619 private _changes : any ;
1720
18- constructor ( @Optional ( ) public model : NgModel , el : ElementRef , renderer : Renderer , differs : IterableDiffers ) {
19- super ( el , renderer , differs ) ;
21+ constructor ( @Optional ( ) public model : NgModel , el : ElementRef , renderer : Renderer , differs : IterableDiffers , kvalDiffers : KeyValueDiffers , cdr : ChangeDetectorRef ) {
22+ super ( el , renderer , differs , kvalDiffers , cdr ) ;
2023 if ( model ) {
2124 model . valueAccessor = this ;
2225 this . _model = model ;
@@ -25,8 +28,14 @@ export class IgComboComponent extends IgControlBase<IgCombo> implements ControlV
2528
2629 ngOnInit ( ) {
2730 let that = this ;
31+ const valueKey = this [ "valueKey" ] || this . options . valueKey ;
32+ if ( this . dataSource === null || this . dataSource === undefined ) {
33+ this . dataSource = this . options [ "dataSource" ] ;
34+ }
35+ if ( ! this . options [ "dataSource" ] && this . dataSource ) {
36+ this . options [ "dataSource" ] = this . dataSource ;
37+ }
2838 super . ngOnInit ( ) ;
29- this . _dataSource = jQuery . extend ( true , [ ] , this . _config . dataSource ) ;
3039
3140 if ( this . _model ) {
3241 // D.P. #244 only attach selectionchanged handler if there's a model to update
@@ -40,10 +49,10 @@ export class IgComboComponent extends IgControlBase<IgCombo> implements ControlV
4049
4150 if ( ui . owner . options . multiSelection . enabled ) {
4251 that . _model . viewToModelUpdate ( items . map ( function ( item ) {
43- return item . data [ that . _config . valueKey ] ;
52+ return item . data [ valueKey ] ;
4453 } ) ) ;
4554 } else {
46- that . _model . viewToModelUpdate ( items [ 0 ] . data [ that . _config . valueKey ] ) ;
55+ that . _model . viewToModelUpdate ( items [ 0 ] . data [ valueKey ] ) ;
4756 }
4857 } ) ;
4958 //manually call writeValue, because the LifeCycle has been changed and writeValue is executed before ngOnInit
@@ -69,44 +78,64 @@ export class IgComboComponent extends IgControlBase<IgCombo> implements ControlV
6978 this . onTouched = fn ;
7079 }
7180
72- ngDoCheck ( ) {
73- if ( this . _differ != null && this . _allowChangeDetection ) {
74- this . optionChange ( ) ;
75- this . _allowChangeDetection = false ;
76- var diff = [ ] ;
77- var element = jQuery ( this . _el ) ;
78- var i , j , valKey = this . _config . valueKey , record , item ;
79-
80- //check for changes in collection
81- if ( ! ( this . _config . dataSource instanceof Array ) ) {
82- return ;
83- }
84- this . _changes = this . _differ . diff ( this . _config . dataSource ) ;
85- if ( this . _config . dataSource && this . _config . dataSource . length !== this . _dataSource . length ) {
86- this . _dataSource = jQuery . extend ( true , [ ] , this . _config . dataSource ) ;
87- if ( this . _changes ) {
88- element . data ( "igCombo" ) . dataBind ( ) ;
89- if ( this . model && this . model . value ) {
90- this . writeValue ( this . model . value ) ;
91- }
92- }
93- }
81+ dataSourceApplyChanges ( changes ) {
82+ const element = jQuery ( this . _el ) ;
83+ element . data ( "igCombo" ) . dataBind ( ) ;
84+ if ( this . model && this . model . value ) {
85+ this . writeValue ( this . model . value ) ;
86+ }
87+ }
88+ updateComboItem ( rec , val , key , index ) {
89+ const element = jQuery ( this . _el ) ;
90+ const comboItem = element . data ( "igCombo" ) . itemsFromIndex ( index ) ;
91+ element . data ( "igCombo" ) . _updateItem ( comboItem . element , rec ) ;
92+ if ( element . data ( "igCombo" ) . isSelected ( comboItem . element ) ) {
93+ //should update the input
94+ element . data ( "igCombo" ) . _updateInputValues ( false ) ;
95+ }
9496
95- if ( ! this . equalsDiff ( this . _config . dataSource , this . _dataSource , diff ) ) {
96- this . _dataSource = jQuery . extend ( true , [ ] , this . _config . dataSource ) ;
97- for ( i = 0 ; i < diff . length ; i ++ ) {
98- for ( j = 0 ; j < diff [ i ] . txlog . length ; j ++ ) {
99- record = this . _config . dataSource [ diff [ i ] . index ] ;
100- item = element . data ( "igCombo" ) . itemsFromIndex ( diff [ i ] . index ) ;
101- element . data ( "igCombo" ) . _updateItem ( item . element , record ) ;
102- if ( element . data ( "igCombo" ) . isSelected ( item . element ) ) {
103- //should update the input
104- element . data ( "igCombo" ) . _updateInputValues ( false ) ;
105- }
97+ }
98+ public ngOnChanges ( changes : SimpleChanges ) : void {
99+ const ds = "dataSource" ;
100+ if ( ds in changes ) {
101+ const value = changes [ ds ] . currentValue ;
102+ if ( value ) {
103+ try {
104+ this . _differ = this . _differs . find ( value ) . create ( ) ;
105+ this . _changes = [ ] ;
106+ for ( var i = 0 ; i < this . dataSource . length ; i ++ ) {
107+ this . _changes . push ( this . kvalDiffers . find ( { } ) . create ( ) ) ;
108+ }
109+ }
110+ catch ( e ) {
111+ throw new Error ( "Only binding to arrays is supported." ) ;
112+ }
113+ }
114+ }
115+ super . ngOnChanges ( changes ) ;
116+ }
117+ ngDoCheck ( ) {
118+ if ( this . _differ ) {
119+ const changes = this . _differ . diff ( this . dataSource ) ;
120+ //check if grid is initialized
121+ const combo = jQuery ( this . _el ) . data ( this . _widgetName ) ;
122+ if ( changes && combo ) {
123+ this . dataSourceApplyChanges ( changes ) ;
124+ }
125+ if ( this . _changes && combo ) {
126+ //check recs
127+ for ( var i = 0 ; i < this . dataSource . length ; i ++ ) {
128+ var item = this . dataSource [ i ] ;
129+ var recChanges = this . _changes [ i ] . diff ( item ) ;
130+ if ( recChanges ) {
131+ recChanges . forEachChangedItem ( ( change : any ) => {
132+ this . updateComboItem ( item , change . currentValue , change . key , i ) ;
133+ } ) ;
106134 }
107135 }
108136 }
109- }
137+ }
138+ super . ngDoCheck ( ) ;
110139 }
111140
112141 /**
0 commit comments