| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <meta http-equiv="Content-type" content="text/html; charset=utf-8"> |
| 5 | <title>MVC Test for dynamic label and calculating totals</title> |
| 6 | <style type="text/css"> |
| 7 | @import "../css/app-format.css"; |
| 8 | @import "../../../../dijit/themes/claro/claro.css"; |
| 9 | .cell { width: 30%; display:inline-block; } |
| 10 | </style> |
| 11 | |
| 12 | <script type="text/javascript" data-dojo-config="parseOnLoad:0,isDebug:1,async:1,mvc:{debugBindings:1}" src="../../../../dojo/dojo.js"></script> |
| 13 | <script type="text/javascript"> |
| 14 | var model; |
| 15 | |
| 16 | require([ |
| 17 | "doh/runner", |
| 18 | "dojo/dom", |
| 19 | "dojo/parser", |
| 20 | "dojo/when", |
| 21 | "dijit/registry", |
| 22 | "dojox/mvc/getStateful", |
| 23 | "dijit/form/TextBox", |
| 24 | "dijit/form/Button", |
| 25 | "dojox/mvc/Group", |
| 26 | "dojox/mvc/Repeat", |
| 27 | "dojox/mvc/Output" |
| 28 | ], function(doh, ddom, parser, when, registry, getStateful){ |
| 29 | |
| 30 | // Order data |
| 31 | orderData = { |
| 32 | "Serial" : "111", |
| 33 | "First" : "", |
| 34 | "Lab" : "TEST", |
| 35 | "Last" : "last", |
| 36 | "Email" : "email", |
| 37 | "Age" : "33" |
| 38 | }; |
| 39 | |
| 40 | // Initial sum data |
| 41 | sumData = { |
| 42 | "one" : 1, |
| 43 | "two" : 2, |
| 44 | "three" : 3, |
| 45 | "four" : 4, |
| 46 | "total" : 10 |
| 47 | }; |
| 48 | |
| 49 | // Initial repeat sum data |
| 50 | sumRepeatData = { |
| 51 | "list" : [ |
| 52 | {"item" : 1}, |
| 53 | {"item" : 2}, |
| 54 | {"item" : 3}, |
| 55 | {"item" : 4} |
| 56 | ], |
| 57 | "total" : 10 |
| 58 | }; |
| 59 | |
| 60 | sumValuesConverter = { |
| 61 | format: function(value){ |
| 62 | console.log("in sumValuesConverter format value ="+value); |
| 63 | value = value || 0; |
| 64 | return value; |
| 65 | }, |
| 66 | parse: function(value){ |
| 67 | console.log("in sumValuesConverter parse value ="+value); |
| 68 | recomputeTotal(); |
| 69 | return value; |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | ageLabelConverter = { |
| 74 | format: function(value){ |
| 75 | console.log("in ageLabelConverter format value ="+value); |
| 76 | if(!value){ |
| 77 | return "Enter Age"; |
| 78 | }else{ |
| 79 | return "Enter "+value+"'s age"; |
| 80 | } |
| 81 | }, |
| 82 | parse: function(value){ |
| 83 | console.log("in ageLabelConverter parse value ="+value); |
| 84 | return value; |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | sumRepeatValuesConverter = { |
| 90 | format: function(value){ |
| 91 | console.log("in sumRepeatValuesConverter format value ="+value); |
| 92 | value = value || 0; |
| 93 | return value; |
| 94 | // recomputeRepeatTotal(); |
| 95 | // return value; |
| 96 | }, |
| 97 | parse: function(value){ |
| 98 | console.log("in sumRepeatValuesConverter parse value ="+value); |
| 99 | recomputeRepeatTotal(); |
| 100 | return value; |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | //summodel = mvc.newStatefulModel({ data : sumData }); |
| 105 | summodel = getStateful(sumData ); |
| 106 | |
| 107 | //sumrepeatmodel = mvc.newStatefulModel({ data : sumRepeatData }); |
| 108 | sumrepeatmodel = getStateful(sumRepeatData); |
| 109 | |
| 110 | //model = mvc.newStatefulModel({ data : order }); |
| 111 | model = getStateful(orderData); |
| 112 | |
| 113 | function recomputeTotal() { |
| 114 | require([ |
| 115 | "dijit/registry", |
| 116 | "dojox/mvc/at" |
| 117 | ], function(registry, at){ |
| 118 | var total = 0; |
| 119 | var wone = parseInt(registry.byId("oneInput").get("value")) || 0; |
| 120 | var wtwo = parseInt(registry.byId("twoInput").get("value")) || 0; |
| 121 | var wthree = parseInt(registry.byId("threeInput").get("value")) || 0; |
| 122 | var wfour = parseInt(registry.byId("fourInput").get("value")) || 0; |
| 123 | total = wone + wtwo + wthree + wfour; |
| 124 | var wtotal = registry.byId("totalOutput"); |
| 125 | wtotal.set("value", total); |
| 126 | }); |
| 127 | } |
| 128 | |
| 129 | function recomputeRepeatTotal() { |
| 130 | require([ |
| 131 | "dijit/registry", |
| 132 | "dojox/mvc/at" |
| 133 | ], function(registry, at){ |
| 134 | var total = 0; |
| 135 | for(var i=0;i<sumrepeatmodel.list.length;i++){ |
| 136 | var item = parseInt(registry.byId("input"+i).get("value")) || 0; |
| 137 | console.log("sumrepeatmodel item = "+item); |
| 138 | total = total + item; |
| 139 | } |
| 140 | var wtotal = registry.byId("totalrepeatOutput"); |
| 141 | wtotal.set("value", total); |
| 142 | }); |
| 143 | } |
| 144 | |
| 145 | when(parser.parse(), function(){ |
| 146 | recomputeTotal(); |
| 147 | |
| 148 | doh.register("Synchronize text box and label", [{ |
| 149 | name: "Check first value", |
| 150 | runTest: function(){ |
| 151 | doh.is("", registry.byId("firstInput").get("value"), "First should reflect initial data"); |
| 152 | doh.is("last", registry.byId("lastnameInput").get("value"), "Last should reflect initial data"); |
| 153 | doh.is("email", registry.byId("emailInput").get("value"), "Email should reflect initial data"); |
| 154 | doh.t(/\s*\(Enter Age\)\s*/, registry.byId("ageOutput").innerHTML, "First should reflect initial data"); |
| 155 | doh.is("10", registry.byId("totalOutput").get("value"), "totalOutput should reflect initial data"); |
| 156 | doh.is("10", registry.byId("totalrepeatOutput").get("value"), "totalrepeatOutput should reflect initial data"); |
| 157 | } |
| 158 | }, { |
| 159 | name: "Change first and check age label", |
| 160 | runTest: function(){ |
| 161 | model.set("First", "John"); |
| 162 | doh.is("John", registry.byId("firstInput").get("value"), "First should reflect initial data"); |
| 163 | doh.t(/\s*\(Enter John's Age\)\s*/, registry.byId("ageOutput").innerHTML, "First should reflect initial data"); |
| 164 | } |
| 165 | }, { |
| 166 | name: "Change oneInput and item 0 and check totalOutput and totalrepeatOutput", |
| 167 | runTest: function(){ |
| 168 | summodel.set("one", 11); |
| 169 | doh.is("20", registry.byId("totalOutput").get("value"), "totalOutput should reflect updated value"); |
| 170 | sumrepeatmodel.list[0].set("item", 211); |
| 171 | doh.is("220", registry.byId("totalrepeatOutput").get("value"), "totalrepeatOutput should reflect updated value"); |
| 172 | } |
| 173 | }, { |
| 174 | name: "blank oneInput and item 0 and check totalOutput and totalrepeatOutput", |
| 175 | runTest: function(){ |
| 176 | summodel.set("one", ""); |
| 177 | doh.is("9", registry.byId("totalOutput").get("value"), "totalOutput should reflect updated value"); |
| 178 | doh.is("0", registry.byId("oneInput").get("value"), "oneInput should reflect updated value"); |
| 179 | sumrepeatmodel.list[0].set("item", ""); |
| 180 | doh.is("9", registry.byId("totalrepeatOutput").get("value"), "totalrepeatOutput should reflect updated value"); |
| 181 | doh.is("0", registry.byId("input0").get("value"), "input0 should reflect updated value"); |
| 182 | } |
| 183 | }, { |
| 184 | name: "Check model reset", |
| 185 | setUp: function(){ |
| 186 | //model.reset(); |
| 187 | //summodel.reset(); |
| 188 | //sumrepeatmodel.reset(); |
| 189 | summodel.set(sumData); |
| 190 | sumrepeatmodel.set(sumRepeatData); |
| 191 | model.set(orderData); |
| 192 | }, |
| 193 | runTest: function(){ |
| 194 | doh.is("", registry.byId("firstInput").get("value"), "First should reflect initial data"); |
| 195 | doh.is("last", registry.byId("lastnameInput").get("value"), "Last should reflect initial data"); |
| 196 | doh.is("email", registry.byId("emailInput").get("value"), "Email should reflect initial data"); |
| 197 | doh.t(/\s*\(Enter Age\)\s*/, registry.byId("ageOutput").innerHTML, "First should reflect initial data"); |
| 198 | doh.is("10", registry.byId("totalOutput").get("value"), "totalOutput should reflect initial data"); |
| 199 | doh.is("10", registry.byId("totalrepeatOutput").get("value"), "totalrepeatOutput should reflect initial data"); |
| 200 | } |
| 201 | |
| 202 | }]); |
| 203 | |
| 204 | doh.run(); |
| 205 | }); |
| 206 | }); |
| 207 | </script> |
| 208 | </head> |
| 209 | <body class="claro"> |
| 210 | <script type="dojo/require">at: "dojox/mvc/at"</script> |
| 211 | <div id="wrapper"> |
| 212 | <div id="header"> |
| 213 | <div id="navigation"> |
| 214 | </div> |
| 215 | <div id="headerInsert"> |
| 216 | <h2>MVC Test for dynamic label and calculating totals</h2> |
| 217 | </div> |
| 218 | </div> |
| 219 | <div id="main"> |
| 220 | <div id="leftNav"></div> |
| 221 | <div id="mainContent"> |
| 222 | <h4>Update Age label with first name when it is set.</h4> |
| 223 | <div data-dojo-type="dojox.mvc.Group" |
| 224 | data-dojo-props="target: model"> |
| 225 | <div class="row"> |
| 226 | <label class="cell" for="firstInput">Enter first name:</label> |
| 227 | <input class="cell" id="firstInput" data-dojo-type="dijit.form.TextBox" |
| 228 | data-dojo-props="value: at('rel:', 'First')"> |
| 229 | </div> |
| 230 | <div class="row"> |
| 231 | <label class="cell" for="lastnameInput">Enter last name:</label> |
| 232 | <input class="cell" id="lastnameInput" data-dojo-type="dijit.form.TextBox" |
| 233 | data-dojo-props="value: at('rel:', 'Last')"> |
| 234 | </div> |
| 235 | <div class="row"> |
| 236 | <label class="cell" for="emailInput">Enter email:</label> |
| 237 | <input class="cell" id="emailInput" data-dojo-type="dijit.form.TextBox" |
| 238 | data-dojo-props="value: at('rel:', 'Email')"> |
| 239 | </div> |
| 240 | <div class="row"> |
| 241 | <label data-dojo-type="dojox.mvc.Output" class="cell" for="ageInput" id="ageOutput" |
| 242 | data-dojo-props="value: at('rel:', 'First').direction(at.from).transform(ageLabelConverter)"></label> |
| 243 | <input class="cell" id="ageInput" data-dojo-type="dijit.form.TextBox" |
| 244 | data-dojo-props="value: at('rel:', 'Age')"> |
| 245 | </div> |
| 246 | </div> |
| 247 | <br/> |
| 248 | <h4>Update total when any of the fields are changed.</h4> |
| 249 | <div data-dojo-type="dojox.mvc.Group" |
| 250 | data-dojo-props="target: summodel"> |
| 251 | <div class="row"> |
| 252 | <label class="cell" for="oneInput">Item one:</label> |
| 253 | <input class="cell" id="oneInput" data-dojo-type="dijit.form.TextBox" |
| 254 | data-dojo-props="value: at('rel:', 'one').transform(sumValuesConverter)"> |
| 255 | </div> |
| 256 | <div class="row"> |
| 257 | <label class="cell" for="twoInput">Item two:</label> |
| 258 | <input class="cell" id="twoInput" data-dojo-type="dijit.form.TextBox" |
| 259 | data-dojo-props="value: at('rel:', 'two').transform(sumValuesConverter)"> |
| 260 | </div> |
| 261 | <div class="row"> |
| 262 | <label class="cell" for="threeInput">Item three:</label> |
| 263 | <input class="cell" id="threeInput" data-dojo-type="dijit.form.TextBox" |
| 264 | data-dojo-props="value: at('rel:', 'three').transform(sumValuesConverter)"> |
| 265 | </div> |
| 266 | <div class="row"> |
| 267 | <label class="cell" for="fourInput">Item four:</label> |
| 268 | <input class="cell" id="fourInput" data-dojo-type="dijit.form.TextBox" |
| 269 | data-dojo-props="value: at('rel:', 'four').transform(sumValuesConverter)"> |
| 270 | </div> |
| 271 | <div class="row"> |
| 272 | <label class="cell" for="totalOutput">Total:</label> |
| 273 | <div class="cell" id="totalOutput" data-dojo-type="dojox.mvc.Output" |
| 274 | data-dojo-props="value: at('rel:', 'total')"></div> |
| 275 | </div> |
| 276 | <br/> |
| 277 | <h4>Update total when any of the fields are changed - Using a Repeat.</h4> |
| 278 | <div id="outergroupId" data-dojo-type="dojox.mvc.Group" |
| 279 | data-dojo-props="target: sumrepeatmodel"> |
| 280 | <div id="repeatId" data-dojo-type="dojox.mvc.Repeat" |
| 281 | data-dojo-props="children: at('rel:', 'list')"> |
| 282 | <div data-dojo-type="dojox.mvc.Group" |
| 283 | data-dojo-props="target: at('rel:${this.index}')"> |
| 284 | <div class="row"> |
| 285 | <label class="cell" for="input${this.index}">Item ${this.index}:</label> |
| 286 | <input class="cell" id="input${this.index}" data-dojo-type="dijit.form.TextBox" |
| 287 | data-dojo-props="value: at('rel:', 'item').transform(sumRepeatValuesConverter)"> |
| 288 | </div> |
| 289 | </div> |
| 290 | </div> |
| 291 | <div class="row"> |
| 292 | <label class="cell" for="totalrepeatOutput">Total:</label> |
| 293 | <div class="cell" id="totalrepeatOutput" data-dojo-type="dojox.mvc.Output" |
| 294 | data-dojo-props="value: at('rel:', 'total')"></div> |
| 295 | </div> |
| 296 | </div> |
| 297 | <br/> |
| 298 | <br/> |
| 299 | Model: |
| 300 | <button id="reset" type="button" data-dojo-type="dijit.form.Button" |
| 301 | data-dojo-props="onClick: function(){model.set(orderData);summodel.set(sumData);sumrepeatmodel.set(sumRepeatData);}">Reset</button> |
| 302 | </div></div></div></div> |
| 303 | </body> |
| 304 | </html> |