-
Notifications
You must be signed in to change notification settings - Fork 73
/
app.js
1616 lines (1426 loc) · 60.9 KB
/
app.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
/**
*
* The main ThemeRoller JavaScript file. Handles pretty much everything
*
*
* jQuery Mobile Framework : ThemeRoller
* Copyright (c) jQuery Project
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
*
* Originally created by Tyler Benziger: https://github.com/TylerBenziger
*
*/
(function( $, window, undefined ) {
//Object that can be used across multiple files to reference certain functions
TR = {};
window.TR = TR;
TR.styleArray = [];
TR.tokens = {};
TR.version = "";
TR.showStartEnd = [];
TR.firstAdd = 1;
TR.welcomed = 0;
TR.tabCount = 5;
TR.alpha = [ "global", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" ];
TR.num = { "global": 0, "a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6, "g": 7, "h": 8, "i": 9, "j": 10, "k": 11, "l": 12, "m": 13, "n": 14, "o": 15, "p": 16, "q": 17, "r": 18, "s": 19, "t": 20, "u": 21, "v": 22, "w": 23, "x": 24, "y": 25, "z": 26 };
TR.timerID = 0;
TR.movingColor = 0;
TR.firstLoad = 1;
TR.hexDigits = new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
//cleaning up a few classes and adding a few attributes so the
//inspector works properly after jQuery Mobile has done its markup injection
TR.addInspectorAttributes = function( swatch ) {
var slider = TR.iframe.find( "[name=slider][data-theme=" + swatch + "]" ).siblings( "div" ),
select = TR.iframe.find( "select[data-theme=" + swatch + "]" ),
btn = select.siblings( "a" ),
radio = TR.iframe.find( ".ui-content" ).each(function() {
$( this ).find( ".ui-radio:first span:first" ).removeClass( "ui-corner-top" );
});
slider.attr( "data-form", "ui-btn-down-" + swatch ).attr( "data-theme", swatch );
slider.find( "a" ).attr( "data-form", "ui-btn-up-" + swatch ).attr( "data-theme", swatch );
btn.attr( "data-theme", swatch ).attr( "data-form", "ui-btn-up-" + swatch );
btn.find( ".ui-icon" ).attr( "data-form", "ui-icon" );
}
//adds a most-recent-color to the five right-most draggables in the quickswatch panel
TR.addMostRecent = function( color ) {
var found = 0, quickswatch = $( "#quickswatch" );
quickswatch.find( ".color-drag:gt(31)" ).each(function() {
if( color == $(this).css("background-color") ) {
found = 1;
}
});
if( !found ) {
quickswatch.find( ".color-drag:last" ).remove();
quickswatch.find( ".colors .color-drag:eq(31)" ).removeClass( "separator" );
var new_color = $( "<div class=\"color-drag separator\" style=\"background-color: " + color + "\"></div>" );
new_color.draggable({
appendTo: "body",
revert: true,
revertDuration: 200,
opacity: 1,
containment: "document",
cursor: "move",
helper: "clone",
zIndex: 3000,
iframeFix: true,
drag: function() {
TR.movingColor = 1;
}
});
quickswatch.find( ".colors .color-drag:eq(30)" ).after( new_color );
}
}
//adds some CSS, a tab panel, and a preview for the new swatch
TR.addSwatch = function( new_style ) {
$( ".delete-swatch-a" ).show();
if( TR.tabCount < 28 ) {
var next_tab = TR.tabCount + 1,
lower = TR.alpha[TR.tabCount - 1],
upper = lower.toUpperCase();
//new_style flag is only set if styles have not been added to TR.styleArray
//or CSS yet. correctNumberOfSwatches calles addSwatch with new_style=false
//so this block is skipped
if( new_style ) {
//defining TR.styleArray indices
var indices = [],
reg = new RegExp( "a-.*" );
for( var j in TR.styleArray ) {
if( j.search(reg) != -1 ) {
indices.push( j );
}
}
for( var k in indices ) {
var index = indices[k] + "",
suffix = index.substr( 1, index.length - 1 );
TR.styleArray[lower + suffix] = TR.styleArray[index];
}
//adding swatch to CSS
reg = new RegExp( "\\/\\* " + upper + "\\s*\\n-*\\*\\/" );
var css = TR.styleBlock.text(),
start = css.search( /\/\* A.*\n-*\*\// ),
end;
if(TR.tabCount > 3) {
end = css.search( /\/\* B.*\n-*\*\// );
} else {
end = css.search( /\/\* Structure /);
}
var swatch_a = css.substring( start, end ),
temp_css_template = swatch_a.replace( /-a,/g, "-" + lower + "," ).replace( /-a\s/g, "-" + lower + " " )
.replace( /{a-/g, "{" + lower + "-" ).replace( /\/\*\sA/, "/* " + upper ),
current_css = TR.styleBlock.text();
current_css = current_css.replace( /\/\*\sStructure\s/, temp_css_template + "\n\n/* Structure " );
TR.styleBlock.text( current_css );
}
//giving the contents of the new tab
var temp_panel_template = TR.panel_template.replace( /Swatch A/, "Swatch " + upper )
.replace( /"a\-/g, "\"" + lower + "-" ).replace( /\-a"/g, "-" + lower + "\"" );
$( "#tabs" ).tabs( "add", "#tab" + (TR.tabCount + 1), "+" )
.find( "ul li a[href=#tab" + TR.tabCount + "]" ).text( upper );
$( "#tab" + TR.tabCount ).html( temp_panel_template );
TR.updateFormValues( $("#tab" + TR.tabCount) );
//adding swatch to preview document
var temp_swatch_template = TR.swatch_template.replace( /"a"/g, "\"" + lower + "\"" ).replace( />A<\/h1>/g, ">" + upper + "</h1>" )
.replace( /-a\s/g, "-" + lower + " " ).replace( /-a\"/g, "-" + lower + "\"" );
$( temp_swatch_template ).insertAfter( TR.iframe.find(".swatch:last") );
var iframe_window = $( "iframe" )[0].contentWindow;
//This is a bug in JQM. Header initialization is using a live pagecreate handler on the page
//ideally we should be able to write iframe_window.$(".swatch:last").trigger("create");
iframe_window.$( ".ui-page" ).trigger( "pagecreate" );
//adding data-form attribute to slider
TR.addInspectorAttributes( lower );
//redefine the token array
TR.initStyleArray( "refresh" );
//binds all appropriate events for accordions and new tab
TR.updateThemeRoller( TR.tabCount );
//adjust the height of the add-swatch box
var swatch_height = TR.iframe.find( ".swatch:last" ).outerHeight();
TR.iframe.find( ".add-swatch" ).height( swatch_height );
if( TR.firstAdd ) {
//apply paging of the tabs
$( "#tabs" ).tabs("paging", {
cycle: true,
follow: true,
tabsPerPage: 0,
followOnSelect: true,
selectOnAdd: false
});
TR.firstAdd = 0;
}
TR.refreshIframe( TR.alpha[TR.tabCount - 1] );
TR.iframe.find( "[data-role=dialog]" ).remove();
//reconfigure binding of addSwatch event
$( "[href=#tab" + TR.tabCount + "]" ).unbind( "click", TR.addSwatchEvent );
$( "[href=#tab" + next_tab + "]" ).bind( "click", TR.addSwatchEvent );
TR.tabCount++;
}
}
//used for binding and unbinding to addSwatch
TR.addSwatchEvent = function(e) {
e.preventDefault();
TR.addSwatch( true );
}
//When a color is dropped this function applies a color to the element
//automatically detecting things like
TR.applyColor = function( color, prefix ) {
var color_arr = color.split( "" ),
red = parseInt( (color_arr[1] + color_arr[2]), 16 ),
green = parseInt( (color_arr[3] + color_arr[4]), 16 ),
blue = parseInt( (color_arr[5] + color_arr[6]), 16 ),
gray = TR.grayValue( color ),
swatch = prefix.substr( 0, 1 ),
element = prefix.substr( 2, prefix.length - 2 );
element = prefix.split( "-" );
element[0] = "";
element = element.join( "" );
//if we're on a button hover we call this same function with button down as element
if( element == "bhover" ) {
color = TR.percentColor( color, 1.15 );
TR.applyColor( color, swatch + "-bdown");
}
//if we're on a button up we call same function with button hover
//which then calls it with button down
if( element == "bup" ) {
TR.applyColor( color, swatch + "-bhover" );
}
//if we're on button down then the gradient gets flipped
var start, end;
if( element != "bdown" ) {
start = TR.computeGradient(color, 50)[0];
end = TR.computeGradient(color, 50)[1];
} else {
start = TR.computeGradient(color, 50)[1];
end = TR.computeGradient(color, 50)[0];
}
if( element != "link" ) {
//anything but a link has gradients and text color
$( "input[data-name=" + prefix + "-background-color]" ).val( color ).css( "background-color", color );
$( "input[data-name=" + prefix + "-background-start]" ).val( start ).css( "background-color", start );
$( "input[data-name=" + prefix + "-background-end]" ).val( end ).css( "background-color", start );
$( ".slider[data-name=" + prefix + "-background-color]" ).slider( {value: 50} )
.find( "a" ).css({"background-color": color, "border-color": color});
TR.styleArray[prefix + "-background-start"] = start;
TR.styleArray[prefix + "-background-end"] = end;
TR.styleArray[prefix + "-background-color"] = color;
//special border for content body elements
if( element != "body" ) {
$( "input[data-name=" + prefix + "-border]" ).val( color ).css( "background-color", color );
TR.styleArray[prefix + "-border"] = color;
} else {
var border;
if( gray > 90 ) {
border = TR.percentColor( color, 0.55 );
} else {
border = TR.percentColor( color, 1.4 );
}
$( "input[data-name=" + prefix + "-border]" ).val( border ).css( "background-color", border );
TR.styleArray[prefix + "-border"] = border;
}
//contrast calculation for text color
if( gray > 150 ) {
$( "input[data-name=" + prefix + "-color]" ).val( "#000000" ).css( "background-color", "#000000" );
$( "input[data-name=" + prefix + "-shadow-color]" ).val( "#eeeeee" ).css( "background-color", "#eeeeee" );
TR.styleArray[prefix + "-color"] = "#000000";
TR.styleArray[prefix + "-shadow-color"] = "#eeeeee";
} else {
$( "input[data-name=" + prefix + "-color]" ).val( "#ffffff" ).css( "background-color", "#ffffff" );
$( "input[data-name=" + prefix + "-shadow-color]" ).val( "#444444" ).css( "background-color", "#444444" );
TR.styleArray[prefix + "-color"] = "#ffffff";
TR.styleArray[prefix + "-shadow-color"] = "#444444";
}
} else {
//links are lighter on hover and darker once visited
var lighter = TR.percentColor( color, 1.15 );
var darker = TR.percentColor( color, 0.65 );
TR.styleArray[swatch + "-body-link-color"] = color;
TR.styleArray[swatch + "-body-link-active"] = color;
TR.styleArray[swatch + "-body-link-hover"] = lighter;
TR.styleArray[swatch + "-body-link-visited"] = darker;
$( "input[data-name=" + swatch + "-body-link-color]" ).val( color ).css( "background-color", color );
$( "input[data-name=" + swatch + "-body-link-active]" ).val( color ).css( "background-color", color );
$( "input[data-name=" + swatch + "-body-link-hover]" ).val( darker ).css( "background-color", lighter );
$( "input[data-name=" + swatch + "-body-link-visited]" ).val( lighter ).css( "background-color", darker );
}
//we've updated TR.styleArray now update the CSS
TR.updateAllCSS();
}
//pass in a color and a slider value and get back a tuple with start and end colors
//of a gradient - the slider value is a number between 0 and 100
//greater than 50 -> convex gradient, less than 50 -> concave
TR.computeGradient = function( color, slider_value ) {
var color_arr = color.split( "" );
var red = parseInt( (color_arr[1] + color_arr[2]), 16 );
var green = parseInt( (color_arr[3] + color_arr[4]), 16 );
var blue = parseInt( (color_arr[5] + color_arr[6]), 16 );
var convex, red_start, green_start, blue_start, percent;
if( slider_value >= 40 ) {
convex = 1;
percent = 1 + ( slider_value - 40 ) / 100;
} else {
convex = 0;
percent = 1 + ( 40 - slider_value ) / 100;
}
if( percent * red > 255 ) {
red_start = "FF";
} else {
red_start = TR.padNumber( Math.floor(percent * red).toString( 16 ), 2 );
}
if( percent * green > 255 ) {
green_start = "FF";
} else {
green_start = TR.padNumber( Math.floor(percent * green).toString( 16 ), 2 );
}
if( percent * blue > 255 ) {
blue_start = "FF";
} else {
blue_start = TR.padNumber( Math.floor(percent * blue).toString( 16 ), 2 );
}
if( convex ) {
percent = ( 100 - (slider_value - 40) ) / 100;
} else {
percent = ( 100 - (40 - slider_value) ) / 100;
}
var red_end = TR.padNumber( Math.floor(percent * red).toString( 16 ), 2 );
var green_end = TR.padNumber( Math.floor(percent * green).toString( 16 ), 2 );
var blue_end = TR.padNumber( Math.floor(percent * blue).toString( 16 ), 2 );
var start, end;
if( convex ) {
start = "#" + red_start + "" + green_start + "" + blue_start + "";
end = "#" + red_end + "" + green_end + "" + blue_end + "";
} else {
start = "#" + red_end + "" + green_end + "" + blue_end + "";
end = "#" + red_start + "" + green_start + "" + blue_start + "";
}
return [start, end];
}
//method used to calculate the number of swatches in the CSS and adjusts
//the number of tab panels in TR and the number of swatches in the preview
TR.correctNumberOfSwatches = function() {
var style = TR.styleBlock.text();
var matches = style.match( /\/\*\s[A-Z]\s*-*\*\//g );
if( !matches ) {
if(TR.firstLoad) {
TR.firstLoad = 0;
//initial load - the theme specified cannot be found on the server
var error_message = $( "<h3>Invalid Theme</h3><p>Reminder: We can only store this theme URL on the server for 30 days, then it will be deleted. \
Download a theme to keep a copy safe that you can import later.</p>" ).css( "color", "#f00" );
$( "#welcome h1" ).after( error_message );
//import default theme
$.ajax({
url: "jqm/" + TR.version + "/default.css",
dataType: "text",
mimeType: "text/plain",
success: function( data ) {
$( "#upload textarea" ).val( data );
TR.styleBlock.text( data );
correctNumberOfSwatches();
}
});
} else {
//alert( "Invalid theme file. Please import unminified files only with all original CSS comments in place." );
}
//return early because the file is of the incorrect format
return;
} else {
if(TR.firstLoad) {
TR.firstLoad = 0;
}
}
var swatch_counter = matches.length + 2;
//start at A and go over current tabs and update form values
for( var i = 1; i < TR.tabCount; i++ ) {
TR.updateFormValues( $("#tab" + i) );
}
//add appropriate number of tabs to TR and swatches to preview
for( ; TR.tabCount < swatch_counter && TR.tabCount < 28; ) {
TR.addSwatch( false );
}
//remove swatches from preview if necessary and tabs
for( ; TR.tabCount > swatch_counter; TR.tabCount-- ) {
TR.iframe.find( ".swatch:last" ).remove();
$( "#tabs .ui-tabs-panel:last" ).attr( "id", "tab" + (TR.tabCount - 1) );
$( "#tabs ul li a:contains(\"+\")" ).attr( "href", "#tab" + (TR.tabCount - 1) );
if( $("#tabs").tabs("option", "selected") == (TR.tabCount - 2) ) {
$( "#tabs" ).tabs( "select", 0 );
}
$( "#tabs" ).tabs( "remove", TR.tabCount - 2 );
}
}
//using the TR.styleArray to successfully copy over data to appropriate swatches
//and then deleting last swatch so the one clicked appears to be deleted
TR.deleteSwatch = function( e, ele ) {
e.preventDefault();
var delete_class = ele.attr( "class" );
var letter = delete_class.substr( delete_class.length - 1, delete_class.length );
var number = TR.num[letter];
//moving content of the tabs back one starting at the one we're deleting
for( var i = number + 1; i <= TR.tabCount - 2; i++ ) {
var current_letter = TR.alpha[i];
var current_number = TR.num[current_letter];
var indices = [];
for( var j in TR.styleArray ) {
var reg = new RegExp( "^" + current_letter + "-.*" );
if( j.match(reg) ) {
indices.push( j.match(reg) );
}
}
for( var k in indices ) {
var index = indices[k] + "";
var suffix = index.substr( 1, index.length - 1 );
TR.styleArray[TR.alpha[current_number - 1] + suffix] = TR.styleArray[index];
if( (current_number + 2) == TR.tabCount ) {
delete( TR.styleArray[index] );
}
}
TR.updateFormValues( $("#tab" + i) );
}
//deleting the last swatch's rules in the TR.styleArray
var last_letter = TR.alpha[TR.tabCount - 2];
var prefix = last_letter + "-";
var newStyleArray = {};
for( var i in TR.styleArray ) {
if( i.indexOf(prefix) != 0 ) {
newStyleArray[i] = TR.styleArray[i];
}
}
TR.styleArray = newStyleArray;
//delete the swatch's CSS from the file
var css = TR.styleBlock.text();
var start_reg = new RegExp ("\\/\\* " + TR.alpha[TR.tabCount - 2].toUpperCase() + "\\s*\\n-*\\*\\/" );
var end_reg = new RegExp( "\\/\\* Structure \\*\\/" );
var start = css.search( start_reg );
var end = css.search( end_reg );
var part1 = css.substring(0, start);
var part2 = css.substring(end, css.length);
TR.styleBlock.text( part1 + part2 );
TR.tabCount--;
$( "#tabs" ).find( ".ui-tabs-panel:last" ).attr( "id", "tab" + TR.tabCount ).end()
.find( "ul li a:contains(\"+\")" ).attr( "href", "#tab" + TR.tabCount );
TR.iframe.find( ".swatch:last" ).remove();
if( $("#tabs").tabs("option", "selected") == TR.tabCount - 1 ) {
$( "#tabs" ).tabs( "select", TR.tabCount - 2 );
}
$( "#tabs" ).tabs( "remove", TR.tabCount - 1 );
if( TR.tabCount == 3 ) {
$( ".delete-swatch-a" ).hide();
}
var swatch_height = TR.iframe.find( ".swatch:last" ).outerHeight();
TR.iframe.find( ".add-swatch" ).height(swatch_height);
TR.initStyleArray( "refresh" );
TR.updateAllCSS();
return false;
}
//initializes the dialogs
TR.dialogs = function() {
$( "#welcome" ).dialog({
autoOpen: false,
width: 560,
modal: true,
resizable: false,
draggable: false,
buttons: {
"Get Rolling": function() {
$( this ).dialog( "close" );
}
}
});
$( "#share" ).dialog({
autoOpen: false,
modal: true,
width: 800,
resizable: false,
draggable: false,
buttons: {
"Done": function() {
$( this ).dialog( "close" );
}
}
});
$( "#tr_window" ).dialog({
autoOpen: false,
modal: false,
width: 320,
height: "auto",
position: [0, 116],
maxWidth: 320,
dialogClass: "tr_widget"
});
$( "#importing" ).dialog({
autoOpen: false,
modal: true,
resizeable: false,
draggable: false,
height: 70
});
$( "#upload" ).dialog({
autoOpen: false,
modal: true,
width: 800,
height: 430,
resizable: false,
draggable: false,
buttons: {
"Cancel": function() {
$( "#upload" ).dialog( "close" );
},
"Import": function() {
$( "#upload" ).dialog( "close" );
if( $( "#load-css" ).val() != "" ) {
TR.styleBlock.text( $("#load-css").val() );
TR.initStyleArray();
TR.correctNumberOfSwatches();
}
}
}
});
$( "#help" ).dialog({
autoOpen: false,
width: 700,
height: 400,
modal: true,
resizable: false,
draggable: false,
buttons: {
"Close": function() {
$( this ).dialog( "close" );
}
}
});
$( "#download" ).dialog({
autoOpen: false,
modal: true,
width: 850,
resizable: false,
draggable: false,
buttons: {
"Close": function() {
$( this ).dialog( "close" );
},
"Download Zip": function() {
var theme_name = $( "input", this ).val();
if( theme_name && theme_name.indexOf(" ") == -1 ) {
$.ajax({
url: "./zip.php",
type: "POST",
data: "theme_name=" + $( "input", this ).val() + "&file=" + encodeURIComponent(TR.styleBlock.text()),
dataType: "text",
mimeType: "text/plain",
beforeSend: function() {
//loading gif here
},
success: function(response) {
window.location = response;
}
});
} else {
alert( "Invalid theme name" );
}
}
}
});
$( "#import-default" ).click(function(e) {
e.preventDefault();
$.ajax({
url: "jqm/" + TR.version + "/default_theme.css",
dataType: "text",
mimeType: "text/plain",
success: function( data ) {
$( "#load-css" ).val( data );
}
});
});
//ajax call performed when share link is clicked
$( "#generate_url" ).click(function(e) {
e.preventDefault();
$( "#share" ).dialog( "open" );
var post_data = "file=" + TR.styleBlock.text();
$.ajax({
type: "post",
url: "share.php",
data: post_data,
beforeSend: function() {
$( "#share" ).dialog("open" );
},
success: function( data ) {
$( "#share input" ).val( data );
}
});
});
//help dialog
$( "#tr_help" ).click(function(e) {
e.preventDefault();
$( "#help" ).dialog( "open" );
});
//upload dialog
$( "#tr_upload" ).click(function() {
$( "#upload" ).dialog( "open" );
return false;
});
//download dialog
$( "#tr_download" ).click(function() {
$( "#download" ).dialog( "open" );
return false;
});
//removing the close button from ui-dialogs
$( ".tr_widget .ui-dialog-titlebar-close" ).remove();
}
//initializes the draggable colors and the drop method
TR.draggableColors = function() {
//draggable colors
$( ".color-drag:not(.disabled)" ).draggable({
appendTo: "body",
revert: true,
revertDuration: 200,
opacity: 1,
containment: "document",
cursor: "move",
helper: "clone",
zIndex: 3000,
iframeFix: true,
drag: function() {
TR.movingColor = 1;
}
});
//droppable for colorwell
$( ".colorwell" ).droppable({
accept: ".color-drag",
hoverClass: "hover",
drop: function() {
var $this = $( this );
var color = TR.rgbtohex( $(".ui-draggable-dragging").css("background-color") );
$( ".ui-draggable .ui-draggable-dragging" ).trigger( "drop" );
$this.val( color ).css( "background-color", color );
$this.trigger( "change" );
}
});
//large mouseup event detects if the user is dragging a color
//if so it runs throught the dom to see if the mouse position is above
//an acceptable element, if so it calls applyColor on that element
$(document).mouseup(function(e) {
var classtokey = {
".ui-bar-" : "-bar",
".ui-body-" : "-body",
".ui-btn-up-": "-bup",
".ui-link": "-link",
".ui-btn-active": "-active"
}
//different alpha array - no global
var alphabet = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" ];
var droppables = [ ".ui-btn-up-", ".ui-bar-", ".ui-body-" ];
if(TR.movingColor) {
TR.movingColor = 0;
var frame_offset = $( "iframe" ).offset();
var el_offset = TR.iframe.find( ".ui-bar-a" ).offset();
var element = null;
var el_class = "";
//loop through possible classes and if mouse is above one,
//end the loop (order is based on precedence)
for( var i = 0; i < droppables.length; i++ ) {
for( var j = 0; j < alphabet.length; j++ ) {
TR.iframe.find( droppables[i] + alphabet[j] ).each(function() {
var $this = $( this );
var top = frame_offset.top + $this.offset().top - TR.iframe.scrollTop(), bottom = top + $this.outerHeight(),
left = frame_offset.left + $this.offset().left, right = left + $this.outerWidth();
if( e.pageX <= right && e.pageX >= left && e.pageY <= bottom && e.pageY >= top ) {
el_class = droppables[i] + alphabet[j];
element = $this;
return false;
}
});
if( element ) {
break;
}
}
if( element ) {
break;
}
}
//another loop to check if over a link or the active state
TR.iframe.find( ".ui-link, .ui-btn-active" ).each(function() {
var $this = $( this );
var top = frame_offset.top + $this.offset().top - TR.iframe.scrollTop(), bottom = top + $this.outerHeight(),
left = frame_offset.left + $this.offset().left, right = left + $this.outerWidth();
if( e.pageX <= right && e.pageX >= left && e.pageY <= bottom && e.pageY >= top ) {
if( $this.hasClass("ui-btn-active") ) {
el_class = ".ui-btn-active";
} else {
el_class = ".ui-link";
}
element = $this;
return false;
}
});
if( !element ) {
//if no acceptable element was found, drop the color
$( ".ui-draggable .ui-draggable-dragging" ).trigger( "drop" );
} else {
//else apply the color and select the element in the panel
var swatch = element.attr( "data-theme" );
if( el_class == ".ui-btn-active" ) {
swatch = "global";
}
if( el_class.indexOf(".ui-bar") != -1 ) {
swatch = element.attr( "data-swatch" );
}
var color = $( ".color-drag.ui-draggable-dragging" ).css( "background-color" ) || $( ".kuler-color.ui-draggable-dragging" ).css( "background-color" );
color = TR.rgbtohex( color );
for( var i in classtokey ) {
if( el_class.indexOf(i) != -1 ) {
TR.applyColor( color, swatch + classtokey[i] );
break;
}
}
TR.selectElement(element);
//store color in most recent colors
var color = element.css("background-color");
TR.addMostRecent( color );
}
}
});
//triggering change on current colorwell forces the update of the css as well as
//the change in background of the slider
$( "#colorpicker" ).bind( "mousemove", function() {
$( ".colorwell[data-name=" + $("*:focus").attr("data-name") + "]" ).trigger( "change" );
});
}
//takes a hex color and computes the grayscale value
TR.grayValue = function( color ) {
var color_arr = color.split( "" );
var red = parseInt( ( color_arr[1] + color_arr[2] ), 16 );
var green = parseInt( ( color_arr[3] + color_arr[4] ), 16 );
var blue = parseInt( ( color_arr[5] + color_arr[6] ), 16 );
return ( red + green + blue ) / 3;
}
TR.hex = function(x) {
return isNaN(x) ? "00" : TR.hexDigits[(x - x % 16) / 16] + TR.hexDigits[x % 16];
}
//sets up bindings for the addSwatch method
TR.initAddSwatch = function() {
var swatch_height = TR.iframe.find( ".swatch:last" ).outerHeight();
TR.iframe.find( ".add-swatch" ).height(swatch_height);
TR.iframe.find( "#add-swatch" ).bind( "click", TR.addSwatchEvent );
$( "[href=#tab" + TR.tabCount + "]" ).bind( "click", TR.addSwatchEvent );
}
//binds TR controls to updateAllCSS
//also takes care of things like slider handle color, etc.
TR.initControls = function() {
//Font Family
$( "[data-type=font-family]" ).bind( "blur change keyup", function() {
var name = $( this ).attr( "data-name" );
TR.styleArray[name] = "font-family: " + this.value;
TR.updateAllCSS();
});
//Link, Text Color, Text Shadow, Border Color
$( "[data-type=link], [data-type=color], [data-type=text-shadow], [data-type=border]" )
.bind( "blur change keyup", function(){
TR.styleArray[$( this ).attr( "data-name" )] = this.value;
TR.updateAllCSS();
});
//Corner Radius
$( "input[data-type=radius]" ).bind("change keyup mouseup", function() {
var $this = $( this );
var name = $this.attr( "data-name" );
var slider = $( ".slider[data-type=radius][data-name=" + name + "]" );
var val = parseFloat( $this.val().replace(/[^0-9\.]/g, "") );
slider.slider( "value", val );
TR.styleArray[name] = $this.val();
TR.updateAllCSS();
});
//Corner Radius
$( ".slider[data-type=radius]" ).bind("change slide mouseup", function() {
var $this = $( this );
var name = $this.attr( "data-name" );
var input = $( "input[data-type=radius][data-name=" + name + "]" );
input.val( $this.slider("value") + "em" );
TR.styleArray[name] = input.val();
TR.updateAllCSS();
});
//Background Colors
$( "[data-type=background]" ).bind( "blur slide mouseup change keyup", function(event, slider) {
if( !TR.timerID ) {
TR.timerID = setTimeout(function() {
TR.timerID = 0;
}, 100);
var index = $( this ).attr( "data-name" ) + "";
index = index.split( "-" );
var prefix = index[0] + "-" + index[1];
var color = $( "input[data-type=background][data-name|=" + prefix + "]" ).val();
var slider_value = $( ".slider[data-type=background][data-name|=" + prefix + "]" ).slider( "value" );
var start_end = TR.computeGradient(color, slider_value);
var start = start_end[0];
var end = start_end[1];
$( "[data-type=start][data-name=" + prefix + "-background-start]" ).val( start ).css( "background-color", start );
$( "[data-type=end][data-name=" + prefix + "-background-end]" ).val( end ).css( "background-color", end );
TR.styleArray[prefix + "-background-color"] = color;
TR.styleArray[prefix + "-background-start"] = start;
TR.styleArray[prefix + "-background-end"] = end;
TR.updateAllCSS();
}
});
//Start and End colors
$( "[data-type=start] , [data-type=end]" ).bind( "blur mouseup change keyup", function() {
var index = $( this ).attr( "data-name" ) + "";
index = index.split( "-" );
var prefix = index[0] + "-" + index[1];
var start = $( "[data-type=start][data-name=" + prefix + "-background-start]" ).val();
var end = $( "[data-type=end][data-name=" + prefix + "-background-end]" ).val();
TR.styleArray[prefix + "-background-start"] = start;
TR.styleArray[prefix + "-background-end"] = end;
TR.updateAllCSS();
});
//Icon Set
$( "[data-type=icon_set]" ).bind( "blur mouseup", function() {
TR.styleArray[$( this ).attr( "data-name" )] = "url(../jqm/" + TR.version + "/images/icons-18-" + this.value + ".png)";
TR.styleArray["global-large-icon-set"] = "url(../jqm/" + TR.version + "images/icons-36-" + this.value + ".png)";
TR.updateAllCSS();
});
//Box Shadow
$( "[data-type=box_shadow]" ).bind( "blur change keyup", function() {
var color_el = $( "[data-type=box_shadow]:first" );
var opac_el = $( "[data-type=box_shadow]:eq(1)" );
var color_arr = color_el.val().split( "" );
var red = parseInt( (color_arr[1] + color_arr[2]), 16 );
var green = parseInt( (color_arr[3] + color_arr[4]), 16 );
var blue = parseInt( (color_arr[5] + color_arr[6]), 16 );
TR.styleArray["global-box-shadow-color"] = "rgba(" + red + "," + green + "," + blue + "," + ( parseFloat(opac_el.val()) / 100 ) + ")";
TR.updateAllCSS();
});
//Box Shadow
$( "[data-type=box_shadow][data-name=global-box-shadow-size]" ).bind( "blur change keyup", function() {
TR.styleArray["global-box-shadow-size"] = $(this).val();
TR.updateAllCSS();
});
//Icon Disc
$( "[data-type=icon_disc]" ).bind( "blur change keyup", function() {
var elements = [];
$( "[data-name=global-icon-disc]" ).each(function() {
elements.push( this.value );
});
color_arr = elements[1].split( "" );
var red = parseInt( (color_arr[1] + color_arr[2]), 16 );
var green = parseInt( (color_arr[3] + color_arr[4]), 16 );
var blue = parseInt( (color_arr[5] + color_arr[6]), 16 );
if( elements[0] == "with_disc" ) {
TR.styleArray["global-icon-color"] = "#FFFFFF";
TR.styleArray["global-icon-disc"] = "rgba(" + red + "," + green + "," + blue + "," + ( parseFloat(elements[2]) / 100 ) + ")";
TR.styleArray["global-icon-shadow"] = "rgba(255,255,255,.4)";
} else {
TR.styleArray["global-icon-disc"] = "transparent";
TR.styleArray["global-icon-shadow"] = "transparent";
}
TR.updateAllCSS();
});
//global more dictionary keeps track of which start-end fields are showing
//i.e. more["a-bar-background"] = "showing" means that the next click of that .more should
//hide that set of start-end fields
$( ".more" ).click(function() {
var index = $( this ).attr( "data-name" );
if( TR.showStartEnd[index] ) {
if( TR.showStartEnd[index] == "showing" ) {
$( ".start-end[data-name=" + index + "]" ).hide( "slide", {
direction: "up"
}, 300);
$( this ).text( "+" );
TR.showStartEnd[index] = "hiding";
} else {
$( ".start-end[data-name=" + index + "]" ).show( "slide", {
direction: "up"
}, 300);
$( this ).text( "-" );
TR.showStartEnd[index] = "showing";
}
} else {
$( ".start-end[data-name=" + index + "]" ).show( "slide", {
direction: "up"
}, 300 );
$( this ).text( "-" );
TR.showStartEnd[index] = "showing";
}
return false;
});
//change colorwell -> change bg color of its corresponding slider
$( ".colorwell" ).bind( "change" , function() {
var $this = $( this );
$( ".slider[data-name=" + $this.attr("data-name") + "][data-type=" + $this.attr("data-type") + "] a").css({
"background": $this.val(),
"border-color": $this.val()
});
});
//initialize colors of sliders
$( "input[data-type=background]" ).each(function() {
var $this = $( this )
$( ".slider[data-type=background][data-name=" + $this.attr("data-name") + "] a" ).css({
"background": $this.val(),
"border-color": $this.val()
});
});
$( "[class|=\"delete-swatch\"]" ).click( function(e) {
TR.deleteSwatch( e, $(this) );
});
}
//intialize mouseover and click events for Inspector
TR.initInspector = function() {
//click on an element with inspector-on and select element in panel
TR.iframe.find( "[data-form]" ).mouseup(function(e) {
if( $("[data-id=inspector-on]").hasClass("on") ) {
e.stopPropagation();
TR.selectElement( $(this) );
frame.find( "#highlight" ).show();
}
});
//highlight moves as you mouseover things
TR.iframe.find( "[data-form]" ).bind( "mouseover", function() {
var $this = $( this );
if( $("[data-id=inspector-on]").hasClass("on") ) {
var left = $this.offset().left, top = $this.offset().top,
width = $this.outerWidth(), height = $this.outerHeight();
var parent = this;