diff options
Diffstat (limited to 'node_modules/montage/core/event/binding.js')
-rwxr-xr-x | node_modules/montage/core/event/binding.js | 1413 |
1 files changed, 1413 insertions, 0 deletions
diff --git a/node_modules/montage/core/event/binding.js b/node_modules/montage/core/event/binding.js new file mode 100755 index 00000000..0ce154a1 --- /dev/null +++ b/node_modules/montage/core/event/binding.js | |||
@@ -0,0 +1,1413 @@ | |||
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 | // I stopped caring about whether or not this splice could be considered an ADDITION or REMOVAL | ||
92 | // all splices result in a modification now; we could change this if we want to | ||
93 | changeType = ChangeTypes.MODIFICATION; | ||
94 | |||
95 | if (this.dispatchChangeEvent) { | ||
96 | changeEvent = new ChangeEventConstructor(); | ||
97 | changeEvent.minus = removedMembers; | ||
98 | changeEvent.plus = addedMembers; | ||
99 | changeEvent.changeIndex = index; | ||
100 | changeEvent.propertyChange = changeType; | ||
101 | this.dispatchEvent(changeEvent); | ||
102 | } | ||
103 | |||
104 | if (this.dispatchChangeAtIndexEvent) { | ||
105 | |||
106 | netChange = addedCount - removedCount; | ||
107 | |||
108 | if (typeof howMany === "undefined") { | ||
109 | // no howMany argument given: remove all elements after index? | ||
110 | // TODO this may only be in some implementations | ||
111 | affectedIndexCount = originalCount + addedCount; | ||
112 | } else if (0 === netChange) { | ||
113 | // No net change; affects only how many expected | ||
114 | affectedIndexCount = addedCount; | ||
115 | } else if (netChange > 0) { | ||
116 | // Net gain; affects from start to end of original array + net gain | ||
117 | affectedIndexCount = (originalCount - startIndex) + (netChange); | ||
118 | } else { | ||
119 | // Net loss; affects from start to end of original array | ||
120 | affectedIndexCount = originalCount - startIndex; | ||
121 | } | ||
122 | |||
123 | for (i = 0; i < affectedIndexCount; i++) { | ||
124 | changeIndex = startIndex + i; | ||
125 | |||
126 | changeEvent = new ChangeEventConstructor(); | ||
127 | changeEvent.type = "change@" + changeIndex; | ||
128 | |||
129 | // old value at changeIndex was either: | ||
130 | // - removed outright, or replaced | ||
131 | // - moved somewhere in the array due to a gain or loss | ||
132 | changeEvent.minus = (i < removedCount) ? removedMembers[i] : this[changeIndex + netChange]; | ||
133 | |||
134 | changeEvent.plus = this[changeIndex]; | ||
135 | changeEvent.changeIndex = changeIndex; | ||
136 | changeEvent.propertyChange = changeType; | ||
137 | this.dispatchEvent(changeEvent); | ||
138 | } | ||
139 | } | ||
140 | |||
141 | return removedMembers; | ||
142 | }, | ||
143 | enumerable: false, | ||
144 | configurable: true | ||
145 | }); | ||
146 | //Removes the first element from an array and returns that element. This method changes the length of the array. | ||
147 | Object.defineProperty(ChangeEventDispatchingArray, "_shift", { | ||
148 | value: Array.prototype.shift, | ||
149 | enumerable: false, | ||
150 | configurable: true | ||
151 | }); | ||
152 | |||
153 | /** | ||
154 | @function module:montage/core/event/binding.ChangeEventDispatchingArray#shift | ||
155 | */ | ||
156 | Object.defineProperty(ChangeEventDispatchingArray, "shift", { | ||
157 | value: function() { | ||
158 | |||
159 | if (0 === this.length) { | ||
160 | return; | ||
161 | } | ||
162 | |||
163 | var result, i, countI, changeEvent; | ||
164 | |||
165 | result = this._shift.call(this); | ||
166 | |||
167 | if (this.dispatchChangeEvent) { | ||
168 | changeEvent = new ChangeEventConstructor(); | ||
169 | changeEvent.minus = result; | ||
170 | changeEvent.plus = this[0]; | ||
171 | changeEvent.changeIndex = 0; | ||
172 | changeEvent.propertyChange = ChangeTypes.REMOVAL; | ||
173 | this.dispatchEvent(changeEvent); | ||
174 | } | ||
175 | |||
176 | if (this.dispatchChangeAtIndexEvent) { | ||
177 | // A single item was just removed form the front; notify all index listeners | ||
178 | // (including listeners for the index that is now undefined at the end) | ||
179 | for (i = 0,countI = this.length + 1; i < countI; i++) { | ||
180 | changeEvent = new ChangeEventConstructor(); | ||
181 | changeEvent.type = "change@" + i; | ||
182 | changeEvent.minus = i === 0 ? result : this[i - 1]; | ||
183 | changeEvent.plus = this[i]; | ||
184 | changeEvent.changeIndex = i; | ||
185 | changeEvent.propertyChange = ChangeTypes.REMOVAL; | ||
186 | this.dispatchEvent(changeEvent); | ||
187 | } | ||
188 | } | ||
189 | return result; | ||
190 | }, | ||
191 | enumerable: false, | ||
192 | configurable: true | ||
193 | }); | ||
194 | |||
195 | //Adds one or more elements to the beginning of an array and returns the new length of the array. | ||
196 | Object.defineProperty(ChangeEventDispatchingArray, "_unshift", { | ||
197 | value: Array.prototype.unshift, | ||
198 | enumerable: false, | ||
199 | configurable: true | ||
200 | }); | ||
201 | |||
202 | /** | ||
203 | @function module:montage/core/event/binding.ChangeEventDispatchingArray#unshift | ||
204 | */ | ||
205 | Object.defineProperty(ChangeEventDispatchingArray, "unshift", { | ||
206 | value: function() { | ||
207 | |||
208 | var addedCount = arguments.length, i, countI, changeEvent; | ||
209 | |||
210 | countI = this._unshift.apply(this, arguments); | ||
211 | |||
212 | if (this.dispatchChangeEvent) { | ||
213 | changeEvent = new ChangeEventConstructor(); | ||
214 | changeEvent.minus = undefined; | ||
215 | changeEvent.plus = Array.prototype.slice.call(arguments, 0); | ||
216 | changeEvent.changeIndex = 0; | ||
217 | changeEvent.propertyChange = ChangeTypes.ADDITION; | ||
218 | this.dispatchEvent(changeEvent); | ||
219 | } | ||
220 | |||
221 | if (this.dispatchChangeAtIndexEvent) { | ||
222 | for (i = 0; i < countI; i++) { | ||
223 |