diff options
author | Pierre Frisch | 2011-12-22 07:25:50 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-01-27 11:18:17 -0800 |
commit | b89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch) | |
tree | 0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /node_modules/montage/data/selector.js | |
parent | 2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff) | |
download | ninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz |
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'node_modules/montage/data/selector.js')
-rwxr-xr-x | node_modules/montage/data/selector.js | 2220 |
1 files changed, 2220 insertions, 0 deletions
diff --git a/node_modules/montage/data/selector.js b/node_modules/montage/data/selector.js new file mode 100755 index 00000000..063342c9 --- /dev/null +++ b/node_modules/montage/data/selector.js | |||
@@ -0,0 +1,2220 @@ | |||
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 | @module montage/data/selector | ||
8 | @requires montage/core/core | ||
9 | @requires montage/data/controllistener | ||
10 | @requires montage/data/pledge | ||
11 | @requires montage/core/exception | ||
12 | @requires montage/core/shim/structures | ||
13 | @requires montage/core/promise | ||
14 | @requires montage/core/logger | ||
15 | */ | ||
16 | var Montage = require("montage").Montage; | ||
17 | var ControlListener = require("data/controllistener").ControlListener; | ||
18 | var Pledge = require("data/pledge").Pledge; | ||
19 | var PledgedSortedSet = require("data/pledge").PledgedSortedSet; | ||
20 | var Exception = require("core/exception").Exception; | ||
21 | var Map = require("core/shim/structures").Map; | ||
22 | var Set = require("core/shim/structures").Set; | ||
23 | var OrderedSet = require("core/shim/structures").OrderedSet; | ||
24 | var Promise = require("core/promise").Promise; | ||
25 | var logger = require("core/logger").logger("selector"); | ||
26 | /** | ||
27 | @class module:montage/data/selector.SelectorRegistry | ||
28 | @classdesc The selector registry provides a documented way to extend the list of selectors.<br> | ||
29 | By default, selector should provide an in-memory evaluator. | ||
30 | @extends module:montage/core/core.Montage | ||
31 | */ | ||
32 | |||
33 | var _selectorRegistry = null; | ||
34 | var SelectorRegistry = exports.SelectorRegistry = Montage.create(Montage,/** @lends module:montage/data/selector.SelectorRegistry# */ { | ||
35 | |||
36 | _selectorTable: { | ||
37 | value: new Map(), | ||
38 | writable: false, | ||
39 | serializable: true, | ||
40 | distinct: true, | ||
41 | enumerable: false | ||
42 | }, | ||
43 | /** | ||
44 | Description TODO | ||
45 | @private | ||
46 | */ | ||
47 | _evaluatorTable: { | ||
48 | value: new Map(), | ||
49 | writable: false, | ||
50 | serializable: true, | ||
51 | distinct: true, | ||
52 | enumerable: false | ||
53 | }, | ||
54 | /** | ||
55 | Description TODO | ||
56 | @function | ||
57 | @returns itself | ||
58 | */ | ||
59 | init: { | ||
60 | serializable: false, | ||
61 | enumerable: false, | ||
62 | value: function() { | ||
63 | // Patch the collections to accept filterWithSelector | ||
64 | Selector._patchCollections(); | ||
65 | return this; | ||
66 | } | ||
67 | }, | ||
68 | |||
69 | /* | ||
70 | * | ||
71 | */ | ||
72 | /** | ||
73 | Register a new selector.<br> | ||
74 | This method creates a new property on the Selector prototype for each of the names returned by the aliases method of the selector being registered. | ||
75 | @function | ||
76 | @param {Prototype} selector The selector prototype. | ||
77 | @returns {Prototype} selector | ||
78 | */ | ||
79 | registerSelector: { | ||
80 | value: function(selector) { | ||
81 | if ((selector != null) && (typeof selector.evaluate === 'function') && (typeof selector.aliases != null)) { | ||
82 | if (typeof selector.aliases === 'string') { | ||
83 | this.__registerSelector(selector.aliases, selector); | ||
84 | } else if (Selector.isArray(selector.aliases)) { | ||
85 | var alias, index; | ||
86 | var aliases = selector.aliases; | ||
87 | for (index = 0; typeof (alias = aliases[index]) !== "undefined"; index++) { | ||
88 | this.__registerSelector(alias, selector); | ||
89 | } | ||
90 | } | ||
91 | } | ||
92 | return selector; | ||
93 | } | ||
94 | }, | ||
95 | |||
96 | /** | ||
97 | Description TODO | ||
98 | @private | ||
99 | */ | ||
100 | __registerSelector: { | ||
101 | enumerable: false, | ||
102 | value: function(alias, selector) { | ||
103 | var operatorDefinition; | ||
104 | if (this.selectorForKey(alias) === null) { | ||
105 | operatorDefinition = { | ||
106 | value: function() { | ||
107 | var newSelector = selector.create(); | ||
108 | return newSelector.initWithSelector.call(newSelector, this, Array.prototype.slice.call(arguments)); | ||
109 | }, | ||
110 | enumerable: true, | ||
111 | serializable: false | ||
112 | } | ||
113 | Montage.defineProperty(Selector, alias, operatorDefinition); | ||
114 | // | ||
115 | // We need to check the evaluator has the visitor methods for custom selectors | ||
116 | this._evaluatorTable.forEach(function(evaluator, key) { | ||
117 | if (typeof evaluator[selector.visitorMethodName] === 'undefined') { | ||
118 | var method = selector.visitorMethodForEvaluatorID(evaluator.evaluatorId); | ||
119 | if (typeof method === 'function') { | ||
120 | Object.defineProperty(evaluator, selector.visitorMethodName, method); | ||
121 | } | ||
122 | } | ||
123 | }); | ||
124 | // | ||
125 | this._selectorTable.set(alias, selector); | ||
126 | } else { | ||
127 | throw Exception.create().initWithMessageTargetAndMethod("Selector already defined", selector, alias); | ||
128 | } | ||
129 | } | ||
130 | }, | ||
131 | /** | ||
132 | Description TODO | ||
133 | @function | ||
134 | @param {String} key TODO | ||
135 | @returns {Selector} type | ||
136 | */ | ||
137 | selectorForKey: { | ||
138 | value: function(key) { | ||
139 | var selector = this._selectorTable.get(key); | ||
140 | return (typeof selector !== 'undefined' ? selector : null); | ||
141 | } | ||
142 | }, | ||
143 | |||
144 | /* | ||
145 | * Remove a previously registered selector | ||
146 | */ | ||
147 | /** | ||
148 | Remove a previously registered selector. | ||
149 | @function | ||
150 | @param {Prototype} selector To be deregistered. | ||
151 | @returns selector | ||
152 | */ | ||
153 | deregisterSelector: { | ||
154 | value: function(selector) { | ||
155 | if ((selector == null) || (typeof selector.aliases == null)) { | ||
156 | return null; | ||
157 | } | ||
158 | if (typeof selector.aliases === 'string') { | ||
159 | this.__deregisterSelector(selector.aliases, selector); | ||
160 | } else if (Selector.isArray(selector.aliases)) { | ||
161 | var alias, index; | ||
162 | var aliases = selector.aliases; | ||
163 | for (index = 0; typeof (alias = aliases[index]) !== "undefined"; index++) { | ||
164 | this.__deregisterSelector(alias, selector); | ||
165 | } | ||
166 | } | ||
167 | return selector; | ||
168 | } | ||
169 | }, | ||
170 | |||
171 | /** | ||
172 | Description TODO | ||
173 | @private | ||
174 | */ | ||
175 | __deregisterSelector: { | ||
176 | enumerable: false, | ||
177 | value: function(alias, selector) { | ||
178 | // TODO [PJYF Aug 31 2011] Needs to be remove the property but I am not sure how. | ||
179 | this._selectorTable.delete(alias); | ||
180 | } | ||
181 | }, | ||
182 | /** | ||
183 | Description TODO | ||
184 | @function | ||
185 | @param {Prototype} evaluator To be registered. | ||
186 | @returns evaluator | ||
187 | */ | ||
188 | registerEvaluator: { | ||
189 | value: function(evaluator) { | ||
190 | this._evaluatorTable.set(evaluator.evaluatorId, evaluator); | ||
191 | // We need to check the evaluator has the visitor methods for custom selectors | ||
192 | this._selectorTable.forEach(function(selector, key) { | ||
193 | if (typeof evaluator[selector.visitorMethodName] === 'undefined') { | ||
194 | var method = selector.visitorMethodForEvaluatorID(evaluator.evaluatorId); | ||
195 | if (typeof method === 'function') { | ||
196 | Object.defineProperty(evaluator, selector.visitorMethodName, method); | ||
197 | } | ||
198 | } | ||
199 | }); | ||
200 | return evaluator; | ||
201 | } | ||
202 | }, | ||
203 | /** | ||
204 | Description TODO | ||
205 | @function | ||
206 | @returns evaluatorIds | ||
207 | */ | ||
208 | evaluatorIds: { | ||
209 | get: function() { | ||
210 | var evaluatorIds = new Array(); | ||
211 | this._evaluatorTable.forEach(function(element, key) { | ||
212 | evaluatorIds.push(key); | ||
213 | }); | ||
214 | return evaluatorIds; | ||
215 | } | ||
216 | }, | ||
217 | /** | ||
218 | Description TODO | ||
219 | @function | ||
220 | @param {String} evaluatorId The evaluator id | ||
221 | @returns value | ||
222 | */ | ||
223 | evaluatorForId: { | ||
224 | value: function(evaluatorId) { | ||
225 | var value = this._evaluatorTable.get(evaluatorId); | ||
226 | if (typeof value === 'undefined') { | ||
227 | return null; | ||