diff options
Diffstat (limited to 'node_modules/montage-user/core/gate.js')
-rwxr-xr-x | node_modules/montage-user/core/gate.js | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/node_modules/montage-user/core/gate.js b/node_modules/montage-user/core/gate.js new file mode 100755 index 00000000..0910cc46 --- /dev/null +++ b/node_modules/montage-user/core/gate.js | |||
@@ -0,0 +1,195 @@ | |||
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/core/gate | ||
8 | @requires montage/core/core | ||
9 | @requires montage/core/logger | ||
10 | */ | ||
11 | var Montage = require("montage").Montage, | ||
12 | logger = require("core/logger").logger("gate"); | ||
13 | /** | ||
14 | @class module:montage/core/gate.Gate | ||
15 | @extends module:montage/core/core.Montage | ||
16 | */ | ||
17 | var Gate = exports.Gate = Montage.create(Montage,/** @lends module:montage/core/gate.Gate# */ { | ||
18 | /** | ||
19 | @function | ||
20 | @returns {Gate} A new Gate instance. | ||
21 | */ | ||
22 | init: { | ||
23 | enumerable: false, | ||
24 | value: function() { | ||
25 | this.reset(); | ||
26 | return this; | ||
27 | } | ||
28 | }, | ||
29 | /** | ||
30 | @function | ||
31 | @param {String} delegate The delegate to be initialized. | ||
32 | @returns itself | ||
33 | */ | ||
34 | initWithDelegate: { | ||
35 | enumerable: false, | ||
36 | value: function(delegate) { | ||
37 | this.reset(); | ||
38 | this.delegate = delegate; | ||
39 | return this; | ||
40 | } | ||
41 | }, | ||
42 | /** | ||
43 | @function | ||
44 | @param {String} propertyDescriptor The propertyDescriptor to be initialized. | ||
45 | @returns itself | ||
46 | */ | ||
47 | initWithDescriptor: { | ||
48 | enumerable: false, | ||
49 | value: function(propertyDescriptor) { | ||
50 | var fieldName; | ||
51 | this.reset(); | ||
52 | for (fieldName in propertyDescriptor) { | ||
53 | this.setField(fieldName, propertyDescriptor[fieldName].value); | ||
54 | } | ||
55 | return this; | ||
56 | } | ||
57 | }, | ||
58 | /** | ||
59 | |||
60 | @type {Property} | ||
61 | @default {Number} 0 | ||
62 | */ | ||
63 | count: { | ||
64 | value: 0 | ||
65 | }, | ||
66 | /** | ||
67 | |||
68 | @type {Property} | ||
69 | @default {String} null | ||
70 | */ | ||
71 | table: { | ||
72 | value: null | ||
73 | }, | ||
74 | /** | ||
75 | @function | ||
76 | @param {Array} aFieldName The aFieldName array. | ||
77 | @returns !table or table[aFieldName] | ||
78 | */ | ||
79 | getField: { | ||
80 | enumerable: false, | ||
81 | value: function(aFieldName) { | ||
82 | var table = this.table; | ||
83 | return !table || table[aFieldName]; | ||
84 | } | ||
85 | }, | ||
86 | /** | ||
87 | @function | ||
88 | @param {Array} aFieldName The aFieldName array. | ||
89 | @param {Number} value The count on the array. | ||
90 | */ | ||
91 | setField: { | ||
92 | enumerable: false, | ||
93 | value: function(aFieldName, value) { | ||
94 | var table = this.table, | ||
95 | fieldValue, | ||
96 | oldCount = this.count; | ||
97 | |||
98 | table = (!table ? this.table = {} : table); | ||
99 | |||
100 | fieldValue = table[aFieldName]; | ||
101 | |||
102 | if (typeof fieldValue === "undefined" && !value) { | ||
103 | // new field | ||
104 | this.count++; | ||
105 | } else if (typeof fieldValue !== "undefined" && fieldValue !== value) { | ||
106 | if (value) { | ||
107 | this.count--; | ||
108 | } else { | ||
109 | this.count++; | ||
110 | } | ||
111 | } else if (value) { | ||
112 | logger.error(this, aFieldName + " was not set before."); | ||
113 | } | ||
114 | table[aFieldName] = !!value; | ||
115 | if (this.count === 0 && oldCount > 0) { | ||
116 | this.callDelegateMethod(true); | ||
117 | } else if (oldCount === 0 && this.count > 0) { | ||
118 | this.callDelegateMethod(false); | ||
119 | } | ||
120 | } | ||
121 | }, | ||
122 | /** | ||
123 | @function | ||
124 | @param {Array} aFieldName The aFieldName array to be removed. | ||
125 | */ | ||
126 | removeField: { | ||
127 | enumerable: false, | ||
128 | value: function(aFieldName) { | ||
129 | var table = this.table, fieldValue = table[aFieldName]; | ||
130 | if (typeof fieldValue !== "undefined" && !fieldValue) { | ||
131 | // if the value was false decrement the count | ||
132 | this.count--; | ||
133 | } | ||
134 | delete table[aFieldName]; | ||
135 | } | ||
136 | }, | ||
137 | /** | ||
138 | |||
139 | @type {Property} | ||
140 | @default {String} null | ||
141 | */ | ||
142 | delegate: { | ||
143 | enumerable: false, | ||
144 | value: null | ||
145 | }, | ||
146 | /** | ||
147 | @function | ||
148 | @param {Number} value The value to be called. | ||
149 | */ | ||
150 | callDelegateMethod: { | ||
151 | value: function(value) { | ||
152 | var delegateMethod; | ||
153 | if (this.delegate && typeof (delegateMethod = this.delegate["gateDidBecome" + (value ? "True" : "False")]) === "function") { | ||
154 | delegateMethod.call(this.delegate, this); | ||
155 | } | ||
156 | }, | ||
157 | enumerable: false | ||
158 | }, | ||
159 | /** | ||
160 | @type {Function} | ||
161 | @returns this.count === 0 | ||
162 | */ | ||
163 | value: { | ||
164 | enumerable: false, | ||
165 | get: function() { | ||
166 | return this.count === 0; | ||
167 | } | ||
168 | }, | ||
169 | /** | ||
170 | @function | ||
171 | */ | ||
172 | reset: { | ||
173 | enumerable: false, | ||
174 | value: function() { | ||
175 | this.table = {}; | ||
176 | this.count = 0; | ||
177 | } | ||
178 | }, | ||
179 | /** | ||
180 | @function | ||
181 | @returns {String} result | ||
182 | */ | ||
183 | toString: { | ||
184 | value: function() { | ||
185 | var fieldNames = this._fields, | ||
186 | i, | ||
187 | iField, | ||
188 | result = ""; | ||
189 | for (i = 0; (iField = fieldNames[i]); i++) { | ||
190 | result += iField + "[" + (this._value & fieldNames[iField]) + "], "; | ||
191 | } | ||
192 | return result; | ||
193 | } | ||
194 | } | ||
195 | }); | ||