Debian Codegraph @

dijit/form/MultiSelect.js

raw symbols SCIP
1 define([
2 "dojo/_base/array", // indexOf, map, forEach
3 "dojo/_base/declare", // declare
4 "dojo/dom-geometry", // domGeometry.setMarginBox
5 "dojo/sniff", // has("android")
6 "dojo/query", // query
7 "./_FormValueWidget",
8 "dojo/NodeList-dom" // orphan()
9 ], function(array, declare, domGeometry, has, query, _FormValueWidget){
10
11 // module:
12 // dijit/form/MultiSelect
13
14 var MultiSelect = declare("dijit.form.MultiSelect" + (has("dojo-bidi") ? "_NoBidi" : ""), _FormValueWidget, {
15 // summary:
16 // Widget version of a `<select multiple=multiple>` 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 baseClass: "dijitMultiSelect",
26
27 templateString: "<select multiple='multiple' ${!nameAttrSetting} data-dojo-attach-point='containerNode,focusNode' data-dojo-attach-event='onchange: _onChange'></select>",
28
29 addSelected: function(/*dijit/form/MultiSelect*/ select){
30 // summary:
31 // Move the selected nodes of a passed Select widget
32 // instance to this Select widget.
33 //
34 // example:
35 // | // move all the selected values from "bar" to "foo"
36 // | dijit.byId("foo").addSelected(dijit.byId("bar"));
37
38 select.getSelected().forEach(function(n){
39 this.containerNode.appendChild(n);
40 // scroll to bottom to see item
41 // cannot use scrollIntoView since <option> tags don't support all attributes
42 // does not work on IE due to a bug where <select> always shows scrollTop = 0
43 this.domNode.scrollTop = this.domNode.offsetHeight; // overshoot will be ignored
44 // scrolling the source select is trickier esp. on safari who forgets to change the scrollbar size
45 var oldscroll = select.domNode.scrollTop;
46 select.domNode.scrollTop = 0;
47 select.domNode.scrollTop = oldscroll;
48 }, this);
49 this._set('value', this.get('value'));
50 },
51
52 getSelected: function(){
53 // summary:
54 // Access the NodeList of the selected options directly
55 return query("option", this.containerNode).filter(function(n){
56 return n.selected; // Boolean
57 }); // dojo/NodeList
58 },
59
60 _getValueAttr: function(){
61 // summary:
62 // Hook so get('value') works.
63 // description:
64 // Returns an array of the selected options' values.
65
66 // Don't call getSelect.map() because it doesn't return a real array,
67 // and that messes up dojo.toJson() calls like in the Form.html test
68 return array.map(this.getSelected(), function(n){
69 return n.value;
70 });
71 },
72
73 // Set multiple so parent form widget knows that I return multiple values.
74 // Also adding a no-op custom setter; otherwise the multiple property is applied to the <select> node
75 // which causes problem on Android < 4.4 with all but the first selected item being deselected.
76 multiple: true,
77 _setMultipleAttr: function(val){
78 },
79
80 _setValueAttr: function(/*String[]*/ values){
81 // summary:
82 // Hook so set('value', values) works.
83 // description:
84 // Set the value(s) of this Select based on passed values
85
86 if(has("android")){
87 // Workaround bizarre Android bug where deselecting one option selects another one.
88 // See https://code.google.com/p/android/issues/detail?id=68285.
89 // Could use this code path for all browsers but I worry about IE memory leaks.
90 query("option", this.containerNode).orphan().forEach(function(n){
91 var option = n.ownerDocument.createElement("option");
92 option.value = n.value;
93 option.selected = (array.indexOf(values, n.value) != -1);
94 option.text = n.text;
95 option.originalText = n.originalText; // for bidi support, see has("dojo-bidi") block below
96 this.containerNode.appendChild(option);
97 }, this);
98 }else {
99 query("option", this.containerNode).forEach(function(n){
100 n.selected = (array.indexOf(values, n.value) != -1);
101 });
102 }
103
104 this.inherited(arguments);
105 },
106
107 invertSelection: function(/*Boolean?*/ onChange){
108 // summary:
109 // Invert the selection
110 // onChange: Boolean
111 // If false, onChange is not fired.
112 var val = [];
113 query("option", this.containerNode).forEach(function(n){
114 if(!n.selected){
115 val.push(n.value);
116 }
117 });
118 this._setValueAttr(val, !(onChange === false || onChange == null));
119 },
120
121 _onChange: function(/*Event*/){
122 this._handleOnChange(this.get('value'), true);
123 },
124
125 // for layout widgets:
126 resize: function(/*Object*/ size){
127 if(size){
128 domGeometry.setMarginBox(this.domNode, size);
129 }
130 },
131
132 postCreate: function(){
133 this._set('value', this.get('value'));
134 this.inherited(arguments);
135 }
136 });
137
138 if(has("dojo-bidi")){
139 MultiSelect = declare("dijit.form.MultiSelect", MultiSelect, {
140 addSelected: function(/*dijit/form/MultiSelect*/ select){
141 select.getSelected().forEach(function(n){
142 n.text = this.enforceTextDirWithUcc(this.restoreOriginalText(n), n.text);
143 }, this);
144 this.inherited(arguments);
145 },
146
147 _setTextDirAttr: function(textDir){
148 // to insure the code executed only when _BidiSupport loaded, and only
149 // when there was a change in textDir
150 if((this.textDir != textDir || !this._created) && this.enforceTextDirWithUcc){
151 this._set("textDir", textDir);
152
153 query("option", this.containerNode).forEach(function(option){
154 // If the value wasn't defined explicitly, it the same object as
155 // option.text. Since the option.text will be modified (by wrapping of UCC)
156 // we want to save the original option.value for form submission.
157 if(!this._created && option.value === option.text){
158 option.value = option.text;
159 }
160 // apply the bidi support
161 option.text = this.enforceTextDirWithUcc(option, option.originalText || option.text);
162 }, this);
163 }
164 }
165 });
166 }
167
168 return MultiSelect;
169 });