MultiSelect.js.uncompressed.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. define("dijit/form/MultiSelect", [
  2. "dojo/_base/array", // array.indexOf, array.map
  3. "dojo/_base/declare", // declare
  4. "dojo/dom-geometry", // domGeometry.setMarginBox
  5. "dojo/query", // query
  6. "./_FormValueWidget"
  7. ], function(array, declare, domGeometry, query, _FormValueWidget){
  8. // module:
  9. // dijit/form/MultiSelect
  10. return declare("dijit.form.MultiSelect", _FormValueWidget, {
  11. // summary:
  12. // Widget version of a `<select multiple=true>` element,
  13. // for selecting multiple options.
  14. // size: Number
  15. // Number of elements to display on a page
  16. // NOTE: may be removed in version 2.0, since elements may have variable height;
  17. // set the size via style="..." or CSS class names instead.
  18. size: 7,
  19. templateString: "<select multiple='true' ${!nameAttrSetting} data-dojo-attach-point='containerNode,focusNode' data-dojo-attach-event='onchange: _onChange'></select>",
  20. addSelected: function(/*dijit/form/MultiSelect*/ select){
  21. // summary:
  22. // Move the selected nodes of a passed Select widget
  23. // instance to this Select widget.
  24. //
  25. // example:
  26. // | // move all the selected values from "bar" to "foo"
  27. // | dijit.byId("foo").addSelected(dijit.byId("bar"));
  28. select.getSelected().forEach(function(n){
  29. if(this.restoreOriginalText){
  30. n.text = this.enforceTextDirWithUcc(this.restoreOriginalText(n), n.text);
  31. }
  32. this.containerNode.appendChild(n);
  33. // scroll to bottom to see item
  34. // cannot use scrollIntoView since <option> tags don't support all attributes
  35. // does not work on IE due to a bug where <select> always shows scrollTop = 0
  36. this.domNode.scrollTop = this.domNode.offsetHeight; // overshoot will be ignored
  37. // scrolling the source select is trickier esp. on safari who forgets to change the scrollbar size
  38. var oldscroll = select.domNode.scrollTop;
  39. select.domNode.scrollTop = 0;
  40. select.domNode.scrollTop = oldscroll;
  41. },this);
  42. this._set('value', this.get('value'));
  43. },
  44. getSelected: function(){
  45. // summary:
  46. // Access the NodeList of the selected options directly
  47. return query("option",this.containerNode).filter(function(n){
  48. return n.selected; // Boolean
  49. }); // dojo/NodeList
  50. },
  51. _getValueAttr: function(){
  52. // summary:
  53. // Hook so get('value') works.
  54. // description:
  55. // Returns an array of the selected options' values.
  56. // Don't call getSelect.map() because it doesn't return a real array,
  57. // and that messes up dojo.toJson() calls like in the Form.html test
  58. return array.map(this.getSelected(), function(n){
  59. return n.value;
  60. });
  61. },
  62. multiple: true, // for Form
  63. _setValueAttr: function(/*Array*/ values, /*Boolean?*/ priorityChange){
  64. // summary:
  65. // Hook so set('value', values) works.
  66. // description:
  67. // Set the value(s) of this Select based on passed values
  68. query("option",this.containerNode).forEach(function(n){
  69. n.selected = (array.indexOf(values,n.value) != -1);
  70. });
  71. this.inherited(arguments);
  72. },
  73. invertSelection: function(/*Boolean?*/ onChange){
  74. // summary:
  75. // Invert the selection
  76. // onChange: Boolean
  77. // If false, onChange is not fired.
  78. var val = [];
  79. query("option",this.containerNode).forEach(function(n){
  80. if(!n.selected){ val.push(n.value); }
  81. });
  82. this._setValueAttr(val, !(onChange === false || onChange == null));
  83. },
  84. _onChange: function(/*Event*/){
  85. this._handleOnChange(this.get('value'), true);
  86. },
  87. // for layout widgets:
  88. resize: function(/*Object*/ size){
  89. if(size){
  90. domGeometry.setMarginBox(this.domNode, size);
  91. }
  92. },
  93. postCreate: function(){
  94. this._set('value', this.get('value'));
  95. this.inherited(arguments);
  96. },
  97. _setTextDirAttr: function(textDir){
  98. // to insure the code executed only when _BidiSupport loaded, and only
  99. // when there was a change in textDir
  100. if((this.textDir != textDir || !this._created) && this.enforceTextDirWithUcc){
  101. this._set("textDir", textDir);
  102. query("option",this.containerNode).forEach(function(option){
  103. // If the value wasn't defined explicitly, it the same object as
  104. // option.text. Since the option.text will be modified (by wrapping of UCC)
  105. // we want to save the original option.value for form submission.
  106. if(!this._created && option.value === option.text){
  107. option.value = option.text;
  108. }
  109. // apply the bidi support
  110. option.text = this.enforceTextDirWithUcc(option, option.originalText || option.text);
  111. },this);
  112. }
  113. }
  114. });
  115. });