| 1 |
define(["dojo/_base/kernel", "dojo/_base/lang", "dojo/_base/declare", "dojo/dom-class", "dojo/dom-construct", |
| 2 |
"dojo/_base/array", "dojo/dom-prop", "dojo/dom-style", "dijit/_WidgetBase", "dijit/layout/_LayoutWidget"], |
| 3 |
function(kernel, lang, declare, domClass, domConstruct, arrayUtil, domProp, domStyle, _WidgetBase, _LayoutWidget){ |
| 4 |
|
| 5 |
kernel.experimental("dojox.layout.TableContainer"); |
| 6 |
|
| 7 |
var TableContainer = declare("dojox.layout.TableContainer", _LayoutWidget, { |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
cols: 1, |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
labelWidth: "100", |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
showLabels: true, |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
orientation: "horiz", |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
spacing: 1, |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
customClass: "", |
| 54 |
|
| 55 |
postCreate: function(){ |
| 56 |
this.inherited(arguments); |
| 57 |
this._children = []; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
this.connect(this, "set", function(name, value){ |
| 62 |
if(value && (name == "orientation" || name == "customClass" || name == "cols")) { |
| 63 |
this.layout(); |
| 64 |
} |
| 65 |
}); |
| 66 |
}, |
| 67 |
|
| 68 |
startup: function() { |
| 69 |
if(this._started) { |
| 70 |
return; |
| 71 |
} |
| 72 |
this.inherited(arguments); |
| 73 |
if(this._initialized) { |
| 74 |
return; |
| 75 |
} |
| 76 |
var children = this.getChildren(); |
| 77 |
if(children.length < 1) { |
| 78 |
return; |
| 79 |
} |
| 80 |
this._initialized = true; |
| 81 |
|
| 82 |
domClass.add(this.domNode, "dijitTableLayout"); |
| 83 |
|
| 84 |
|
| 85 |
arrayUtil.forEach(children, function(child){ |
| 86 |
if(!child.started && !child._started) { |
| 87 |
child.startup(); |
| 88 |
} |
| 89 |
}); |
| 90 |
this.layout(); |
| 91 |
this.resize(); |
| 92 |
}, |
| 93 |
|
| 94 |
resize: function(){ |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
arrayUtil.forEach(this.getChildren(), function(child){ |
| 100 |
if(typeof child.resize == "function") { |
| 101 |
child.resize(); |
| 102 |
} |
| 103 |
}); |
| 104 |
}, |
| 105 |
|
| 106 |
layout: function(){ |
| 107 |
|
| 108 |
|
| 109 |
if(!this._initialized){ |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
var children = this.getChildren(); |
| 114 |
|
| 115 |
var childIds = {}; |
| 116 |
var _this = this; |
| 117 |
|
| 118 |
function addCustomClass(node, type, count) { |
| 119 |
if(_this.customClass != "") { |
| 120 |
var clazz = _this.customClass+ "-" + (type || node.tagName.toLowerCase()); |
| 121 |
domClass.add(node, clazz); |
| 122 |
|
| 123 |
if(arguments.length > 2) { |
| 124 |
domClass.add(node, clazz + "-" + count); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
arrayUtil.forEach(this._children, lang.hitch(this, function(child){ |
| 131 |
childIds[child.id] = child; |
| 132 |
})); |
| 133 |
|
| 134 |
arrayUtil.forEach(children, lang.hitch(this, function(child, index){ |
| 135 |
if(!childIds[child.id]) { |
| 136 |
|
| 137 |
this._children.push(child); |
| 138 |
} |
| 139 |
})); |
| 140 |
|
| 141 |
|
| 142 |
var table = domConstruct.create("table", { |
| 143 |
"width": "100%", |
| 144 |
"class": "tableContainer-table tableContainer-table-" + this.orientation, |
| 145 |
"cellspacing" : this.spacing |
| 146 |
}, |
| 147 |
this.domNode); |
| 148 |
|
| 149 |
var tbody = domConstruct.create("tbody"); |
| 150 |
table.appendChild(tbody); |
| 151 |
|
| 152 |
addCustomClass(table, "table", this.orientation); |
| 153 |
|
| 154 |
var width = Math.floor(100 / this.cols) + "%"; |
| 155 |
|
| 156 |
var labelRow = domConstruct.create("tr", {}, tbody); |
| 157 |
var childRow = (!this.showLabels || this.orientation == "horiz") |
| 158 |
? labelRow : domConstruct.create("tr", {}, tbody); |
| 159 |
var maxCols = this.cols * (this.showLabels ? 2 : 1); |
| 160 |
var numCols = 0; |
| 161 |
|
| 162 |
|
| 163 |
arrayUtil.forEach(this._children, lang.hitch(this, function(child, index){ |
| 164 |
|
| 165 |
var colspan = child.colspan || 1; |
| 166 |
|
| 167 |
if(colspan > 1) { |
| 168 |
colspan = this.showLabels ? |
| 169 |
Math.min(maxCols - 1, colspan * 2 -1): Math.min(maxCols, colspan); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
if(numCols + colspan - 1 + (this.showLabels ? 1 : 0)>= maxCols) { |
| 174 |
numCols = 0; |
| 175 |
labelRow = domConstruct.create("tr", {}, tbody); |
| 176 |
childRow = this.orientation == "horiz" ? labelRow : domConstruct.create("tr", {}, tbody); |
| 177 |
} |
| 178 |
var labelCell; |
| 179 |
|
| 180 |
|
| 181 |
if(this.showLabels) { |
| 182 |
labelCell = domConstruct.create("td", {"class": "tableContainer-labelCell"}, labelRow); |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
if(child.spanLabel) { |
| 187 |
domProp.set(labelCell, this.orientation == "vert" ? "rowspan" : "colspan", 2); |
| 188 |
} |
| 189 |
else { |
| 190 |
|
| 191 |
addCustomClass(labelCell, "labelCell"); |
| 192 |
var labelProps = {"for": child.get("id")}; |
| 193 |
var label = domConstruct.create("label", labelProps, labelCell); |
| 194 |
|
| 195 |
if(Number(this.labelWidth) > -1 || |
| 196 |
String(this.labelWidth).indexOf("%") > -1) { |
| 197 |
|
| 198 |
|
| 199 |
domStyle.set(labelCell, "width", |
| 200 |
String(this.labelWidth).indexOf("%") < 0 |
| 201 |
? this.labelWidth + "px" : this.labelWidth); |
| 202 |
} |
| 203 |
|
| 204 |
label.innerHTML = child.get("label") || child.get("title"); |
| 205 |
} |
| 206 |
} |
| 207 |
var childCell; |
| 208 |
|
| 209 |
if(child.spanLabel && labelCell) { |
| 210 |
childCell = labelCell; |
| 211 |
} else { |
| 212 |
childCell = domConstruct.create("td", { |
| 213 |
"class" : "tableContainer-valueCell" |
| 214 |
}, childRow); |
| 215 |
} |
| 216 |
if(colspan > 1) { |
| 217 |
domProp.set(childCell, "colspan", colspan); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
addCustomClass(childCell, "valueCell", index); |
| 222 |
|
| 223 |
childCell.appendChild(child.domNode); |
| 224 |
numCols += colspan + (this.showLabels ? 1 : 0); |
| 225 |
})); |
| 226 |
|
| 227 |
if(this.table) { |
| 228 |
this.table.parentNode.removeChild(this.table); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
arrayUtil.forEach(children, function(child){ |
| 233 |
if(typeof child.layout == "function") { |
| 234 |
child.layout(); |
| 235 |
} |
| 236 |
}); |
| 237 |
this.table = table; |
| 238 |
this.resize(); |
| 239 |
}, |
| 240 |
|
| 241 |
destroyDescendants: function( preserveDom){ |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
arrayUtil.forEach(this._children, function(child){ child.destroyRecursive(preserveDom); }); |
| 246 |
}, |
| 247 |
|
| 248 |
_setSpacingAttr: function(value) { |
| 249 |
|
| 250 |
|
| 251 |
this.spacing = value; |
| 252 |
if(this.table) { |
| 253 |
this.table.cellspacing = Number(value); |
| 254 |
} |
| 255 |
} |
| 256 |
}); |
| 257 |
|
| 258 |
TableContainer.ChildWidgetProperties = { |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
label: "", |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
title: "", |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
spanLabel: false, |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
colspan: 1 |
| 280 |
}; |
| 281 |
|
| 282 |
|
| 283 |
lang.extend(_WidgetBase, TableContainer.ChildWidgetProperties); |
| 284 |
|
| 285 |
return TableContainer; |
| 286 |
}); |