@@ -19,6 +19,23 @@ export default class AngularColorPickerController {
1919 return ;
2020 }
2121
22+ if ( newValue !== undefined && oldValue !== undefined && ! this . hasOwnProperty ( 'initialNgModel' ) ) {
23+ this . initialNgModel = newValue ;
24+ }
25+
26+ // check dirty/pristine state
27+ if ( this . hasOwnProperty ( 'initialNgModel' ) ) {
28+ if ( newValue === this . initialNgModel ) {
29+ if ( typeof this . $scope . control [ 0 ] . $setPristine === 'function' ) {
30+ this . $scope . control [ 0 ] . $setPristine ( ) ;
31+ }
32+ } else {
33+ if ( typeof this . $scope . control [ 0 ] . $setDirty === 'function' ) {
34+ this . $scope . control [ 0 ] . $setDirty ( ) ;
35+ }
36+ }
37+ }
38+
2239 if ( newValue !== undefined && newValue !== null && newValue . length > 4 ) {
2340 var color = tinycolor ( newValue ) ;
2441
@@ -45,10 +62,6 @@ export default class AngularColorPickerController {
4562 }
4663
4764 this . $scope . control [ 0 ] . $setValidity ( this . $element . attr ( 'name' ) , this . isValid ) ;
48-
49- if ( newValue !== oldValue && oldValue !== undefined && typeof this . $scope . control [ 0 ] . $setDirty === 'function' ) {
50- this . $scope . control [ 0 ] . $setDirty ( ) ;
51- }
5265 } else {
5366 if ( newValue === null || newValue === '' ) {
5467 this . hue = 0 ;
@@ -100,12 +113,28 @@ export default class AngularColorPickerController {
100113 if ( ! this . options . inline && ( this . visible || this . $element [ 0 ] . querySelector ( '.color-picker-panel' ) . offsetParent !== null ) ) {
101114
102115 this . visible = false ;
103- this . $scope . $apply ( ) ;
116+ this . $scope . $applyAsync ( ) ;
104117
105118 this . eventApiDispatch ( 'onClose' , [ event ] ) ;
106119 }
107120 } ;
108121
122+ this . api . clear = ( event ) => {
123+ if ( this . ngModel !== '' ) {
124+ this . ngModel = '' ;
125+
126+ this . eventApiDispatch ( 'onClear' , [ event ] ) ;
127+ }
128+ } ;
129+
130+ this . api . reset = ( event ) => {
131+ if ( this . ngModel !== this . initialNgModel ) {
132+ this . ngModel = this . initialNgModel ;
133+
134+ this . eventApiDispatch ( 'onReset' , [ event ] ) ;
135+ }
136+ } ;
137+
109138 this . api . getElement = ( ) => {
110139 return this . $element ;
111140 } ;
@@ -192,6 +221,10 @@ export default class AngularColorPickerController {
192221 this . $document . off ( 'mouseup' , this . onMouseUp ) ;
193222 this . $document . off ( 'mousemove' , this . onMouseMove ) ;
194223
224+ this . $document . off ( 'touchstart' , this . onMouseDown ) ;
225+ this . $document . off ( 'touchend' , this . onMouseUp ) ;
226+ this . $document . off ( 'touchmove' , this . onMouseMove ) ;
227+
195228 this . eventApiDispatch ( 'onDestroy' ) ;
196229 } ) ;
197230
@@ -203,9 +236,22 @@ export default class AngularColorPickerController {
203236 this . $document . on ( 'mouseup' , this . onMouseUp . bind ( this ) ) ;
204237 this . $document . on ( 'mousemove' , this . onMouseMove . bind ( this ) ) ;
205238
239+ // setup touch events
240+ this . $document . on ( 'touchstart' , this . onMouseDown . bind ( this ) ) ;
241+ this . $document . on ( 'touchend' , this . onMouseUp . bind ( this ) ) ;
242+ this . $document . on ( 'touchmove' , this . onMouseMove . bind ( this ) ) ;
243+
244+ // grid click
206245 this . find ( '.color-picker-grid' ) . on ( 'click' , this . onColorClick . bind ( this ) ) ;
246+ this . find ( '.color-picker-grid' ) . on ( 'touchend' , this . onColorClick . bind ( this ) ) ;
247+
248+ // hue click
207249 this . find ( '.color-picker-hue' ) . on ( 'click' , this . onHueClick . bind ( this ) ) ;
250+ this . find ( '.color-picker-hue' ) . on ( 'touchend' , this . onHueClick . bind ( this ) ) ;
251+
252+ // opacity click
208253 this . find ( '.color-picker-opacity' ) . on ( 'click' , this . onOpacityClick . bind ( this ) ) ;
254+ this . find ( '.color-picker-opacity' ) . on ( 'touchend' , this . onOpacityClick . bind ( this ) ) ;
209255 }
210256
211257 onMouseDown ( event ) {
@@ -314,25 +360,64 @@ export default class AngularColorPickerController {
314360 }
315361
316362 initConfig ( ) {
317- if ( ! this . options ) {
318- this . options = { } ;
319- }
320-
321- this . options . disabled = this . options . disabled === undefined ? false : this . options . disabled ;
322- this . options . alpha = this . options . alpha === undefined ? true : this . options . alpha ;
323- this . options . case = this . options . case === undefined ? 'upper' : this . options . case ;
324- this . options . format = this . options . format === undefined ? 'hsl' : this . options . format ;
325- this . options . pos = this . options . pos === undefined ? 'bottom left' : this . options . pos ;
326- this . options . swatch = this . options . swatch === undefined ? true : this . options . swatch ;
327- this . options . swatchOnly = this . options . swatchOnly === undefined ? false : this . options . swatchOnly ;
328- this . options . swatchPos = this . options . swatchPos === undefined ? 'left' : this . options . swatchPos ;
329- this . options . swatchBootstrap = this . options . swatchBootstrap === undefined ? true : this . options . swatchBootstrap ;
330- this . options . inline = this . options . inline === undefined ? false : this . options . inline ;
331- this . options . placeholder = this . options . placeholder === undefined ? '' : this . options . placeholder ;
363+ this . options = this . merge ( this . options , {
364+ disabled : false ,
365+ hue : true ,
366+ alpha : true ,
367+ case : 'upper' ,
368+ format : 'hsl' ,
369+ pos : 'bottom left' ,
370+ swatch : true ,
371+ swatchOnly : false ,
372+ swatchPos : 'left' ,
373+ swatchBootstrap : true ,
374+ inline : false ,
375+ placeholder : '' ,
376+ close : {
377+ show : false ,
378+ label : 'Close' ,
379+ class : '' ,
380+ } ,
381+ clear : {
382+ show : false ,
383+ label : 'Clear' ,
384+ class : '' ,
385+ } ,
386+ reset : {
387+ show : false ,
388+ label : 'Reset' ,
389+ class : '' ,
390+ } ,
391+ } ) ;
332392
333393 this . visible = this . options . inline ;
334394 }
335395
396+ merge ( options , defaultOptions ) {
397+ var newObject = { } ;
398+ var attr ;
399+
400+ for ( attr in defaultOptions ) {
401+ if ( defaultOptions . hasOwnProperty ( attr ) ) {
402+ newObject [ attr ] = defaultOptions [ attr ] ;
403+ }
404+ }
405+
406+ if ( typeof options === 'object' ) {
407+ for ( attr in options ) {
408+ if ( options . hasOwnProperty ( attr ) ) {
409+ if ( typeof options [ attr ] === 'object' ) {
410+ newObject [ attr ] = this . merge ( options [ attr ] , newObject [ attr ] ) ;
411+ } else {
412+ newObject [ attr ] = options [ attr ] ;
413+ }
414+ }
415+ }
416+ }
417+
418+ return newObject ;
419+ }
420+
336421 focus ( ) {
337422 this . find ( '.color-picker-input' ) [ 0 ] . focus ( ) ;
338423 }
0 commit comments