]>
Commit | Line | Data |
---|---|---|
2f01fe57 | 1 | /* |
81bea17a | 2 | Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved. |
2f01fe57 AD |
3 | Available via Academic Free License >= 2.1 OR the modified BSD license. |
4 | see: http://dojotoolkit.org/license for details | |
5 | */ | |
6 | ||
7 | ||
81bea17a AD |
8 | if(!dojo._hasResource["dijit.form.MultiSelect"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. |
9 | dojo._hasResource["dijit.form.MultiSelect"] = true; | |
2f01fe57 AD |
10 | dojo.provide("dijit.form.MultiSelect"); |
11 | dojo.require("dijit.form._FormWidget"); | |
81bea17a AD |
12 | |
13 | ||
14 | dojo.declare("dijit.form.MultiSelect", dijit.form._FormValueWidget, { | |
15 | // summary: | |
16 | // Widget version of a <select multiple=true> element, | |
17 | // for selecting multiple options. | |
18 | ||
19 | // size: Number | |
20 | // Number of elements to display on a page | |
21 | // NOTE: may be removed in version 2.0, since elements may have variable height; | |
22 | // set the size via style="..." or CSS class names instead. | |
23 | size: 7, | |
24 | ||
25 | templateString: "<select multiple='true' ${!nameAttrSetting} dojoAttachPoint='containerNode,focusNode' dojoAttachEvent='onchange: _onChange'></select>", | |
26 | ||
27 | attributeMap: dojo.delegate(dijit.form._FormWidget.prototype.attributeMap, { | |
28 | size: "focusNode" | |
29 | }), | |
30 | ||
31 | reset: function(){ | |
32 | // summary: | |
33 | // Reset the widget's value to what it was at initialization time | |
34 | ||
35 | // TODO: once we inherit from FormValueWidget this won't be needed | |
36 | this._hasBeenBlurred = false; | |
37 | this._setValueAttr(this._resetValue, true); | |
38 | }, | |
39 | ||
40 | addSelected: function(/*dijit.form.MultiSelect*/ select){ | |
41 | // summary: | |
42 | // Move the selected nodes of a passed Select widget | |
43 | // instance to this Select widget. | |
44 | // | |
45 | // example: | |
46 | // | // move all the selected values from "bar" to "foo" | |
47 | // | dijit.byId("foo").addSelected(dijit.byId("bar")); | |
48 | ||
49 | select.getSelected().forEach(function(n){ | |
50 | this.containerNode.appendChild(n); | |
51 | // scroll to bottom to see item | |
52 | // cannot use scrollIntoView since <option> tags don't support all attributes | |
53 | // does not work on IE due to a bug where <select> always shows scrollTop = 0 | |
54 | this.domNode.scrollTop = this.domNode.offsetHeight; // overshoot will be ignored | |
55 | // scrolling the source select is trickier esp. on safari who forgets to change the scrollbar size | |
56 | var oldscroll = select.domNode.scrollTop; | |
57 | select.domNode.scrollTop = 0; | |
58 | select.domNode.scrollTop = oldscroll; | |
59 | },this); | |
60 | }, | |
61 | ||
62 | getSelected: function(){ | |
63 | // summary: | |
64 | // Access the NodeList of the selected options directly | |
65 | return dojo.query("option",this.containerNode).filter(function(n){ | |
66 | return n.selected; // Boolean | |
67 | }); // dojo.NodeList | |
68 | }, | |
69 | ||
70 | _getValueAttr: function(){ | |
71 | // summary: | |
72 | // Hook so get('value') works. | |
73 | // description: | |
74 | // Returns an array of the selected options' values. | |
75 | return this.getSelected().map(function(n){ | |
76 | return n.value; | |
77 | }); | |
78 | }, | |
79 | ||
80 | multiple: true, // for Form | |
81 | ||
82 | _setValueAttr: function(/*Array*/ values){ | |
83 | // summary: | |
84 | // Hook so set('value', values) works. | |
85 | // description: | |
86 | // Set the value(s) of this Select based on passed values | |
87 | dojo.query("option",this.containerNode).forEach(function(n){ | |
88 | n.selected = (dojo.indexOf(values,n.value) != -1); | |
89 | }); | |
90 | }, | |
91 | ||
92 | invertSelection: function(onChange){ | |
93 | // summary: | |
94 | // Invert the selection | |
95 | // onChange: Boolean | |
96 | // If null, onChange is not fired. | |
97 | dojo.query("option",this.containerNode).forEach(function(n){ | |
98 | n.selected = !n.selected; | |
99 | }); | |
100 | this._handleOnChange(this.get('value'), onChange == true); | |
101 | }, | |
102 | ||
103 | _onChange: function(/*Event*/ e){ | |
104 | this._handleOnChange(this.get('value'), true); | |
105 | }, | |
106 | ||
107 | // for layout widgets: | |
108 | resize: function(/*Object*/ size){ | |
109 | if(size){ | |
110 | dojo.marginBox(this.domNode, size); | |
111 | } | |
112 | }, | |
113 | ||
114 | postCreate: function(){ | |
115 | this._onChange(); | |
116 | } | |
2f01fe57 | 117 | }); |
81bea17a | 118 | |
2f01fe57 | 119 | } |