aboutsummaryrefslogtreecommitdiff
path: root/js/components/gradientpicker.reel/gradientpicker.js
blob: b10f66243256806e493b72089b74a196afcb98d8 (plain)
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
388
389
390
391
392
393
394
395
396
397
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
</copyright> */

////////////////////////////////////////////////////////////////////////
//
var Montage = 			require("montage/core/core").Montage,
	Component = 		require("montage/ui/component").Component;
////////////////////////////////////////////////////////////////////////
//Exporting as ColorWheel
exports.GradientPicker = Montage.create(Component, {
	////////////////////////////////////////////////////////////////////
	//
	hasTemplate: {
		enumerable: true,
        value: true
    },
    ////////////////////////////////////////////////////////////////////
    //
    _updating: {
        enumerable: false,
        value: false
    },
    ////////////////////////////////////////////////////////////////////
    //
    _value: {
        enumerable: false,
        value: null
    },
	////////////////////////////////////////////////////////////////////
    //
    value: {
        enumerable: true,
        get: function() {
            return this._value;
        },
        set: function(value) {
            this._value = value;
        }
    },
    ////////////////////////////////////////////////////////////////////
    //
    _mode: {
        enumerable: false,
        value: 'linear'
    },
	////////////////////////////////////////////////////////////////////
    //
    mode: {
        enumerable: true,
        get: function() {
            return this._mode;
        },
        set: function(value) {
        	this.application.ninja.colorController.colorPopupManager.hideColorChipPopup();
        	//
            this._mode = value;
            //
            this._dispatchEvent('change', false);
        }
    },
    ////////////////////////////////////////////////////////////////////
    //
    prepareForDraw: {
    	enumerable: false,
    	value: function() {
    		//
		}
    },
    ////////////////////////////////////////////////////////////////////
    //
    willDraw: {
    	enumerable: false,
    	value: function() {
    		//Getting component views from layout
    		this.element._components = {trackCover: this.element.getElementsByClassName('cp_gp_slider')[0].getElementsByClassName('cover')[0], gradientTrack: this.element.getElementsByClassName('cp_gp_slider')[0].getElementsByClassName('track')[0], stopsContainer: this.element.getElementsByClassName('cp_gp_slider')[0].getElementsByClassName('chips')[0]};
    		this.element._trackWidth = parseInt(getComputedStyle(this.element._components.stopsContainer).getPropertyCSSValue('width').cssText);
    		//TODO: Fix events and remove this hack
    		this.element._components.trackCover.addEventListener('mouseover', function () {
				if (!this._updating) {    		
	    			this.element._components.trackCover.style.display = 'none';
    			}
    		}.bind(this), true);
    		//
    		this.element.getElementsByClassName('cp_gp_linear')[0].addEventListener('change', function (e){
    			this.mode = 'linear';
    		}.bind(this), true);
    		//
    		this.element.getElementsByClassName('cp_gp_radial')[0].addEventListener('change', function (e){
    			this.mode = 'radial';
    		}.bind(this), true);	
		}
    },
    ////////////////////////////////////////////////////////////////////
    //
    draw: {
    	enumerable: false,
    	value: function() {
    		//
    		if (this.mode === 'linear') {
    			this.element.getElementsByClassName('cp_gp_linear')[0].checked = 'true';
    		} else if (this.mode === 'radial') {
    			this.element.getElementsByClassName('cp_gp_radial')[0].checked = 'true';
    		}
    		//
    		if (!this.value) {
    			this.addDefaultStops();
			} else {
				//Temp holder
				var stops = this.value;
				//Adding stops from preset value
				for (var i=0; stops[i]; i++) {
					this.addStop({color: {mode: stops[i].mode, value: stops[i].value}, percent:stops[i].position}, true);
				}
			}
    	}
    },
    ////////////////////////////////////////////////////////////////////
    //
   	didDraw: {
    	enumerable: false,
    	value: function() {
			//
			this.element._components.gradientTrack.addEventListener('click', this, false);
    		
    		
    		
    		
    		
    		////////////////////////////////////////////////////////////
			////////////////////////////////////////////////////////////
			//TODO: Determing a better way to get screen position
			var element = this.element._components.gradientTrack;
    		this.element._trackX = 0, this.element._trackY = 0;
    		//
   			while (element && !isNaN(element.offsetLeft) && !isNaN(element.offsetTop)) {
    			this.element._trackX += element.offsetLeft - element.scrollLeft;
    			this.element._trackY += element.offsetTop - element.scrollTop;
    			element = element.parentNode;
	    	}
    		////////////////////////////////////////////////////////////
    		////////////////////////////////////////////////////////////
    		
    		//This is forever changing, not sure why
    		//console.log(this.element._trackX, this.element._trackY);
    		
    		
    	}
    },
    ////////////////////////////////////////////////////////////////////
    //
    addDefaultStops: {
    	enumerable: false,
    	value: function() {
    		this.addStop({color: {mode: 'rgb', value: {r: 255, g: 255, b: 255, a: 1, css: 'rgb(255, 255, 255)'}}, percent: 0}, true);
			this.addStop({color: {mode: 'rgb', value: {r: 0, g: 0, b: 0, a: 1, css: 'rgb(0, 0, 0)'}}, percent: 100}, true);
    	}
    },
    ////////////////////////////////////////////////////////////////////
    //
   	addStop: {
    	enumerable: false,
    	value: function(data, silent) {
    		if (this.application.ninja.colorController.colorPopupManager) {
    			//Hiding any open popups (of gradient buttons)
    			this.application.ninja.colorController.colorPopupManager.hideColorChipPopup();
    			//Creating stop elements
    			var stop = document.createElement('div'),
    				holder = document.createElement('div'),
					tooltip = document.createElement('span'),
					button = document.createElement('button');
				//Setting up elements
				stop.appendChild(tooltip);
				stop.appendChild(holder);
				holder.appendChild(button);
				//Adding events to the stops
				stop.addEventListener('mousedown', this, false);
				stop.addEventListener('mouseup', this, false);
				//Storing refereces to buttons and actual stop container
				button.stop = stop;
				stop.button = button;
				//Adding stop to container
				this.element._components.stopsContainer.appendChild(stop);
				//Checking for bounds to add stop
				if (data.percent >= 0 && data.percent <= 100) {
					this.positionStop(stop, data.percent);
					button.stopPosition = data.percent;
				}
				//Creating an instance of input chip
				this.application.ninja.colorController.addButton('chip', button);
				//Initialing button with color data
				button.color(data.color.mode, data.color.value);
				//Button popup data
				button.props = {side: 'top', align: 'center', nocolor: false, wheel: true, palette: true, gradient: false, image: false, offset: -84, panel: true};
				//Listening for color events from button
				button.addEventListener('change', this, false);
				//Dispatching event depending on type of mode
				if (!silent) {
					this._dispatchEvent('change', false);
				} else {
					this._dispatchEvent('change', true);
				}
				//
    		} else {
    				//Handle Error
    		}
    	}
    },
    ////////////////////////////////////////////////////////////////////
    //
    removeStop: {
    	enumerable: false,
    	value: function(stop) {
    		var i, buttons = this.element._components.stopsContainer.getElementsByTagName('button');
    		//
    		if (buttons.length > 2) {
    			//Removing stops
    			this.element._components.stopsContainer.removeChild(stop);
    			//Stopping events related to this current stop
    			this.removeStopMoving();
    		}
    	}
    },
    ////////////////////////////////////////////////////////////////////
    //
    removeStopMoving: {
    	enumerable: false,
    	value: function() {
    		this._updating = false;
    		this.element._components.trackCover.style.display = 'none';
			this._dispatchEvent('change', false);
			document.removeEventListener('mousemove', this, false);
			document.removeEventListener('mouseup', this, false);
    	}
    },
    ////////////////////////////////////////////////////////////////////
    //
    positionStop: {
    	enumerable: false,
    	value: function(stop, percent) {
    		try {
    			if (percent<0) {
	    			percent = 0;
	    		} else if (percent>100) {
	    			percent = 100;
	    		}
	    		
	    		
	    		
	    		
	    		////////////////////////////////////////////////////////////
	    		////////////////////////////////////////////////////////////
    			//TODO: toggling visibility because of a browser bug
	    		stop.style.visibility = 'hidden'; //TODO: To be removed
    			var adj = (parseInt(getComputedStyle(stop).getPropertyCSSValue('width').cssText)*percent/100)/this.element._trackWidth;
    			stop.style.left = Math.round(percent-Math.round(adj*100))+'%';
    			stop.button.stopPosition = percent;
    			stop.style.visibility = 'visible'; //TODO: To be removed
    			////////////////////////////////////////////////////////////
    			////////////////////////////////////////////////////////////
    			
    			
    			
    			
    		} catch (e) {
    			//TEMP
    		}
    	}
    },
    ////////////////////////////////////////////////////////////////////
    //TODO: Add color detection canvas to get actual color
    handleClick: {
    	enumerable: false,
    	value: function(e) {
    		//Logic to get color from canvas data would go here
    		var data = {};
    		data.mode = 'rgb';
    		data.value = {r: 100, g: 100, b: 100, a: 1, css: 'rgb(100, 100, 100)'};
    		//
    		this.addStop({color: data, percent: Math.round(100*(e._event.offsetX/e._event.target.offsetWidth))});
    	}
    },
    ////////////////////////////////////////////////////////////////////
    //
    handleMouseup: {
    	enumerable: false,
    	value: function(e) {
			this.removeStopMoving();
    	}
    },
    ////////////////////////////////////////////////////////////////////
    //
    handleMousedown: {
    	enumerable: false,
    	value: function(e) {
			//
			var i, buttons = this.element._components.stopsContainer.getElementsByTagName('button');
			this.currentStop = e._event.target.stop;
    		//Looping through other stops to swap depths
    		for (i=0; buttons[i]; i++) {
    			buttons[i].stop.style.zIndex = 1;
    		}
			//Setting the depth of the current button to the highest
			this.currentStop.style.zIndex = buttons.length+1;
			//Adding events for actions while moving
			document.addEventListener('mousemove', this, false);
			document.addEventListener('mouseup', this, false);
    	}
    },
    ////////////////////////////////////////////////////////////////////
    //
    handleMousemove: {
    	enumerable: false,
    	value: function(e) {
    		//
    		this._updating = true;
    		//
    		this.application.ninja.colorController.colorPopupManager.hideColorChipPopup();
    		//
    		if ((e._event.y+this.hack.y) > this.element._trackY+70 || (e._event.y+this.hack.y) < this.element._trackY) {
    			this.removeStop(this.currentStop);
    		}
    		//
    		if (this.currentStop.button.stopPosition !== Math.round(((e._event.x+this.hack.x)-(this.element._trackX-23))/this.element._trackWidth*100)) {
    			this.element._components.trackCover.style.display = 'block';
    		}
    		//
    		this.positionStop(this.currentStop, Math.round(((e._event.x+this.hack.x)-(this.element._trackX-23))/this.element._trackWidth*100));
    	}
    },
    ////////////////////////////////////////////////////////////////////
    //
    handleChange: {
    	enumerable: false,
    	value: function(e) {
    		this.application.ninja.colorController.colorView.colorManager.input = this.application.ninja.colorController.colorView.previousInput;
			this._dispatchEvent('change', false);
			this.application.ninja.colorController.colorView.colorManager.input = 'chip';
    	}
    },
    ////////////////////////////////////////////////////////////////////
    //Dispatching custom event
    _dispatchEvent: {
        value: function(type, userInitiated) {
        	//
        	var actionEvent = document.createEvent("CustomEvent"), buttons = this.element._components.stopsContainer.getElementsByTagName('button'), stops = [], css, previewCss = '-webkit-gradient(linear, left top, right top';
        	//Preventing an events of less than 2 stops since there'll be a reset
        	if (buttons.length < 2) {
        		return;
        	}
        	//Initializing CSS string
        	if (this.mode === 'radial') {
        		css = '-webkit-radial-gradient(center, ellipse cover';
        	} else {
        		css = '-webkit-gradient(linear, left top, right top';
        	}
        	//Creating stops array
        	for (var i=0; i < buttons.length; i++) {
        		stops.push({value: buttons[i].colorValue, mode: buttons[i].colorMode, position: buttons[i].stopPosition});
        	}
        	//Sorting array (must be sorted for radial gradients, at least in Chrome
        	stops.sort(function(a,b){return a.position - b.position});
        	//Looping through stops in gradient to create CSS (actual and preview)
        	for (var i=0; i < stops.length; i++) {
        		//Addint to CSS String
        		if (this.mode === 'radial' && stops[i].value) {
        			css += ', '+stops[i].value.css+' '+stops[i].position+'% ';
        			//The CSS string for the preview bar is always linear
	        		previewCss += ', color-stop('+stops[i].position+'%,'+stops[i].value.css+')';
        		} else if (stops[i].value){
	        		css += ', color-stop('+stops[i].position+'%,'+stops[i].value.css+')';
	        		//The CSS string for the preview bar is always linear
	        		previewCss += ', color-stop('+stops[i].position+'%,'+stops[i].value.css+')';
        		} else {
        			//
        		}
        	}
        	//Closing the CSS strings
        	css += ')';
        	previewCss += ')';
        	//console.log(previewCss);
        	//Setting the preview track background
        	this.element._components.gradientTrack.style.background = previewCss;
        	//Storing the stops
        	this.value = stops;
        	//Initializing and storing data for event
            actionEvent.initEvent(type, true, true);
            actionEvent.type = type;
            actionEvent.wasSetByCode = userInitiated;
            actionEvent.gradient = {stops: this.value, mode: this.mode, gradientMode: this.mode, css: css};
            this.dispatchEvent(actionEvent);
        }
    }
    ////////////////////////////////////////////////////////////////////
});