diff options
Diffstat (limited to 'node_modules/montage-user/core/event/binding.js')
-rwxr-xr-x | node_modules/montage-user/core/event/binding.js | 1420 |
1 files changed, 1420 insertions, 0 deletions
diff --git a/node_modules/montage-user/core/event/binding.js b/node_modules/montage-user/core/event/binding.js new file mode 100755 index 00000000..7a15e5b9 --- /dev/null +++ b/node_modules/montage-user/core/event/binding.js | |||
@@ -0,0 +1,1420 @@ | |||
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 | /** | ||
8 | @module montage/core/event/binding | ||
9 | @requires montage/core/core | ||
10 | @requires montage/core/event/mutable-event | ||
11 | @requires montage/core/serializer | ||
12 | @requires montage/core/deserializer | ||
13 | @requires montage/core/event/event-manager | ||
14 | */ | ||
15 | |||
16 | var Montage = require("montage").Montage, | ||
17 | ChangeEventConstructor = require("core/event/mutable-event")._Change, | ||
18 | ChangeTypes = require("core/event/mutable-event").ChangeTypes, | ||
19 | Serializer = require("core/serializer").Serializer, | ||
20 | Deserializer = require("core/deserializer").Deserializer, | ||
21 | defaultEventManager = require("core/event/event-manager").defaultEventManager, | ||
22 | AT_TARGET = 2; | ||
23 | |||
24 | /** | ||
25 | @member external:Array#dispatchChangeEvent | ||
26 | */ | ||
27 | Object.defineProperty(Array.prototype, "dispatchChangeEvent", {value: false, enumerable: false, writable: true}); | ||
28 | |||
29 | /** | ||
30 | @member external:Array#dispatchChangeAtIndexEvent | ||
31 | */ | ||
32 | Object.defineProperty(Array.prototype, "dispatchChangeAtIndexEvent", {value: false, enumerable: false, writable: true}); | ||
33 | |||
34 | /** | ||
35 | @member external:Array#dispatchChangeAtLengthEvent | ||
36 | */ | ||
37 | Object.defineProperty(Array.prototype, "dispatchChangeAtLengthEvent", {value: false, enumerable: false, writable: true}); | ||
38 | |||
39 | /** | ||
40 | @class module:montage/core/event/binding.ChangeEventDispatchingArray | ||
41 | */ | ||
42 | var ChangeEventDispatchingArray = exports.ChangeEventDispatchingArray = []; | ||
43 | |||
44 | /** | ||
45 | @function external:Array#addContentEventListener | ||
46 | @param {string} type Event type | ||
47 | @param {object|function} listener Event listener. | ||
48 | @param {boolean} useCapture Specifies whether to listen for the event during the capture phase. | ||
49 | */ | ||
50 | Object.defineProperty(Array.prototype, "addContentEventListener", { | ||
51 | value: function(type, listener, useCapture) { | ||
52 | }, | ||
53 | enumerable: false, | ||
54 | configurable: true | ||
55 | }); | ||
56 | |||
57 | |||
58 | Object.defineProperty(ChangeEventDispatchingArray, "_splice", { | ||
59 | value: Array.prototype.splice, | ||
60 | enumerable: false, | ||
61 | configurable: true | ||
62 | }); | ||
63 | |||
64 | /** | ||
65 | @function module:montage/core/event/binding.ChangeEventDispatchingArray#splice | ||
66 | @param {string} type Event type | ||
67 | @param {object|function} listener Event listener. | ||
68 | @param {boolean} useCapture Specifies whether to listen for the event during the capture phase. | ||
69 | */ | ||
70 | Object.defineProperty(ChangeEventDispatchingArray, "splice", { | ||
71 | value: function(index, howMany/*[, element1[, ...[, elementN]]]*/) { | ||
72 | |||
73 | var originalCount = this.length, | ||
74 | addedCount = arguments.length - 2, /* elements to add less the index and howMany parameters*/ | ||
75 | removedCount, | ||
76 | netChange, | ||
77 | i, changeType, changeEvent, affectedIndexCount, startIndex, changeIndex, | ||
78 | removedMembers, | ||
79 | addedMembers = []; | ||
80 | |||
81 | if (addedCount > 0) { | ||
82 | addedMembers = this.slice.call(arguments, 2); | ||
83 | } | ||
84 | |||
85 | // Index may be positive (from the front) or negative (from the back) figure out the positive one | ||
86 | // now in anticipation of needing it when dispatching events | ||
87 | startIndex = index >= 0 ? index : this.length + index; | ||
88 | removedMembers = this._splice.apply(this, arguments); | ||
89 | removedCount = removedMembers.length; | ||
90 | |||
91 | netChange = addedCount - removedCount; | ||
92 | |||
93 | // Find the most accurate propertyChange type for this splice, | ||
94 | // For the most part it's considered a modification unless the length of the array was modified | ||
95 | // if only to not bother notifying listeners for changes of the length of this array | ||
96 | changeType = ChangeTypes.MODIFICATION; | ||
97 | |||
98 | if (netChange > 0) { | ||
99 | changeType = ChangeTypes.ADDITION; | ||
100 | } else if (netChange < 0) { | ||
101 | changeType = ChangeTypes.REMOVAL; | ||
102 | } | ||
103 | |||
104 | if (this.dispatchChangeEvent) { | ||
105 | changeEvent = new ChangeEventConstructor(); | ||
106 | changeEvent.minus = removedMembers; | ||
107 | changeEvent.plus = addedMembers; | ||
108 | changeEvent.changeIndex = index; | ||
109 | changeEvent.propertyChange = changeType; | ||
110 | this.dispatchEvent(changeEvent); | ||
111 | } | ||
112 | |||
113 | if (this.dispatchChangeAtIndexEvent) { | ||
114 | |||
115 | if (typeof howMany === "undefined") { | ||
116 | // no howMany argument given: remove all elements after index? | ||
117 | // TODO this may only be in some implementations | ||
118 | affectedIndexCount = originalCount + addedCount; | ||
119 | } else if (0 === netChange) { | ||
120 | // No net change; affects only how many expected | ||
121 | affectedIndexCount = addedCount; | ||
122 | } else if (netChange > 0) { | ||
123 | // Net gain; affects from start to end of original array + net gain | ||
124 | affectedIndexCount = (originalCount - startIndex) + (netChange); | ||
125 | } else { | ||
126 | // Net loss; affects from start to end of original array | ||
127 | affectedIndexCount = originalCount - startIndex; | ||
128 | } | ||
129 | |||
130 | for (i = 0; i < affectedIndexCount; i++) { | ||
131 | changeIndex = startIndex + i; | ||
132 | |||
133 | changeEvent = new ChangeEventConstructor(); | ||
134 | changeEvent.type = "change@" + changeIndex; | ||
135 | |||
136 | // old value at changeIndex was either: | ||
137 | // - removed outright, or replaced | ||
138 | // - moved somewhere in the array due to a gain or loss | ||
139 | changeEvent.minus = (i < removedCount) ? removedMembers[i] : this[changeIndex + netChange]; | ||
140 | |||
141 | changeEvent.plus = this[changeIndex]; | ||
142 | changeEvent.changeIndex = changeIndex; | ||
143 | changeEvent.propertyChange = changeType; | ||
144 | this.dispatchEvent(changeEvent); | ||
145 | } | ||
146 | } | ||
147 | |||
148 | return removedMembers; | ||
149 | }, | ||
150 | enumerable: false, | ||
151 | configurable: true | ||
152 | }); | ||
153 | //Removes the first element from an array and returns that element. This method changes the length of the array. | ||
154 | Object.defineProperty(ChangeEventDispatchingArray, "_shift", { | ||
155 | value: Array.prototype.shift, | ||
156 | enumerable: false, | ||
157 | configurable: true | ||
158 | }); | ||
159 | |||
160 | /** | ||
161 | @function module:montage/core/event/binding.ChangeEventDispatchingArray#shift | ||
162 | */ | ||
163 | Object.defineProperty(ChangeEventDispatchingArray, "shift", { | ||
164 | value: function() { | ||
165 | |||
166 | if (0 === this.length) { | ||
167 | return; | ||
168 | } | ||
169 | |||
170 | var result, i, countI, changeEvent; | ||
171 | |||
172 | result = this._shift.call(this); | ||
173 | |||
174 | if (this.dispatchChangeEvent) { | ||
175 | changeEvent = new ChangeEventConstructor(); | ||
176 | changeEvent.minus = result; | ||
177 | changeEvent.plus = this[0]; | ||
178 | changeEvent.changeIndex = 0; | ||
179 | changeEvent.propertyChange = ChangeTypes.REMOVAL; | ||
180 | this.dispatchEvent(changeEvent); | ||
181 | } | ||
182 | |||
183 | if (this.dispatchChangeAtIndexEvent) { | ||
184 | // A single item was just removed form the front; notify all index listeners | ||
185 | // (including listeners for the index that is now undefined at the end) | ||
186 | for (i = 0,countI = this.length + 1; i < countI; i++) { | ||
187 | changeEvent = new ChangeEventConstructor(); | ||
188 | changeEvent.type = "change@" + i; | ||
189 | changeEvent.minus = i === 0 ? result : this[i - 1]; | ||
190 | changeEvent.plus = this[i]; | ||
191 | changeEvent.changeIndex = i; | ||
192 | changeEvent.propertyChange = ChangeTypes.REMOVAL; | ||
193 | this.dispatchEvent(changeEvent); | ||
194 | } | ||
195 | } | ||
196 | return result; | ||
197 | }, | ||
198 | enumerable: false, | ||
199 | configurable: true | ||
200 | }); | ||
201 | |||
202 | //Adds one or more elements to the beginning of an array and returns the new length of the array. | ||
203 | Object.defineProperty(ChangeEventDispatchingArray, "_unshift", { | ||
204 | value: Array.prototype.unshift, | ||
205 | enumerable: false, | ||
206 | configurable: true | ||
207 | }); | ||
208 | |||
209 | /** | ||
210 | @function module:montage/core/event/binding.ChangeEventDispatchingArray#unshift | ||
211 | */ | ||
212 | Object.defineProperty(ChangeEventDispatchingArray, "unshift", { | ||
213 | value: function() { | ||
214 | |||
215 | var addedCount = arguments.length, i, countI, changeEvent; | ||
216 | |||
217 | countI = this._unshift.apply(this, arguments); | ||
218 | |||
219 | if (this.dispatchChangeEvent) { | ||
220 | changeEvent = new ChangeEventConstructor(); | ||
221 |