diff options
Diffstat (limited to 'js/components/combobox.reel/combobox.js')
-rw-r--r-- | js/components/combobox.reel/combobox.js | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/js/components/combobox.reel/combobox.js b/js/components/combobox.reel/combobox.js new file mode 100644 index 00000000..f262bb06 --- /dev/null +++ b/js/components/combobox.reel/combobox.js | |||
@@ -0,0 +1,140 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | var Montage = require("montage/core/core").Montage; | ||
8 | var Component = require("montage/ui/component").Component; | ||
9 | |||
10 | exports.Combobox = Montage.create(Component, { | ||
11 | |||
12 | _valueSyncedWithInputField: { | ||
13 | enumerable: false, | ||
14 | value: false | ||
15 | }, | ||
16 | |||
17 | _wasSetByCode: { | ||
18 | enumerable: false, | ||
19 | value: true | ||
20 | }, | ||
21 | |||
22 | labelField: { | ||
23 | value: null | ||
24 | }, | ||
25 | |||
26 | labelFunction: { | ||
27 | value: null | ||
28 | }, | ||
29 | |||
30 | _items: { | ||
31 | value: [] | ||
32 | }, | ||
33 | |||
34 | items: { | ||
35 | enumerable: true, | ||
36 | serializable: true, | ||
37 | get: function() { | ||
38 | return this._items; | ||
39 | }, | ||
40 | set: function(value) { | ||
41 | if (value !== this._items) | ||
42 | { | ||
43 | this._items = value; | ||
44 | this._valueSyncedWithInputField = false; | ||
45 | this.needsDraw = true; | ||
46 | } | ||
47 | } | ||
48 | }, | ||
49 | |||
50 | _value: { | ||
51 | enumerable: false, | ||
52 | value: null | ||
53 | }, | ||
54 | |||
55 | value: { | ||
56 | enumerable: true, | ||
57 | serializable: true, | ||
58 | get: function() { | ||
59 | return this._value; | ||
60 | }, | ||
61 | set: function(value) { | ||
62 | if (value !== this._value) | ||
63 | { | ||
64 | this._value = value; | ||
65 | this.needsDraw = true; | ||
66 | |||
67 | var e = document.createEvent("CustomEvent"); | ||
68 | e.initEvent("change", true, true); | ||
69 | e.type = "change"; | ||
70 | e._wasSetByCode = this._wasSetByCode; | ||
71 | e.value = this._value; | ||
72 | this.dispatchEvent(e); | ||
73 | |||
74 | this._wasSetByCode = false; | ||
75 | } | ||
76 | } | ||
77 | }, | ||
78 | |||
79 | handleChange: | ||
80 | { | ||
81 | value:function(event) | ||
82 | { | ||
83 | this._valueSyncedWithInputField = true; | ||
84 | this._wasSetByCode = false; | ||
85 | this.value = this.element.value; | ||
86 | this.needsDraw = true; | ||
87 | } | ||
88 | }, | ||
89 | |||
90 | willDraw: { | ||
91 | value: function() { | ||
92 | if(!this._valueSyncedWithInputField) | ||
93 | { | ||
94 | this.element.innerHTML = ""; | ||
95 | |||
96 | var optionItem = document.createElement("option"); | ||
97 | var items = this._items; | ||
98 | var len = items.length; | ||
99 | |||
100 | var i; | ||
101 | for (i = 0; i < len; i++) | ||
102 | { | ||
103 | var current = items[i]; | ||
104 | optionItem = document.createElement("option"); | ||
105 | optionItem.value = current; | ||
106 | if(this.labelFunction) | ||
107 | { | ||
108 | optionItem.innerText = this.labelFunction(current); | ||
109 | } | ||
110 | else if(this.labelField) | ||
111 | { | ||
112 | optionItem.innerText = current[this.labelField]; | ||
113 | } | ||
114 | else | ||
115 | { | ||
116 | optionItem.innerText = current; | ||
117 | } | ||
118 | this.element.appendChild(optionItem); | ||
119 | } | ||
120 | } | ||
121 | } | ||
122 | }, | ||
123 | |||
124 | draw: { | ||
125 | value: function() { | ||
126 | if(!this._valueSyncedWithInputField) | ||
127 | { | ||
128 | this.element.value = this._value; | ||
129 | } | ||
130 | this._valueSyncedWithInputField = false; | ||
131 | } | ||
132 | }, | ||
133 | |||
134 | prepareForDraw: { | ||
135 | value: function() { | ||
136 | this.element.addEventListener("change", this, false); | ||
137 | } | ||
138 | } | ||
139 | |||
140 | }); \ No newline at end of file | ||