| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <meta http-equiv="Content-type" content="text/html; charset=utf-8"> |
| 5 | <title>Loan Form (Using DOJO within MVC Support)</title> |
| 6 | <style type="text/css"> |
| 7 | @import "css/app-format.css"; |
| 8 | @import "../../../dijit/themes/claro/claro.css"; |
| 9 | </style> |
| 10 | <script type="text/javascript" data-dojo-config="parseOnLoad:0,isDebug:1,async:1,mvc:{debugBindings:1}" src="../../../dojo/dojo.js"></script> |
| 11 | <script type="text/javascript"> |
| 12 | var lookup, loanModel, validHousingPercentConverter = { |
| 13 | format: function(value){ |
| 14 | return value <= 33; |
| 15 | } |
| 16 | }, nonZeroRelevanceConverter = { |
| 17 | format: function(value){ |
| 18 | return value <= 0; |
| 19 | } |
| 20 | }; |
| 21 | |
| 22 | require([ |
| 23 | "dojo/_base/array", |
| 24 | "dojo/_base/declare", |
| 25 | "dojo/_base/lang", |
| 26 | "dojo/request/xhr", |
| 27 | "dojo/parser", |
| 28 | "dojox/charting/Chart", |
| 29 | "dojox/charting/themes/PlotKit/blue", |
| 30 | "dojox/mvc/at", |
| 31 | "dojox/mvc/computed", |
| 32 | "dojo/Stateful", |
| 33 | "dojox/mvc/StatefulSeries", |
| 34 | "dijit/form/TextBox", |
| 35 | "dijit/form/Button", |
| 36 | "dijit/form/ValidationTextBox", |
| 37 | "dijit/form/NumberTextBox", |
| 38 | "dojox/charting/plot2d/Pie", |
| 39 | "dojox/mvc/_TextBoxExtensions", |
| 40 | "dojox/mvc/Group", |
| 41 | "dojox/mvc/ModelRefController", |
| 42 | "dojox/mvc/Output", |
| 43 | "dojo/domReady!" |
| 44 | ], function(array, declare, lang, dxhr, parser, Chart, blue, at, computed, Stateful, StatefulSeries){ |
| 45 | |
| 46 | lookup = function(model){ |
| 47 | if(isNaN(model.get("Zip"))){ return; } |
| 48 | dxhr.get("zips/" + model.get("Zip") + ".json", { |
| 49 | content: {postalcode: model.get("Zip"), country: model.get("County")}, |
| 50 | preventCache: true, |
| 51 | handleAs: "json" |
| 52 | }).then(function(data){ |
| 53 | model.set("City", data.postalcodes[0].placeName); |
| 54 | model.set("County", data.postalcodes[0].adminName2); |
| 55 | model.set("State", data.postalcodes[0].adminCode1); |
| 56 | model.set("isZipValid", true); |
| 57 | }, function(e){ |
| 58 | model.set("City", ""); |
| 59 | model.set("County", ""); |
| 60 | model.set("State", ""); |
| 61 | model.set("isZipValid", false); |
| 62 | }); |
| 63 | }; |
| 64 | |
| 65 | loanModel = new Stateful({ |
| 66 | Name: "John Doe", |
| 67 | Street: "", |
| 68 | City: "", |
| 69 | County: "", |
| 70 | State: "", |
| 71 | Zip: "", |
| 72 | Country: "US", |
| 73 | BaseIncome: 50000, |
| 74 | BonusIncome: 10000, |
| 75 | Mortgage: 1000, |
| 76 | Taxes: 500, |
| 77 | OtherHousing: 1200 |
| 78 | }); |
| 79 | |
| 80 | computed(loanModel, "TotalIncome", function(BaseIncome, BonusIncome){ |
| 81 | return BaseIncome + BonusIncome; |
| 82 | }, at(loanModel, "BaseIncome"), at(loanModel, "BonusIncome")); |
| 83 | |
| 84 | computed(loanModel, "TotalHousing", function(Mortgage, Taxes, OtherHousing){ |
| 85 | return Mortgage + Taxes + OtherHousing; |
| 86 | }, at(loanModel, "Mortgage"), at(loanModel, "Taxes"), at(loanModel, "OtherHousing")); |
| 87 | |
| 88 | computed(loanModel, "HousingPercent", function(TotalHousing, TotalIncome){ |
| 89 | return Math.round(TotalHousing / TotalIncome * 100); |
| 90 | }, at(loanModel, "TotalHousing"), at(loanModel, "TotalIncome")); |
| 91 | |
| 92 | computed(loanModel, "HousingPercentError", function(HousingPercent){ |
| 93 | return HousingPercent > 33 ? "Housing should be less than 1/3 total expenses!" : ""; |
| 94 | }, at(loanModel, "HousingPercent")); |
| 95 | |
| 96 | computed(loanModel, "HousingPercentDisabled", function(HousingPercent){ |
| 97 | return HousingPercent <= 0 ? "disabled" : null; |
| 98 | }, at(loanModel, "HousingPercent")); |
| 99 | |
| 100 | parser.parse(); |
| 101 | |
| 102 | new Chart("expenseChart") |
| 103 | .setTheme(blue) |
| 104 | .addPlot("default", {type: "Pie"}) |
| 105 | .addSeries("default", new StatefulSeries([at(loanModel, "Mortgage"), |
| 106 | at(loanModel, "Taxes"), at(loanModel, "OtherHousing")])).render(); |
| 107 | }); |
| 108 | </script> |
| 109 | </head> |
| 110 | <body class="claro" style="background-image: url(images/validating_form_pattern.png)"> |
| 111 | <script type="dojo/require">at: "dojox/mvc/at"</script> |
| 112 | <div id="wrapper"> |
| 113 | <div id="addressLookupController" class="dijitDisplayNone" |
| 114 | data-dojo-type="dojox.mvc.ModelRefController" |
| 115 | data-dojo-props="model: loanModel, ownProps: {_setZipAttr: 1, _setCountyAttr: 1}"> |
| 116 | <script type="dojo/method" event="_setZipAttr" args="value"> |
| 117 | var changed = this.model.Zip != value; |
| 118 | this.model._changeAttrValue("Zip", value); |
| 119 | if(changed){ |
| 120 | lookup(this.model); |
| 121 | } |
| 122 | return this; |
| 123 | </script> |
| 124 | <script type="dojo/method" event="_setCountyAttr" args="value"> |
| 125 | var changed = this.model.County != value; |
| 126 | this.model._changeAttrValue("County", value); |
| 127 | if(changed){ |
| 128 | lookup(this.model); |
| 129 | } |
| 130 | return this; |
| 131 | </script> |
| 132 | </div> |
| 133 | <div id="header"> |
| 134 | <div id="navigation"> |
| 135 | </div> |
| 136 | <div id="headerInsert"> |
| 137 | <h1>Big Red Loan</h1> |
| 138 | <h2>The one to choose when you're in the red...</h2> |
| 139 | </div> |
| 140 | </div> |
| 141 | <div id="main"> |
| 142 | <div id="leftNav"></div> |
| 143 | <div id="mainContent" data-dojo-type="dojox.mvc.Group" |
| 144 | data-dojo-props="target: at('widget:addressLookupController')"> |
| 145 | <h3>Borrower information</h3> |
| 146 | <hr size="6" class="rule"/> |
| 147 | <div class="spacer"></div> |
| 148 | <div class="row"> |
| 149 | <div class="cell"><label for="borrowerInput">Name:</label></div> |
| 150 | <div class="cell"> |
| 151 | <input id="borrowerInput" data-dojo-type="dijit.form.TextBox" |
| 152 | data-dojo-props="value: at('rel:', 'Name')"/> |
| 153 | </div> |
| 154 | </div> |
| 155 | <div class="row"> |
| 156 | <div class="cell"><label for="zipInput">Zipcode (try 10706):</label></div> |
| 157 | <div class="cell"> |
| 158 | <input id="zipInput" data-dojo-type="dijit.form.NumberTextBox" |
| 159 | data-dojo-props="value: at('rel:', 'Zip'), |
| 160 | valid: at('rel:', 'isZipValid')" |
| 161 | constraints="{pattern:'#'}" regExp="\d{5}" required="true" invalidMessage="Invalid Zip Code."/> |
| 162 | </div> |
| 163 | </div> |
| 164 | <div class="row"> |
| 165 | <div class="cell"><label for="cityInput">City:</label></div> |
| 166 | <div class="cell"> |
| 167 | <input id="cityInput" data-dojo-type="dijit.form.TextBox" |
| 168 | data-dojo-props="value: at('rel:', 'City')"/> |
| 169 | </div> |
| 170 | </div> |
| 171 | <div class="row"> |
| 172 | <div class="cell"><label for="countyInput">County:</label></div> |
| 173 | <div class="cell"> |
| 174 | <input id="countyInput" data-dojo-type="dijit.form.TextBox" |
| 175 | data-dojo-props="value: at('rel:', 'County')"/> |
| 176 | </div> |
| 177 | </div> |
| 178 | <div class="row"> |
| 179 | <div class="cell"><label for="stateInput">State:</label></div> |
| 180 | <div class="cell"> |
| 181 | <input id="stateInput" data-dojo-type="dijit.form.TextBox" |
| 182 | data-dojo-props="value: at('rel:', 'State')"/> |
| 183 | </div> |
| 184 | </div> |
| 185 | <div class="spacer"></div> |
| 186 | <h3>Expenses</h3> |
| 187 | <hr size="6" class="rule"/> |
| 188 | <div class="spacer"></div> |
| 189 | <table style="width:900px;"> |
| 190 | <tr style="width:900px;"> |
| 191 | <td style="width:400px;"> |
| 192 | <div class="row"> |
| 193 | <div class="cell"><label for="housing">Mortgage:</label></div> |
| 194 | <div class="cell"> |
| 195 | <input id="housing" data-dojo-type="dijit.form.NumberTextBox" |
| 196 | data-dojo-props="value: at('rel:', 'Mortgage')"/> |
| 197 | </div> |
| 198 | </div> |
| 199 | <div class="row"> |
| 200 | <div class="cell"> |
| 201 | <label>Real Estate Taxes:</label> |
| 202 | </div> |
| 203 | <div class="cell"> |
| 204 | <input id="taxes" data-dojo-type="dijit.form.NumberTextBox" |
| 205 | data-dojo-props="value: at('rel:', 'Taxes')"/> |
| 206 | </div> |
| 207 | </div> |
| 208 | <div class="row"> |
| 209 | <div class="cell"><label>Other Housing:</label></div> |
| 210 | <div class="cell"> |
| 211 | <input id="otherHousing" type="text" data-dojo-type="dijit.form.NumberTextBox" |
| 212 | data-dojo-props="value: at('rel:', 'OtherHousing')"/> |
| 213 | </div> |
| 214 | </div> |
| 215 | <div class="row"> |
| 216 | <div class="cell"><label for="transportation">Total Housing:</label></div> |
| 217 | <div class="cell"> |
| 218 | <input id="totalHousing" data-dojo-type="dijit.form.NumberTextBox" |
| 219 | data-dojo-props="value: at('rel:', 'TotalHousing'), |
| 220 | disabled: at('rel:', 'TotalHousing').direction(at.from).transform(nonZeroRelevanceConverter)"/> |
| 221 | </div> |
| 222 | </div> |
| 223 | </td> |
| 224 | <td style="width:200px;"> |
| 225 | <div id="expenseChart" style="display:block; width: 150px; height: 150px;"></div> |
| 226 | </td> |
| 227 | </tr> |
| 228 | </table> |
| 229 | <h3>Income</h3> |
| 230 | <hr size="6" class="rule"/> |
| 231 | <div class="spacer"></div> |
| 232 | <div class="row"> |
| 233 | <div class="cell"><label for="baseIncome">Base Income:</label></div> |
| 234 | <div class="cell"> |
| 235 | <input id="baseIncome" type="text" data-dojo-type="dijit.form.NumberTextBox" |
| 236 | data-dojo-props="value: at('rel:', 'BaseIncome')"/> |
| 237 | </div> |
| 238 | </div> |
| 239 | <div class="row"> |
| 240 | <div class="cell"><label>Bonus Income:</label></div> |
| 241 | <div class="cell"> |
| 242 | <input id="bonusIncome" type="text" data-dojo-type="dijit.form.NumberTextBox" |
| 243 | data-dojo-props="value: at('rel:', 'BonusIncome')"/> |
| 244 | </div> |
| 245 | </div> |
| 246 | <div class="row"> |
| 247 | <div class="cell"><label for="total">Total Income:</label></div> |
| 248 | <div class="cell"> |
| 249 | <input id="total" data-dojo-type="dijit.form.NumberTextBox" |
| 250 | data-dojo-props="value: at('rel:', 'TotalIncome')"/> |
| 251 | </div> |
| 252 | </div> |
| 253 | <h3>Analysis</h3> |
| 254 | <hr size="6" class="rule"/> |
| 255 | <div class="spacer"></div> |
| 256 | <div class="row"> |
| 257 | <div class="cell"><label for="percentHousing">Percent housing (under 33%):</label></div> |
| 258 | <div class="cell"> |
| 259 | <input id="percentHousing" data-dojo-type="dijit.form.NumberTextBox" |
| 260 | data-dojo-props="value: at('rel:', 'HousingPercent'), |
| 261 | valid: at('rel:', 'HousingPercent').direction(at.from).transform(validHousingPercentConverter), |
| 262 | disabled: at('rel:', 'HousingPercent').direction(at.from).transform(nonZeroRelevanceConverter)" |
| 263 | invalidMessage="Housing should be less than 1/3 total expenses!"/> |
| 264 | </div> |
| 265 | </div> |
| 266 | </div> |
| 267 | </div> |
| 268 | </div> |
| 269 | </body> |
| 270 | </html> |