| 1 |
define([ |
| 2 |
"dojo/_base/array", |
| 3 |
"dojo/_base/lang", |
| 4 |
"dojo/_base/declare", |
| 5 |
"./_Container", |
| 6 |
"./at", |
| 7 |
"./Group", |
| 8 |
"dijit/form/TextBox" |
| 9 |
], function(array, lang, declare, Container, at){ |
| 10 |
|
| 11 |
return declare("dojox.mvc.Generate", [Container], { |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
_counter : 0, |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
_defaultWidgetMapping: {"String" : "dijit/form/TextBox"}, |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
_defaultClassMapping: {"Label" : "generate-label-cell", "String" : "generate-dijit-cell", "Heading" : "generate-heading", "Row" : "row"}, |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
_defaultIdNameMapping: {"String" : "textbox_t"}, |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
children: null, |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
_relTargetProp : "children", |
| 48 |
|
| 49 |
startup: function(){ |
| 50 |
this.inherited(arguments); |
| 51 |
this._setChildrenAttr(this.children); |
| 52 |
}, |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
_setChildrenAttr: function( value){ |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
var children = this.children; |
| 63 |
this._set("children", value); |
| 64 |
|
| 65 |
if(this.binding != value){ |
| 66 |
this.set("ref", value); |
| 67 |
} |
| 68 |
if(this._started && (!this._builtOnce || children != value)){ |
| 69 |
this._builtOnce = true; |
| 70 |
this._buildContained(value); |
| 71 |
} |
| 72 |
}, |
| 73 |
|
| 74 |
_buildContained: function( children){ |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
if(!children){ return; } |
| 84 |
|
| 85 |
this._destroyBody(); |
| 86 |
|
| 87 |
this._counter = 0; |
| 88 |
this.srcNodeRef.innerHTML = this._generateBody(children); |
| 89 |
|
| 90 |
this._createBody(); |
| 91 |
}, |
| 92 |
|
| 93 |
_generateBody: function( children, hideHeading){ |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
if(children === void 0){ return ""; } |
| 105 |
|
| 106 |
var body = []; |
| 107 |
var isStatefulModel = lang.isFunction(children.toPlainObject); |
| 108 |
|
| 109 |
function generateElement(value, prop){ |
| 110 |
if(isStatefulModel ? (value && lang.isFunction(value.toPlainObject)) : !lang.isFunction(value)){ |
| 111 |
if(lang.isArray(value)){ |
| 112 |
body.push(this._generateRepeat(value, prop)); |
| 113 |
}else if(isStatefulModel ? value.value : ((value == null || {}.toString.call(value) != "[object Object]") && (!(value || {}).set || !(value || {}).watch))){ |
| 114 |
|
| 115 |
body.push(this._generateTextBox(prop, isStatefulModel)); |
| 116 |
}else{ |
| 117 |
body.push(this._generateGroup(value, prop, hideHeading)); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
if(lang.isArray(children)){ |
| 123 |
array.forEach(children, generateElement, this); |
| 124 |
}else{ |
| 125 |
for(var s in children){ |
| 126 |
if(children.hasOwnProperty(s)){ |
| 127 |
generateElement.call(this, children[s], s); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return body.join(""); |
| 133 |
}, |
| 134 |
|
| 135 |
_generateRepeat: function( children, repeatHeading){ |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
var headingClass = (this.classMapping && this.classMapping["Heading"]) ? this.classMapping["Heading"] : this._defaultClassMapping["Heading"]; |
| 147 |
return '<div data-dojo-type="dojox/mvc/Group" data-dojo-props="target: at(\'rel:\', \'' + repeatHeading + '\')" + id="' + this.id + '_r' + this._counter++ + '">' |
| 148 |
+ '<div class="' + headingClass + '\">' + repeatHeading + '</div>' |
| 149 |
+ this._generateBody(children, true) |
| 150 |
+ '</div>'; |
| 151 |
}, |
| 152 |
|
| 153 |
_generateGroup: function( model, groupHeading, hideHeading){ |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
var html = ['<div data-dojo-type="dojox/mvc/Group" data-dojo-props="target: at(\'rel:\', \'' + groupHeading + '\')" + id="' + this.id + '_g' + this._counter++ + '">']; |
| 166 |
if(!hideHeading){ |
| 167 |
var headingClass = (this.classMapping && this.classMapping["Heading"]) ? this.classMapping["Heading"] : this._defaultClassMapping["Heading"]; |
| 168 |
html.push('<div class="' + headingClass + '\">' + groupHeading + '</div>'); |
| 169 |
} |
| 170 |
html.push(this._generateBody(model) + '</div>'); |
| 171 |
return html.join(""); |
| 172 |
}, |
| 173 |
|
| 174 |
_generateTextBox: function( prop, referToValue){ |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
var idname = this.idNameMapping ? this.idNameMapping["String"] : this._defaultIdNameMapping["String"]; |
| 186 |
idname = idname + this._counter++; |
| 187 |
var widClass = this.widgetMapping ? this.widgetMapping["String"] : this._defaultWidgetMapping["String"]; |
| 188 |
var labelClass = (this.classMapping && this.classMapping["Label"]) ? this.classMapping["Label"] : this._defaultClassMapping["Label"]; |
| 189 |
var stringClass = (this.classMapping && this.classMapping["String"]) ? this.classMapping["String"] : this._defaultClassMapping["String"]; |
| 190 |
var rowClass = (this.classMapping && this.classMapping["Row"]) ? this.classMapping["Row"] : this._defaultClassMapping["Row"]; |
| 191 |
var bindingSyntax = 'value: at(\'rel:' + (referToValue && prop || '') + '\', \'' + (referToValue ? 'value' : prop) + '\')'; |
| 192 |
|
| 193 |
return '<div class="' + rowClass + '\">' + |
| 194 |
'<label class="' + labelClass + '\">' + prop + ':</label>' + |
| 195 |
'<input class="' + stringClass + '\" data-dojo-type="' + widClass + '\"' + |
| 196 |
' data-dojo-props="name: \'' + idname + '\', ' + bindingSyntax + '" id="' + idname + '\"></input>' + |
| 197 |
'</div>'; |
| 198 |
} |
| 199 |
}); |
| 200 |
}); |