-
Notifications
You must be signed in to change notification settings - Fork 26
/
tileeditor.js
2147 lines (1931 loc) · 84.9 KB
/
tileeditor.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
/* Tile Editor for HousePanel
*
* Original version by @nitwit on SmartThings forum
* heavily modified by Ken Washington @kewashi on the forum
*
* Designed for use only with HousePanel for Hubitat and SmartThings
* (c) Ken Washington 2017 - 2020
*
*/
var et_Globals = {};
var savedSheet;
var priorIcon = "none";
var defaultOverlay = "block";
var tileCount = 0;
$.fn.isAuto = function(dimension){
// will detect auto widths including percentage changes
if (dimension == 'width'){
var originalWidth = this.css("width");
var parentWidth = this.parent().css("width");
// pick some weird big number
var testWidth = 2000;
this.parent().css({width: testWidth});
var newWidth = this.css("width");
this.parent().css({width: parentWidth});
// console.log(originalWidth, newWidth, parentWidth);
if ( newWidth > originalWidth ) {
return true;
} else{
return false;
}
} else if (dimension == 'height'){
var originalHeight = this.height();
// this.append('<div id="testzzz"></div>');
// var testHeight = originalHeight+500;
// $('#testzzz').css({height: testHeight});
var newHeight = this.height();
// $('#testzzz').remove();
if( newHeight > originalHeight ) {
return true;
} else{
return false;
}
} else {
return false;
}
};
function getOnOff(str_type, subid) {
var onoff = ["",""];
// handle the cases for custom tiles that could have any subid starting with valid names
if ( subid.startsWith("switch" ) ) {
onoff = ["on","off","flash"];
} else if ( (str_type==="momentary") && subid.startsWith("momentary" ) ) {
onoff = ["on","off"];
} else if ( subid.startsWith("contact" ) || subid.startsWith("door" ) || subid.startsWith("valve" ) ) {
onoff = ["open","closed"];
} else if ( subid.startsWith("lock" ) ) {
onoff = ["locked","unlocked"];
} else if ( subid.startsWith("motion") ) {
onoff = ["active","inactive"];
} else if ( subid.startsWith("pistonName" ) ) {
onoff = ["firing","idle"];
} else if ( subid.startsWith("thermostatFanMode" ) ) {
onoff = ["auto","on"];
} else if ( subid.startsWith("thermostatMode" ) ) {
onoff = ["heat","cool","auto","off"];
} else if ( subid.startsWith("thermostatOperatingState" ) ) {
onoff = ["idle","heating","cooling","off"];
} else if ( subid.startsWith("musicstatus" ) || subid.startsWith("playbackStatus") ) {
onoff = ["stopped","paused","playing"];
} else if ( subid.startsWith("musicmute" ) || (str_type==="audio" && subid.startsWith("mute")) ) {
onoff = ["muted","unmuted"];
} else if ( subid.startsWith("presence" ) ) {
onoff = ["present","absent"];
} else if ( subid.startsWith("state" ) ) {
onoff = ["Away","Home","Night","Disarmed"];
}
return onoff;
}
function getCssRuleTarget(str_type, subid, thingindex, useall) {
// get the scope to use
var scope = $("#scopeEffect").val();
// if ( useall!==0 && useall!==1 && useall!==2 ) {
if ( scope=== "alltypes") { useall= 1; }
else if ( scope=== "alltiles") { useall= 2; }
else { useall = 0; }
// }
if ( useall!==0 && useall!==1 && useall!==2 ) {
useall= 0;
}
var target = "";
if ( str_type==="page" || str_type==="panel" ) {
if ( subid==="tab" ) {
target = "li.ui-tabs-tab.ui-state-default";
if ( useall===0 ) { target+= '.tab-'+thingindex; }
// target+= ",.tab-" +thingindex + ">a.ui-tabs-anchor";
} else if ( subid==="tabon" ) {
target = "li.ui-tabs-tab.ui-state-default.ui-tabs-active";
if ( useall===0 ) { target+= '.tab-'+thingindex; }
// target+= ",.tab-" +thingindex + ">a.ui-tabs-anchor";
} else if ( subid==="head" ) {
target = "div.thingname.page";
if ( useall < 2 ) {
target+= '.t_'+thingindex;
}
} else {
target = "div.panel";
if ( useall < 1 ) { target+= '.panel-'+thingindex; }
}
// if a tile isn't specified we default to changing all things
} else if ( thingindex===null || thingindex===undefined || thingindex==="all" ) {
target = "div.thing";
if ( str_type && useall < 2 ) {
target+= "." + str_type + "-thing";
}
} else if ( subid==="head" ) {
target = "div.thingname";
if ( useall < 2 ) { target+= "." + str_type; }
if ( useall < 1 ) {
target+= '.t_'+thingindex;
// target+= " span.n_"+thingindex;
}
// handle special case when whole tile is being requested
} else if ( subid==="wholetile" || subid==="tile" ) {
target = "div.thing";
if ( useall < 2 ) { target+= "." + str_type + "-thing"; }
if ( useall < 1 ) { target+= '.p_'+thingindex; }
} else if ( subid==="overlay" ) {
target = "div.overlay";
if ( useall < 2 ) {
if ( subid.startsWith("music-") ) {
target+= ".music-controls";
} else {
target+= "." + subid;
}
}
if ( useall < 1 ) { target+= '.v_'+thingindex; }
// main handling of type with subid specific case
// starts just like overlay but adds all the specific subid stuff
} else {
// handle music controls special case
// target = "div." + str_type + "-thing div.overlay";
if ( useall===2 ) {
target = "div.thing";
} else if ( useall===1 ) {
target = "div.thing." + str_type + "-thing";
} else {
target = "div.thing." + str_type + "-thing." + "p_" + thingindex;
}
// set the overlay wrapper
// target += " div.overlay";
if ( subid.startsWith("music-") ) {
target += " div.overlay.music-controls";
// } else if ( subid==="forecastIcon" || subid==="weatherIcon" ) {
// target += " div.weather_icons";
// } else if ( subid==="feelsLike" || (str_type==="weather" && subid==="temperature") ) {
// target += " div.weather_temps";
} else if ( subid.endsWith("-dn") || subid.endsWith("-up") ) {
target += " div.overlay." + subid.substring(0,subid.length-3);
} else {
target += " div.overlay." + subid;
}
if ( useall === 0 ) { target+= '.v_'+thingindex; }
// for everything other than levels, set the subid target
// levels use the overlay layer only
// set the subid which is blank if it matches the tile type
// edit... changed to only use the subid since that is all we need
// this enables custom tile editing to work properly
// since the str_type can be any linked item for those
// var subidtag = "." + subid;
// if ( subid===str_type ) {
// subidtag = "";
// }
if ( subid!=="level" && subid!=="head" ) {
// if ( subid!=="head" ) {
// target+= " div."+str_type;
// handle special thermostat wrapper case
if ( useall===2 ){
target+= " div";
// } else if ( subid === "cool" || subid==="heat" ) {
// target+= " div." + subid + "-val";
} else {
// target+= " div."+str_type + subidtag;
target+= " div." + subid;
}
if ( useall === 0 ) target+= '.p_'+thingindex;
}
// get the on/off state
// set the target to determine on/off status
// we always use the very specific target to this tile
if ( subid==="name" || subid==="track" || subid==="weekday" ||
subid==="color" || subid==="level" ||
subid==="cool" || subid==="heat" || subid==="stream" ) {
on = "";
} else {
// var onofftarget = "div.overlay." + subid + '.v_' + thingindex + " div."+str_type + subidtag + '.p_'+thingindex;
var on = $("#onoffTarget").html();
if ( on && !$.isNumeric(on) && (on.indexOf(" ") === -1) ) {
on = "."+on;
} else {
on = "";
}
}
// if ( on==="." ) { on= ""; }
target = target + on;
}
return target;
}
function toggleTile(target, str_type, subid) {
// var target = "#tileDialog " + getCssRuleTarget(str_type, subid, thingindex);
var swval = $(target).html();
console.log("toggleTile: target= ", target, " tile type= "+str_type+" subid= "+subid + " swval= ", swval);
$('#onoffTarget').html("");
// activate the icon click to use this
var onoff = getOnOff(str_type, subid);
var newsub = 0;
if ( onoff && onoff.length > 0 ) {
for ( var i=0; i < onoff.length; i++ ) {
var oldsub = onoff[i];
if ( $(target).hasClass(oldsub) ) {
$(target).removeClass(oldsub);
console.log("Removing attribute (" + oldsub + ") from wysiwyg display for tile: " + str_type + " swval = " + swval);
}
if ( oldsub === swval ) {
newsub = i+1;
if ( newsub >= onoff.length ) { newsub= 0; }
$(target).addClass( onoff[newsub] );
$(target).html( onoff[newsub] );
$('#onoffTarget').html(onoff[newsub]);
console.log("Adding attribute (" + onoff[newsub] + ") to wysiwyg display for tile: " + str_type);
break;
}
}
}
};
// activate ability to click on icons
function setupIcons(category, old_str_type, old_thingindex) {
$("#iconList").off("click","img");
$("#iconList").on("click","img", function() {
var str_type = $("#tileDialog").attr("str_type");
var thingindex = $("#tileDialog").attr("thingindex");
var img = $(this).attr("src");
var subid = $("#subidTarget").html();
var strIconTarget = getCssRuleTarget(str_type, subid, thingindex);
console.log("Clicked on img= "+img+" Category= "+category+" strIconTarget= "+strIconTarget+" type= "+str_type+" subid= "+subid+" index= "+thingindex);
iconSelected(category, strIconTarget, img, str_type, subid, thingindex);
});
}
function initDialogBinds(str_type, thingindex) {
$('#noIcon').on('change', function() {
var subid = $("#subidTarget").html();
var str_type = $("#tileDialog").attr("str_type");
var thingindex = $("#tileDialog").attr("thingindex");
var cssRuleTarget = getCssRuleTarget(str_type, subid, thingindex);
var strEffect = getBgEffect();
if( $("#noIcon").is(':checked') ){
priorIcon = $(cssRuleTarget).css("background-image");
addCSSRule(cssRuleTarget, "background-image: none" + strEffect + ";");
} else {
// removeCSSRule(cssRuleTarget, thingindex, "background-image:");
if ( priorIcon!=="none" ) {
addCSSRule(cssRuleTarget, "background-image: " + priorIcon + strEffect + ";");
}
}
});
// new button to process the name change
$("#processName").on("click", function (event) {
var target1 = getCssRuleTarget(str_type, "head", thingindex);
var target2 = getCssRuleTarget(str_type, "name", thingindex);
var newname = $("#editName").val();
$(target1).html(newname);
$(target2).html(newname);
saveTileEdit(str_type, thingindex, newname);
// cm_Globals.reload = true;
event.stopPropagation;
});
$("#iconSrc").on('change', function (event) {
getIcons(str_type, thingindex);
event.stopPropagation;
});
// set the header name
// var target1 = "span.n_"+thingindex;
var target1 = getCssRuleTarget(str_type, "head", thingindex);
var newname = $(target1).html();
$("#editName").val(newname);
// set the scope dropdown list
// var newscope = getScope(str_type, false);
// $("#scopeEffect").html(newscope);
$("#bgSize").on('change', function(event) {
var subid = $("#subidTarget").html();
var str_type = $("#tileDialog").attr("str_type");
var thingindex = $("#tileDialog").attr("thingindex");
updateSize(str_type, subid, thingindex);
event.stopPropagation;
});
$("#autoBgSize").on('change', function(event) {
var subid = $("#subidTarget").html();
var str_type = $("#tileDialog").attr("str_type");
var thingindex = $("#tileDialog").attr("thingindex");
if ( $("#autoBgSize").is(":checked") ) {
$("#bgSize").prop("disabled", true);
} else {
$("#bgSize").prop("disabled", false);
}
updateSize(str_type, subid, thingindex);
event.stopPropagation;
});
// set overall tile height
$("#tileHeight").on('change', function(event) {
var str_type = $("#tileDialog").attr("str_type");
var thingindex = $("#tileDialog").attr("thingindex");
var newsize = parseInt( $("#tileHeight").val() );
var rule = "height: " + newsize.toString() + "px;";
// alert('type = ' + str_type + " rule= " + rule);
if ( str_type==="page" ) {
addCSSRule(getCssRuleTarget(str_type, 'panel', thingindex), rule);
} else {
addCSSRule(getCssRuleTarget(str_type, 'tile', thingindex), rule);
}
event.stopPropagation;
});
// set overall tile width and header and overlay for all subitems
$("#tileWidth").on('change', function(event) {
var str_type = $("#tileDialog").attr("str_type");
var thingindex = $("#tileDialog").attr("thingindex");
var newsize = parseInt( $("#tileWidth").val() );
var rule = "width: " + newsize.toString() + "px;";
if ( str_type==="page" ) {
addCSSRule(getCssRuleTarget(str_type, 'panel', thingindex), rule);
} else {
addCSSRule(getCssRuleTarget(str_type, 'tile', thingindex), rule);
addCSSRule(getCssRuleTarget(str_type, 'head', thingindex), rule);
if ( str_type==="switchlevel" || str_type==="bulb" ) {
addCSSRule("div.overlay.level.v_"+thingindex+" .ui-slider", rule);
}
}
// handle special case of thermostats that need to have widths fixed
if ( str_type === "thermostat" ) {
var midsize = newsize - 64;
rule = "width: " + midsize.toString() + "px;";
addCSSRule( "div.thermostat-thing.p_"+thingindex+" div.heat-val", rule);
addCSSRule( "div.thermostat-thing.p_"+thingindex+" div.cool-val", rule);
}
event.stopPropagation;
});
// set overall tile width and header and overlay for all subitems
$("#autoTileHeight").on('change', function(event) {
var rule;
var str_type = $("#tileDialog").attr("str_type");
var thingindex = $("#tileDialog").attr("thingindex");
if($("#autoTileHeight").is(':checked')) {
rule = "height: auto;";
$("#tileHeight").prop("disabled", true);
$("#tileHeight").css("background-color","gray");
} else {
var newsize = parseInt( $("#tileHeight").val() );
if ( !newsize || newsize <=0 ) {
newsize = 150;
if ( str_type==="page" ) { newsize = 600; }
}
rule = "height: " + newsize.toString() + "px;";
$("#tileHeight").prop("disabled", false);
$("#tileHeight").css("background-color","white");
}
if ( str_type==="page" ) {
addCSSRule(getCssRuleTarget(str_type, 'panel', thingindex), rule);
} else {
addCSSRule(getCssRuleTarget(str_type, "tile", thingindex), rule);
}
event.stopPropagation;
});
$("#autoTileWidth").on('change', function(event) {
var rule;
var midrule;
var str_type = $("#tileDialog").attr("str_type");
var thingindex = $("#tileDialog").attr("thingindex");
if($("#autoTileWidth").is(':checked')) {
rule = "width: auto;";
midrule = "width: 72px;";
$("#tileWidth").prop("disabled", true);
$("#tileWidth").css("background-color","gray");
} else {
var newsize = parseInt( $("#tileWidth").val() );
if ( !newsize || newsize <=0 ) {
newsize = 120;
if ( str_type==="page" ) { newsize = 1200; }
}
rule = "width: " + newsize.toString() + "px;";
$("#tileWidth").prop("disabled", false);
$("#tileWidth").css("background-color","white");
var midsize = newsize - 64;
midrule = "width: " + midsize.toString() + "px;";
}
if ( str_type==="page" ) {
addCSSRule(getCssRuleTarget(str_type, 'panel', thingindex), rule);
} else {
addCSSRule(getCssRuleTarget(str_type, 'tile', thingindex), rule);
if ( str_type==="switchlevel" || str_type==="bulb" ) {
addCSSRule("div.overlay.level.v_"+thingindex+" .ui-slider", rule);
}
}
if ( str_type === "thermostat" ) {
addCSSRule( "div.thermostat-thing.p_"+thingindex+" div.heat-val", midrule);
addCSSRule( "div.thermostat-thing.p_"+thingindex+" div.cool-val", midrule);
}
event.stopPropagation;
});
// set overall tile width and header and overlay for all subitems
$("#editHeight").on('change', function(event) {
var newsize = parseInt( $("#editHeight").val() );
var subid = $("#subidTarget").html();
var str_type = $("#tileDialog").attr("str_type");
var thingindex = $("#tileDialog").attr("thingindex");
if ( subid !== "wholetile" ) {
var target = getCssRuleTarget(str_type, subid, thingindex);
var rule = "height: " + newsize.toString() + "px;";
if ( subid==="temperature" || subid==="feelsLike" ) {
var halfnew = newsize - 5;
rule += " line-height: " + halfnew.toString() + "px;";
}
addCSSRule(target, rule);
}
event.stopPropagation;
});
// set overall tile width and header and overlay for all subitems
$("#editWidth").on('change', function(event) {
var newsize = parseInt( $("#editWidth").val() );
var subid = $("#subidTarget").html();
var str_type = $("#tileDialog").attr("str_type");
var thingindex = $("#tileDialog").attr("thingindex");
if ( subid !== "wholetile" ) {
var target = getCssRuleTarget(str_type, subid, thingindex);
var rule = "width: " + newsize.toString() + "px;";
addCSSRule(target, rule);
}
event.stopPropagation;
});
// set the item height
$("#autoHeight").on('change', function(event) {
var subid = $("#subidTarget").html();
var str_type = $("#tileDialog").attr("str_type");
var thingindex = $("#tileDialog").attr("thingindex");
var rule;
if ( $("#autoHeight").is(":checked") ) {
// special handling for default temperature circles
if ( subid==="temperature" || subid==="feelsLike" ) {
rule = "height: 50px; line-height: 45px;";
} else {
rule = "height: auto;";
}
$("#editHeight").prop("disabled", true);
$("#editHeight").css("background-color","gray");
} else {
var newsize = parseInt( $("#editHeight").val() );
// special handling for default temperature circles
$("#editHeight").prop("disabled", false);
$("#editHeight").css("background-color","white");
if ( newsize === 0 ) {
if ( subid === "wholetile" ) {
rule = "height: 150px;";
} else if ( subid==="temperature" || subid==="feelsLike" ) {
rule = "height: 50px; line-height: 45px;";
} else {
rule = "height: 16px;";
}
} else {
newsize = newsize.toString() + "px;";
rule = "height: " + newsize;
}
}
if ( subid !== "wholetile" ) {
addCSSRule(getCssRuleTarget(str_type, subid, thingindex), rule);
}
event.stopPropagation;
});
// set the item width
$("#autoWidth").on('change', function(event) {
var subid = $("#subidTarget").html();
if ( subid !== "wholetile" ) {
var str_type = $("#tileDialog").attr("str_type");
var thingindex = $("#tileDialog").attr("thingindex");
var rule;
if ( $("#autoWidth").is(":checked") ) {
// special handling for default temperature circles
if ( subid==="temperature" || subid==="feelsLike" ) {
rule = "width: 50px;";
} else if ( str_type==="page" && subid==="panel") {
rule = "width: 100%; padding-left: 0px; padding-right: 0px;";
} else {
rule = "width: 100%;";
}
$("#editWidth").prop("disabled", true);
$("#editWidth").css("background-color","gray");
addCSSRule(getCssRuleTarget(str_type, subid, thingindex), rule);
} else {
var newsize = parseInt( $("#editWidth").val() );
$("#editWidth").prop("disabled", false);
$("#editWidth").css("background-color","white");
if ( newsize === 0 ) {
if ( subid==="temperature" || subid==="feelsLike" ) {
rule = "width: 50px;";
} else if ( str_type==="page" && subid==="panel") {
rule = "width: 100%; padding-left: 0px; padding-right: 0px;";
} else {
rule = "width: 100%;";
}
} else {
newsize = newsize.toString() + "px;";
// rule = "width: " + newsize + " display: inline-block;";
rule = "width: " + newsize;
}
addCSSRule(getCssRuleTarget(str_type, subid, thingindex), rule);
}
}
event.stopPropagation;
});
// set padding for selected item
$("#topPadding").on('change', function(event) {
var subid = $("#subidTarget").html();
var str_type = $("#tileDialog").attr("str_type");
var thingindex = $("#tileDialog").attr("thingindex");
var newsize = parseInt( $("#topPadding").val() );
if ( !newsize || isNaN(newsize) ) {
newsize = "0px;";
} else {
newsize = newsize.toString() + "px;";
}
var rule;
if ( subid === "wholetile" || subid === "panel" ) {
rule = "background-position-y: " + newsize;
} else if ( subid==="temperature" || subid==="feelsLike" ||
subid==="weatherIcon" || subid==="forecastIcon" ) {
rule = "margin-top: " + newsize;
} else {
rule = "padding-top: " + newsize;
}
addCSSRule(getCssRuleTarget(str_type, subid, thingindex), rule);
event.stopPropagation;
});
// set padding for selected item
$("#leftPadding").on('change', function(event) {
var subid = $("#subidTarget").html();
var str_type = $("#tileDialog").attr("str_type");
var thingindex = $("#tileDialog").attr("thingindex");
var newsize = parseInt( $("#leftPadding").val() );
if ( !newsize || isNaN(newsize) ) {
newsize = "0px;";
} else {
newsize = newsize.toString() + "px;";
}
var rule;
if ( subid === "wholetile" || subid === "panel") {
rule = "background-position-x: " + newsize;
} else if ( subid==="temperature" || subid==="feelsLike" ||
subid==="weatherIcon" || subid==="forecastIcon" ) {
rule = "margin-left: " + newsize;
} else {
rule = "padding-left: " + newsize;
}
addCSSRule(getCssRuleTarget(str_type, subid, thingindex), rule);
event.stopPropagation;
});
}
function iconlist() {
var dh = "";
dh += "<div id='editicon'>";
dh += "<div id='iconChoices'>";
dh += "<select name=\"iconSrc\" id=\"iconSrc\" class=\"ddlDialog\"></select>";
dh += "<input type='checkbox' id='noIcon'>";
dh += "<label class=\"iconChecks\" for=\"noIcon\">None</label>";
dh += "</div>";
var align = "";
align += "<div id='alignIcon' class='radiogroup'>";
align+= '<input id="iconleft" type="radio" name="alignicon" value="left"><label for="iconleft">Left</label>';
align+= '<input id="iconcenter" type="radio" name="alignicon" value="center" checked><label for="iconcenter">Center</label>';
align+= '<input id="iconright" type="radio" name="alignicon" value="right"><label for="iconright">Right</label>';
align += "</div>";
dh += align;
dh += "<div id='iconList'></div>";
dh += "</div>";
return dh;
}
function editSection(str_type, thingindex) {
var dh = "";
dh += "<div id='editSection'>";
dh += effectspicker(str_type, thingindex);
dh += sizepicker(str_type, thingindex);
dh += "</div>";
return dh;
}
function getScope(str_type, ftime) {
var dh = "";
if ( ftime ) {
if ( str_type==="page" ) {
dh += "<option id=\"tscope1\" value=\"thistile\" selected>This page</option>";
dh += "<option id=\"tscope2\" value=\"alltypes\">All pages</option>";
dh += "<option id=\"tscope3\" value=\"alltiles\">All pages</option>";
} else {
dh += "<option id=\"tscope1\" value=\"thistile\" selected>This " + str_type + " tile</option>";
dh += "<option id=\"tscope2\" value=\"alltypes\">All " + str_type + " tiles</option>";
dh += "<option id=\"tscope3\" value=\"alltiles\">All tiles</option>";
}
} else {
if ( str_type==="page" ) {
$("#tscope1").text("This page");
$("#tscope2").text("All pages");
$("#tscope3").text("All pages");
} else {
$("#tscope1").text("This " + str_type + " tile");
$("#tscope2").text("All " + str_type + " tiles");
$("#tscope3").text("All tiles");
}
}
return dh;
}
function effectspicker(str_type, thingindex) {
var dh = "";
var target = getCssRuleTarget(str_type, "head", thingindex);
var name = $(target).html();
var labelname;
if ( str_type==="page" ) {
labelname = "Page Name:";
} else {
labelname = "Tile Name:";
}
// alert("Name = " + name);
// Title changes and options
dh += "<div class='colorgroup'><label id=\"labelName\">" + labelname + "</label><input name=\"editName\" id=\"editName\" class=\"ddlDialog\" value=\"" + name +"\"></div>";
dh += "<div class='colorgroup'><button id='processName' type='button'>Save Name</button></div>";
//Effects
dh += "<div class='colorgroup'><label>Effect Scope:</label>";
dh += "<select name=\"scopeEffect\" id=\"scopeEffect\" class=\"ddlDialog\">";
dh += getScope(str_type, true);
dh += "</select>";
dh += "</div>";
return dh;
}
function sizepicker(str_type, thingindex) {
var dh = "";
var subid = setsubid(str_type);
var target = getCssRuleTarget(str_type, subid, thingindex); // "div.thing";
var size = $(target).css("background-size");
// alert("old size: " + size);
size = parseInt(size);
if ( isNaN(size) ) {
size = 80;
if ( subid === "wholetile" ) { size = 150; }
}
// icon size effects
dh += "<div class='sizeText'></div>";
dh += "<div class='editSection_input'>";
dh += "<label for='bgSize'>Background Size: </label>";
dh += "<input size='8' type=\"number\" min='10' max='2400' step='10' id=\"bgSize\" value=\"" + size + "\"/>";
dh += "</div>";
dh += "<div class='editSection_input'><input type='checkbox' id='autoBgSize'><label class=\"iconChecks\" for=\"autoBgSize\">Auto?</label></div>";
// overall tile size effect -- i dont' know why I had this set different?
// var target2 = "div.thing."+str_type+"-thing";
var target2 = target;
var th = $(target2).css("height");
var tw = $(target2).css("width");
if ( !th || th.indexOf("px") === -1 ) {
th= 0;
} else {
th = parseInt(th);
}
if ( tw==="auto" || !tw || tw.indexOf("px") === -1 ) {
tw= 0;
} else {
tw = parseInt(tw);
}
var h = $(target).css("height");
var w = $(target).css("width");
if ( !h || !h.hasOwnProperty("indexOf") || h.indexOf("px") === -1 ) {
h= 0;
} else {
h = parseInt(h);
}
if ( !w || !w.hasOwnProperty("indexOf") || w.indexOf("px") === -1 ) {
w= 0;
} else {
w = parseInt(w);
}
dh += "<div class='sizeText'>Overall Tile Size</div>";
dh += "<div class='editSection_input'>";
dh += "<label for='tileHeight'>Tile H: </label>";
dh += "<input size='8' type=\"number\" min='10' max='1200' step='10' id=\"tileHeight\" value=\"" + th + "\"/>";
dh += "</div>";
dh += "<div class='editSection_input autochk'>";
dh += "<label for='tileWidth'>Tile W: </label>";
dh += "<input size='8' type=\"number\" min='10' max='1200' step='10' id=\"tileWidth\" value=\"" + tw + "\"/>";
dh += "</div>";
dh += "<div class='editSection_input autochk'><input type='checkbox' id='autoTileHeight'><label class=\"iconChecks\" for=\"autoTileHeight\">Auto H?</label></div>";
dh += "<div class='editSection_input autochk'><input type='checkbox' id='autoTileWidth'><label class=\"iconChecks\" for=\"autoTileWidth\">Auto W?</label></div>";
dh += "<div class='sizeText'><p>Item Size & Position:</p></div>";
dh += "<div class='editSection_input autochk'>";
dh += "<label for='editHeight'>Item H: </label>";
dh += "<input size='4' type=\"number\" min='5' max='1200' step='5' id=\"editHeight\" value=\"" + h + "\"/>";
dh += "</div>";
dh += "<div>";
dh += "<div class='editSection_input autochk'>";
dh += "<label for='editWidth'>Item W: </label>";
dh += "<input size='4' type=\"number\" min='5' max='1200' step='5' id=\"editWidth\" value=\"" + w + "\"/>";
dh += "</div>";
dh += "</div>";
dh += "<div class='editSection_input autochk'><input type='checkbox' id='autoHeight'><label class=\"iconChecks\" for=\"autoHeight\">Auto H?</label></div>";
dh += "<div class='editSection_input autochk'><input type='checkbox' id='autoWidth'><label class=\"iconChecks\" for=\"autoWidth\">Auto W?</label></div>";
// font size (returns px not pt)
var ptop = parseInt($(target).css("padding-top"));
var pleft = parseInt($(target).css("padding-left"));
if ( subid === "wholetile" || subid === "panel") {
ptop = parseInt($(target).css("background-position-y"));
pleft = parseInt($(target).css("background-position-x"));
}
if ( !ptop || isNaN(ptop) ) { ptop = 0; }
if ( !pleft || isNaN(pleft) ) { pleft = 0; }
dh += "<div class='editSection_input'>";
dh += "<label for='topPadding'>Top Padding:</label>\t";
dh += "<input size='4' type=\"number\" min='0' max='100' step='5' id=\"topPadding\" value=\"" + ptop + "\"/>";
dh += "</div>"; dh += "<div class='editSection_input'>";
dh += "<label for='leftPadding'>Left Padding:</label>\t";
dh += "<input size='4' type=\"number\" min='0' max='100' step='5' id=\"leftPadding\" value=\"" + pleft + "\"/>";
dh += "</div>";
return dh;
}
function colorpicker(str_type, thingindex) {
var dh = "";
// this section is loaded later with a bunch of color pickers
// including script to respond to picked color
dh += "<div id='colorpicker'>";
// dh += "<button id='editReset' type='button'>Reset</button>";
dh += "<div class='colorgroup'><label>Feature Selected:</label>";
var firstsub = setsubid(str_type);
var onoff = getOnOff(str_type, firstsub);
dh += "<div id='subidTarget' class='dlgtext'>" + firstsub + "</div>";
dh += "<div id='onoffTarget' class='dlgtext'>" + onoff[0] + "</div>";
dh+= "</div></div>";
// alert(firstsub + " " + onoff);
return dh;
}
// popup dialog box now uses createModal
function editTile(str_type, thingindex, aid, bid, thingclass, hubnum, htmlcontent) {
var returnURL;
try {
returnURL = $("input[name='returnURL']").val();
} catch(e) {
returnURL = "housepanel.php";
}
et_Globals.aid = aid;
et_Globals.id = bid;
et_Globals.hubnum = hubnum;
et_Globals.reload = false;
// save the sheet upon entry for cancel handling
savedSheet = document.getElementById('customtiles').sheet;
// * DIALOG START *
var dialog_html = "<div id='tileDialog' class='tileDialog' str_type='" +
str_type + "' thingindex='" + thingindex +"' >";
// header
if ( str_type==="page" ) {
dialog_html += "<div class='editheader' id='editheader'>Editing Page#" + hubnum +
" of Name: " + thingindex + "</div>";
} else {
if ( hubnum < 0 ) {
var hubstr = " Hub not applicable";
} else {
hubstr = " From hub #" + hubnum;
}
dialog_html += "<div class='editheader' id='editheader'>Editing Tile #" + thingindex +
" of Type: " + str_type + hubstr + "</div>";
}
// option on the left side - colors and options
dialog_html += colorpicker(str_type, thingindex);
dialog_html += editSection(str_type, thingindex);
// icons on the right side
dialog_html += iconlist();
// tileEdit display on the far right side
dialog_html += "<div id='tileDisplay' class='tileDisplay'>";
dialog_html += "<div id='editInfo' class='editInfo'>Select or Change State</div>";
// we either use the passed in content or make an Ajax call to get the content
var jqxhr = null;
if ( str_type==="page" ) {
var roomname = thingindex;
var roomnum = hubnum;
// thingindex = 1000 + parseInt(roomnum,10);
// dialog_html += "<div class=\"" + thingclass + "\" id='wysiwyg'></div>";
// dialog_html += "<div class=\"thing " + str_type + "-thing\" id='wysiwyg'></div>";
jqxhr = $.post(returnURL,
{useajax: "wysiwyg", id: roomnum, type: 'page', tile: thingindex, value: roomname, attr: ''},
function (presult, pstatus) {
if (pstatus==="success" ) {
htmlcontent = presult;
}
}
);
} else if ( htmlcontent ) {
// dialog_html += "<div class=\"" + thingclass + "\" id='wysiwyg'>" + htmlcontent + "</div>";
htmlcontent = "<div class=\"" + thingclass + "\" id='wysiwyg'>" + htmlcontent + "</div>";
} else {
// put placeholder and populate after Ajax finishes retrieving true wysiwyg content
// dialog_html += "<div class=\"thing " + str_type + "-thing p_"+thingindex+"\" id='wysiwyg'></div>";
jqxhr = $.post(returnURL,
{useajax: "wysiwyg", id: '', type: '', tile: thingindex, value: '', attr: ''},
function (presult, pstatus) {
if (pstatus==="success" ) {
htmlcontent = presult;
}
}
);
}
dialog_html += "<div id='subsection'></div>";
dialog_html += "</div>";
// * DIALOG_END *
dialog_html += "</div>";
// create a function to display the tile
var dodisplay = function() {
var pos = {top: 100, left: 200, zindex: 99999};
createModal("modalid", dialog_html, "body", true, pos,
// function invoked upon leaving the dialog
function(ui, content) {
$("body").off("keydown");
var clk = $(ui).attr("name");
// alert("clk = "+clk);
if ( clk==="okay" ) {
var newname = $("#editName").val();
saveTileEdit(str_type, thingindex, newname);
} else if ( clk==="cancel" ) {
cancelTileEdit(str_type, thingindex);
}
tileCount = 0;
},
// function invoked upon starting the dialog
function(hook, content) {
$("body").on("keydown",function(e) {
if ( e.which===13 ){
$("#modalokay").click();
}
if ( e.which===27 ){
$("#modalcancel").click();
}
});
$("#modalid").draggable();
}
);
};
if ( jqxhr ) {
jqxhr.done(function() {
dodisplay();
$("#editInfo").after(htmlcontent);
tileCount++;
setupClicks(str_type, thingindex);
});
} else {
dodisplay();
$("#editInfo").after(htmlcontent);
tileCount++;
setupClicks(str_type, thingindex);
}
}
function setupClicks(str_type, thingindex) {
var firstsub = setsubid(str_type);
var target1 = getCssRuleTarget(str_type, firstsub, thingindex);
toggleTile($(target1), str_type, firstsub);
// alert("target= " + target1 + " type= " + str_type + " firstsub= " + firstsub);
initColor(str_type, firstsub, thingindex);
initDialogBinds(str_type, thingindex);
loadSubSelect(str_type, firstsub, thingindex);
getIcons(str_type, thingindex);
var trigger = "div"; // div." + str_type + ".p_"+thingindex;
$("#wysiwyg").on('click', trigger, function(event) {
// load up our silent tags
$("#tileDialog").attr("str_type",str_type);
$("#tileDialog").attr("thingindex",thingindex);
var subid = $(event.target).attr("subid");
if ( !subid || subid===undefined ) {
if ( $(event.target).hasClass("thingname") ) {
subid = "head";
} else {
subid = "wholetile";
}
}
// update everything to reflect current tile
toggleTile(event.target, str_type, subid);
// alert("target= " + event.target.toString() + " type= " + str_type + " subid= " + subid);
initColor(str_type, subid, thingindex);
initDialogBinds(str_type, thingindex);
loadSubSelect(str_type, subid, thingindex);
var newtitle;
if ( str_type==="page" ) {
newtitle = "Editing Page with Name: " + thingindex;
$("#labelName").html("Page Name:");
} else {
newtitle = "Editing Tile #" + thingindex + " of Type: " + str_type;
$("#labelName").html("Tile Name:");
}
newtitle+= " (editing " + tileCount + " items)";
$("#editheader").html(newtitle);
event.stopPropagation();
});
$("#scopeEffect").off('change');
$("#scopeEffect").on('change', function(event) {
var str_type = $("#tileDialog").attr("str_type");
var thingindex = $("#tileDialog").attr("thingindex");
var subid = $("#subidTarget").html();
initColor(str_type, subid, thingindex);
event.stopPropagation();
});
}