| 1 | <!DOCTYPE html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta http-equiv="Content-type" content="text/html; charset=utf-8"> |
| 5 | <title>Testing MultiSelect form widget | The Dojo Toolkit</title> |
| 6 | |
| 7 | <style type="text/css"> |
| 8 | #select, #select2 { |
| 9 | width:255px; |
| 10 | height:300px; |
| 11 | overflow:auto; |
| 12 | } |
| 13 | div#sel1, div#sel2 { |
| 14 | float: left; |
| 15 | } |
| 16 | div#leftRightButtons { |
| 17 | float: left; |
| 18 | padding: 10em 0.5em 0 0.5em; |
| 19 | } |
| 20 | </style> |
| 21 | |
| 22 | <script type="text/javascript" src="../boilerplate.js"></script> |
| 23 | |
| 24 | <script type="text/javascript"> |
| 25 | require([ |
| 26 | "doh/runner", |
| 27 | "dojo/query", |
| 28 | "dojo/dom-form", |
| 29 | "dojo/_base/json", |
| 30 | "dojo/_base/array", |
| 31 | "dojo/_base/lang", |
| 32 | "dojo/dom", |
| 33 | "dojo/dom-class", |
| 34 | "dojo/parser", |
| 35 | "dijit/dijit", |
| 36 | "dijit/focus", |
| 37 | "dijit/registry", |
| 38 | "dijit/form/MultiSelect", |
| 39 | "dijit/form/Button", |
| 40 | "dijit/form/Form", |
| 41 | "dijit/tests/helpers", |
| 42 | "dojo/domReady!" |
| 43 | ], function(doh, query, domForm, json, array, lang, dom, domClass, parser, |
| 44 | dijit, focus, registry, MultiSelect, Button, Form, helpers){ |
| 45 | |
| 46 | parser.parse(); |
| 47 | |
| 48 | // Add mode=test to the URL to run unit tests. |
| 49 | var test = /mode=test/i.test(window.location.href); |
| 50 | |
| 51 | // ref a clonable node, then split it between two selects |
| 52 | var c = query(".clone")[0]; |
| 53 | var l = -1; |
| 54 | opt = function(){ |
| 55 | return dom.byId((++l % 2 == 0 ? "select" : "select2" )); |
| 56 | }; |
| 57 | // based on the the 'dijit' object |
| 58 | var count=0; |
| 59 | var select1Values = "dojo._defaultEasing"; |
| 60 | for(var i in dijit){ |
| 61 | var n = opt().appendChild(lang.clone(c)); |
| 62 | n.value = count++; |
| 63 | if(l % 2 == 0){ |
| 64 | select1Values += "," + i; |
| 65 | } |
| 66 | n.innerHTML = i; |
| 67 | } |
| 68 | |
| 69 | // turn any non-data-dojo-type selects into widgets programatically: |
| 70 | query("select").forEach(function(n){ |
| 71 | if(!dijit.byNode(n)){ |
| 72 | var foo = new dijit.form.MultiSelect({ name: n.name }, n).startup(); |
| 73 | } |
| 74 | }); |
| 75 | |
| 76 | // listen to the "move items" buttons |
| 77 | query("button.switch") |
| 78 | .on("click",function(e){ |
| 79 | switch(e.target.id.toString()){ |
| 80 | case "left" : registry.byId("select").addSelected(registry.byId("select2")); break; |
| 81 | case "right" : registry.byId("select2").addSelected(registry.byId("select")); break; |
| 82 | } |
| 83 | }); |
| 84 | |
| 85 | // listen to the invert buttons |
| 86 | query("button.invert") |
| 87 | .on("click",function(e){ |
| 88 | switch(e.target.id.toString()){ |
| 89 | case "i1" : registry.byId("select").invertSelection(); break; |
| 90 | case "i2" : registry.byId("select2").invertSelection(); break; |
| 91 | case "i3" : registry.byId("select3").invertSelection(); break; |
| 92 | } |
| 93 | }); |
| 94 | |
| 95 | // there is only one debug button |
| 96 | query(".debug").on("click",function(e){ |
| 97 | console.log('select value:',registry.byId("select").get('value') + '/' + registry.byId("select").value); |
| 98 | console.log('select2 value:',registry.byId("select2").get('value') + '/' + registry.byId("select2").value); |
| 99 | console.log('select3 value:',registry.byId("select3").get('value') + '/' + registry.byId("select3").value); |
| 100 | }); |
| 101 | |
| 102 | query("#formSubmit").on("click", function(e){ |
| 103 | // see what the real form says about our widgets: |
| 104 | var vals = domForm.toJson("test"); |
| 105 | console.log(vals); |
| 106 | }); |
| 107 | |
| 108 | query("#s1").on("click", function(e){ |
| 109 | registry.byId('select3').set('value', ['VA', 'WA']); |
| 110 | }); |
| 111 | |
| 112 | if(test){ |
| 113 | doh.register("API", [ |
| 114 | function initial(t){ |
| 115 | var vals = json.fromJson(domForm.toJson("test")); |
| 116 | t.is(select1Values, query("option", registry.byId('select').containerNode).map(function(n){ return n.innerHTML; }).join(',')); |
| 117 | t.is("", registry.byId('select').get('value').join(''), 'easing widget value'); |
| 118 | t.is("", registry.byId('select2').get('value').join(''), 'second widget value'); |
| 119 | t.is("TN", registry.byId('select3').get('value').join(''), 'select3 widget value'); |
| 120 | t.is("", vals.easing, 'easing form value'); |
| 121 | t.is("", vals.second, 'second form value'); |
| 122 | t.is("TN", vals.select3, 'select3 form value'); |
| 123 | t.t(domClass.contains(registry.byId('select2').domNode, "dijitMultiSelect"), 'second base class'); |
| 124 | t.t(domClass.contains(registry.byId('select3').domNode, "dijitMultiSelect"), 'select3 base class'); |
| 125 | t.t(domClass.contains(registry.byId('select3').domNode, "addedClass"), 'select3 added class'); |
| 126 | }, |
| 127 | { |
| 128 | name: "set", |
| 129 | timeout: 1000, |
| 130 | runTest: function(t){ |
| 131 | var d = new doh.Deferred(); |
| 132 | registry.byId('select3').set('value', ['TN','FL']); |
| 133 | setTimeout(d.getTestCallback(function(){ |
| 134 | var vals = json.fromJson(domForm.toJson("test")); |
| 135 | t.is("TN,FL", registry.byId('select3').get('value').join(','), "select3 widget value"); |
| 136 | t.is("TN,FL", vals.select3, "select3 form value"); |
| 137 | }), 500); |
| 138 | return d; |
| 139 | } |
| 140 | } |
| 141 | ]); |
| 142 | |
| 143 | doh.run(); |
| 144 | } |
| 145 | }); |
| 146 | </script> |
| 147 | </head> |
| 148 | <body style="padding:20px" role="main"> |
| 149 | |
| 150 | <h1 class="testTitle">dijit.form.MultiSelect:</h1> |
| 151 | <p>Select one or more items in First or Second list and move them between lists using the buttons provided.</p> |
| 152 | <form action="#" method="get" id="test" onsubmit="return false"> |
| 153 | |
| 154 | <div> |
| 155 | <div id="sel1" role="presentation"> |
| 156 | <label for="select">First list:</label><br> |
| 157 | <select id="select" multiple size="7" name="easing" tabindex="1"> |
| 158 | <option class="clone" value="dojo._defaultEasing">dojo._defaultEasing</option> |
| 159 | </select> |
| 160 | </div> |
| 161 | <div id="leftRightButtons" role="presentation"> |
| 162 | <span> |
| 163 | <button class="switch" id="left" title="Move Items to First list"><</button> |
| 164 | <button class="switch" id="right" title="Move Items to Second list">></button> |
| 165 | </span> |
| 166 | </div> |
| 167 | <div id="sel2" role="presentation"> |
| 168 | <label for="select2">Second list:</label><br> |
| 169 | <select id="select2" multiple size="7" name="second"> |
| 170 | </select> |
| 171 | </div> |
| 172 | </div> |
| 173 | |
| 174 | <br style="clear: both;"/><br> |
| 175 | |
| 176 | <button class='invert' id="i1">invert first list</button> |
| 177 | <button class="invert" id="i2">invert second list</button> |
| 178 | <button id="formSubmit" type="submit">Submit</button> |
| 179 | |
| 180 | |
| 181 | <button class="debug">call get('value')</button> |
| 182 | |
| 183 | <h3><label for="select3">markup:</label></h3> |
| 184 | |
| 185 | <select class="addedClass" id="select3" multiple data-dojo-type="dijit/form/MultiSelect" |
| 186 | data-dojo-props='name:"select3",style:{height:"200px", width:"175px", border:"5px solid #ededed"}'> |
| 187 | |
| 188 | <option value="TN" selected>Tennessee</option> |
| 189 | <option value="VA">Virginia</option> |
| 190 | <option value="WA">Washington</option> |
| 191 | <option value="FL">Florida</option> |
| 192 | <option value="CA">California</option> |
| 193 | |
| 194 | </select> |
| 195 | </form> |
| 196 | <br><br> |
| 197 | <button class='invert' id="i3">invert markup list</button> |
| 198 | <button class='set' id="s1">set markup list to [VA, WA]</button> |
| 199 | </body> |
| 200 | </html> |