-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLivingColor.js
2114 lines (1802 loc) · 80.1 KB
/
LivingColor.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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// LivingColor.js 1.1
// -------------------
// Author: Luke Dennis
// Website: http://lukifer.github.com/LivingColor
// License: http://opensource.org/licenses/mit-license.php
(function(){
$ = jQuery || Zepto;
window.LivingColor =
{
"rules": [],
"rulesList": [],
"options": {},
"prefix": "",
"$div": false,
"$selector": false,
"attachEvents": function()
{
$(document)
.on("click", "#LivingColorAdd", LivingColor.addRule)
.on("change", "#LivingColor input[type=text]", LivingColor.inputChange)
.on("focus", "#LivingColor input[type=text]", LivingColor.inputFocus)
.on("keydown", "#LivingColor input", LivingColor.inputKeyDown)
.on("click", "#LivingColor .minimize", LivingColor.minimize)
.on("click", "#LivingColor .move", LivingColor.togglePosition)
.on("click", "#LivingColor .target", LivingColor.toggleTargetMode)
.on("change", "#LivingColorList", LivingColor.colorListChange)
.on("click", "#LivingColorListSave", LivingColor.saveNewColorList)
.on("click", ".living-color-target", LivingColor.targetClick)
.on("click", "#LivingColor .remove", LivingColor.removeRow)
.on("click", "#LivingColor .filters", LivingColor.clickFilters)
.on("click", "#LivingColorFiltersReset", LivingColor.filtersReset)
.on("change mousemove", "#LivingColorFilters input[type='range']", LivingColor.sliderChange)
.on("change", "#LivingColorFilters input[type='number']", LivingColor.sliderValueChange)
.on("change", "#LivingColor li a.filters", LivingColor.filtersChange)
;
// Drag functionality: currently disabled
// .on("drag", "#LivingColor", LivingColor.drag)
// .on("dragend", "#LivingColor", function(e){e.stopPropagation();})
},
// Nothing happens until this function is called
"start": function(opts)
{
LivingColor.options = $.extend({
"minimized": false,
"defaultScheme": false
}, opts);
// Figure out which prefix we'll need
var htmlStyle = $("html")[0].style;
if(htmlStyle['webkitFilter'] !== undefined)
LivingColor.prefix = "webkit";
else if(htmlStyle['mozFilter'] !== undefined)
LivingColor.prefix = "moz";
else if(htmlStyle['msFilter'] !== undefined)
LivingColor.prefix = "ms";
// Inject a style tag
if(!this['$style'])
{
var $style = $('<style type="text/css"></style>');
var css = "";
$.each(this.styles, function(sel, val)
{
var vals = val.split(";");
$.each(vals, function(n, v)
{
var cssKeyVal = v.split(":");
if(cssKeyVal.length != 2) return true;
var cssKey = $.trim(cssKeyVal[0]);
var cssVal = $.trim(cssKeyVal[1]);
var prefixedKeys = ["filter", "transform", "transition", "user-select", "appearance", "box-sizing"];
if($.inArray(cssKey, prefixedKeys) != -1)
{
val += "-ms-"+cssKey+": "+cssVal+";";
val += "-moz-"+cssKey+": "+cssVal+";";
val += "-webkit-"+cssKey+": "+cssVal+";";
}
});
css += sel+" { "+val+" } ";
});
$style[0].appendChild(document.createTextNode(css));
this['$style'] = $style;
$("head")[0].appendChild($style[0]);
}
// Announce our presence
$("html").addClass("living-color-active");
// Create a hidden popup for image filters
LivingColor.createFiltersPopup();
// Get or create the list of color rules
LivingColor.loadRulesList();
// Load active rules and display
LivingColor.loadRules(LivingColor.rulesList.active);
LivingColor.render();
// Attach events
LivingColor.attachEvents();
// Trigger the starting rules
$("#LivingColor input[type=text]").change();
$("#LivingColor a.filters").change();
// Cache our primary interface div
var $div = $("#LivingColor");
this["$div"] = $div;
// Older Firefox = No CSS filters
if(navigator.userAgent.match(/Firefox\/3[0-4]/))
$div.addClass("nofilters");
},
// Fetch list of color schemes from local storage
"loadRulesList": function(name)
{
var theRulesList = JSON.parse(localStorage.getItem("LC_colorRules") || "false");
if(!theRulesList || !theRulesList.names)
{
theRulesList = {"names": ["Default Scheme"], "active": "Default Scheme"};
localStorage.setItem("LC_colorRules", JSON.stringify(theRulesList));
if(typeof LivingColor.options.defaultScheme === "object" && LivingColor.options.defaultScheme.length)
{
LivingColor.rules = LivingColor.options.defaultScheme;
localStorage.setItem("LC_colorRulesDefault Scheme", JSON.stringify(LivingColor.rules));
}
}
LivingColor.rulesList = theRulesList;
},
// Fetch the details of a color scheme from local storage
"loadRules": function(name)
{
var theRules = localStorage.getItem("LC_colorRules"+name);
if(theRules)
theRules = JSON.parse(theRules) || [{"selector": "", "color": "", "backgroundColor": ""}]
else
{
theRules = [{"selector": "", "color": "", "backgroundColor": ""}];
localStorage.setItem("LC_colorRules"+name, JSON.stringify(theRules));
}
LivingColor.rules = theRules;
},
// Clear all customized style rules
"resetAll": function()
{
if(LivingColor.rules.length > 0) $.each(LivingColor.rules, function(n, rule)
{
var prefixedFilterRule = LivingColor.prefixedFilterRule();
$(rule.selector)
.css("color", "")
.css("backgroundColor", "")
.each(function(n, el)
{
el.style[prefixedFilterRule] = "";
})
;
});
},
// Apply all customized style rules from current color scheme
"applyAll": function()
{
if(LivingColor.rules.length > 0) $.each(LivingColor.rules, function(n, rule)
{
var prefixedFilterRule = LivingColor.prefixedFilterRule();
$(rule.selector).each(function(n, el)
{
var $el = $(el);
if($el.is(".LivingColorImmune") == false && $el.parents(".LivingColorImmune").length === 0)
{
$el
.css("color", rule.color ? "#"+rule.color : "")
.css("backgroundColor", "#"+rule.backgroundColor)
;
if(rule.filters && rule.filters.length)
$el[0].style[prefixedFilterRule] = rule.filters.join(" ");
}
});
});
},
// Update only the element(s) that were just changed
"liveColorChange": function(el)
{
var $el = $(el);
var $li = $el.parent();
// If the selector was changed, manually trigger update events for all components of this rule
if($el.is(".selector"))
{
LivingColor.liveColorChange($li.children("[name='color']")[0]);
LivingColor.liveColorChange($li.children("[name='backgroundColor']")[0]);
LivingColor.liveColorChange($li.children("a.filters")[0]);
return;
}
// Fetch DOM elements and values
var $sel = $li.children(".selector");
var $col = $li.children(".textColor");
var $bgc = $li.children(".backgroundColor");
var selector = $sel.val();
var color = $col.val();
var bgColor = $bgc.val();
selector = $.trim(selector);
if(selector == "") return;
// Apply CSS image filter
if($el.is("a.filters"))
{
var prefixedFilterRule = LivingColor.prefixedFilterRule();
var filterString = $el.data("filters");
if(!filterString) filterString = "";
$(selector).each(function(n, el)
{
var $el = $(el);
if($el.is(".LivingColorImmune") == false && $el.parents(".LivingColorImmune").length === 0)
el.style[prefixedFilterRule] = filterString;
});
}
else
{
// Apply text color
if(el != $bgc[0])
{
$(selector).each(function(n, el)
{
var $el = $(el);
if($el.is(".LivingColorImmune") == false && $el.parents(".LivingColorImmune").length === 0)
$el.css("color", color ? "#"+color : "");
});
}
// Apply background color
if(el != $col[0])
{
$(selector).each(function(n, el)
{
var $el = $(el);
if($el.is(".LivingColorImmune") == false && $el.parents(".LivingColorImmune").length === 0)
$el.css("backgroundColor", bgColor ? "#"+bgColor : "");
});
}
}
},
// Add a blank rule
"addRule": function()
{
var newRule = {
"selector": "",
"color": "",
"backgroundColor": "",
"filters": []
};
LivingColor.rules.push(newRule);
$("#LivingColor ul")
.toggleClass("overflown", LivingColor.rules.length > 10)
.append(LivingColor.renderOne(newRule));
jscolor.bind();
},
// Fired when the selector or color text boxes are altered
"inputChange": function(e)
{
var $input = $(this);
var $li = $input.parent("li");
var index;
$("#LivingColor li").each(function(n, li)
{
if(li == $li[0]) { index = n; return false; }
});
if(index === undefined) return;
LivingColor.rules[index][$input.attr("name")] = $input.val();
if($input.is(".selector"))
{
// If the selector is changed, we need to remove the styles on the last selector
var last = $input.data("last-selector");
if(last && last != $input.val())
{
var prefixedFilterRule = LivingColor.prefixedFilterRule();
$(last).each(function(n, el)
{
var $el = $(el);
if($el.is(".LivingColorImmune") == false && $el.parents(".LivingColorImmune").length === 0)
{
$el
.css("color", "")
.css("backgroundColor", "")
;
if(rule.filters && rule.filters.length)
$el[0].style[prefixedFilterRule] = "";
}
});
}
// Cache the selector for use next time
$input.data("last-selector", $input.val());
// Apply the new color rules
LivingColor.liveColorChange($input[0]);
}
localStorage.setItem("LC_colorRules"+LivingColor.rulesList.active, JSON.stringify(LivingColor.rules));
},
// Cache the selector div (jscolor does all the other heavy lifting)
"inputFocus": function(e)
{
LivingColor['$selector'] = $(this).parent("li").children(".selector");
},
// Listen for Escape key
"inputKeyDown": function(e)
{
if(e.keyCode == 27)
$(this).blur();
},
// Minimized state toggle
"minimize": function(e)
{
var $div = $("#LivingColor");
if($div.hasClass("minimized"))
{
$div.removeClass("minimized");
$div.find("h4 a.minimize").attr("title", "Minimize");
}
else
{
$div.addClass("minimized");
$div.find("h4 a.minimize").attr("title", "Un-minimize");
}
},
// Move to and from top/bottom position
"togglePosition": function(e)
{
$LivingColor = $("#LivingColor");
var transitionEnd = LivingColor.prefix && LivingColor.prefix != "moz"
? LivingColor.prefix+"TransitionEnd" : "transitionend";
if($LivingColor.hasClass("top"))
{
$LivingColor.one(transitionEnd, function()
{
$LivingColor.removeClass("top");
$LivingColor.find("h4 a.move").attr("title", "Move to the top");
$LivingColor.css("top", "");
});
$LivingColor.css("top", (window.innerHeight - $LivingColor.height() - 10)+"px");
LivingColor.rulesList.top = 0;
}
else // if .hasClass("top") == false
{
$LivingColor.one(transitionEnd, function()
{
$LivingColor.addClass("top");
$LivingColor.find("h4 a.move").attr("title", "Move to the bottom");
$LivingColor.css("bottom", "");
});
$LivingColor.css("bottom", (window.innerHeight - $LivingColor.height() - 10)+"px");
LivingColor.rulesList.top = 1;
}
// Remember position
localStorage.setItem("LC_colorRules", JSON.stringify(LivingColor.rulesList));
},
// Trigger DOM node targeting (crosshairs)
"toggleTargetMode": function(e)
{
if($("body").hasClass("living-color-targeting"))
{
$("body").removeClass("living-color-targeting");
$(".living-color-target").removeClass("living-color-target");
$(document).off("mouseover.living-color");
}
else
{
$("body").addClass("living-color-targeting");
// Highlight targeted node
$(document).on("mouseover.living-color", "*", function(e)
{
var $target = $(e.target);
if($target.parents("#LivingColor").length > 0) return;
$(".living-color-target").removeClass("living-color-target");
$target.addClass("living-color-target");
});
}
},
// When targeted node is clicked, create a new rule
"targetClick": function(e)
{
e.preventDefault();
e.stopPropagation();
var sel = "";
var $target = $(e.target);
// If we have an id, we just use that
if($target.attr("id"))
{
sel = "#"+$target.attr("id");
}
// Otherwise, tag and class, wrapped in the closest id we can find: "#myid p.myclass", or "#myid p"
else
{
var classes =
$.trim
(
// Ignore our own custom classes
$target[0].className
.replace("living-color-targeting", "")
.replace("living-color-target", "")
.replace("living-color", "")
)
.split(/\s+/)
;
sel += $target[0].tagName.toLowerCase();
if(classes.length > 0 && classes[0] != "") sel += "."+classes.join(".");
// Try to find the closest parent id, if possible
var id = false;
var $item = $target;
while($item = $item.parent())
{
if($item.length < 1) break;
if($item.attr("id"))
{
id = $item.attr("id");
break;
}
if($item[0].className && $item[0].className != "")
{
var classes = $.trim($item[0].className.replace("living-color", "")).split(/\s+/);
if(classes.length > 0 && classes[0] != "")
sel = "."+classes.join(".")+" "+sel;
}
}
if(id) sel = "#"+id+" "+sel;
}
// Toggle target mode off
$("#LivingColor h4 .target").click();
// Add a new rule and populate with our new selector
$("#LivingColorAdd").click();
$("#LivingColor ul li:last-child input:eq(0)").val(sel);
return false;
},
// Remove a row
"removeRow": function(e)
{
// Fetch row to delete and remove
var $li = $(e.target).parent("li");
var index = $li.prevAll("li").length;
// Confirm, maybe
if($li.children(".selector").val() && !confirm('Delete this item?'))
return;
LivingColor.rules.splice(index, 1);
// Display and save
LivingColor.render();
localStorage.setItem("LC_colorRules"+LivingColor.rulesList.active, JSON.stringify(LivingColor.rules));
},
// Revert filters to defaults
"filtersReset": function()
{
var filters = LivingColor.getFilterDefaults();
$.each(filters, function(key, obj)
{
var shortname = key.replace(" ", "-").toLowerCase();
$("#LivingColorFilters li."+shortname+" input").val(obj['default']);
});
$("#LivingColorFilters input:first").change();
},
// Get the browser prefix for CSS filters (JS version, not CSS version: eg, webkitFilters)
"prefixedFilterRule": function()
{
if(LivingColor.prefix)
return LivingColor.prefix+'Filter';
else
return "filter";
},
// Toggle and move Filters modal
"clickFilters": function()
{
var $a = $(this);
var $li = $a.parent("li");
var $filters = $("#LivingColorFilters");
var top = $a.offset().top;
LivingColor['$selector'] = $li.find(".selector");
$filters.data("link", this);
// Top mode: appear just below text box
if($("#LivingColor").is(".top"))
{
top += $a.height() + 3;
if($filters.is(":hidden") || parseInt($filters.css("top")) != top)
{
$filters
.css("bottom", "auto")
.css("top", top+"px")
.show()
;
$(document).on("click.filtersCloseClick", LivingColor.filtersCloseClick);
}
else LivingColor.filtersClose();
}
// Bottom mode: appear just above text box
else
{
if($filters.is(":hidden") || parseInt($filters.css("top")) != top)
{
$filters
.css("top", "auto")
.css("bottom", (window.innerHeight - top - 3)+"px")
.show()
;
$(document).on("click.filtersCloseClick", LivingColor.filtersCloseClick);
}
else LivingColor.filtersClose();
}
// Defaults
var filterVals = {
"brightness": "100",
"contrast": "100",
"saturate": "100",
"opacity": "100",
"hue-rotate": "0",
"invert": "0",
"grayscale": "0",
"sepia": "0",
"blur": "0",
};
// Populate user-visible text box with CSS value for current filters, for copy/pasta convenience
var index = $li.prevAll("li").length;
if(LivingColor.rules[index] !== undefined && LivingColor.rules[index]["filters"] !== undefined)
{
var savedFilters = LivingColor.rules[index]["filters"];
$.each(savedFilters, function(n, str)
{
var split = str.split("(");
if(split.length > 1)
filterVals[split[0].replace(" ", "-").toLowerCase()] = parseInt(split[1]);
});
if(savedFilters.length > 0)
$("#LivingColorFiltersTextarea").val((LivingColor.prefix?"-"+LivingColor.prefix+"-":"")
+"filter: "+savedFilters.join(" "));
else
$("#LivingColorFiltersTextarea").val("");
}
// Populate controls with current rule values
$.each(filterVals, function(key, val)
{
$("#LivingColorFilters li."+key+" input").val(val);
})
},
// Filter Change Event: <input type="range" />
"sliderChange": function(e)
{
if(e.type === "mousemove" && e.which !== 1)
return;
var $this = $(this);
var $value = $this.parent().children('input[type="number"]');
$value.val($this.val());
LivingColor.updateFilters(e.type == "change");
},
// Filter Change Event: <input type="number" />
"sliderValueChange": function(e)
{
var $range = $(this).parent().children('input[type="range"]');
var min = parseInt($range.attr("min"));
var max = parseInt($range.attr("max"));
var val = $(this).val();
if(/[\-0-9]/.test(val) && parseInt(val) >= min && parseInt(val) <= max)
$range.val(val);
else
$(this).val($range.val());
LivingColor.updateFilters(e.type == "change");
},
// Filter Change Event: Manual trigger on the link itself
"filtersChange": function(e)
{
LivingColor.liveColorChange(e.target);
},
// Update CSS filter string and apply
"updateFilters": function(andSave)
{
var $filters = $("#LivingColorFilters");
var $a = $($filters.data("link"));
var $li = $a.parent("li");
var sel = $li.children(".selector");
// Build array of CSS components
var filterVals = [];
$filters.find("li").each(function(n, li)
{
var $li = $(li);
var key = li.className;
if(key == "reset" || key == "textarea") return true;
var val = $li.children("label").children("input[type='number']").val();
if(val == $li.data("default")) return true;
// Most values are percentages; provide correct units for the exceptions
var str = key+"("+val+")";
if(key === "blur")
str = str.replace(")", "px)");
else if(key === "hue-rotate")
str = str.replace(")", "deg)");
else
str = str.replace(")", "%)");
filterVals.push(str);
});
// Create string and cache to link
var filterString = filterVals.join(" ");
$a .data("filters", filterString)
.data("alt", filterString)
.toggleClass("active", filterString !== "")
;
$("#LivingColorFiltersTextarea").val((LivingColor.prefix?"-"+LivingColor.prefix+"-":"")+"filter: "+filterString);
// Trigger UI update
LivingColor.liveColorChange($a[0]);
// Only save if requested
if(andSave)
{
// Find index of li
var index = $li.prevAll("li").length;
// Do the save
LivingColor.rules[index]["filters"] = filterVals;
localStorage.setItem("LC_colorRules"+LivingColor.rulesList.active, JSON.stringify(LivingColor.rules));
}
},
// Click off of filters popup to close
"filtersCloseClick": function(e)
{
if($(e.target).parents("#LivingColorFilters").length < 1)
{
LivingColor.filtersClose();
}
},
// Do the closing thang
"filtersClose": function(e)
{
$("#LivingColorFilters").hide();
$(document).off("click.filtersCloseClick");
},
// Not yet enabled
"drag": function(e)
{
if(e.originalEvent.pageY < (window.innerHeight/2))
$('#LivingColor').addClass("top");
else
$('#LivingColor').removeClass("top");
},
// Switch to a new set of rules
"colorListChange": function(e)
{
// Route to special "meta" actions if needed (New/Rename/Delete)
var optClass = $("#LivingColorList :selected").attr("class");
switch(optClass)
{
case "new":
return LivingColor.saveNewColorList(true);
case "copy":
return LivingColor.saveNewColorList(false);
case "rename":
return LivingColor.renameColorList();
case "delete":
return LivingColor.deleteColorList();
}
var newActive = $("#LivingColorList").val();
if(!newActive) return;
// Clear out old rules and refresh
LivingColor.resetAll();
LivingColor.rulesList.active = newActive;
LivingColor.loadRules(newActive);
LivingColor.render();
LivingColor.applyAll();
// Save the new active default
localStorage.setItem("LC_colorRules", JSON.stringify(LivingColor.rulesList));
},
// Clone current rules under a new name
"saveNewColorList": function(reset)
{
var newName = prompt("Save this color scheme as:");
if(!newName) return;
if($.inArray(newName, LivingColor.rulesList.names) != -1)
return alert("That name is already in use");
LivingColor.rulesList.names.push(newName);
LivingColor.rulesList.active = newName;
localStorage.setItem("LC_colorRules", JSON.stringify(LivingColor.rulesList));
localStorage.setItem("LC_colorRules"+newName, reset ? "[]" : JSON.stringify(LivingColor.rules));
LivingColor.render();
},
// Change the name of current color scheme
"renameColorList": function()
{
var newName = prompt('Choose a new name for "'+LivingColor.rulesList.active+'"');
if(!newName) return;
var index = $.inArray(LivingColor.rulesList.active, LivingColor.rulesList.names);
if(index == -1) return;
var oldName = LivingColor.rulesList.active;
LivingColor.rulesList.names[index] = newName;
LivingColor.rulesList.active = newName;
localStorage.setItem("LC_colorRules", JSON.stringify(LivingColor.rulesList));
localStorage.setItem("LC_colorRules"+newName, JSON.stringify(LivingColor.rules));
localStorage.removeItem("LC_colorRules"+oldName);
LivingColor.render();
},
// Delete color scheme, after confirming
"deleteColorList": function()
{
if(!confirm('Delete "'+LivingColor.rulesList.active+'"?'))
return;
var index = $.inArray(LivingColor.rulesList.active, LivingColor.rulesList.names);
if(index == -1) return;
// Delete
LivingColor.rulesList.names.splice(index, 1);
localStorage.removeItem("LC_colorRules"+LivingColor.rulesList.active);
// Change to previous scheme, creating a blank default if necessary
if(LivingColor.rulesList.names.length == 0)
LivingColor.rulesList.names = ["Default Scheme"];
if(index !== 0)
index--;
// Save and re-render
LivingColor.rulesList.active = LivingColor.rulesList.names[index];
localStorage.setItem("LC_colorRules", JSON.stringify(LivingColor.rulesList));
LivingColor.render();
},
// Convenience function for list of filters
"getFilterDefaults": function()
{
return {
"Brightness": {"max": 200, "default": "100"},
"Contrast": {"max": 200, "default": "100"},
"Saturate": {"max": 200, "default": "100"},
"Invert": {"max": 100, "default": "0"},
"Hue Rotate": {"max": 360, "default": "0"},
"Opacity": {"max": 100, "default": "100"},
"Blur": {"max": 50, "default": "0"},
"Sepia": {"max": 100, "default": "0"},
"Grayscale": {"max": 100, "default": "0"},
};
},
// Spawn ye olde popupe for filterse
"createFiltersPopup": function()
{
var filters = LivingColor.getFilterDefaults();
var html = '<div id="LivingColorFilters" class="LivingColorImmune"><ul>'
$.each(filters, function(name, obj)
{
var shortname = name.replace(' ', '-').toLowerCase();
html += '<li class="'+shortname+'" data-default="'+obj['default']+'"><label><span>'+name+'</span> '
+'<input type="range" min="0" max="'+obj.max+'" /> '
+'<input type="number" name="'+shortname+'" pattern="[\\-0-9]{1,3}" /><span class="suffix"></span>'
+'</label></li>';
});
html += '<li class="textarea"><textarea id="LivingColorFiltersTextarea" rows="2" readonly="readonly"></textarea></li>';
html += '<li class="reset"><button id="LivingColorFiltersReset">Reset</button></li>';
html += '</ul></div>';
var $div = $(html).hide();
$("body").append($div);
},
// Render the primary interface, creating from scratch if necessary
"render": function()
{
var $div = $("#LivingColor");
if($div.length < 1)
{
$("body").append('<div id="LivingColor" class="LivingColorImmune"></div>');
$div = $("#LivingColor");
if(LivingColor.options.minimized)
$div.addClass("minimized");
}
if(typeof LivingColor.rulesList !== "object")
LivingColor.rulesList = {};
if(LivingColor.rulesList.top !== false)
$div.addClass("top");
// Render add button
var html = '<h4><a class="target minimizehide" title="Target an element to style"><span>⌖</span></a>'
//var html = '<h4><a class="target minimizehide"><span></span></a>'
+'<span class="minimizehide">Living Color</span>'
+'<a class="move minimizehide" title="Move to the '
+(LivingColor.rulesList.top?"bottom":"top")+'"></a>'
+'<a class="minimize" title="Minimize"></a></h4>'
+'<div class="wrap"><ul></ul>'
+'<div class="controls">'
+'<div class="button" id="LivingColorAdd"><input type="button" value="+ Add Rule" /></div>';
// Render select menu for color schemes
var activeName = LivingColor.rulesList.active;
html += '<div class="button custom-select ff-hack"><select id="LivingColorList">';
$.each(LivingColor.rulesList.names, function(n, name)
{
html += '<option value="'+name+'"'+(activeName == name ? ' selected="selected"' : '')+'>'+name+'</option>';
});
html += '<option disabled="disabled"></option>';
html += '<option class="new">New Scheme...</option>';
html += '<option class="copy">Copy "'+activeName+'"...</option>';
html += '<option class="rename">Rename "'+activeName+'"...</option>';
html += '<option class="delete">Delete "'+activeName+'"...</option>';
html += '</select></div>';
html += '</div></div>';
$div.html(html);
// Render rules
html = '';
if(LivingColor.rules.length < 1)
LivingColor.rules.push({ "selector": "", "color": "", "backgroundColor": "", "filters": [] });
if(LivingColor.rules.length > 0) $.each(LivingColor.rules, function(n, rule){
html += LivingColor.renderOne(rule);
});
$div
.children(".wrap")
.children("ul")
.toggleClass("overflown", LivingColor.rules.length > 10)
.html(html)
;
// Attach color picker
jscolor.bind();
$("#jscolor").addClass("LivingColorImmune");
},
// Render each row of primary interface
"renderOne": function(rule)
{
if(!rule.filters) rule.filters = [];
return '<li>'
+ '<input type="text" name="selector" class="selector" value="'+rule.selector+'" placeholder="Selector (#id .class)" />'
+ '<input type="text" name="color" class="textColor color {required:false,onImmediateChange:\'LivingColor.liveColorChange(this.valueElement)\'}" value="'+rule.color+'" placeholder="Text" />'
+ '<input type="text" name="backgroundColor" class="backgroundColor color {required:false,onImmediateChange:\'LivingColor.liveColorChange(this.valueElement)\'}" value="'+rule.backgroundColor+'" placeholder="BG" />'
+ '<a class="filters icon'+(rule.filters.length ? " active" : "")+'" data-filters="'+rule.filters.join(" ")+'" title="Image filters: '+(rule.filters.length ? rule.filters.join(" ") : "None")+'"></a>'
+ '<a class="remove icon" title="Remove"></a>'
+ '</li>';
},
// Styles
"$style": false,
"styles":
{
"#LivingColor": "position: fixed; bottom: 10px; right: 10px; width: 320px; max-height: 350px; z-index: 9999; "
+"overflow: hidden; padding: 0; background-color: #ccc; border-radius: 5px; box-shadow: 0 0px 7px 0 black; "
+"background-image: url('"+LivingColorImageBG()+"'); transform: translateZ(0);"
+"transition: top 250ms ease-in-out,bottom 250ms ease-in-out,width 250ms ease-in-out,max-height 250ms ease-in-out;",
"#LivingColor.top": "bottom: auto; top: 10px;",
"#LivingColor h4": "position: relative; color: #ddd; background-color: #222; user-select: none; "
+"font-size: 12px; font-family: Helvetica; margin: 0; padding: 5px 0 3px; height: 12px;",
"#LivingColor h4 a": "display: block; position: absolute; cursor: pointer; opacity: 0.4;",
"#LivingColor h4 a:hover": "cursor: pointer; opacity: 0.8;",
"#LivingColor h4 a::after": "content: ''; position: absolute; display: block;",
"#LivingColor h4 .target": "top: 3px; height: 14px; left: 5px; width: 14px; color: #ddd; overflow: hidden;",
"#LivingColor h4 .target span": "font-size: 32px; position: relative; top: -19px; left: -2px;",
".living-color-targeting #LivingColor h4 .target": "color: orange; opacity: 1.0;",
"#LivingColor h4 .move": "top: 0; height: 20px; right: 25px; width: 22px;",
"#LivingColor h4 .move::after": "bottom: 4px; left: 5px; height: 0; width: 0;"
+"border-style: solid; border-width: 0 5px 10px 5px; border-color: transparent transparent #ddd transparent;",
"#LivingColor.top h4 .move::after": "border-width: 10px 5px 0 5px; border-color: #ddd transparent transparent transparent;",
"#LivingColor h4 .minimize": "right: 3px; top: 0; width: 22px; height: 20px;",
"#LivingColor h4 .minimize::after": "right: 5px; bottom: 4px; height: 3px; width: 12px;"
+"background: #ddd; border-radius: 1px;",
"#LivingColor .wrap": "padding: 0px; margin: 0; transition: width 250ms linear;",
"#LivingColor.minimized": "width: 27px; max-height: 20px; ",
"#LivingColor h4 *": "transition: opacity 120ms linear;",
"#LivingColor.minimized .wrap": "overflow: hidden;",
"#LivingColor.minimized .minimizehide": "opacity: 0;",
"#LivingColor ul": "margin: 8px 7px 0 7px; padding: 0; max-height: 290px; overflow-x: hidden; overflow-y: auto;",
"#LivingColor li": "list-style: none; white-space: nowrap; height: 29px; min-height: 29px;",
"#LivingColor li input": "font-size: 12px; line-height: 12px; padding: 5px; margin: 2px 3px;"
+"border: 0; border-radius: 3px; box-shadow: 0 0 3px 0px #444 inset;",
"#LivingColor li input::-webkit-input-placeholder": "line-height: 16px;",
"#LivingColor li input::-moz-placeholder": "line-height: 16px;",
"#LivingColor li input::placeholder": "line-height: 16px;",
"#LivingColor li .selector": "width: 124px;",
"#LivingColor .overflown .selector": "width: 104px;",
"#LivingColor li .color": "width: 48px; text-align: center; ",
"#LivingColor .controls": "margin: 7px 0 0 0; padding: 7px 7px 8px 7px; white-space: nowrap;"
+"background-color: rgba(130,125,130,0.3); box-shadow: inset 0 7px 9px -7px rgba(0,0,0,0.4);"
+"user-select: none;",
"#LivingColor .controls .custom-select": "width: 180px; margin: 0 2px;",
"#LivingColor .controls input": "margin: 1px 2px 0; border: 0; height; 12px; padding: 4px 8px 2px 8px;"
+"appearance: none; background-color: transparent; color: #444;"
+"font-size: 12px; line-height: 12px; font-weight: 700; font-family: helvetica,sans-serif;",
"#LivingColor .controls input:hover": "cursor: pointer",
"#LivingColorAdd": "float: right;",
"#LivingColor li a.icon": "cursor: pointer; display: inline-block; width: 16px; height: 16px; position: relative; top: 3px; background-size: 16px 16px; background-position: center; background-repeat: no-repeat; opacity: 0.6; ",
"#LivingColor li a.remove": "background-image: url('"+LivingColorRemoveIcon()+"');"
+"filter: grayscale(100%); margin-left: 4px;",
"#LivingColor li a.filters": "background-image: url('"+LivingColorFiltersIcon()+"');"
+"filter: brightness(10%) grayscale(100%);",