jquery.window.js
9.92 KB
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
/**
* jQuery EasyUI 1.4.1
*
* Copyright (c) 2009-2014 www.jeasyui.com. All rights reserved.
*
* Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt
* To use it on other terms please contact us at info@jeasyui.com
*
*/
/**
* window - jQuery EasyUI
*
* Dependencies:
* panel
* draggable
* resizable
*
*/
(function($){
function moveWindow(target, param){
var state = $.data(target, 'window');
if (param){
if (param.left != null) state.options.left = param.left;
if (param.top != null) state.options.top = param.top;
}
$(target).panel('move', state.options);
if (state.shadow){
state.shadow.css({
left: state.options.left,
top: state.options.top
});
}
}
/**
* center the window only horizontally
*/
function hcenter(target, tomove){
var opts = $.data(target, 'window').options;
var pp = $(target).window('panel');
var width = pp._outerWidth();
if (opts.inline){
var parent = pp.parent();
opts.left = Math.ceil((parent.width() - width) / 2 + parent.scrollLeft());
} else {
opts.left = Math.ceil(($(window)._outerWidth() - width) / 2 + $(document).scrollLeft());
}
if (tomove){moveWindow(target);}
}
/**
* center the window only vertically
*/
function vcenter(target, tomove){
var opts = $.data(target, 'window').options;
var pp = $(target).window('panel');
var height = pp._outerHeight();
if (opts.inline){
var parent = pp.parent();
opts.top = Math.ceil((parent.height() - height) / 2 + parent.scrollTop());
} else {
opts.top = Math.ceil(($(window)._outerHeight() - height) / 2 + $(document).scrollTop());
}
if (tomove){moveWindow(target);}
}
function create(target){
var state = $.data(target, 'window');
var opts = state.options;
var win = $(target).panel($.extend({}, state.options, {
border: false,
doSize: true, // size the panel, the property undefined in window component
closed: true, // close the panel
cls: 'window',
headerCls: 'window-header',
bodyCls: 'window-body ' + (opts.noheader ? 'window-body-noheader' : ''),
onBeforeDestroy: function(){
if (opts.onBeforeDestroy.call(target) == false){return false;}
if (state.shadow){state.shadow.remove();}
if (state.mask){state.mask.remove();}
},
onClose: function(){
if (state.shadow){state.shadow.hide();}
if (state.mask){state.mask.hide();}
opts.onClose.call(target);
},
onOpen: function(){
if (state.mask){
state.mask.css({
display:'block',
zIndex: $.fn.window.defaults.zIndex++
});
}
if (state.shadow){
state.shadow.css({
display:'block',
zIndex: $.fn.window.defaults.zIndex++,
left: opts.left,
top: opts.top,
width: state.window._outerWidth(),
height: state.window._outerHeight()
});
}
state.window.css('z-index', $.fn.window.defaults.zIndex++);
opts.onOpen.call(target);
},
onResize: function(width, height){
var popts = $(this).panel('options');
$.extend(opts, {
width: popts.width,
height: popts.height,
left: popts.left,
top: popts.top
});
if (state.shadow){
state.shadow.css({
left: opts.left,
top: opts.top,
width: state.window._outerWidth(),
height: state.window._outerHeight()
});
}
opts.onResize.call(target, width, height);
},
onMinimize: function(){
if (state.shadow){state.shadow.hide();}
if (state.mask){state.mask.hide();}
state.options.onMinimize.call(target);
},
onBeforeCollapse: function(){
if (opts.onBeforeCollapse.call(target) == false){return false;}
if (state.shadow){state.shadow.hide();}
},
onExpand: function(){
if (state.shadow){state.shadow.show();}
opts.onExpand.call(target);
}
}));
state.window = win.panel('panel');
// create mask
if (state.mask){state.mask.remove();}
if (opts.modal == true){
state.mask = $('<div class="window-mask"></div>').insertAfter(state.window);
state.mask.css({
width: (opts.inline ? state.mask.parent().width() : getPageArea().width),
height: (opts.inline ? state.mask.parent().height() : getPageArea().height),
display: 'none'
});
}
// create shadow
if (state.shadow){state.shadow.remove();}
if (opts.shadow == true){
state.shadow = $('<div class="window-shadow"></div>').insertAfter(state.window);
state.shadow.css({
display: 'none'
});
}
// if require center the window
if (opts.left == null){hcenter(target);}
if (opts.top == null){vcenter(target);}
moveWindow(target);
if (!opts.closed){
win.window('open'); // open the window
}
}
/**
* set window drag and resize property
*/
function setProperties(target){
var state = $.data(target, 'window');
state.window.draggable({
handle: '>div.panel-header>div.panel-title',
disabled: state.options.draggable == false,
onStartDrag: function(e){
if (state.mask) state.mask.css('z-index', $.fn.window.defaults.zIndex++);
if (state.shadow) state.shadow.css('z-index', $.fn.window.defaults.zIndex++);
state.window.css('z-index', $.fn.window.defaults.zIndex++);
if (!state.proxy){
state.proxy = $('<div class="window-proxy"></div>').insertAfter(state.window);
}
state.proxy.css({
display:'none',
zIndex: $.fn.window.defaults.zIndex++,
left: e.data.left,
top: e.data.top
});
state.proxy._outerWidth(state.window._outerWidth());
state.proxy._outerHeight(state.window._outerHeight());
setTimeout(function(){
if (state.proxy) state.proxy.show();
}, 500);
},
onDrag: function(e){
state.proxy.css({
display:'block',
left: e.data.left,
top: e.data.top
});
return false;
},
onStopDrag: function(e){
state.options.left = e.data.left;
state.options.top = e.data.top;
$(target).window('move');
state.proxy.remove();
state.proxy = null;
}
});
state.window.resizable({
disabled: state.options.resizable == false,
onStartResize:function(e){
if (state.pmask){state.pmask.remove();}
state.pmask = $('<div class="window-proxy-mask"></div>').insertAfter(state.window);
state.pmask.css({
zIndex: $.fn.window.defaults.zIndex++,
left: e.data.left,
top: e.data.top,
width: state.window._outerWidth(),
height: state.window._outerHeight()
});
if (state.proxy){state.proxy.remove();}
state.proxy = $('<div class="window-proxy"></div>').insertAfter(state.window);
state.proxy.css({
zIndex: $.fn.window.defaults.zIndex++,
left: e.data.left,
top: e.data.top
});
state.proxy._outerWidth(e.data.width)._outerHeight(e.data.height);
},
onResize: function(e){
state.proxy.css({
left: e.data.left,
top: e.data.top
});
state.proxy._outerWidth(e.data.width);
state.proxy._outerHeight(e.data.height);
return false;
},
onStopResize: function(e){
$(target).window('resize', e.data);
state.pmask.remove();
state.pmask = null;
state.proxy.remove();
state.proxy = null;
}
});
}
function getPageArea() {
if (document.compatMode == 'BackCompat') {
return {
width: Math.max(document.body.scrollWidth, document.body.clientWidth),
height: Math.max(document.body.scrollHeight, document.body.clientHeight)
}
} else {
return {
width: Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth),
height: Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight)
}
}
}
// when window resize, reset the width and height of the window's mask
$(window).resize(function(){
$('body>div.window-mask').css({
width: $(window)._outerWidth(),
height: $(window)._outerHeight()
});
setTimeout(function(){
$('body>div.window-mask').css({
width: getPageArea().width,
height: getPageArea().height
});
}, 50);
});
$.fn.window = function(options, param){
if (typeof options == 'string'){
var method = $.fn.window.methods[options];
if (method){
return method(this, param);
} else {
return this.panel(options, param);
}
}
options = options || {};
return this.each(function(){
var state = $.data(this, 'window');
if (state){
$.extend(state.options, options);
} else {
state = $.data(this, 'window', {
options: $.extend({}, $.fn.window.defaults, $.fn.window.parseOptions(this), options)
});
if (!state.options.inline){
// $(this).appendTo('body');
document.body.appendChild(this);
}
}
create(this);
setProperties(this);
});
};
$.fn.window.methods = {
options: function(jq){
var popts = jq.panel('options');
var wopts = $.data(jq[0], 'window').options;
return $.extend(wopts, {
closed: popts.closed,
collapsed: popts.collapsed,
minimized: popts.minimized,
maximized: popts.maximized
});
},
window: function(jq){
return $.data(jq[0], 'window').window;
},
move: function(jq, param){
return jq.each(function(){
moveWindow(this, param);
});
},
hcenter: function(jq){
return jq.each(function(){
hcenter(this, true);
});
},
vcenter: function(jq){
return jq.each(function(){
vcenter(this, true);
});
},
center: function(jq){
return jq.each(function(){
hcenter(this);
vcenter(this);
moveWindow(this);
});
}
};
$.fn.window.parseOptions = function(target){
return $.extend({}, $.fn.panel.parseOptions(target), $.parser.parseOptions(target, [
{draggable:'boolean',resizable:'boolean',shadow:'boolean',modal:'boolean',inline:'boolean'}
]));
};
// Inherited from $.fn.panel.defaults
$.fn.window.defaults = $.extend({}, $.fn.panel.defaults, {
zIndex: 9000,
draggable: true,
resizable: true,
shadow: true,
modal: false,
inline: false, // true to stay inside its parent, false to go on top of all elements
// window's property which difference from panel
title: 'New Window',
collapsible: true,
minimizable: true,
maximizable: true,
closable: true,
closed: false
});
})(jQuery);