1 | define([ |
---|
2 | "dojo/_base/array", // array.forEach |
---|
3 | "dojo/_base/declare", // declare |
---|
4 | "dojo/dom-attr", // domAttr.set |
---|
5 | "dojo/has", |
---|
6 | "dojo/i18n", // i18n.getLocalization |
---|
7 | "dojo/i18n!./nls/ComboBox" |
---|
8 | ], function(array, declare, domAttr, has, i18n){ |
---|
9 | |
---|
10 | // module: |
---|
11 | // dijit/form/_ComboBoxMenuMixin |
---|
12 | |
---|
13 | var ComboBoxMenuMixin = declare("dijit.form._ComboBoxMenuMixin" + (has("dojo-bidi") ? "_NoBidi" : ""), null, { |
---|
14 | // summary: |
---|
15 | // Focus-less menu for internal use in `dijit/form/ComboBox` |
---|
16 | // tags: |
---|
17 | // private |
---|
18 | |
---|
19 | // _messages: Object |
---|
20 | // Holds "next" and "previous" text for paging buttons on drop down |
---|
21 | _messages: null, |
---|
22 | |
---|
23 | postMixInProperties: function(){ |
---|
24 | this.inherited(arguments); |
---|
25 | this._messages = i18n.getLocalization("dijit.form", "ComboBox", this.lang); |
---|
26 | }, |
---|
27 | |
---|
28 | buildRendering: function(){ |
---|
29 | this.inherited(arguments); |
---|
30 | |
---|
31 | // fill in template with i18n messages |
---|
32 | this.previousButton.innerHTML = this._messages["previousMessage"]; |
---|
33 | this.nextButton.innerHTML = this._messages["nextMessage"]; |
---|
34 | }, |
---|
35 | |
---|
36 | _setValueAttr: function(/*Object*/ value){ |
---|
37 | this._set("value", value); |
---|
38 | this.onChange(value); |
---|
39 | }, |
---|
40 | |
---|
41 | onClick: function(/*DomNode*/ node){ |
---|
42 | if(node == this.previousButton){ |
---|
43 | this._setSelectedAttr(null); |
---|
44 | this.onPage(-1); |
---|
45 | }else if(node == this.nextButton){ |
---|
46 | this._setSelectedAttr(null); |
---|
47 | this.onPage(1); |
---|
48 | }else{ |
---|
49 | this.onChange(node); |
---|
50 | } |
---|
51 | }, |
---|
52 | |
---|
53 | // stubs |
---|
54 | onChange: function(/*Number*/ /*===== direction =====*/){ |
---|
55 | // summary: |
---|
56 | // Notifies ComboBox/FilteringSelect that user selected an option. |
---|
57 | // tags: |
---|
58 | // callback |
---|
59 | }, |
---|
60 | |
---|
61 | onPage: function(/*Number*/ /*===== direction =====*/){ |
---|
62 | // summary: |
---|
63 | // Notifies ComboBox/FilteringSelect that user clicked to advance to next/previous page. |
---|
64 | // tags: |
---|
65 | // callback |
---|
66 | }, |
---|
67 | |
---|
68 | onClose: function(){ |
---|
69 | // summary: |
---|
70 | // Callback from dijit.popup code to this widget, notifying it that it closed |
---|
71 | // tags: |
---|
72 | // private |
---|
73 | this._setSelectedAttr(null); |
---|
74 | }, |
---|
75 | |
---|
76 | _createOption: function(/*Object*/ item, labelFunc){ |
---|
77 | // summary: |
---|
78 | // Creates an option to appear on the popup menu subclassed by |
---|
79 | // `dijit/form/FilteringSelect`. |
---|
80 | |
---|
81 | var menuitem = this._createMenuItem(); |
---|
82 | var labelObject = labelFunc(item); |
---|
83 | if(labelObject.html){ |
---|
84 | menuitem.innerHTML = labelObject.label; |
---|
85 | }else{ |
---|
86 | menuitem.appendChild( |
---|
87 | menuitem.ownerDocument.createTextNode(labelObject.label) |
---|
88 | ); |
---|
89 | } |
---|
90 | // #3250: in blank options, assign a normal height |
---|
91 | if(menuitem.innerHTML == ""){ |
---|
92 | menuitem.innerHTML = " "; // |
---|
93 | } |
---|
94 | |
---|
95 | return menuitem; |
---|
96 | }, |
---|
97 | |
---|
98 | createOptions: function(results, options, labelFunc){ |
---|
99 | // summary: |
---|
100 | // Fills in the items in the drop down list |
---|
101 | // results: |
---|
102 | // Array of items |
---|
103 | // options: |
---|
104 | // The options to the query function of the store |
---|
105 | // |
---|
106 | // labelFunc: |
---|
107 | // Function to produce a label in the drop down list from a dojo.data item |
---|
108 | |
---|
109 | this.items = results; |
---|
110 | |
---|
111 | // display "Previous . . ." button |
---|
112 | this.previousButton.style.display = (options.start == 0) ? "none" : ""; |
---|
113 | domAttr.set(this.previousButton, "id", this.id + "_prev"); |
---|
114 | // create options using _createOption function defined by parent |
---|
115 | // ComboBox (or FilteringSelect) class |
---|
116 | // #2309: |
---|
117 | // iterate over cache nondestructively |
---|
118 | array.forEach(results, function(item, i){ |
---|
119 | var menuitem = this._createOption(item, labelFunc); |
---|
120 | menuitem.setAttribute("item", i); // index to this.items; use indirection to avoid mem leak |
---|
121 | domAttr.set(menuitem, "id", this.id + i); |
---|
122 | this.nextButton.parentNode.insertBefore(menuitem, this.nextButton); |
---|
123 | }, this); |
---|
124 | // display "Next . . ." button |
---|
125 | var displayMore = false; |
---|
126 | // Try to determine if we should show 'more'... |
---|
127 | if(results.total && !results.total.then && results.total != -1){ |
---|
128 | if((options.start + options.count) < results.total){ |
---|
129 | displayMore = true; |
---|
130 | }else if((options.start + options.count) > results.total && options.count == results.length){ |
---|
131 | // Weird return from a data store, where a start + count > maxOptions |
---|
132 | // implies maxOptions isn't really valid and we have to go into faking it. |
---|
133 | // And more or less assume more if count == results.length |
---|
134 | displayMore = true; |
---|
135 | } |
---|
136 | }else if(options.count == results.length){ |
---|
137 | //Don't know the size, so we do the best we can based off count alone. |
---|
138 | //So, if we have an exact match to count, assume more. |
---|
139 | displayMore = true; |
---|
140 | } |
---|
141 | |
---|
142 | this.nextButton.style.display = displayMore ? "" : "none"; |
---|
143 | domAttr.set(this.nextButton, "id", this.id + "_next"); |
---|
144 | }, |
---|
145 | |
---|
146 | clearResultList: function(){ |
---|
147 | // summary: |
---|
148 | // Clears the entries in the drop down list, but of course keeps the previous and next buttons. |
---|
149 | var container = this.containerNode; |
---|
150 | while(container.childNodes.length > 2){ |
---|
151 | container.removeChild(container.childNodes[container.childNodes.length - 2]); |
---|
152 | } |
---|
153 | this._setSelectedAttr(null); |
---|
154 | }, |
---|
155 | |
---|
156 | highlightFirstOption: function(){ |
---|
157 | // summary: |
---|
158 | // Highlight the first real item in the list (not Previous Choices). |
---|
159 | this.selectFirstNode(); |
---|
160 | }, |
---|
161 | |
---|
162 | highlightLastOption: function(){ |
---|
163 | // summary: |
---|
164 | // Highlight the last real item in the list (not More Choices). |
---|
165 | this.selectLastNode(); |
---|
166 | }, |
---|
167 | |
---|
168 | selectFirstNode: function(){ |
---|
169 | this.inherited(arguments); |
---|
170 | if(this.getHighlightedOption() == this.previousButton){ |
---|
171 | this.selectNextNode(); |
---|
172 | } |
---|
173 | }, |
---|
174 | |
---|
175 | selectLastNode: function(){ |
---|
176 | this.inherited(arguments); |
---|
177 | if(this.getHighlightedOption() == this.nextButton){ |
---|
178 | this.selectPreviousNode(); |
---|
179 | } |
---|
180 | }, |
---|
181 | |
---|
182 | getHighlightedOption: function(){ |
---|
183 | return this.selected; |
---|
184 | } |
---|
185 | }); |
---|
186 | |
---|
187 | if(has("dojo-bidi")){ |
---|
188 | ComboBoxMenuMixin = declare("dijit.form._ComboBoxMenuMixin", ComboBoxMenuMixin, { |
---|
189 | _createOption: function(){ |
---|
190 | var menuitem = this.inherited(arguments); |
---|
191 | |
---|
192 | // update menuitem.dir if BidiSupport was required |
---|
193 | this.applyTextDir(menuitem); |
---|
194 | |
---|
195 | return menuitem; |
---|
196 | } |
---|
197 | }); |
---|
198 | } |
---|
199 | |
---|
200 | return ComboBoxMenuMixin; |
---|
201 | }); |
---|