1 | define([ |
---|
2 | "dojo/_base/declare", // declare |
---|
3 | "dojo/dom", // dom.byId |
---|
4 | "dojo/_base/lang", // lang.trim |
---|
5 | "dojo/query", // query |
---|
6 | "dojo/store/Memory", |
---|
7 | "../registry" // registry.add registry.remove |
---|
8 | ], function(declare, dom, lang, query, MemoryStore, registry){ |
---|
9 | |
---|
10 | // module: |
---|
11 | // dijit/form/DataList |
---|
12 | |
---|
13 | function toItem(/*DOMNode*/ option){ |
---|
14 | // summary: |
---|
15 | // Convert `<option>` node to hash |
---|
16 | return { |
---|
17 | id: option.value, |
---|
18 | value: option.value, |
---|
19 | name: lang.trim(option.innerText || option.textContent || '') |
---|
20 | }; |
---|
21 | } |
---|
22 | |
---|
23 | return declare("dijit.form.DataList", MemoryStore, { |
---|
24 | // summary: |
---|
25 | // Inefficient but small data store specialized for inlined data via OPTION tags |
---|
26 | // |
---|
27 | // description: |
---|
28 | // Provides a store for inlined data like: |
---|
29 | // |
---|
30 | // | <datalist> |
---|
31 | // | <option value="AL">Alabama</option> |
---|
32 | // | ... |
---|
33 | |
---|
34 | constructor: function(params, srcNodeRef){ |
---|
35 | // summary: |
---|
36 | // Create the widget. |
---|
37 | // params: Object|null |
---|
38 | // Hash of initialization parameters for widget, including scalar values (like title, duration etc.) |
---|
39 | // and functions, typically callbacks like onClick. |
---|
40 | // The hash can contain any of the widget's properties, excluding read-only properties. |
---|
41 | // srcNodeRef: DOMNode|String |
---|
42 | // Attach widget to this DOM node. |
---|
43 | |
---|
44 | // store pointer to original DOM tree |
---|
45 | this.domNode = dom.byId(srcNodeRef); |
---|
46 | |
---|
47 | lang.mixin(this, params); |
---|
48 | if(this.id){ |
---|
49 | registry.add(this); // add to registry so it can be easily found by id |
---|
50 | } |
---|
51 | this.domNode.style.display = "none"; |
---|
52 | |
---|
53 | this.inherited(arguments, [{ |
---|
54 | data: query("option", this.domNode).map(toItem) |
---|
55 | }]); |
---|
56 | }, |
---|
57 | |
---|
58 | destroy: function(){ |
---|
59 | registry.remove(this.id); |
---|
60 | }, |
---|
61 | |
---|
62 | fetchSelectedItem: function(){ |
---|
63 | // summary: |
---|
64 | // Get the option marked as selected, like `<option selected>`. |
---|
65 | // Not part of dojo.data API. |
---|
66 | var option = query("> option[selected]", this.domNode)[0] || query("> option", this.domNode)[0]; |
---|
67 | return option && toItem(option); |
---|
68 | } |
---|
69 | }); |
---|
70 | }); |
---|