-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialoger.js
522 lines (397 loc) · 11.9 KB
/
dialoger.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
Dialoger.defaultOptions = {
isVisible : true, // 是否可见
isDragable : true, // 是否可拖动
isScalable : true, // 是否可缩放大小
isCollapse : false, // 是否缩起
isMaximize : false, // 是否最大化
zIndex : 1000, // css z-index 起始值
parent : document.body, // 父元素
// 大小和位置
width: 250,
height: 250,
left: 200,
top: 200,
// 最大和最小范围限制
minWidth : 100,
minHeight : 100,
maxWidth : 300,
maxHeight : 300,
// 默认窗体信息
header : "title",
body : "body",
footer : "footer",
edgeSize : 8 // 可缩放大小边界区域
}
function Dialoger(opts) {
var options = this.constructor.defaultOptions;
// 扩展默认参数
this.options = Util.extend(options,opts);
// 初始化
this.init();
}
Dialoger.prototype = {
constructor : Dialoger,
// 初始化
init : function() {
var opts = this.options;
this._create(); // 创建窗口元素
this._setStatus();
this._headerMax(); // 双击最大化
this._setDragResize(); // 设置拖拽缩小放大窗体
this._setToInstance(); // 放到窗口实例集合中
this._bindSetToFront(); // 绑定点击 修改层级(移动到最前)
},
_setToInstance : function(){
if(!this.constructor.allInstance){
this.constructor.allInstance = [];
}
this.constructor.allInstance.push(this);
},
_bindSetToFront : function(){
var _self = this;
var allInstance = this.constructor.allInstance;
Util.event.on(this.winElement, 'mousedown', function(){
for(var i = 0, j = allInstance.length; i < j; i++){
if(allInstance[i] === _self){
allInstance.splice(i,1);
allInstance.push(_self);
}
allInstance[i].winElement.style.zIndex =
_self.options.zIndex + (5 * i);
}
});
},
// 初始设置 窗口的 层级 z-index
_setToFront : function(){
var len = 0;
if(this.constructor.allInstance){
len = this.constructor.allInstance.length;
}
this.winElement.style.zIndex = this.options.zIndex + (5 * len);
},
// 创建窗口
_create : function () {
var opts = this.options;
// dialoger-window
var winElement = this.winElement = document.createElement('div');
winElement.className = "dialoger-window";
// dialoger-window-container
var winContainer = this.winContainer = document.createElement('div');
winContainer.className = "dialoger-window-container";
winElement.appendChild(winContainer);
// dialoger-window-header
var winHeader = this.winHeader = document.createElement('div');
winHeader.className = "dialoger-window-header";
winHeader.innerHTML = opts.header;
winContainer.appendChild(winHeader);
// dialoger-window-body
var winBody = this.winBody = document.createElement('div');
winBody.className = "dialoger-window-body";
winBody.innerHTML = opts.body;
winContainer.appendChild(winBody);
// dialoger-window-footer
var winFooter = this.winFooter = document.createElement('div');
winFooter.className = "dialoger-window-footer";
winFooter.innerHTML = opts.footer;
winContainer.appendChild(winFooter);
this.options.parent.appendChild(winElement);
},
_setStatus : function(argument) {
var opts = this.options;
this.moveTo(opts.left, opts.top);
this.resizeTo(opts.width, opts.height);
this.show();
this.collapse();
this.setDragAble();
this._setToFront(); // 设置层级
},
// 设置可拖动
setDragAble : function(argument) {
var _self = this;
Util.event.on(_self.winHeader, "mousedown", dragDown);
var startPosition, // 窗口起始位置
startAxis, // 鼠标起始位置
parentSize // 拖动范围大小
// 点击拖动绑定
function dragDown(e) {
var opts = _self.options;
if(opts.isMaximize){return};
e = Util.event.getEvent(e);
startAxis = Util.event.getPageAxis(e);
startPosition = {
left : opts.left,
top : opts.top
}
parentSize = {
width : opts.parent.clientWidth,
height : opts.parent.clientHeight
}
Util.event.on(document, 'mousemove', dragMove);
Util.event.on(document, 'mouseup', dragStop);
}
// 移动过程
function dragMove(e) {
e = Util.event.getEvent(e);
var endAxis = Util.event.getPageAxis(e);
var changedAxis = {
x : endAxis.x - startAxis.x,
y : endAxis.y - startAxis.y
}
var resultLeft = startPosition.left + changedAxis.x;
var resultTop = startPosition.top + changedAxis.y;
if(resultLeft < 0){
resultLeft = 0;
}
if(resultTop < 0){
resultTop = 0;
}
if(resultLeft + _self.options.width > parentSize.width){
resultLeft = parentSize.width - _self.options.width
}
if(resultTop + _self.options.height > parentSize.height){
resultTop = parentSize.height - _self.options.height
}
_self.moveTo(resultLeft, resultTop);
}
// 停止拖动
function dragStop(e){
e = Util.event.getEvent(e);
Util.event.off(document, 'mousemove', dragMove)
Util.event.off(document, 'mouseup', dragStop)
}
},
// 设置是否可拖动
toggleDragAble : function(isDragable){
isDragable = isDragable === undefined ? !this.options.isDragable : isDragable;
this.options.isDragable =isDragable;
this.winHeader.style.cursor = isDragable ? "move" : "default"
},
// 移动到指定位置
moveTo : function(left, top) {
var style = this.winElement.style;
left !== null && (style.left = left + "px", this.options.left = left);
top !== null && (style.top = top + "px", this.options.top = top);
},
// 改变大小
resizeTo : function(width, height) {
var style = this.winElement.style;
width !== null && (style.width = width + "px", this.options.width = width);
height !== null && (style.height = height + "px", this.options.height = height);
},
// 设置显示状态
show : function(isShow) {
isShow = isShow === undefined ? this.options.isVisible : isShow;
this.winElement.style.display = (isShow ? "block" : "none");
this.isVisible = isShow;
},
// 切换显示状态
toggle : function() {
this.show[!this.isVisible]()
},
// 最大化窗口
maximize : function() {
var opts = this.options;
// 保存原始状态
this.preStatus = {
left : opts.left,
top : opts.top,
width : opts.width,
height : opts.height
}
this.resizeTo(
opts.parent.clientWidth,
opts.parent.clientHeight
);
this.moveTo(0, 0);
opts.isMaximize = true;
this.toggleDragAble(false);
},
// 恢复窗口大小
restore : function() {
var preStatus = this.preStatus;
this.resizeTo(preStatus.width, preStatus.height);
this.moveTo(preStatus.left, preStatus.top);
this.options.isMaximize = false;
this.toggleDragAble(true);
},
// 折叠窗口
collapse : function() {
},
// 双击切换最大化
_headerMax : function(){
var _self = this;
Util.event.on(this.winHeader, 'dblclick', function(){
_self.options.isMaximize ? _self.restore() : _self.maximize();
});
},
// 销毁窗口
destory : function() {
this.winElement.parentNode.removeChild(this.winElement);
},
// 设置缩放鼠标样式
_setDragCursor : function(){
var _self = this;
Util.event.on(this.winElement, "mousemove", changeDragCursor);
var winStyle = _self.winElement.style, cursor, direction, edgeSize = _self.options.edgeSize;
function changeDragCursor(e){
if(_self.isDraging || _self.options.isMaximize){return} // 正在拖动或最大化,直接返回
e = Util.event.getEvent(e);
var winEdge = _self.winElement.getBoundingClientRect();
if(e.clientX > winEdge.right - edgeSize){ // E
cursor = "e-resize";
direction = "E";
if(e.clientY > winEdge.bottom - edgeSize){ // SE
cursor = "se-resize";
direction = "SE";
}else if(e.clientY < winEdge.top + edgeSize){ // NE
cursor = "ne-resize";
direction = "NE";
}
}else if(e.clientX < winEdge.left + edgeSize){ // W
cursor = "w-resize";
direction = "W";
if(e.clientY > winEdge.bottom - edgeSize){ // SW
cursor = "sw-resize";
direction = "SW";
}else if(e.clientY < winEdge.top + edgeSize){ // NW
cursor = "nw-resize";
direction = "NW";
}
}else if(e.clientY > winEdge.bottom - edgeSize){ // S
cursor = "s-resize";
direction = "S";
}else if(e.clientY < winEdge.top + edgeSize){ // N
cursor = "n-resize";
direction = "N";
}else{
cursor = "default";
direction = false;
}
_self.winHeader.style.cursor =
(direction ? "inherit" : "move");
winStyle.cursor = cursor;
_self.direction = direction;
}
},
// 拖动 缩小放大窗体
_setDragResize : function(){
var _self = this;
this._setDragCursor(); // 设置鼠标样式
Util.event.on(this.winElement, "mousedown", resizeDown);
var startPosition, // 窗口起始位置
startSize, // 窗体起始大小
startAxis, // 鼠标起始位置
parentSize, // 父容器内容大小
opts = this.options;
function resizeDown(e){
e = Util.event.getEvent(e);
startPosition = {
left : opts.left,
top : opts.top
}
startSize = {
width : opts.width,
height : opts.height
}
startAxis = {
x : e.clientX,
y : e.clientY
}
parentSize = {
width : opts.parent.clientWidth,
height : opts.parent.clientHeight
}
Util.event.on(document, 'mousemove', resizeMove);
Util.event.on(document, 'mouseup', resizeStop);
}
function resizeMove(e){
_self.isDraging = true;
e = Util.event.getEvent(e);
var changedAxis = {
x : e.clientX - startAxis.x,
y : e.clientY - startAxis.y
}
var width = startSize.width,
height = startSize.height,
left = startPosition.left,
top = startPosition.top;
switch(_self.direction){
case "E" :
width = startSize.width + changedAxis.x;
break;
case "S" :
height = startSize.height + changedAxis.y;
break;
case "W" :
width = startSize.width - changedAxis.x;
left = startPosition.left + changedAxis.x;
break;
case "N" :
height = startSize.height - changedAxis.y;
top = startPosition.top + changedAxis.y;
break;
case "SE" :
height = startSize.height + changedAxis.y;
width = startSize.width + changedAxis.x;
break;
case "SW" :
width = startSize.width - changedAxis.x;
height = startSize.height + changedAxis.y;
left = startPosition.left + changedAxis.x;
break;
case "NE" :
width = startSize.width + changedAxis.x;
height = startSize.height - changedAxis.y;
top = startPosition.top + changedAxis.y;
break;
case "NW" :
width = startSize.width - changedAxis.x;
left = startPosition.left + changedAxis.x;
height = startSize.height - changedAxis.y;
top = startPosition.top + changedAxis.y;
break;
default :
return false;
}
// 最大最小限制
width = Math.min(opts.maxWidth , width);
width = Math.max(opts.minWidth , width);
height = Math.min(opts.maxHeight , height);
height = Math.max(opts.minHeight , height);
if(_self.direction.indexOf("W") !== -1){
if(width <= opts.minWidth){
left = startPosition.left + startSize.width - opts.minWidth
}else if(width >= opts.maxWidth){
left = startPosition.left - opts.maxWidth + startSize.width
}
}
if(_self.direction.indexOf("N") !== -1){
if(height <= opts.minHeight){
top = startPosition.top + startSize.height - opts.minHeight
}else if(height >= opts.maxHeight){
top = startPosition.top - opts.maxHeight + startSize.height
}
}
if(left <= 0 && _self.direction.indexOf("W") !== -1){
width = startPosition.left + startSize.width;
left = 0;
}
if(top <= 0 && _self.direction.indexOf("N") !== -1){
height = startPosition.top + startSize.height;
top = 0;
}
// 拖动时边界限制
if(width + left > parentSize.width){width = parentSize.width - left}
if(height + top > parentSize.height){height = parentSize.height - top}
if(top < 0){top = 0}
if(left < 0){left = 0}
_self.resizeTo(width, height);
_self.moveTo(left, top);
}
function resizeStop(){
_self.isDraging = false;
Util.event.off(document, 'mousemove', resizeMove);
Util.event.off(document, 'mouseup', resizeStop);
}
}
}