11'use strict' ;
22
33angular . module ( 'color-picker' , [ ] )
4- . directive ( 'colorPicker' , [ '$compile' , function ( $compile ) {
4+ . directive ( 'colorPicker' , [ '$compile' , '$document' , function ( $compile , $document ) {
55 return {
66 restrict : 'A' ,
77 require : '?ngModel' ,
88 scope : {
9- format : '='
9+ format : '=' ,
10+ alpha : '=' ,
11+ swatch : '='
1012 } ,
1113 link : function ( $scope , element , attrs ) {
1214 $scope . init = function ( ) {
@@ -15,26 +17,30 @@ angular.module('color-picker', [])
1517 $scope . saturation = 50 ;
1618 $scope . lightness = 50 ;
1719 $scope . opacity = 100 ;
20+
21+ $document . on ( 'click' , function ( evt ) {
22+ if ( $scope . find ( evt . target ) . length == 0 ) {
23+ $scope . log ( 'Color Picker: Document Hide Event' ) ;
24+ $scope . hide ( ) ;
25+ }
26+ } ) ;
1827 } ;
1928
2029 $scope . createInput = function ( ) {
21- var html , el , template ,
22- currentInput = $scope . find ( '.color-picker-wrapper' ) ;
30+ var html ;
2331
24- if ( currentInput . length > 0 ) {
25- return currentInput ;
26- }
32+ if ( ! $scope . wrapper ) {
33+ element . wrap ( $compile ( angular . element ( '<div class="color-picker-wrapper"></div>' ) ) ( $scope ) ) ;
2734
28- html = '<div class="color-picker-wrapper" ng-class="{\'color-picker-focus\': visible}">' +
29- '<input class="color-picker-input form-control" type="text" ng-model="ngModel" size="7" ng-focus="show()" ng-blur="hide()">' +
30- '<span class="color-picker-swatch" ng-click="focus()">' +
31- '<span class="color-picker-swatch-color" style="background-color: {{swatch}};"></span>' +
35+ html = '<input class="color-picker-input form-control" type="text" ng-model="ngModel" size="7" ng-focus="show()" ng-class="{\'color-picker-input-swatch\': swatch}">' +
36+ '<span class="color-picker-swatch" ng-click="focus()" ng-show="swatch">' +
37+ '<span class="color-picker-swatch-color" style="background-color: {{swatchColor}};"></span>' +
3238 '</span>' +
33- '<div class="color-picker-panel">' +
39+ '<div class="color-picker-panel" ng-show="visible" >' +
3440 '<div class="color-picker-hue color-picker-sprite" ng-click="hueChange($event, true)" ng-mousemove="hueChange($event, false)" ng-mousedown="hueDown()" ng-mouseup="hueUp()">' +
3541 '<div class="color-picker-slider" style="top: {{huePos}}px;"></div>' +
3642 '</div>' +
37- '<div class="color-picker-opacity color-picker-sprite" ng-class="{\'color-picker-hidden\': alpha} " ng-click="opacityChange($event, true)" ng-mousemove="opacityChange($event, false)" ng-mousedown="opacityDown()" ng-mouseup="opacityUp()">' +
43+ '<div class="color-picker-opacity color-picker-sprite" ng-show=" alpha" ng-click="opacityChange($event, true)" ng-mousemove="opacityChange($event, false)" ng-mousedown="opacityDown()" ng-mouseup="opacityUp()">' +
3844 '<div class="color-picker-slider" style="top: {{opacityPos}}px;"></div>' +
3945 '</div>' +
4046 '<div class="color-picker-grid color-picker-sprite" style="background-color: {{grid}};" ng-click="colorChange($event, true)" ng-mousemove="colorChange($event, false)" ng-mousedown="colorDown()" ng-mouseup="colorUp()">' +
@@ -43,69 +49,68 @@ angular.module('color-picker', [])
4349 '<div></div>' +
4450 '</div>' +
4551 '</div>' +
46- '</div>' +
47- '</div>' ;
48-
49- el = angular . element ( html ) ;
50- template = $compile ( el ) ( $scope ) ;
51- //element.append(template);
52- element . after ( template ) ;
53- element . hide ( ) ;
54-
55- $scope . wrapper = el ;
52+ '</div>' ;
5653
57- return el ;
54+ element . after ( $compile ( angular . element ( html ) ) ( $scope ) ) ;
55+ element . addClass ( 'ng-hide' ) ;
56+ $scope . wrapper = element . parent ( ) ;
57+ }
5858 } ;
5959
6060 $scope . focus = function ( ) {
61- $scope . log ( 'color Picker: Focus Event' ) ;
61+ $scope . log ( 'Color Picker: Focus Event' ) ;
6262 $scope . find ( '.color-picker-input' ) . focus ( ) ;
6363 } ;
6464
6565 $scope . show = function ( ) {
66- $scope . log ( 'color Picker: Show Event' ) ;
66+ $scope . log ( 'Color Picker: Show Event' ) ;
6767 $scope . visible = true ;
6868 $scope . hueMouse = false ;
69- $scope . opacityMouse = true ;
69+ $scope . opacityMouse = false ;
7070 $scope . colorMouse = false ;
7171 } ;
7272
7373 $scope . hide = function ( ) {
74- $scope . log ( 'color Picker: Hide Event' ) ;
74+ $scope . log ( 'Color Picker: Hide Event' ) ;
7575 $scope . visible = false ;
76+ $scope . $apply ( ) ;
7677 } ;
7778
7879 $scope . update = function ( ) {
7980 var color = tinycolor ( { h : $scope . hue , s : $scope . saturation , l : $scope . lightness } ) ;
80- color . setAlpha ( $scope . opacity / 100 ) ;
81- $scope . log ( 'color Picker: COLOR CHANGED TO ' , color , $scope . hue , $scope . saturation , $scope . lightness , $scope . opacity ) ;
81+
82+ if ( $scope . alpha ) {
83+ color . setAlpha ( $scope . opacity / 100 ) ;
84+ }
85+
86+ $scope . log ( 'Color Picker: COLOR CHANGED TO ' , color , $scope . hue , $scope . saturation , $scope . lightness , $scope . opacity ) ;
8287
8388 switch ( $scope . format ) {
8489 case 'rgb' :
85- $scope . swatch = color . toRgbString ( ) ;
90+ $scope . swatchColor = color . toRgbString ( ) ;
8691 $scope . ngModel = color . toRgbString ( ) ;
8792 break ;
8893
8994 case 'hex' :
90- $scope . swatch = color . toHexString ( ) ;
95+ $scope . swatchColor = color . toHexString ( ) ;
9196 $scope . ngModel = color . toHexString ( ) ;
9297 break ;
9398
9499 case 'hsv' :
95- $scope . swatch = color . toHsvString ( ) ;
100+ $scope . swatchColor = color . toHslString ( ) ;
96101 $scope . ngModel = color . toHsvString ( ) ;
97102 break ;
98103
99104 default :
100- $scope . swatch = color . toHslString ( ) ;
105+ $scope . swatchColor = color . toHslString ( ) ;
101106 $scope . ngModel = color . toHslString ( ) ;
102107 break ;
103108 }
104109 } ;
105110
106111 $scope . $watch ( 'ngModel' , function ( newValue , oldValue ) {
107112 if ( newValue !== undefined && newValue !== oldValue ) {
108- $scope . log ( 'color Picker: MODEL - CHANGED' , newValue ) ;
113+ $scope . log ( 'Color Picker: MODEL - CHANGED' , newValue ) ;
109114 var color = tinycolor ( newValue ) ;
110115
111116 if ( color . isValid ( ) ) {
@@ -121,6 +126,16 @@ angular.module('color-picker', [])
121126 } ) ;
122127
123128 $scope . $watch ( 'format' , function ( newValue , oldValue ) {
129+ if ( newValue !== undefined && newValue !== oldValue ) {
130+ if ( newValue === 'hex' ) {
131+ $scope . alpha = '' ;
132+ }
133+
134+ $scope . update ( ) ;
135+ }
136+ } ) ;
137+
138+ $scope . $watch ( 'alpha' , function ( newValue , oldValue ) {
124139 if ( newValue !== undefined && newValue !== oldValue ) {
125140 $scope . update ( ) ;
126141 }
@@ -130,26 +145,26 @@ angular.module('color-picker', [])
130145 // HUE
131146 //---------------------------
132147 $scope . hueDown = function ( ) {
133- $scope . log ( 'color Picker: HUE - MOUSE DOWN' ) ;
148+ $scope . log ( 'Color Picker: HUE - MOUSE DOWN' ) ;
134149 $scope . hueMouse = true ;
135150 } ;
136151
137152 $scope . hueUp = function ( ) {
138- $scope . log ( 'color Picker: HUE - MOUSE UP' ) ;
153+ $scope . log ( 'Color Picker: HUE - MOUSE UP' ) ;
139154 $scope . hueMouse = false ;
140155 } ;
141156
142157 $scope . hueChange = function ( evt , forceRun ) {
143158 if ( $scope . hueMouse || forceRun ) {
144- $scope . log ( 'color Picker: HUE - MOUSE CHANGE' ) ;
159+ $scope . log ( 'Color Picker: HUE - MOUSE CHANGE' ) ;
145160 var el = $scope . find ( '.color-picker-hue' ) ;
146161 $scope . hue = ( 1 - ( ( evt . pageY - $scope . offset ( el , 'top' ) ) / el . prop ( 'offsetHeight' ) ) ) * 360 ;
147162 }
148163 } ;
149164
150165 $scope . $watch ( 'hue' , function ( newValue , oldValue ) {
151166 if ( newValue !== undefined ) {
152- $scope . log ( 'color Picker: HUE - CHANGED' ) ;
167+ $scope . log ( 'Color Picker: HUE - CHANGED' ) ;
153168 $scope . huePos = ( 1 - ( newValue / 360 ) ) * $scope . find ( '.color-picker-hue' ) . prop ( 'offsetHeight' ) ;
154169 $scope . grid = tinycolor ( { h : newValue , s : 50 , l : 50 } ) . toHslString ( ) ;
155170 $scope . update ( ) ;
@@ -160,26 +175,26 @@ angular.module('color-picker', [])
160175 // OPACITY
161176 //---------------------------
162177 $scope . opacityDown = function ( ) {
163- $scope . log ( 'color Picker: OPACITY - MOUSE DOWN' ) ;
178+ $scope . log ( 'Color Picker: OPACITY - MOUSE DOWN' ) ;
164179 $scope . opacityMouse = true ;
165180 } ;
166181
167182 $scope . opacityUp = function ( ) {
168- $scope . log ( 'color Picker: OPACITY - MOUSE UP' ) ;
183+ $scope . log ( 'Color Picker: OPACITY - MOUSE UP' ) ;
169184 $scope . opacityMouse = false ;
170185 } ;
171186
172187 $scope . opacityChange = function ( evt , forceRun ) {
173188 if ( $scope . opacityMouse || forceRun ) {
174- $scope . log ( 'color Picker: OPACITY - MOUSE CHANGE' ) ;
189+ $scope . log ( 'Color Picker: OPACITY - MOUSE CHANGE' ) ;
175190 var el = $scope . find ( '.color-picker-opacity' ) ;
176191 $scope . opacity = ( 1 - ( ( evt . pageY - $scope . offset ( el , 'top' ) ) / el . prop ( 'offsetHeight' ) ) ) * 100 ;
177192 }
178193 } ;
179194
180195 $scope . $watch ( 'opacity' , function ( newValue , oldValue ) {
181196 if ( newValue !== undefined ) {
182- $scope . log ( 'color Picker: OPACITY - CHANGED' ) ;
197+ $scope . log ( 'Color Picker: OPACITY - CHANGED' ) ;
183198 $scope . opacityPos = ( 1 - ( newValue / 100 ) ) * $scope . find ( '.color-picker-opacity' ) . prop ( 'offsetHeight' ) ;
184199 $scope . update ( ) ;
185200 }
@@ -189,18 +204,18 @@ angular.module('color-picker', [])
189204 // COLOR
190205 //---------------------------
191206 $scope . colorDown = function ( ) {
192- $scope . log ( 'color Picker: COLOR - MOUSE DOWN' ) ;
207+ $scope . log ( 'Color Picker: COLOR - MOUSE DOWN' ) ;
193208 $scope . colorMouse = true ;
194209 } ;
195210
196211 $scope . colorUp = function ( ) {
197- $scope . log ( 'color Picker: COLOR - MOUSE UP' ) ;
212+ $scope . log ( 'Color Picker: COLOR - MOUSE UP' ) ;
198213 $scope . colorMouse = false ;
199214 } ;
200215
201216 $scope . colorChange = function ( evt , forceRun ) {
202217 if ( $scope . colorMouse || forceRun ) {
203- $scope . log ( 'color Picker: COLOR - MOUSE CHANGE' ) ;
218+ $scope . log ( 'Color Picker: COLOR - MOUSE CHANGE' ) ;
204219 var el = $scope . find ( '.color-picker-grid-inner' ) ;
205220 $scope . saturation = ( ( evt . pageX - $scope . offset ( el , 'left' ) ) / el . prop ( 'offsetWidth' ) ) * 100 ;
206221 $scope . lightness = ( 1 - ( ( evt . pageY - $scope . offset ( el , 'top' ) ) / el . prop ( 'offsetHeight' ) ) ) * 100 ;
@@ -209,15 +224,15 @@ angular.module('color-picker', [])
209224
210225 $scope . $watch ( 'saturation' , function ( newValue , oldValue ) {
211226 if ( newValue !== undefined && newValue !== oldValue ) {
212- $scope . log ( 'color Picker: SATURATION - CHANGED' ) ;
227+ $scope . log ( 'Color Picker: SATURATION - CHANGED' ) ;
213228 $scope . saturationPos = ( newValue / 100 ) * $scope . find ( '.color-picker-grid-inner' ) . prop ( 'offsetWidth' ) ;
214229 $scope . update ( ) ;
215230 }
216231 } ) ;
217232
218233 $scope . $watch ( 'lightness' , function ( newValue , oldValue ) {
219234 if ( newValue !== undefined && newValue !== oldValue ) {
220- $scope . log ( 'color Picker: LIGHTNESS - CHANGED' ) ;
235+ $scope . log ( 'Color Picker: LIGHTNESS - CHANGED' ) ;
221236 $scope . lightnessPos = ( 1 - ( newValue / 100 ) ) * $scope . find ( '.color-picker-grid-inner' ) . prop ( 'offsetHeight' ) ;
222237 $scope . update ( ) ;
223238 }
@@ -231,38 +246,63 @@ angular.module('color-picker', [])
231246 console . log . apply ( console , arguments ) ;
232247 } ;
233248
249+ // taken and modified from jQuery's find
234250 $scope . find = function ( selector ) {
235- var el = $scope . wrapper ? $scope . wrapper [ 0 ] : element [ 0 ] ;
251+ var context = $scope . wrapper ? $scope . wrapper [ 0 ] : element [ 0 ] ,
252+ results = [ ] ,
253+ i = 0 ,
254+ nodeType ;
255+
256+
257+ // Same basic safeguard as Sizzle
258+ if ( ! selector ) {
259+ return results ;
260+ }
236261
237- if ( selector . indexOf ( '.' ) === 0 ) {
238- return angular . element ( el . getElementsByClassName ( selector . replace ( '.' , '' ) ) ) ;
262+ if ( typeof selector === 'string' ) {
263+ // Early return if context is not an element or document
264+ if ( ( nodeType = context . nodeType ) !== 1 && nodeType !== 9 ) {
265+ return [ ] ;
266+ }
267+
268+ results = context . querySelectorAll ( selector ) ;
269+
270+ } else {
271+ if ( $scope . contains ( context , selector ) ) {
272+ results . push ( selector ) ;
273+ }
239274 }
275+
276+ return angular . element ( results ) ;
240277 } ;
241278
242- $scope . offset = function ( el , type ) {
243- var offset = null ;
244- // try the jQuery way if possible
245- try {
246- offset = el . offset ( ) ;
247- } catch ( e ) { }
248-
249- if ( offset === null ) {
250- if ( el . length === 0 ) {
251- return null ;
279+ $scope . contains = function ( a , b ) {
280+ if ( b ) {
281+ while ( ( b = b . parentNode ) ) {
282+ if ( b === a ) {
283+ return true ;
284+ }
252285 }
286+ }
287+
288+ return false ;
289+ } ;
253290
254- var rawDom = el [ 0 ] ;
255- var _x = 0 ;
256- var _y = 0 ;
257- var body = document . documentElement || document . body ;
258- var scrollX = window . pageXOffset || body . scrollLeft ;
259- var scrollY = window . pageYOffset || body . scrollTop ;
260- _x = rawDom . getBoundingClientRect ( ) . left + scrollX ;
261- _y = rawDom . getBoundingClientRect ( ) . top + scrollY ;
291+ $scope . offset = function ( el , type ) {
292+ var offset ,
293+ x = 0 ,
294+ y = 0 ,
295+ body = document . documentElement || document . body ;
262296
263- offset = { left : _x , top :_y } ;
297+ if ( el . length === 0 ) {
298+ return null ;
264299 }
265300
301+ x = el [ 0 ] . getBoundingClientRect ( ) . left + ( window . pageXOffset || body . scrollLeft ) ;
302+ y = el [ 0 ] . getBoundingClientRect ( ) . top + ( window . pageYOffset || body . scrollTop ) ;
303+
304+ offset = { left : x , top :y } ;
305+
266306 if ( type !== undefined ) {
267307 return offset [ type ] ;
268308 }
0 commit comments