/
GridContainer.js
633 lines (553 loc) · 18.2 KB
/
GridContainer.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
define([
"dojo/_base/kernel",
"dojo/_base/declare", // declare
"dojo/_base/array",
"dojo/_base/connect",
"dojo/_base/sniff",
"dojo/dom-class",
"dojo/dom-style",
"dojo/dom-geometry",
"dojo/dom-construct",
"dojo/_base/lang",
"dojo/_base/window",
"dojo/ready", // dojo.ready
"dojox/layout/GridContainerLite"
],function(dojo, declare, array, connect, has, domClass, domStyle, geom, domConstruct, lang, win, ready, GridContainerLite){
return declare(
"dojox.layout.GridContainer",
GridContainerLite,
{
// summary:
// A grid containing any kind of objects and acting like web portals.
//
// description:
// This component inherits of all features of gridContainerLite plus :
//
// - Resize colums
// - Add / remove columns
// - Fix columns at left or at right.
// example:
// | <div dojoType="dojox.layout.GridContainer" nbZones="3" isAutoOrganized="true">
// | <div dojoType="dijit.layout.ContentPane">Content Pane 1 : Drag Me !</div>
// | <div dojoType="dijit.layout.ContentPane">Content Pane 2 : Drag Me !</div>
// | <div dojoType="dijit.layout.ContentPane">Content Pane 3 : Drag Me !</div>
// | </div>
//
// example:
// | dojo.ready(function(){
// | var cpane1 = new dijit.layout.ContentPane({ title:"cpane1", content: "Content Pane 1 : Drag Me !" }),
// | cpane2 = new dijit.layout.ContentPane({ title:"cpane2", content: "Content Pane 2 : Drag Me !" }),
// | cpane3 = new dijit.layout.ContentPane({ title:"cpane3", content: "Content Pane 3 : Drag Me !" });
// |
// | var widget = new dojox.layout.GridContainer({
// | nbZones: 3,
// | isAutoOrganized: true
// | }, dojo.byId("idNode"));
// | widget.addChild(cpane1, 0, 0);
// | widget.addChild(cpane2, 1, 0);
// | widget.addChild(cpane3, 2, 1);
// | widget.startup();
// | });
// hasResizableColumns: Boolean
// Allow or not resizing of columns by a grip handle.
hasResizableColumns: true,
// liveResizeColumns: Boolean
// Specifies whether columns resize as you drag (true) or only upon mouseup (false)
liveResizeColumns : false,
// minColWidth: Integer
// Minimum column width in percentage.
minColWidth: 20,
// minChildWidth: Integer
// Minimum children width in pixel (only used for IE6 which doesn't handle min-width css property)
minChildWidth: 150,
// mode: String
// Location to add/remove columns, must be set to 'left' or 'right' (default).
mode: "right",
// isRightFixed: Boolean
// Define if the last right column is fixed.
// Used when you add or remove columns by calling setColumns method.
isRightFixed: false,
// isLeftFixed: Boolean
// Define if the last left column is fixed.
// Used when you add or remove columns by calling setColumns method.
isLeftFixed: false,
startup: function(){
// summary:
// Call the startup of GridContainerLite and place grips
// if user has chosen the hasResizableColumns attribute to true.
//console.log("dojox.layout.GridContainer ::: startup");
this.inherited(arguments);
if(this.hasResizableColumns){
for(var i = 0; i < this._grid.length - 1; i++){
this._createGrip(i);
}
// If widget has a container parent, grips will be placed
// by method onShow.
if(!this.getParent()){
// Fix IE7 :
// The CSS property height:100% for the grip
// doesn't work anytime. It's necessary to wait
// the end of loading before to place grips.
ready(lang.hitch(this, "_placeGrips"));
}
}
},
resizeChildAfterDrop : function(/*Node*/node, /*Object*/targetArea, /*Integer*/indexChild){
// summary:
// Call when a child is dropped.
// description:
// Allow to resize and put grips
// node:
// domNode of dropped widget.
// targetArea:
// AreaManager Object containing information of targetArea
// indexChild:
// Index where the dropped widget has been placed
if(this.inherited(arguments)){
this._placeGrips();
}
},
onShow: function(){
// summary:
// Place grips in the right place when the GridContainer becomes visible.
//console.log("dojox.layout.GridContainer ::: onShow");
this.inherited(arguments);
this._placeGrips();
},
resize: function(){
// summary:
// Resize the GridContainer widget and columns.
// Replace grips if it's necessary.
// tags:
// callback
//console.log("dojox.layout.GridContainer ::: resize");
this.inherited(arguments);
// Fix IE6 :
// IE6 calls method resize itself.
// If the GridContainer is not visible at this time,
// the method _placeGrips can return a negative value with
// contentBox method. (see method _placeGrip() with Fix Ie6 for the height)
if(this._isShown() && this.hasResizableColumns){
this._placeGrips();
}
},
_createGrip: function(/*Integer*/ index){
// summary:
// Create a grip for a specific zone.
// index:
// index where the grip has to be created.
// tags:
// protected
//console.log("dojox.layout.GridContainer ::: _createGrip");
var dropZone = this._grid[index],
grip = domConstruct.create("div", { 'class': "gridContainerGrip" }, this.domNode);
dropZone.grip = grip;
dropZone.gripHandler = [
this.connect(grip, "onmouseover", function(e){
var gridContainerGripShow = false;
for(var i = 0; i < this._grid.length - 1; i++){
if(domClass.contains(this._grid[i].grip, "gridContainerGripShow")){
gridContainerGripShow = true;
break;
}
}
if(!gridContainerGripShow){
domClass.replace(e.target, "gridContainerGripShow", "gridContainerGrip");
}
})[0],
this.connect(grip, "onmouseout", function(e){
if(!this._isResized){
domClass.replace(e.target, "gridContainerGrip", "gridContainerGripShow");
}
})[0],
this.connect(grip, "onmousedown", "_resizeColumnOn")[0],
this.connect(grip, "ondblclick", "_onGripDbClick")[0]
];
},
_placeGrips: function(){
// summary:
// Define the position of a grip and place it on page.
// tags:
// protected
//console.log("dojox.layout.GridContainer ::: _placeGrips");
var gripWidth, height, left = 0, grip;
//var scroll = this.domNode.style.overflowY;
array.forEach(this._grid, function(dropZone){
if(dropZone.grip){
grip = dropZone.grip;
if(!gripWidth){
gripWidth = grip.offsetWidth / 2;
}
left += geom.getMarginBox(dropZone.node).w;
domStyle.set(grip, "left", (left - gripWidth) + "px");
//if(dojo.isIE == 6){ do it fot all navigators
if(!height){
height = geom.getContentBox(this.gridNode).h;
}
if(height > 0){
domStyle.set(grip, "height", height + "px");
}
//}
}
}, this);
},
_onGripDbClick: function(){
// summary:
// Called when a double click is catch. Resize all columns with the same width.
// The method resize of children have to be called.
// tags:
// callback protected
//console.log("dojox.layout.GridContainer ::: _onGripDbClick");
this._updateColumnsWidth(this._dragManager);
this.resize();
},
_resizeColumnOn: function(/*Event*/e){
// summary:
// Connect events to listen the resize action.
// Change the type of width columns (% to px).
// Calculate the minwidth according to the children.
// tags:
// callback
//console.log("dojox.layout.GridContainer ::: _resizeColumnOn", e);
this._activeGrip = e.target;
this._initX = e.pageX;
e.preventDefault();
win.body().style.cursor = "ew-resize";
this._isResized = true;
var tabSize = [];
var grid;
var i;
for(i = 0; i < this._grid.length; i++){
tabSize[i] = geom.getContentBox(this._grid[i].node).w;
}
this._oldTabSize = tabSize;
for(i = 0; i < this._grid.length; i++){
grid = this._grid[i];
if(this._activeGrip == grid.grip){
this._currentColumn = grid.node;
this._currentColumnWidth = tabSize[i];
this._nextColumn = this._grid[i + 1].node;
this._nextColumnWidth = tabSize[i + 1];
}
grid.node.style.width = tabSize[i] + "px";
}
// calculate the minWidh of all children for current and next column
var calculateChildMinWidth = function(childNodes, minChild){
var width = 0;
var childMinWidth = 0;
array.forEach(childNodes, function(child){
if(child.nodeType == 1){
var objectStyle = domStyle.getComputedStyle(child);
var minWidth = (has("ie")) ? minChild : parseInt(objectStyle.minWidth);
childMinWidth = minWidth +
parseInt(objectStyle.marginLeft) +
parseInt(objectStyle.marginRight);
if(width < childMinWidth){
width = childMinWidth;
}
}
});
return width;
};
var currentColumnMinWidth = calculateChildMinWidth(this._currentColumn.childNodes, this.minChildWidth);
var nextColumnMinWidth = calculateChildMinWidth(this._nextColumn.childNodes, this.minChildWidth);
var minPix = Math.round((geom.getMarginBox(this.gridContainerTable).w * this.minColWidth) / 100);
this._currentMinCol = currentColumnMinWidth;
this._nextMinCol = nextColumnMinWidth;
if(minPix > this._currentMinCol){
this._currentMinCol = minPix;
}
if(minPix > this._nextMinCol){
this._nextMinCol = minPix;
}
this._connectResizeColumnMove = connect.connect(win.doc, "onmousemove", this, "_resizeColumnMove");
this._connectOnGripMouseUp = connect.connect(win.doc, "onmouseup", this, "_onGripMouseUp");
},
_onGripMouseUp: function(){
// summary:
// Call on the onMouseUp only if the reiszeColumnMove was not called.
// tags:
// callback
//console.log(dojox.layout.GridContainer ::: _onGripMouseUp");
win.body().style.cursor = "default";
connect.disconnect(this._connectResizeColumnMove);
connect.disconnect(this._connectOnGripMouseUp);
this._connectOnGripMouseUp = this._connectResizeColumnMove = null;
if(this._activeGrip){
domClass.replace(this._activeGrip, "gridContainerGrip", "gridContainerGripShow");
}
this._isResized = false;
},
_resizeColumnMove: function(/*Event*/e){
// summary:
// Change columns size.
// tags:
// callback
//console.log("dojox.layout.GridContainer ::: _resizeColumnMove");
e.preventDefault();
if(!this._connectResizeColumnOff){
connect.disconnect(this._connectOnGripMouseUp);
this._connectOnGripMouseUp = null;
this._connectResizeColumnOff = connect.connect(win.doc, "onmouseup", this, "_resizeColumnOff");
}
var d = e.pageX - this._initX;
if(d == 0){ return; }
if(!(this._currentColumnWidth + d < this._currentMinCol ||
this._nextColumnWidth - d < this._nextMinCol)){
this._currentColumnWidth += d;
this._nextColumnWidth -= d;
this._initX = e.pageX;
this._activeGrip.style.left = parseInt(this._activeGrip.style.left) + d + "px";
if(this.liveResizeColumns){
this._currentColumn.style["width"] = this._currentColumnWidth + "px";
this._nextColumn.style["width"] = this._nextColumnWidth + "px";
this.resize();
}
}
},
_resizeColumnOff: function(/*Event*/e){
// summary:
// Disconnect resize events.
// Change the type of width columns (px to %).
// tags:
// callback
//console.log("dojox.layout.GridContainer ::: _resizeColumnOff");
win.body().style.cursor = "default";
connect.disconnect(this._connectResizeColumnMove);
connect.disconnect(this._connectResizeColumnOff);
this._connectResizeColumnOff = this._connectResizeColumnMove = null;
if(!this.liveResizeColumns){
this._currentColumn.style["width"] = this._currentColumnWidth + "px";
this._nextColumn.style["width"] = this._nextColumnWidth + "px";
//this.resize();
}
var tabSize = [],
testSize = [],
tabWidth = this.gridContainerTable.clientWidth,
node,
update = false,
i;
for(i = 0; i < this._grid.length; i++){
node = this._grid[i].node;
if(has("ie")){
tabSize[i] = geom.getMarginBox(node).w;
testSize[i] = geom.getContentBox(node).w;
}
else{
tabSize[i] = geom.getContentBox(node).w;
testSize = tabSize;
}
}
for(i = 0; i < testSize.length; i++){
if(testSize[i] != this._oldTabSize[i]){
update = true;
break;
}
}
if(update){
var mul = has("ie") ? 100 : 10000;
for(i = 0; i < this._grid.length; i++){
this._grid[i].node.style.width = Math.round((100 * mul * tabSize[i]) / tabWidth) / mul + "%";
}
this.resize();
}
if(this._activeGrip){
domClass.replace(this._activeGrip, "gridContainerGrip", "gridContainerGripShow");
}
this._isResized = false;
},
setColumns: function(/*Integer*/nbColumns){
// summary:
// Set the number of columns.
// nbColumns:
// Number of columns
//console.log("dojox.layout.GridContainer ::: setColumns");
var z, j;
if(nbColumns > 0){
var length = this._grid.length,
delta = length - nbColumns;
if(delta > 0){
var count = [], zone, start, end, nbChildren;
// Check if right or left columns are fixed
// Columns are not taken in account and can't be deleted
if(this.mode == "right"){
end = (this.isLeftFixed && length > 0) ? 1 : 0;
start = (this.isRightFixed) ? length - 2 : length - 1
for(z = start; z >= end; z--){
nbChildren = 0;
zone = this._grid[z].node;
for(j = 0; j < zone.childNodes.length; j++){
if(zone.childNodes[j].nodeType == 1 && !(zone.childNodes[j].id == "")){
nbChildren++;
break;
}
}
if(nbChildren == 0){ count[count.length] = z; }
if(count.length >= delta){
this._deleteColumn(count);
break;
}
}
if(count.length < delta){
connect.publish("/dojox/layout/gridContainer/noEmptyColumn", [this]);
}
}
else{ // mode = "left"
start = (this.isLeftFixed && length > 0) ? 1 : 0;
end = (this.isRightFixed) ? length - 1 : length;
for(z = start; z < end; z++){
nbChildren = 0;
zone = this._grid[z].node;
for(j = 0; j < zone.childNodes.length; j++){
if(zone.childNodes[j].nodeType == 1 && !(zone.childNodes[j].id == "")){
nbChildren++;
break;
}
}
if(nbChildren == 0){ count[count.length] = z; }
if(count.length >= delta){
this._deleteColumn(count);
break;
}
}
if(count.length < delta){
//Not enough empty columns
connect.publish("/dojox/layout/gridContainer/noEmptyColumn", [this]);
}
}
}
else{
if(delta < 0){ this._addColumn(Math.abs(delta)); }
}
if(this.hasResizableColumns){ this._placeGrips(); }
}
},
_addColumn: function(/*Integer*/nbColumns){
// summary:
// Add some columns.
// nbColumns:
// Number of column to added
// tags:
// private
//console.log("dojox.layout.GridContainer ::: _addColumn");
var grid = this._grid,
dropZone,
node,
index,
length,
isRightMode = (this.mode == "right"),
accept = this.acceptTypes.join(","),
m = this._dragManager;
//Add a grip to the last column
if(this.hasResizableColumns && ((!this.isRightFixed && isRightMode)
|| (this.isLeftFixed && !isRightMode && this.nbZones == 1) )){
this._createGrip(grid.length - 1);
}
for(var i = 0; i < nbColumns; i++){
// Fix CODEX defect #53025 :
// Apply acceptType attribute on each new column.
node = domConstruct.create("td", {
'class': "gridContainerZone dojoxDndArea" ,
'accept': accept,
'id': this.id + "_dz" + this.nbZones
});
length = grid.length;
if(isRightMode){
if(this.isRightFixed){
index = length - 1;
grid.splice(index, 0, {
'node': grid[index].node.parentNode.insertBefore(node, grid[index].node)
});
}
else{
index = length;
grid.push({ 'node': this.gridNode.appendChild(node) });
}
}
else{
if(this.isLeftFixed){
index = (length == 1) ? 0 : 1;
this._grid.splice(1, 0, {
'node': this._grid[index].node.parentNode.appendChild(node, this._grid[index].node)
});
index = 1;
}
else{
index = length - this.nbZones;
this._grid.splice(index, 0, {
'node': grid[index].node.parentNode.insertBefore(node, grid[index].node)
});
}
}
if(this.hasResizableColumns){
//Add a grip to resize columns
if((!isRightMode && this.nbZones != 1) ||
(!isRightMode && this.nbZones == 1 && !this.isLeftFixed) ||
(isRightMode && i < nbColumns-1) ||
(isRightMode && i == nbColumns-1 && this.isRightFixed)){
this._createGrip(index);
}
}
// register tnbZoneshe new area into the areaManager
m.registerByNode(grid[index].node);
this.nbZones++;
}
this._updateColumnsWidth(m);
},
_deleteColumn: function(/*Array*/indices){
// summary:
// Remove some columns with indices passed as an array.
// indices:
// Column index array
// tags:
// private
//console.log("dojox.layout.GridContainer ::: _deleteColumn");
var child, grid, index,
nbDelZones = 0,
length = indices.length,
m = this._dragManager;
for(var i = 0; i < length; i++){
index = (this.mode == "right") ? indices[i] : indices[i] - nbDelZones;
grid = this._grid[index];
if(this.hasResizableColumns && grid.grip){
array.forEach(grid.gripHandler, function(handler){
connect.disconnect(handler);
});
domConstruct.destroy(this.domNode.removeChild(grid.grip));
grid.grip = null;
}
m.unregister(grid.node);
domConstruct.destroy(this.gridNode.removeChild(grid.node));
this._grid.splice(index, 1);
this.nbZones--;
nbDelZones++;
}
// last grip
var lastGrid = this._grid[this.nbZones-1];
if(lastGrid.grip){
array.forEach(lastGrid.gripHandler, connect.disconnect);
domConstruct.destroy(this.domNode.removeChild(lastGrid.grip));
lastGrid.grip = null;
}
this._updateColumnsWidth(m);
},
_updateColumnsWidth: function(/*Object*/ manager){
// summary:
// Update the columns width.
// manager:
// dojox.mdnd.AreaManager singleton
// tags:
// private
//console.log("dojox.layout.GridContainer ::: _updateColumnsWidth");
this.inherited(arguments);
if(manager === null){
manager = this._dragManager;
}
manager._dropMode.updateAreas(manager._areaList);
},
destroy: function(){
connect.unsubscribe(this._dropHandler);
this.inherited(arguments);
}
});
});