-
Notifications
You must be signed in to change notification settings - Fork 3
/
gradX.js
749 lines (575 loc) · 25 KB
/
gradX.js
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
;
/*
*
* SAMPLE USAGE DETAILS :
*
* sliders structure :
*
* [
* {
* color: "COLOR",
* position: "POSITION" //0 to 100 without % symbol
* },
* {
* ....
* ....
* },
* ....
* ]
*
*/
'use strict';
//make me jquery UI independent
if (typeof jQuery.fn.draggable === "undefined") {
(function($) {
$.fn.draggable = function() {
//console.log(this);
var ele = document.getElementById(this.attr("id"));
ele.style.top = "121px";
Drag.init(ele, null, 26, 426, 86, 86);
return this;
};
}(jQuery));
}
var gradX = function(id, _options) {
var options = {
targets: [], //[element selector] -> array
sliders: [],
direction: 'left',
//if linear left | top | right | bottom
//if radial left | center | right , top | center | bottom
type: 'linear', //linear | circle | ellipse
code_shown: false, //false | true
change: function(sliders, styles) {
//nothing to do here by default
}
};
//make global
gradx = {
rand_RGB: [],
rand_pos: [],
id: null,
slider_ids: [],
slider_index: 0, //global index for sliders
sliders: [], //contains styles of each slider
direction: "left", //direction of gradient or position of centre in case of radial gradients
type: "linear", //linear or radial
shape: "cover", //radial gradient size
slider_hovered: [],
jQ_present: true,
code_shown: false,
load_jQ: function() {
//handle any library conflicts here
this.gx = jQuery;
},
//very lazy to replace this by jQuery
add_event: function(el, evt, evt_func) {
add_event(el, evt, evt_func);
},
get_random_position: function() {
var pos;
do {
pos = parseInt(Math.random() * 100);
}
while (this.rand_pos.indexOf(pos) > -1);
this.rand_pos.push(pos);
return pos;
},
get_random_rgb: function() {
var R, G, B, color;
do {
R = parseInt(Math.random() * 255);
G = parseInt(Math.random() * 255);
B = parseInt(Math.random() * 255);
color = "rgb(" + R + ", " + G + ", " + B + ")";
}
while (this.rand_RGB.indexOf(color) > -1);
this.rand_RGB.push(color);
return color;
},
//if target element is specified the target's style (background) is updated
update_target: function(values) {
if (this.targets.length > 0) {
//target elements exist
var i, j, ele, len = this.targets.length, v_len = values.length;
for (i = 0; i < len; i++) {
ele = gradx.gx(this.targets[i]);
for (j = 0; j < v_len; j++) {
ele.css("background-image", values[j]);
}
}
}
},
//apply styles on fly
apply_style: function(ele, value) {
var type = 'linear';
if (gradx.type != 'linear') {
type = 'radial';
}
if (value.indexOf(this.direction) > -1) {
//add cross-browser compatibility
var values = [
"-webkit-" + type + "-gradient(" + value + ")",
"-moz-" + type + "-gradient(" + value + ")",
"-ms-" + type + "-gradient(" + value + ")",
"-o-" + type + "-gradient(" + value + ")",
type + "-gradient(" + value + ")"
];
} else {
//normal color
values = [value];
}
var len = values.length, css = '';
while (len > 0) {
len--;
ele.css("background", values[len]);
css += "background: " + values[len] + ";\n";
}
//call the userdefined change function
this.change(this.sliders, values);
this.update_target(values);
gradx.gx('#gradx_code').html(css);
},
//on load
apply_default_styles: function() {
this.update_style_array()
var value = this.get_style_value();
this.apply_style(this.panel, value);
},
//update the slider_values[] while dragging
update_style_array: function() {
this.sliders = [];
var len = gradx.slider_ids.length,
i, offset, position, id;
for (i = 0; i < len; i++) {
id = "#" + gradx.slider_ids[i];
offset = parseInt(gradx.gx(id).css("left"));
position = parseInt((offset / gradx.container_width) * 100);
position -= 6; //TODO: find why this is required
gradx.sliders.push([gradx.gx(id).css("background-color"), position]);
}
this.sliders.sort(function(A, B) {
if (A[1] > B[1])
return 1;
else
return -1;
});
},
//creates the complete css background value to later apply style
get_style_value: function() {
var len = gradx.slider_ids.length;
if (len === 1) {
//since only one slider , so simple background
style_str = this.sliders[0][0];
} else {
var style_str = "", suffix = "";
for (var i = 0; i < len; i++) {
if (this.sliders[i][1] == "") {
style_str += suffix + (this.sliders[i][0]);
} else {
if (this.sliders[i][1] > 100) {
this.sliders[i][1] = 100;
}
style_str += suffix + (this.sliders[i][0] + " " + this.sliders[i][1] + "%");
}
suffix = " , "; //add , from next iteration
}
if (this.type == 'linear') {
//direction, [color stoppers]
style_str = this.direction + " , " + style_str; //add direction for gradient
} else {
//position, type size, [color stoppers]
style_str = this.direction + " , " + this.type + " " + this.shape + " , " + style_str;
}
}
return style_str;
},
//@input rgb string rgb(<red>,<green>,<blue>)
//@output rgb object of form { r: <red> , g: <green> , b : <blue>}
get_rgb_obj: function(rgb) {
//rgb(r,g,b)
rgb = rgb.split("(");
//r,g,b)
rgb = rgb[1];
//r g b)
rgb = rgb.split(",");
return {
r: parseInt(rgb[0]),
g: parseInt(rgb[1]),
b: parseInt(rgb[2])
};
},
load_info: function(ele) {
var id = "#" + ele.id;
this.current_slider_id = id;
//check if current clicked element is an slider
if (this.slider_ids.indexOf(ele.id) > -1) { //javascript does not has # in its id
var color = gradx.gx(id).css("backgroundColor");
//but what happens if @color is not in RGB ? :(
var rgb = this.get_rgb_obj(color);
var left = gradx.gx(id).css("left");
if (parseInt(left) > 26 && parseInt(left) < 426) {
gradx.gx("#gradx_slider_info") //info element cached before
.css("left", left)
.show();
}
this.set_colorpicker(rgb);
console.log(rgb);
}
},
//add slider
add_slider: function(sliders) {
var id, slider, k, position, value, delta;
if (sliders.length === 0) {
sliders = [//default sliders
{
color: gradx.get_random_rgb(),
position: gradx.get_random_position() //x percent of gradient panel(400px)
},
{
color: gradx.get_random_rgb(),
position: gradx.get_random_position()
}
];
}
obj = sliders;
for (k in obj) {
if (typeof obj[k].position === "undefined")
break;
//convert % to px based on containers width
var delta = 26; //range: 26px tp 426px
position = parseInt((obj[k].position * this.container_width) / 100) + delta + "px";
id = "gradx_slider_" + (this.slider_index); //create an id for this slider
this.sliders.push(
[
obj[k].color,
obj[k].position
]
);
this.slider_ids.push(id); //for reference wrt to id
slider = "<div class='gradx_slider' id='" + id + "'></div>";
gradx.gx("#gradx_start_sliders_" + this.id).append(slider);
gradx.gx('#' + id).css("backgroundColor", obj[k].color).css("left", position);
this.slider_index++;
}
for (var i = 0, len = this.slider_ids.length; i < len; i++) {
gradx.gx('#' + this.slider_ids[i]).draggable({
containment: 'parent',
axis: 'x',
start: function() {
if (gradx.jQ_present)
gradx.current_slider_id = "#" + gradx.gx(this).attr("id"); //got full jQuery power here !
},
drag: function() {
gradx.update_style_array();
gradx.apply_style(gradx.panel, gradx.get_style_value());
var left = gradx.gx(gradx.current_slider_id).css("left");
if (parseInt(left) > 26 && parseInt(left) < 426) {
gradx.gx("#gradx_slider_info") //info element cached before
.css("left", left)
.show();
} /*else {
if (parseInt(left) > 120) {
left = "272px";
} else {
left = "120px";
}
gradx.gx("#gradx_slider_info") //info element cached before
.css("left", left)
.show();
}*/
var color = gradx.gx(gradx.current_slider_id).css("backgroundColor");
//but what happens if @color is not in RGB ? :(
var rgb = gradx.get_rgb_obj(color);
gradx.cp.spectrum("set", rgb);
}
}).click(function() {
gradx.load_info(this);
return false;
});
}
},
set_colorpicker: function(clr) {
gradx.cp.spectrum({
move: function(color) {
if (gradx.current_slider_id != false) {
var rgba = color.toRgbString();
gradx.gx(gradx.current_slider_id).css('background-color', rgba);
gradx.update_style_array();
gradx.apply_style(gradx.panel, gradx.get_style_value());
}
},
change: function() {
gradx.gx("#gradx_slider_info").hide();
},
flat: true,
showAlpha: true,
color: clr,
clickoutFiresChange: true,
showInput: true,
showButtons: false
});
},
generate_options: function(options) {
var len = options.length,
name, state,
str = '';
for (var i = 0; i < len; i++) {
name = options[i].split(" ");
name = name[0];
if (i < 2) {
state = name[1];
} else {
state = '';
}
name = name.replace("-", " ");
str += '<option value=' + options[i] + ' ' + state + '>' + name + '</option>';
}
return str;
},
generate_radial_options: function() {
var options;
options = ["horizontal-center disabled", "center selected", "left", "right"];
gradx.gx('#gradx_gradient_subtype').html(gradx.generate_options(options));
options = ["vertical-center disabled", "center selected", "top", "bottom"];
gradx.gx('#gradx_gradient_subtype2').html(gradx.generate_options(options)).show();
},
generate_linear_options: function() {
var options;
options = ["horizontal-center disabled", "left selected", "right", "top", "bottom"];
gradx.gx('#gradx_gradient_subtype').html(gradx.generate_options(options));
gradx.gx('#gradx_gradient_subtype2').hide();
},
destroy: function() {
var options = {
targets: [], //[element selector] -> array
sliders: [],
direction: 'left',
//if linear left | top | right | bottom
//if radial left | center | right , top | center | bottom
type: 'linear', //linear | circle | ellipse
code_shown: false, //false | true
change: function(sliders, styles) {
//nothing to do here by default
}
};
for (var k in options) {
gradx[k] = options[k];
}
},
load_gradx: function(id, sliders) {
this.me = gradx.gx(id);
this.id = id.replace("#", "");
id = this.id;
this.current_slider_id = false;
var html = "<div class='gradx'>\n\
<div id='gradx_add_slider' class='gradx_add_slider gradx_btn'><i class='icon icon-add'></i>add</div>\n\
<div class='gradx_slectboxes'>\n\
<select id='gradx_gradient_type' class='gradx_gradient_type'>\n\
<option value='linear'>Linear</option>\n\
<option value='circle'>Radial - Circle</option>\n\
<option value='ellipse'>Radial - Ellipse</option>\n\
</select>\n\
<select id='gradx_gradient_subtype' class='gradx_gradient_type'>\n\
<option id='gradx_gradient_subtype_desc' value='gradient-direction' disabled>gradient direction</option>\n\
<option value='left' selected>Left</option>\n\
<option value='right'>Right</option>\n\
<option value='top'>Top</option>\n\
<option value='bottom'>Bottom</option>\n\
</select>\n\
<select id='gradx_gradient_subtype2' class='gradx_gradient_type gradx_hide'>\n\
</select>\n\
<select id='gradx_radial_gradient_size' class='gradx_gradient_type gradx_hide'>\n\
</select>\n\
</div>\n\
<div class='gradx_container' id='gradx_" + id + "'>\n\
<div id='gradx_stop_sliders_" + id + "'></div>\n\
<div class='gradx_panel' id='gradx_panel_" + id + "'></div>\n\
<div class='gradx_start_sliders' id='gradx_start_sliders_" + id + "'>\n\
<div class='cp-default' id='gradx_slider_info'>\n\
<div id='gradx_slider_controls'>\n\
<div id='gradx_delete_slider' class='gradx_btn'><i class='icon icon-remove'></i>delete</div>\n\
</div>\n\
<div id='gradx_slider_content'></div>\n\
</div> \n\
</div>\n\
</div>\n\
<div id='gradx_show_code' class='gradx_show_code gradx_btn'><i class='icon icon-file-css'></i><span>show the code</span></div>\n\
<div id='gradx_show_presets' style='display:none' class='gradx_show_presets gradx_btn'><i class='icon icon-preset'></i><span>show presets</span></div>\n\
<textarea class='gradx_code' id='gradx_code'></textarea>\n\
</div>";
this.me.html(html);
//generates html to select the different gradient sizes
// *only available for radial gradients
var gradient_size_val = ["gradient-size disabled", "closest-side selected", "closest-corner", "farthest-side", "farthest-corner", "contain", "cover"],
option_str = '';
option_str = gradx.generate_options(gradient_size_val);
gradx.gx('#gradx_radial_gradient_size').html(option_str);
//cache divs for fast reference
this.container = gradx.gx("#gradx_" + id);
this.panel = gradx.gx("#gradx_panel_" + id);
//.hide();
//this.info.hide();
this.container_width = 400 //HARDCODE;
this.add_slider(sliders);
gradx.add_event(document, 'click', function() {
// if(!gradx.jQ_present){
if (!gradx.slider_hovered[id]) {
gradx.gx("#gradx_slider_info").hide();
return false;
}
});
gradx.gx('#gradx_add_slider').click(function() {
gradx.add_slider([
{
color: gradx.get_random_rgb(),
position: gradx.get_random_position() //no % symbol
}
]);
gradx.update_style_array();
gradx.apply_style(gradx.panel, gradx.get_style_value());//(where,style)
});
//cache the element
gradx.cp = gradx.gx('#gradx_slider_content');
//call the colorpicker plugin
gradx.set_colorpicker("blue");
gradx.gx('#gradx_delete_slider').click(function() {
gradx.gx(gradx.current_slider_id).remove();
gradx.gx("#gradx_slider_info").hide();
var id = gradx.current_slider_id.replace("#", "");
//remove all references from array for current deleted slider
for (var i = 0; i < gradx.slider_ids.length; i++) {
if (gradx.slider_ids[i] == id) {
gradx.slider_ids.splice(i, 1);
}
}
//apply modified style after removing the slider
gradx.update_style_array();
gradx.apply_style(gradx.panel, gradx.get_style_value());
gradx.current_slider_id = false; //no slider is selected
});
gradx.gx('#gradx_code').focus(function() {
var $this = gradx.gx(this);
$this.select();
// Work around Chrome's little problem
$this.mouseup(function() {
// Prevent further mouseup intervention
$this.unbind("mouseup");
return false;
});
});
gradx.gx('#gradx_gradient_type').change(function() {
var type = gradx.gx(this).val(), options, option_str = '';
if (type !== "linear") {
//gradx.gx('#gradx_radial_gradient_size').show();
gradx.generate_radial_options();
} else {
gradx.generate_linear_options();
gradx.gx('#gradx_gradient_subtype').val("left");
}
gradx.type = type;
gradx.direction = gradx.gx('#gradx_gradient_subtype').val();
gradx.apply_style(gradx.panel, gradx.get_style_value());//(where,style)
});
//change type onload userdefined
if (this.type !== "linear") {
gradx.gx('#gradx_gradient_type').val(this.type);
gradx.generate_radial_options();
var h, v;
if (this.direction !== 'left') {
//user has passed his own direction
var center;
if (this.direction.indexOf(",") > -1) {
center = this.direction.split(",");
} else {
//tolerate user mistakes
center = this.direction.split(" ");
}
h = center[0];
v = center[1];
//update the center points in the corr. select boxes
gradx.gx('#gradx_gradient_subtype').val(h);
gradx.gx('#gradx_gradient_subtype2').val(v);
} else {
var h = gradx.gx('#gradx_gradient_subtype').val();
var v = gradx.gx('#gradx_gradient_subtype2').val();
}
gradx.direction = h + " " + v;
gradx.apply_style(gradx.panel, gradx.get_style_value());//(where,style)
} else {
//change direction if not left
if (this.direction !== 'left') {
gradx.gx('#gradx_gradient_subtype').val(this.direction);
}
}
gradx.gx('#gradx_gradient_subtype').change(function() {
if (gradx.type === 'linear') {
gradx.direction = gradx.gx(this).val();
} else {
var h = gradx.gx(this).val();
var v = gradx.gx('#gradx_gradient_subtype2').val();
gradx.direction = h + " " + v;
}
gradx.apply_style(gradx.panel, gradx.get_style_value());//(where,style)
});
gradx.gx('#gradx_gradient_subtype2').change(function() {
var h = gradx.gx('#gradx_gradient_subtype').val();
var v = gradx.gx(this).val();
gradx.direction = h + " " + v;
gradx.apply_style(gradx.panel, gradx.get_style_value());//(where,style)
});
//not visible
gradx.gx('#gradx_radial_gradient_size').change(function() {
gradx.shape = gradx.gx(this).val();
gradx.apply_style(gradx.panel, gradx.get_style_value());//(where,style)
});
gradx.gx('#gradx_show_code').click(function() {
if (gradx.code_shown) {
//hide it
gradx.code_shown = false;
gradx.gx('#gradx_show_code span').text("show the code");
gradx.gx("#gradx_code").hide();
}
else {
//show it
gradx.gx('#gradx_show_code span').text("hide the code");
gradx.gx("#gradx_code").show();
gradx.code_shown = true;
}
});
//show or hide onload
if (gradx.code_shown) {
//show it
gradx.gx('#gradx_show_code span').text("hide the code");
gradx.gx("#gradx_code").show();
}
gradx.add_event(document.getElementById('gradx_slider_info'), 'mouseout', function() {
gradx.slider_hovered[id] = false;
});
gradx.add_event(document.getElementById('gradx_slider_info'), 'mouseover', function() {
gradx.slider_hovered[id] = true;
});
}
};
function add_event(element, event, event_function)
{
if (element.attachEvent) //Internet Explorer
element.attachEvent("on" + event, function() {
event_function.call(element);
});
else if (element.addEventListener) //Firefox & company
element.addEventListener(event, event_function, false); //don't need the 'call' trick because in FF everything already works in the right way
}
;
//load jQuery library into gradx.gx
gradx.load_jQ();
/* merge _options into options */
gradx.gx.extend(options, _options);
//apply options to gradx object
for (var k in options) {
//load the options into gradx object
gradx[k] = options[k];
}
gradx.load_gradx(id, gradx.sliders);
gradx.apply_default_styles();
};