-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaedit-panel.js
1304 lines (1209 loc) · 60.5 KB
/
aedit-panel.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
/************************************************************************
* Library: Web 2.0 UI for jQuery (using prototypical inheritance)
* - Following objects defined
* - w2layout - layout widget
* - $().w2layout - jQuery wrapper
* - Dependencies: jQuery, w2utils, w2toolbar, w2tabs
*
* == NICE TO HAVE ==
* - onResize for the panel
* - add more panel title positions (left=rotated, right=rotated, bottom)
* - bug: resizer is visible (and onHover) when panel is hidden.
* - bug: when you assign content before previous transition completed.
*
* == 1.5 changes
* - $('#layout').w2layout() - if called w/o argument then it returns layout object
* - negative -size for left/right panels
*
************************************************************************/
(function () {
var w2layout = function (options) {
this.box = null; // DOM Element that holds the element
this.name = null; // unique name for w2ui
this.panels = [];
this.tmp = {};
this.padding = 14; // panel padding
this.resizer = 14; // resizer width or height
this.style = '';
this.onShow = null;
this.onHide = null;
this.onResizing = null;
this.onResizerClick = null;
this.onRender = null;
this.onRefresh = null;
this.onResize = null;
this.onDestroy = null;
$.extend(true, this, w2obj.layout, options);
};
var w2panels = ['top', 'left', 'main', 'preview', 'right', 'bottom'];
// ====================================================
// -- Registers as a jQuery plugin
$.fn.w2layout = function(method) {
if ($.isPlainObject(method)) {
// check name parameter
if (!w2utils.checkName(method, 'w2layout')) return;
var panels = method.panels || [];
var object = new w2layout(method);
$.extend(object, { handlers: [], panels: [] });
// add defined panels
for (var p = 0, len = panels.length; p < len; p++) {
object.panels[p] = $.extend(true, {}, w2layout.prototype.panel, panels[p]);
if ($.isPlainObject(object.panels[p].tabs) || $.isArray(object.panels[p].tabs)) initTabs(object, panels[p].type);
if ($.isPlainObject(object.panels[p].toolbar) || $.isArray(object.panels[p].toolbar)) initToolbar(object, panels[p].type);
}
// add all other panels
for (var p1 = 0; p1 < w2panels.length; p1++) {
if (object.get(w2panels[p1]) !== null) continue;
object.panels.push($.extend(true, {}, w2layout.prototype.panel, { type: w2panels[p1], hidden: (w2panels[p1] !== 'main'), size: 50 }));
}
if ($(this).length > 0) {
object.render($(this)[0]);
}
w2ui[object.name] = object;
return object;
} else {
var obj = w2ui[$(this).attr('name')];
if (!obj) return null;
if (arguments.length > 0) {
if (obj[method]) obj[method].apply(obj, Array.prototype.slice.call(arguments, 1));
return this;
} else {
return obj;
}
}
function initTabs(object, panel, tabs) {
var pan = object.get(panel);
if (pan !== null && typeof tabs == 'undefined') tabs = pan.tabs;
if (pan === null || tabs === null) return false;
// instanciate tabs
if ($.isArray(tabs)) tabs = { tabs: tabs };
$().w2destroy(object.name + '_' + panel + '_tabs'); // destroy if existed
pan.tabs = $().w2tabs($.extend({}, tabs, { owner: object, name: object.name + '_' + panel + '_tabs' }));
pan.show.tabs = true;
return true;
}
function initToolbar(object, panel, toolbar) {
var pan = object.get(panel);
if (pan !== null && typeof toolbar == 'undefined') toolbar = pan.toolbar;
if (pan === null || toolbar === null) return false;
// instanciate toolbar
if ($.isArray(toolbar)) toolbar = { items: toolbar };
$().w2destroy(object.name + '_' + panel + '_toolbar'); // destroy if existed
pan.toolbar = $().w2toolbar($.extend({}, toolbar, { owner: object, name: object.name + '_' + panel + '_toolbar' }));
pan.show.toolbar = true;
return true;
}
};
// ====================================================
// -- Implementation of core functionality
w2layout.prototype = {
// default setting for a panel
panel: {
type : null, // left, right, top, bottom
title : '',
size : 100, // width or height depending on panel name
minSize : 20,
maxSize : false,
hidden : false,
resizable : false,
overflow : 'auto',
style : '',
content : '', // can be String or Object with .render(box) method
tabs : null,
toolbar : null,
width : null, // read only
height : null, // read only
show : {
toolbar : false,
tabs : false
},
onRefresh : null,
onShow : null,
onHide : null
},
// alias for content
html: function (panel, data, transition) {
return this.content(panel, data, transition);
},
content: function (panel, data, transition) {
var obj = this;
var p = this.get(panel);
// if it is CSS panel
if (panel == 'css') {
$('#layout_'+ obj.name +'_panel_css').html('<style>'+ data +'</style>');
return true;
}
if (p === null) return false;
if (typeof data == 'undefined' || data === null) {
return p.content;
} else {
if (data instanceof jQuery) {
console.log('ERROR: You can not pass jQuery object to w2layout.content() method');
return false;
}
var pname = '#layout_'+ this.name + '_panel_'+ p.type;
var current = $(pname + '> .w2ui-panel-content');
var panelTop = 0;
if (current.length > 0) {
$(pname).scrollTop(0);
panelTop = $(current).position().top;
}
if (p.content === '') {
p.content = data;
this.refresh(panel);
} else {
p.content = data;
if (!p.hidden) {
if (transition !== null && transition !== '' && typeof transition != 'undefined') {
// apply transition
var div1 = $(pname + '> .w2ui-panel-content');
div1.after('<div class="w2ui-panel-content new-panel" style="'+ div1[0].style.cssText +'"></div>');
var div2 = $(pname + '> .w2ui-panel-content.new-panel');
div1.css('top', panelTop);
div2.css('top', panelTop);
if (typeof data == 'object') {
data.box = div2[0]; // do not do .render(box);
data.render();
} else {
div2.html(data);
}
w2utils.transition(div1[0], div2[0], transition, function () {
div1.remove();
div2.removeClass('new-panel');
div2.css('overflow', p.overflow);
// IE Hack
obj.resize();
if (window.navigator.userAgent.indexOf('MSIE') != -1) setTimeout(function () { obj.resize(); }, 100);
});
}
}
this.refresh(panel);
}
}
// IE Hack
obj.resize();
if (window.navigator.userAgent.indexOf('MSIE') != -1) setTimeout(function () { obj.resize(); }, 100);
return true;
},
load: function (panel, url, transition, onLoad) {
var obj = this;
if (panel == 'css') {
$.get(url, function (data, status, xhr) { // should always be $.get as it is template
obj.content(panel, xhr.responseText);
if (onLoad) onLoad();
});
return true;
}
if (this.get(panel) !== null) {
$.get(url, function (data, status, xhr) { // should always be $.get as it is template
obj.content(panel, xhr.responseText, transition);
if (onLoad) onLoad();
// IE Hack
obj.resize();
if (window.navigator.userAgent.indexOf('MSIE') != -1) setTimeout(function () { obj.resize(); }, 100);
});
return true;
}
return false;
},
sizeTo: function (panel, size) {
var obj = this;
var pan = obj.get(panel);
if (pan === null) return false;
// resize
$(obj.box).find(' > div > .w2ui-panel').css(w2utils.cssPrefix('transition', '.2s'));
setTimeout(function () {
obj.set(panel, { size: size });
}, 1);
// clean
setTimeout(function () {
$(obj.box).find(' > div > .w2ui-panel').css(w2utils.cssPrefix('transition', '0s'));
obj.resize();
}, 500);
return true;
},
show: function (panel, immediate) {
var obj = this;
// event before
var eventData = this.trigger({ phase: 'before', type: 'show', target: panel, object: this.get(panel), immediate: immediate });
if (eventData.isCancelled === true) return;
var p = obj.get(panel);
if (p === null) return false;
p.hidden = false;
if (immediate === true) {
$('#layout_'+ obj.name +'_panel_'+panel).css({ 'opacity': '1' });
obj.trigger($.extend(eventData, { phase: 'after' }));
obj.resize();
} else {
// resize
$('#layout_'+ obj.name +'_panel_'+panel).css({ 'opacity': '0' });
$(obj.box).find(' > div > .w2ui-panel').css(w2utils.cssPrefix('transition', '.2s'));
setTimeout(function () { obj.resize(); }, 1);
// show
setTimeout(function() {
$('#layout_'+ obj.name +'_panel_'+ panel).css({ 'opacity': '1' });
}, 250);
// clean
setTimeout(function () {
$(obj.box).find(' > div > .w2ui-panel').css(w2utils.cssPrefix('transition', '0s'));
obj.trigger($.extend(eventData, { phase: 'after' }));
obj.resize();
}, 500);
}
return true;
},
hide: function (panel, immediate) {
var obj = this;
// event before
var eventData = this.trigger({ phase: 'before', type: 'hide', target: panel, object: this.get(panel), immediate: immediate });
if (eventData.isCancelled === true) return;
var p = obj.get(panel);
if (p === null) return false;
p.hidden = true;
if (immediate === true) {
$('#layout_'+ obj.name +'_panel_'+panel).css({ 'opacity': '0' });
obj.trigger($.extend(eventData, { phase: 'after' }));
obj.resize();
} else {
// hide
$(obj.box).find(' > div > .w2ui-panel').css(w2utils.cssPrefix('transition', '.2s'));
$('#layout_'+ obj.name +'_panel_'+panel).css({ 'opacity': '0' });
setTimeout(function () { obj.resize(); }, 1);
// clean
setTimeout(function () {
$(obj.box).find(' > div > .w2ui-panel').css(w2utils.cssPrefix('transition', '0s'));
obj.trigger($.extend(eventData, { phase: 'after' }));
obj.resize();
}, 500);
}
return true;
},
toggle: function (panel, immediate) {
//console.log(this);
var p = this.get(panel);
if (p === null) return false;
if (p.hidden) return this.show(panel, immediate != "DISABLE HIDE ANIMATION");
else return this.hide(panel, immediate != "DISABLE HIDE ANIMATION");
},
set: function (panel, options) {
var ind = this.get(panel, true);
if (ind === null) return false;
$.extend(this.panels[ind], options);
// refresh only when content changed
if (typeof options['content'] != 'undefined') this.refresh(panel);
// show/hide resizer
this.resize(); // resize is needed when panel size is changed
return true;
},
get: function (panel, returnIndex) {
for (var p = 0; p < this.panels.length; p++) {
if (this.panels[p].type == panel) {
if (returnIndex === true) return p; else return this.panels[p];
}
}
return null;
},
el: function (panel) {
var el = $('#layout_'+ this.name +'_panel_'+ panel +'> .w2ui-panel-content');
if (el.length != 1) return null;
return el[0];
},
hideToolbar: function (panel) {
var pan = this.get(panel);
if (!pan) return;
pan.show.toolbar = false;
$('#layout_'+ this.name +'_panel_'+ panel +'> .w2ui-panel-toolbar').hide();
this.resize();
},
showToolbar: function (panel) {
var pan = this.get(panel);
if (!pan) return;
pan.show.toolbar = true;
$('#layout_'+ this.name +'_panel_'+ panel +'> .w2ui-panel-toolbar').show();
this.resize();
},
toggleToolbar: function (panel) {
var pan = this.get(panel);
if (!pan) return;
if (pan.show.toolbar) this.hideToolbar(panel); else this.showToolbar(panel);
},
hideTabs: function (panel) {
var pan = this.get(panel);
if (!pan) return;
pan.show.tabs = false;
$('#layout_'+ this.name +'_panel_'+ panel +'> .w2ui-panel-tabs').hide();
this.resize();
},
showTabs: function (panel) {
var pan = this.get(panel);
if (!pan) return;
pan.show.tabs = true;
$('#layout_'+ this.name +'_panel_'+ panel +'> .w2ui-panel-tabs').show();
this.resize();
},
toggleTabs: function (panel) {
var pan = this.get(panel);
if (!pan) return;
if (pan.show.tabs) this.hideTabs(panel); else this.showTabs(panel);
},
render: function (box) {
var obj = this;
// if (window.getSelection) window.getSelection().removeAllRanges(); // clear selection
var time = (new Date()).getTime();
// event before
var eventData = obj.trigger({ phase: 'before', type: 'render', target: obj.name, box: box });
if (eventData.isCancelled === true) return;
if (typeof box != 'undefined' && box !== null) {
if ($(obj.box).find('#layout_'+ obj.name +'_panel_main').length > 0) {
$(obj.box)
.removeAttr('name')
.removeClass('w2ui-layout')
.html('');
}
obj.box = box;
}
if (!obj.box) return false;
$(obj.box)
.attr('name', obj.name)
.addClass('w2ui-layout')
.html('<div></div>');
if ($(obj.box).length > 0) $(obj.box)[0].style.cssText += obj.style;
// create all panels
for (var p1 = 0; p1 < w2panels.length; p1++) {
var pan = obj.get(w2panels[p1]);
var aspect = (pan.type === 'top' ||
pan.type === 'bottom' ||
pan.type === 'preview') ? 'horizontal' : 'vertical';
var label = "Toggle ";
var icon1 = '<span class="fa fa-arrow-up"></span> ';
var icon2 = ' <span class="fa fa-arrow-down"></span> ';
switch (pan.type) {
case "top":
label += " top panel";
break;
case "bottom":
label += " bottom panel";
icon1 = '<span class="fa fa-arrow-down"></span> ';
icon2 = ' <span class="fa fa-arrow-up"></span> ';
break;
case "left":
label += " left panel(s)";
break;
case "right":
label += "right panel(s)";
break;
default:
label = "";
break;
}
var html = '<div id="layout_'+ obj.name + '_resizer_'+ w2panels[p1] +
'" class="w2ui-resizer w2ui-' + aspect +
'-resizer"><span class="w2ui-resize-toggle w2ui-resize-toggle-' +
pan.type + '">' + icon1 + label + icon2 + '</span></div>' +
'<div id="layout_'+ obj.name + '_panel_'+ w2panels[p1] +'" class="w2ui-panel">'+
' <div class="w2ui-panel-title"></div>'+
' <div class="w2ui-panel-tabs"></div>'+
' <div class="w2ui-panel-toolbar"></div>'+
' <div class="w2ui-panel-content"></div>'+
'</div>';
$(obj.box).find(' > div').append(html);
// tabs are rendered in refresh()
}
$(obj.box).find(' > div')
.append('<div id="layout_'+ obj.name + '_panel_css" style="position: absolute; top: 10000px;"></div');
obj.refresh(); // if refresh is not called here, the layout will not be available right after initialization
// process event
obj.trigger($.extend(eventData, { phase: 'after' }));
// reinit events
setTimeout(function () { // needed this timeout to allow browser to render first if there are tabs or toolbar
initEvents();
obj.resize();
}, 0);
return (new Date()).getTime() - time;
function initEvents() {
obj.tmp.events = {
resize : function (event) {
w2ui[obj.name].resize();
},
mouseMove : resizeMove,
mouseUp : resizeStop,
resizeStart : resizeStart
};
$(window).on('resize', obj.tmp.events.resize);
}
function resizeMove(evnt) {
if (!obj.box) return;
if (!evnt) evnt = window.event;
if (typeof obj.tmp.resize == 'undefined') return;
var panel = obj.get(obj.tmp.resize.type);
// reset panel size if hidden to prevent jumping
if (panel.hidden) {obj.tmp.y = 0; obj.tmp.x = 0;}
// event before
var tmp = obj.tmp.resize;
var eventData = obj.trigger({ phase: 'before', type: 'resizing', target: obj.name, object: panel, originalEvent: evnt,
panel: tmp ? tmp.type : 'all', diff_x: tmp ? tmp.diff_x : 0, diff_y: tmp ? tmp.diff_y : 0 });
if (eventData.isCancelled === true) return;
var resize_x = evnt.originalEvent.touches != undefined ?
evnt.originalEvent.touches[0].clientX :
evnt.clientX;
resize_x -= tmp.x;
var resize_y = evnt.originalEvent.touches != undefined ?
evnt.originalEvent.touches[0].clientY :
evnt.clientY;
resize_y -= tmp.y;
var mainPanel = obj.get('main');
switch (tmp.type) {
case 'left':
if (panel.minSize - resize_x > panel.width) {
resize_x = panel.minSize - panel.width;
}
if (panel.maxSize && (panel.width + resize_x > panel.maxSize)) {
resize_x = panel.maxSize - panel.width;
}
if (mainPanel.minSize + resize_x > mainPanel.width) {
resize_x = mainPanel.width - mainPanel.minSize;
}
// add padding
resize_x -= (obj.padding/2);
break;
case 'right':
if (panel.minSize + resize_x > panel.width) {
resize_x = panel.width - panel.minSize;
}
if (panel.maxSize && (panel.width - resize_x > panel.maxSize)) {
resize_x = panel.width - panel.maxSize;
}
if (mainPanel.minSize - resize_x > mainPanel.width) {
resize_x = mainPanel.minSize - mainPanel.width;
}
// remove padding
resize_x += (obj.padding/2);
break;
case 'top':
if (panel.minSize - resize_y > panel.height) {
resize_y = panel.minSize - panel.height;
}
if (panel.maxSize && (panel.height + resize_y > panel.maxSize)) {
resize_y = panel.maxSize - panel.height;
}
if (mainPanel.minSize + resize_y > mainPanel.height) {
resize_y = mainPanel.height - mainPanel.minSize;
}
// remove padding
//resize_y -= (obj.padding/2);
break;
case 'bottom':
if (panel.minSize + resize_y > panel.height) {
resize_y = panel.height - panel.minSize;
}
if (panel.maxSize && (panel.height - resize_y > panel.maxSize)) {
resize_y = panel.height - panel.maxSize;
}
if (mainPanel.minSize - resize_y > mainPanel.height) {
resize_y = mainPanel.minSize - mainPanel.height;
}
// add padding
resize_y += (obj.padding/2);
break;
}
tmp.diff_x = resize_x;
tmp.diff_y = resize_y;
// ANONYMOUS FUNCTION IS SLOW IN CHROME - AVOID!
// window.requestAnimationFrame(function() {updateSize()});
window.requestAnimationFrame(updateSize);
// event after
obj.trigger($.extend(eventData, { phase: 'after' }));
}
function updateSize(force) {
// refresh layout every n frames or x ms after last cursor move
// for smoothest perf on low cpus, keep n high and x low
obj.fps ++;
if (obj.fps < 4 && force !== true) {
clearTimeout(obj.resizeTimer);
obj.resizeTimer = setTimeout(function(){updateSize(true)}, 20);
return;
}
//if (force === undefined)
obj.fps = 0;
//if (Math.random() > 0.2) return;
if (obj.tmp.resize !== undefined) {
var p = $('#layout_'+ obj.name + '_resizer_'+ obj.tmp.resize.type);
if (!p.hasClass('active')) p.addClass('active');
switch (obj.tmp.resize.type) {
case 'top':
case 'preview':
case 'bottom':
obj.tmp.resize.diff_x = 0;
if (p.length > 0) p[0].style.top = (obj.tmp.resize.value + obj.tmp.resize.diff_y) + 'px';
break;
case 'left':
case 'right':
obj.tmp.resize.diff_y = 0;
if (p.length > 0) p[0].style.left = (obj.tmp.resize.value + obj.tmp.resize.diff_x) + 'px';
break;
}
var ptop = obj.get('top');
var pbottom = obj.get('bottom');
var panel = obj.get(obj.tmp.resize.type);
var height = parseInt($(obj.box).height());
var width = parseInt($(obj.box).width());
var str = String(panel.size);
var ns, nd;
var offset = $(obj.box).offset();
switch (obj.tmp.resize.type) {
case 'top':
ns = parseInt(panel.sizeCalculated) + obj.tmp.resize.diff_y;
obj.tmp.resize.y = ns+offset.top;
nd = 0;
break;
case 'bottom':
ns = parseInt(panel.sizeCalculated) - obj.tmp.resize.diff_y;
obj.tmp.resize.y = height-ns+offset.top;
nd = 0;
break;
case 'preview':
ns = parseInt(panel.sizeCalculated) - obj.tmp.resize.diff_y;
nd = (ptop && !ptop.hidden ? ptop.sizeCalculated : 0) +
(pbottom && !pbottom.hidden ? pbottom.sizeCalculated : 0);
obj.tmp.resize.y = height-ns+offset.top;
break;
case 'left':
ns = parseInt(panel.sizeCalculated) + obj.tmp.resize.diff_x;
obj.tmp.resize.x = ns+offset.left;
nd = 0;
break;
case 'right':
ns = parseInt(panel.sizeCalculated) - obj.tmp.resize.diff_x;
obj.tmp.resize.x = width-ns+offset.left;
nd = 0;
break;
}
// set size
if (str.substr(str.length-1) == '%') {
panel.size = Math.floor(ns * 100 / (panel.type == 'left' || panel.type == 'right' ? width : height - nd) * 100) / 100 + '%';
} else {
if (String(panel.size).substr(0, 1) == '-') {
panel.size = parseInt(panel.size) - panel.sizeCalculated + ns;
} else {
panel.size = ns;
}
}
obj.tmp.resize.diff_x = 0;
obj.tmp.resize.diff_y = 0;
obj.resize();
// Recreate iframe blockers with new dimensions
if ( obj.iframeBlocks ) {
// temporarily hide iframe while recreating blockers
// prevents firefox resizing bug!
$(".preview-iframe").css("z-index", "120");
obj.iframeBlocks.remove();
delete obj.iframeBlocks;
}
obj.iframeBlocks = $(document).find( 'iframe' ).map(function() {
var iframe = $( this );
return $( '<div>' )
.css( "position", "absolute" )
.css( "z-index", "125" )
.appendTo( iframe.parent() )
.outerWidth( iframe.outerWidth() )
.outerHeight( iframe.outerHeight() )
.offset( iframe.offset() )[ 0 ];
});
$(".preview-iframe").css("z-index", "121");
// Show hidden panel!
setTimeout(function () {
if (obj.tmp.resize !== undefined && panel.hidden)
obj.show(obj.tmp.resize.type, true);
}, 200);
}
}
function resizeStart(type, evnt) {
if ($(evnt.target).hasClass("w2ui-resize-toggle")) {
obj.toggle(type, false); return;
}
if (!obj.box) return;
$(".preview-iframe").css("opacity", "0.2");
$(".panel-container > div").css("opacity", "0.2");
// Block all iframes from capturing mouse events by
// temporarily overlaying an empty div element.
// Solution adapted from jquery UI library.
obj.iframeBlocks = $(document).find( 'iframe' ).map(function() {
var iframe = $( this );
return $( '<div>' )
.css( "position", "absolute" )
.css( "z-index", "125" )
.appendTo( iframe.parent() )
.outerWidth( iframe.outerWidth() )
.outerHeight( iframe.outerHeight() )
.offset( iframe.offset() )[ 0 ];
});
if (!evnt) evnt = window.event;
$(document).off('mousemove', obj.tmp.events.mouseMove).on('mousemove', obj.tmp.events.mouseMove);
$(document).off('mouseup', obj.tmp.events.mouseUp).on('mouseup', obj.tmp.events.mouseUp);
$(document).off('touchmove', obj.tmp.events.mouseMove).on('touchmove', obj.tmp.events.mouseMove);
$(document).off('touchend', obj.tmp.events.mouseUp).on('touchend', obj.tmp.events.mouseUp);
var newX = evnt.originalEvent.touches != undefined ?
evnt.originalEvent.touches[0].clientX :
evnt.clientX;
var newY = evnt.originalEvent.touches != undefined ?
evnt.originalEvent.touches[0].clientY :
evnt.clientY;
obj.tmp.resize = {
type : type,
x : newX,
y : newY,
diff_x : 0,
diff_y : 0,
value : 0
};
// lock all panels
/*
for (var p1 = 0; p1 < w2panels.length; p1++) {
var $tmp = $(obj.el(w2panels[p1])).parent().find('.w2ui-lock');
if ($tmp.length > 0) {
$tmp.attr('locked', 'previous');
} else {
obj.lock(w2panels[p1], { opacity: 0 });
}
}
*/
if (type == 'left' || type == 'right') {
obj.tmp.resize.value = parseInt($('#layout_'+ obj.name +'_resizer_'+ type)[0].style.left);
$("body").css ('cursor','ew-resize');
}
if (type == 'top' || type == 'preview' || type == 'bottom') {
obj.tmp.resize.value = parseInt($('#layout_'+ obj.name +'_resizer_'+ type)[0].style.top);
$("body").css ('cursor','ns-resize');
}
// add padding
if (type == 'left') obj.tmp.resize.x -= (obj.padding/2);
if (type == 'right') obj.tmp.resize.x += (obj.padding/2);
if (type == 'top') obj.tmp.resize.y -= (obj.padding/2);
if (type == 'bottom' || type == 'preview') obj.tmp.resize.y += (obj.padding/2);
}
function resizeStop(evnt) {
$("body").css ('cursor','');
updateSize(true);
if (!obj.box) return;
if (!evnt) evnt = window.event;
$(document).off('mousemove', obj.tmp.events.mouseMove);
$(document).off('mouseup', obj.tmp.events.mouseUp);
if (typeof obj.tmp.resize == 'undefined') return;
// unlock all panels
/*
for (var p1 = 0; p1 < w2panels.length; p1++) {
var $tmp = $(obj.el(w2panels[p1])).parent().find('.w2ui-lock');
if ($tmp.attr('locked') == 'previous') {
$tmp.removeAttr('locked');
} else {
obj.unlock(w2panels[p1]);
}
}
*/
/*
// Unblock all iframes from capturing mouse events by
// removing temporary overlaying empty div elements.
// Solution adapted from jquery UI library.
*/
$('#layout_'+ obj.name + '_resizer_'+ obj.tmp.resize.type).removeClass('active');
delete obj.tmp.resize;
clearTimeout(this.shiftIframe);
this.shiftIframe = setTimeout(function () {
if ( obj.iframeBlocks ) {
obj.iframeBlocks.remove();
delete obj.iframeBlocks;
}
}, 100);
$(".preview-iframe").css("opacity", "1");
$(".panel-container > div").css("opacity", "1");
}
},
refresh: function (panel) {
var obj = this;
// if (window.getSelection) window.getSelection().removeAllRanges(); // clear selection
if (typeof panel == 'undefined') panel = null;
var time = (new Date()).getTime();
// event before
var eventData = obj.trigger({ phase: 'before', type: 'refresh', target: (typeof panel != 'undefined' ? panel : obj.name), object: obj.get(panel) });
if (eventData.isCancelled === true) return;
// obj.unlock(panel);
if (typeof panel == 'string') {
var p = obj.get(panel);
if (p === null) return;
var pname = '#layout_'+ obj.name + '_panel_'+ p.type;
var rname = '#layout_'+ obj.name +'_resizer_'+ p.type;
// apply properties to the panel
$(pname).css({ display: p.hidden ? 'none' : 'block' });
if (p.resizable) $(rname).show(); else $(rname).hide();
// insert content
if (typeof p.content == 'object' && typeof p.content.render === 'function') {
p.content.box = $(pname +'> .w2ui-panel-content')[0];
setTimeout(function () {
// need to remove unnecessary classes
if ($(pname +'> .w2ui-panel-content').length > 0) {
$(pname +'> .w2ui-panel-content')
.removeClass()
.removeAttr('name')
.addClass('w2ui-panel-content')
.css('overflow', p.overflow)[0].style.cssText += ';' + p.style;
}
p.content.render(); // do not do .render(box);
}, 1);
} else {
// need to remove unnecessary classes
if ($(pname +'> .w2ui-panel-content').length > 0) {
$(pname +'> .w2ui-panel-content')
.removeClass()
.removeAttr('name')
.addClass('w2ui-panel-content')
.html(p.content)
.css('overflow', p.overflow)[0].style.cssText += ';' + p.style;
}
}
// if there are tabs and/or toolbar - render it
var tmp = $(obj.box).find(pname +'> .w2ui-panel-tabs');
if (p.show.tabs) {
if (tmp.find('[name='+ p.tabs.name +']').length === 0 && p.tabs !== null) tmp.w2render(p.tabs); else p.tabs.refresh();
} else {
tmp.html('').removeClass('w2ui-tabs').hide();
}
tmp = $(obj.box).find(pname +'> .w2ui-panel-toolbar');
if (p.show.toolbar) {
if (tmp.find('[name='+ p.toolbar.name +']').length === 0 && p.toolbar !== null) tmp.w2render(p.toolbar); else p.toolbar.refresh();
} else {
tmp.html('').removeClass('w2ui-toolbar').hide();
}
// show title
tmp = $(obj.box).find(pname +'> .w2ui-panel-title');
if (p.title) {
tmp.html(p.title).show();
} else {
tmp.html('').hide();
}
} else {
if ($('#layout_'+ obj.name +'_panel_main').length == 0) {
obj.render();
return;
}
obj.resize();
// refresh all of them
for (var p1 = 0; p1 < this.panels.length; p1++) { obj.refresh(this.panels[p1].type); }
}
obj.trigger($.extend(eventData, { phase: 'after' }));
return (new Date()).getTime() - time;
},
resize: function () {
// if (window.getSelection) window.getSelection().removeAllRanges(); // clear selection
if (!this.box) return false;
var time = (new Date()).getTime();
// event before
var tmp = this.tmp.resize;
var eventData = this.trigger({ phase: 'before', type: 'resize', target: this.name,
panel: tmp ? tmp.type : 'all', diff_x: tmp ? tmp.diff_x : 0, diff_y: tmp ? tmp.diff_y : 0 });
if (eventData.isCancelled === true) return;
if (this.padding < 0) this.padding = 0;
// layout itself
var width = parseInt($(this.box).width());
var height = parseInt($(this.box).height());
/*
var name = $(this.box).attr('name');
$('div[name="' + name + '"] > div').css({
*/
$(this.box).find(' > div').css({ // THIS IS A SLOW LOOKUP?
width : width + 'px',
height : height + 'px'
});
var obj = this;
// panels
var pmain = this.get('main');
var pprev = this.get('preview');
var pleft = this.get('left');
var pright = this.get('right');
var ptop = this.get('top');
var pbottom = this.get('bottom');
var smain = true; // main always on
var sprev = (pprev !== null && pprev.hidden !== true ? true : false);
var sleft = (pleft !== null && pleft.hidden !== true ? true : false);
var sright = (pright !== null && pright.hidden !== true ? true : false);
var stop = (ptop !== null && ptop.hidden !== true ? true : false);
var sbottom = (pbottom !== null && pbottom.hidden !== true ? true : false);
var l, t, w, h, e;
// calculate %
for (var p = 0; p < w2panels.length; p++) {
if (w2panels[p] === 'main') continue;
var tmp = this.get(w2panels[p]);
if (!tmp) continue;
var str = String(tmp.size || 0);
if (str.substr(str.length-1) == '%') {
var tmph = height;
if (tmp.type == 'preview') {
tmph = tmph -
(ptop && !ptop.hidden ? ptop.sizeCalculated : 0) -
(pbottom && !pbottom.hidden ? pbottom.sizeCalculated : 0);
}
tmp.sizeCalculated = parseInt((tmp.type == 'left' || tmp.type == 'right' ? width : tmph) * parseFloat(tmp.size) / 100);
} else {
tmp.sizeCalculated = parseInt(tmp.size);
}
tmp.sizeCalculated = Math.max(tmp.sizeCalculated, parseInt(tmp.minSize));
}
// negative size
if (String(pright.size).substr(0, 1) == '-') {
if (sleft && pleft.size.substr(0, 1) == '-') {
console.log('ERROR: you cannot have both left panel.size and right panel.size be negative.');
} else {
pright.sizeCalculated = width - (sleft ? pleft.sizeCalculated : 0) + parseInt(pright.size);
}
}
if (String(pleft.size).substr(0, 1) == '-') {
if (sright && pright.size.substr(0, 1) == '-') {
console.log('ERROR: you cannot have both left panel.size and right panel.size be negative.');
} else {
pleft.sizeCalculated = width - (sright ? pright.sizeCalculated : 0) + parseInt(pleft.size);
}
}
// top if any
if (ptop !== null) {
l = 0;
t = 0 + this.padding;
w = width;
h = ptop.sizeCalculated - this.padding;
$('#layout_'+ this.name +'_panel_top').css({
'display': 'block',
'left': l + 'px',
'top': t + 'px',
'width': w + 'px',
'height': h + 'px'
}).show();
ptop.width = w;
ptop.height = h;
// resizer
if (ptop.resizable) {
t = ptop.sizeCalculated - (this.padding === 0 ? this.resizer : 0);
h = (this.resizer > this.padding ? this.resizer : this.padding);
$('#layout_'+ this.name +'_resizer_top').show().css({
'display': 'block',
'left': l + 'px',
'top': (ptop.hidden === true ? '0px' : t + 'px'),
'width': w + 'px',
'height': h + 'px',
'cursor': 'ns-resize'
}).off('mousedown').on('mousedown', function (event) {
// event before
var eventData = obj.trigger({ phase: 'before', type: 'resizerClick', target: 'top', originalEvent: event });
if (eventData.isCancelled === true) return;
// default action
w2ui[obj.name].tmp.events.resizeStart('top', event);
// event after
obj.trigger($.extend(eventData, { phase: 'after' }));
return false;
}).off('touchstart').on('touchstart', function (event) {
// event before
var eventData = obj.trigger({ phase: 'before', type: 'resizerClick', target: 'top', originalEvent: event });
if (eventData.isCancelled === true) return;
// default action
w2ui[obj.name].tmp.events.resizeStart('top', event);
// event after
obj.trigger($.extend(eventData, { phase: 'after' }));
return false;
});
}
if (ptop.hidden) $('#layout_'+ this.name +'_panel_top').hide();
}
// left if any
if (pleft !== null) {
l = this.padding;
t = 0 + (stop ? ptop.sizeCalculated + this.padding : 0);