CheckedMultiSelect.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. require({cache:{
  2. 'url:dojox/form/resources/_CheckedMultiSelectMenuItem.html':"<tr class=\"dijitReset dijitMenuItem\" dojoAttachPoint=\"focusNode\" role=\"menuitemcheckbox\" tabIndex=\"-1\"\n\tdojoAttachEvent=\"onmouseenter:_onHover,onmouseleave:_onUnhover,ondijitclick:_onClick\"\n\t><td class=\"dijitReset dijitMenuItemIconCell\" role=\"presentation\"\n\t\t><div src=\"${_blankGif}\" alt=\"\" class=\"dijitMenuItemIcon ${_iconClass}\" dojoAttachPoint=\"iconNode\"\n\t\t\t><input class=\"dojoxCheckedMultiSelectCheckBoxInput\" dojoAttachPoint=\"inputNode\" type=\"${_type.type}\"\n\t\t/></div></td\n\t><td class=\"dijitReset dijitMenuItemLabel\" colspan=\"2\" dojoAttachPoint=\"containerNode,labelNode\"></td\n\t><td class=\"dijitReset dijitMenuItemAccelKey\" style=\"display: none\" dojoAttachPoint=\"accelKeyNode\"></td\n\t><td class=\"dijitReset dijitMenuArrowCell\" role=\"presentation\">&nbsp;</td\n></tr>",
  3. 'url:dojox/form/resources/_CheckedMultiSelectItem.html':"<div class=\"dijitReset ${baseClass}\"\n\t><input class=\"${baseClass}Box\" data-dojo-type=\"dijit.form.CheckBox\" data-dojo-attach-point=\"checkBox\" \n\t\tdata-dojo-attach-event=\"_onClick:_changeBox\" type=\"${_type.type}\" data-dojo-props='disabled:${disabled}, readOnly:${readOnly}' baseClass=\"${_type.baseClass}\"\n\t/><div class=\"dijitInline ${baseClass}Label\" data-dojo-attach-point=\"labelNode\" data-dojo-attach-event=\"onclick:_onClick\"></div\n></div>\n",
  4. 'url:dojox/form/resources/CheckedMultiSelect.html':"<div class=\"dijit dijitReset dijitInline dijitLeft\" id=\"widget_${id}\"\n\t><div data-dojo-attach-point=\"comboButtonNode\"\n\t></div\n\t><div data-dojo-attach-point=\"selectNode\" class=\"dijit dijitReset dijitInline ${baseClass}Wrapper\" data-dojo-attach-event=\"onmousedown:_onMouseDown,onclick:focus\"\n\t\t><select class=\"${baseClass}Select dojoxCheckedMultiSelectHidden\" multiple=\"true\" data-dojo-attach-point=\"containerNode,focusNode\"></select\n\t\t><div data-dojo-attach-point=\"wrapperDiv\"></div\n\t></div\n></div>"}});
  5. define("dojox/form/CheckedMultiSelect", [
  6. "dojo/_base/declare",
  7. "dojo/_base/lang",
  8. "dojo/_base/array",
  9. "dojo/_base/event",
  10. "dojo/dom-geometry",
  11. "dojo/dom-class",
  12. "dojo/dom-construct",
  13. "dojo/i18n",
  14. "dijit/_Widget",
  15. "dijit/_TemplatedMixin",
  16. "dijit/_WidgetsInTemplateMixin",
  17. "dijit/registry",
  18. "dijit/Menu",
  19. "dijit/MenuItem",
  20. "dijit/Tooltip",
  21. "dijit/form/_FormSelectWidget",
  22. "dijit/form/ComboButton",
  23. "dojo/text!dojox/form/resources/_CheckedMultiSelectMenuItem.html",
  24. "dojo/text!dojox/form/resources/_CheckedMultiSelectItem.html",
  25. "dojo/text!dojox/form/resources/CheckedMultiSelect.html",
  26. "dojo/i18n!dojox/form/nls/CheckedMultiSelect",
  27. "dijit/form/CheckBox" // template
  28. ], function(declare, lang, array, event, domGeometry, domClass, domConstruct, i18n, Widget, TemplatedMixin, WidgetsInTemplateMixin, registry, Menu, MenuItem, Tooltip, FormSelectWidget, ComboButton, CheckedMultiSelectMenuItem, CheckedMultiSelectItem, CheckedMultiSelect, nlsCheckedMultiSelect){
  29. // module:
  30. // dojox/form/CheckedMultiSelect
  31. // summary:
  32. // Extends the core dojox.form.CheckedMultiSelect to provide a "checkbox" selector
  33. //
  34. /*=====
  35. Widget = dijit._Widget;
  36. TemplatedMixin = dijit._TemplatedMixin;
  37. WidgetsInTemplateMixin = dijit._WidgetsInTemplateMixin;
  38. Menu = dijit.Menu;
  39. MenuItem = dijit.MenuItem;
  40. FormSelectWidget = dijit.form._FormSelectWidget;
  41. =====*/
  42. var formCheckedMultiSelectItem = declare("dojox.form._CheckedMultiSelectItem", [Widget, TemplatedMixin, WidgetsInTemplateMixin], {
  43. // summary:
  44. // The individual items for a CheckedMultiSelect
  45. templateString: CheckedMultiSelectItem,
  46. baseClass: "dojoxMultiSelectItem",
  47. // option: dojox.form.__SelectOption
  48. // The option that is associated with this item
  49. option: null,
  50. parent: null,
  51. // disabled: boolean
  52. // Whether or not this widget is disabled
  53. disabled: false,
  54. // readOnly: boolean
  55. // Whether or not this widget is readOnly
  56. readOnly: false,
  57. postMixInProperties: function(){
  58. // summary:
  59. // Set the appropriate _subClass value - based on if we are multi-
  60. // or single-select
  61. this._type = this.parent.multiple ?
  62. {type: "checkbox", baseClass: "dijitCheckBox"} :
  63. {type: "radio", baseClass: "dijitRadio"};
  64. // use global disabled/readOnly if set to true, otherwise use per-option setting
  65. if(!this.disabled){
  66. this.disabled = this.option.disabled = this.option.disabled||false;
  67. }
  68. if(!this.readOnly){
  69. this.readOnly = this.option.readOnly = this.option.readOnly||false;
  70. }
  71. this.inherited(arguments);
  72. },
  73. postCreate: function(){
  74. // summary:
  75. // Set innerHTML here - since the template gets messed up sometimes
  76. // with rich text
  77. this.inherited(arguments);
  78. this.labelNode.innerHTML = this.option.label;
  79. },
  80. _changeBox: function(){
  81. // summary:
  82. // Called to force the select to match the state of the check box
  83. // (only on click of the checkbox) Radio-based calls _setValueAttr
  84. // instead.
  85. if(this.get("disabled") || this.get("readOnly")){ return; }
  86. if(this.parent.multiple){
  87. this.option.selected = this.checkBox.get('value') && true;
  88. }else{
  89. this.parent.set('value', this.option.value);
  90. }
  91. // fire the parent's change
  92. this.parent._updateSelection();
  93. // refocus the parent
  94. this.parent.focus();
  95. },
  96. _onClick: function(e){
  97. // summary:
  98. // Sets the click state (passes through to the check box)
  99. if(this.get("disabled") || this.get("readOnly")){
  100. event.stop(e);
  101. }else{
  102. this.checkBox._onClick(e);
  103. }
  104. },
  105. _updateBox: function(){
  106. // summary:
  107. // Called to force the box to match the state of the select
  108. this.checkBox.set('value', this.option.selected);
  109. },
  110. _setDisabledAttr: function(value){
  111. // summary:
  112. // Disables (or enables) all the children as well
  113. this.disabled = value||this.option.disabled;
  114. this.checkBox.set("disabled", this.disabled);
  115. domClass.toggle(this.domNode, "dojoxMultiSelectDisabled", this.disabled);
  116. },
  117. _setReadOnlyAttr: function(value){
  118. // summary:
  119. // Sets read only (or unsets) all the children as well
  120. this.checkBox.set("readOnly", value);
  121. this.readOnly = value;
  122. }
  123. });
  124. var formCheckedMultiSelectMenu = declare("dojox.form._CheckedMultiSelectMenu", Menu, {
  125. // summary:
  126. // An internally-used menu for dropdown that allows us a vertical scrollbar
  127. multiple: false,
  128. // summary:
  129. // An internally-used menu for dropdown that allows us a vertical scrollbar
  130. buildRendering: function(){
  131. // summary:
  132. // Stub in our own changes, so that our domNode is not a table
  133. // otherwise, we won't respond correctly to heights/overflows
  134. this.inherited(arguments);
  135. var o = (this.menuTableNode = this.domNode),
  136. n = (this.domNode = domConstruct.create("div", {style: {overflowX: "hidden", overflowY: "scroll"}}));
  137. if(o.parentNode){
  138. o.parentNode.replaceChild(n, o);
  139. }
  140. domClass.remove(o, "dijitMenuTable");
  141. n.className = o.className + " dojoxCheckedMultiSelectMenu";
  142. o.className = "dijitReset dijitMenuTable";
  143. o.setAttribute("role", "listbox");
  144. n.setAttribute("role", "presentation");
  145. n.appendChild(o);
  146. },
  147. resize: function(/*Object*/ mb){
  148. // summary:
  149. // Overridden so that we are able to handle resizing our
  150. // internal widget. Note that this is not a "full" resize
  151. // implementation - it only works correctly if you pass it a
  152. // marginBox.
  153. //
  154. // mb: Object
  155. // The margin box to set this dropdown to.
  156. if(mb){
  157. domGeometry.setMarginBox(this.domNode, mb);
  158. if("w" in mb){
  159. // We've explicitly set the wrapper <div>'s width, so set <table> width to match.
  160. // 100% is safer than a pixel value because there may be a scroll bar with
  161. // browser/OS specific width.
  162. this.menuTableNode.style.width = "100%";
  163. }
  164. }
  165. },
  166. onClose: function(){
  167. this.inherited(arguments);
  168. if(this.menuTableNode){
  169. // Erase possible width: 100% setting from _SelectMenu.resize().
  170. // Leaving it would interfere with the next openDropDown() call, which
  171. // queries the natural size of the drop down.
  172. this.menuTableNode.style.width = "";
  173. }
  174. },
  175. onItemClick: function(/*dijit._Widget*/ item, /*Event*/ evt){
  176. // summary:
  177. // Handle clicks on an item.
  178. // tags:
  179. // private
  180. // this can't be done in _onFocus since the _onFocus events occurs asynchronously
  181. if(typeof this.isShowingNow == 'undefined'){ // non-popup menu
  182. this._markActive();
  183. }
  184. this.focusChild(item);
  185. if(item.disabled || item.readOnly){ return false; }
  186. if(!this.multiple){
  187. // before calling user defined handler, close hierarchy of menus
  188. // and restore focus to place it was when menu was opened
  189. this.onExecute();
  190. }
  191. // user defined handler for click
  192. item.onClick(evt);
  193. }
  194. });
  195. var formCheckedMultiSelectMenuItem = declare("dojox.form._CheckedMultiSelectMenuItem", MenuItem, {
  196. // summary:
  197. // A checkbox-like menu item for toggling on and off
  198. templateString: CheckedMultiSelectMenuItem,
  199. // option: dojox.form.__SelectOption
  200. // The option that is associated with this item
  201. option: null,
  202. // reference of dojox.form._CheckedMultiSelectMenu
  203. parent: null,
  204. // icon of the checkbox/radio button
  205. iconClass: "",
  206. postMixInProperties: function(){
  207. // summary:
  208. // Set the appropriate _subClass value - based on if we are multi-
  209. // or single-select
  210. if(this.parent.multiple){
  211. this._iconClass = "dojoxCheckedMultiSelectMenuCheckBoxItemIcon";
  212. this._type = {type: "checkbox"};
  213. }else{
  214. this._iconClass = "";
  215. this._type = {type: "hidden"};
  216. }
  217. this.disabled = this.option.disabled;
  218. this.checked = this.option.selected;
  219. this.label = this.option.label;
  220. this.readOnly = this.option.readOnly;
  221. this.inherited(arguments);
  222. },
  223. onChange: function(/*Boolean*/ checked){
  224. // summary:
  225. // User defined function to handle check/uncheck events
  226. // tags:
  227. // callback
  228. },
  229. _updateBox: function(){
  230. // summary:
  231. // Called to force the box to match the state of the select
  232. domClass.toggle(this.domNode, "dojoxCheckedMultiSelectMenuItemChecked", !!this.option.selected);
  233. this.domNode.setAttribute("aria-checked", this.option.selected);
  234. this.inputNode.checked = this.option.selected;
  235. if(!this.parent.multiple){
  236. domClass.toggle(this.domNode, "dijitSelectSelectedOption", !!this.option.selected);
  237. }
  238. },
  239. _onClick: function(/*Event*/ e){
  240. // summary:
  241. // Clicking this item just toggles its state
  242. // tags:
  243. // private
  244. if(!this.disabled && !this.readOnly){
  245. if(this.parent.multiple){
  246. this.option.selected = !this.option.selected;
  247. this.parent.onChange();
  248. this.onChange(this.option.selected);
  249. }else{
  250. if(!this.option.selected){
  251. array.forEach(this.parent.getChildren(), function(item){
  252. item.option.selected = false;
  253. });
  254. this.option.selected = true;
  255. this.parent.onChange();
  256. this.onChange(this.option.selected);
  257. }
  258. }
  259. }
  260. this.inherited(arguments);
  261. }
  262. });
  263. var formCheckedMultiSelect = declare("dojox.form.CheckedMultiSelect", FormSelectWidget, {
  264. // summary:
  265. // Extends the core dijit MultiSelect to provide a "checkbox" selector
  266. templateString: CheckedMultiSelect,
  267. baseClass: "dojoxCheckedMultiSelect",
  268. // required: Boolean
  269. // User is required to check at least one item.
  270. required: false,
  271. // invalidMessage: String
  272. // The message to display if value is invalid.
  273. invalidMessage: "$_unset_$",
  274. // _message: String
  275. // Currently displayed message
  276. _message: "",
  277. // dropDown: Boolean
  278. // Drop down version or not
  279. dropDown: false,
  280. // labelText: String
  281. // Label of the drop down button
  282. labelText: "",
  283. // tooltipPosition: String[]
  284. // See description of `Tooltip.defaultPosition` for details on this parameter.
  285. tooltipPosition: [],
  286. setStore: function(store, selectedValue, fetchArgs){
  287. // summary:
  288. // If there is any items selected in the store, the value
  289. // of the widget will be set to the values of these items.
  290. this.inherited(arguments);
  291. var setSelectedItems = function(items){
  292. var value = array.map(items, function(item){ return item.value[0]; });
  293. if(value.length){
  294. this.set("value", value);
  295. }
  296. };
  297. this.store.fetch({query:{selected: true}, onComplete: setSelectedItems, scope: this});
  298. },
  299. postMixInProperties: function(){
  300. this.inherited(arguments);
  301. this._nlsResources = i18n.getLocalization("dojox.form", "CheckedMultiSelect", this.lang);
  302. if(this.invalidMessage == "$_unset_$"){ this.invalidMessage = this._nlsResources.invalidMessage; }
  303. },
  304. _fillContent: function(){
  305. // summary:
  306. // Set the value to be the first, or the selected index
  307. this.inherited(arguments);
  308. // set value from selected option
  309. if(this.options.length && !this.value && this.srcNodeRef){
  310. var si = this.srcNodeRef.selectedIndex || 0; // || 0 needed for when srcNodeRef is not a SELECT
  311. this.value = this.options[si >= 0 ? si : 0].value;
  312. }
  313. if(this.dropDown){
  314. domClass.toggle(this.selectNode, "dojoxCheckedMultiSelectHidden");
  315. this.dropDownMenu = new formCheckedMultiSelectMenu({
  316. id: this.id + "_menu",
  317. style: "display: none;",
  318. multiple: this.multiple,
  319. onChange: lang.hitch(this, "_updateSelection")
  320. });
  321. }
  322. },
  323. startup: function(){
  324. // summary:
  325. // Set the value to be the first, or the selected index
  326. this.inherited(arguments);
  327. if(this.dropDown){
  328. this.dropDownButton = new ComboButton({
  329. label: this.labelText,
  330. dropDown: this.dropDownMenu,
  331. baseClass: "dojoxCheckedMultiSelectButton",
  332. maxHeight: this.maxHeight
  333. }, this.comboButtonNode);
  334. }
  335. },
  336. _onMouseDown: function(e){
  337. // summary:
  338. // Cancels the mousedown event to prevent others from stealing
  339. // focus
  340. event.stop(e);
  341. },
  342. validator: function(){
  343. // summary:
  344. // Overridable function used to validate that an item is selected if required =
  345. // true.
  346. // tags:
  347. // protected
  348. if(!this.required){ return true; }
  349. return array.some(this.getOptions(), function(opt){
  350. return opt.selected && opt.value != null && opt.value.toString().length != 0;
  351. });
  352. },
  353. validate: function(isFocused){
  354. Tooltip.hide(this.domNode);
  355. var isValid = this.isValid(isFocused);
  356. if(!isValid){ this.displayMessage(this.invalidMessage); }
  357. return isValid;
  358. },
  359. isValid: function(/*Boolean*/ isFocused){
  360. // summary:
  361. // Tests if the required items are selected.
  362. // Can override with your own routine in a subclass.
  363. // tags:
  364. // protected
  365. return this.validator();
  366. },
  367. getErrorMessage: function(/*Boolean*/ isFocused){
  368. // summary:
  369. // Return an error message to show if appropriate
  370. // tags:
  371. // protected
  372. return this.invalidMessage;
  373. },
  374. displayMessage: function(/*String*/ message){
  375. // summary:
  376. // Overridable method to display validation errors/hints.
  377. // By default uses a tooltip.
  378. // tags:
  379. // extension
  380. Tooltip.hide(this.domNode);
  381. if(message){
  382. Tooltip.show(message, this.domNode, this.tooltipPosition);
  383. }
  384. },
  385. onAfterAddOptionItem: function(item, option){
  386. // summary:
  387. // a function that can be connected to in order to receive a
  388. // notification that an item as been added to this dijit.
  389. },
  390. _addOptionItem: function(/* dojox.form.__SelectOption */ option){
  391. var item;
  392. if(this.dropDown){
  393. item = new formCheckedMultiSelectMenuItem({
  394. option: option,
  395. parent: this.dropDownMenu
  396. });
  397. this.dropDownMenu.addChild(item);
  398. }else{
  399. item = new formCheckedMultiSelectItem({
  400. option: option,
  401. parent: this,
  402. disabled: this.disabled,
  403. readOnly: this.readOnly
  404. });
  405. this.wrapperDiv.appendChild(item.domNode);
  406. }
  407. this.onAfterAddOptionItem(item, option);
  408. },
  409. _refreshState: function(){
  410. // summary:
  411. // Validate if selection changes.
  412. this.validate(this.focused);
  413. },
  414. onChange: function(newValue){
  415. // summary:
  416. // Validate if selection changes.
  417. this._refreshState();
  418. },
  419. reset: function(){
  420. // summary: Overridden so that the state will be cleared.
  421. this.inherited(arguments);
  422. Tooltip.hide(this.domNode);
  423. },
  424. _updateSelection: function(){
  425. this.inherited(arguments);
  426. this._handleOnChange(this.value);
  427. array.forEach(this._getChildren(), function(item){
  428. item._updateBox();
  429. });
  430. if(this.dropDown && this.dropDownButton){
  431. var i = 0, label = "";
  432. array.forEach(this.options, function(option){
  433. if(option.selected){
  434. i++;
  435. label = option.label;
  436. }
  437. });
  438. this.dropDownButton.set("label", this.multiple ?
  439. lang.replace(this._nlsResources.multiSelectLabelText, {num: i}) :
  440. label);
  441. }
  442. },
  443. _getChildren: function(){
  444. if(this.dropDown){
  445. return this.dropDownMenu.getChildren();
  446. }else{
  447. return array.map(this.wrapperDiv.childNodes, function(n){
  448. return registry.byNode(n);
  449. });
  450. }
  451. },
  452. invertSelection: function(onChange){
  453. // summary: Invert the selection
  454. // onChange: Boolean
  455. // If null, onChange is not fired.
  456. if(this.multiple){
  457. array.forEach(this.options, function(i){
  458. i.selected = !i.selected;
  459. });
  460. this._updateSelection();
  461. }
  462. },
  463. _setDisabledAttr: function(value){
  464. // summary:
  465. // Disable (or enable) all the children as well
  466. this.inherited(arguments);
  467. if(this.dropDown){
  468. this.dropDownButton.set("disabled", value);
  469. }
  470. array.forEach(this._getChildren(), function(node){
  471. if(node && node.set){
  472. node.set("disabled", value);
  473. }
  474. });
  475. },
  476. _setReadOnlyAttr: function(value){
  477. // summary:
  478. // Sets read only (or unsets) all the children as well
  479. this.inherited(arguments);
  480. if("readOnly" in this.attributeMap){
  481. this._attrToDom("readOnly", value);
  482. }
  483. this.readOnly = value;
  484. array.forEach(this._getChildren(), function(node){
  485. if(node && node.set){
  486. node.set("readOnly", value);
  487. }
  488. });
  489. },
  490. uninitialize: function(){
  491. Tooltip.hide(this.domNode);
  492. // Make sure these children are destroyed
  493. array.forEach(this._getChildren(), function(child){
  494. child.destroyRecursive();
  495. });
  496. this.inherited(arguments);
  497. }
  498. });
  499. return formCheckedMultiSelect;
  500. });