diff options
author | Valerio Virgillito | 2012-02-01 00:09:27 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-02-01 00:09:27 -0800 |
commit | 8592cfb89db05f0e52d8c1b8c7046e6f49e3522d (patch) | |
tree | a6f9b5adfe16fd6232708f8a2345cd79056caca9 /node_modules/montage-user/core/event/mutable-event.js | |
parent | c6d1a1da9e7cdf477daf32410d80691eb1e828d5 (diff) | |
download | ninja-8592cfb89db05f0e52d8c1b8c7046e6f49e3522d.tar.gz |
Adding a montage framework copy for the user document
Adding a new montage copy containing the latest version of montage to use the new native widgets
Signed-off-by: Valerio Virgillito <valerio@motorola.com>
Diffstat (limited to 'node_modules/montage-user/core/event/mutable-event.js')
-rwxr-xr-x | node_modules/montage-user/core/event/mutable-event.js | 239 |
1 files changed, 239 insertions, 0 deletions
diff --git a/node_modules/montage-user/core/event/mutable-event.js b/node_modules/montage-user/core/event/mutable-event.js new file mode 100755 index 00000000..f94ee0b9 --- /dev/null +++ b/node_modules/montage-user/core/event/mutable-event.js | |||
@@ -0,0 +1,239 @@ | |||
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/event/mutable-event | ||
8 | @requires montage | ||
9 | @requires montage/core/enum | ||
10 | */ | ||
11 | var Montage = require("montage").Montage, | ||
12 | Enum = require("core/enum").Enum; | ||
13 | |||
14 | // XXX Does not presently function server-side | ||
15 | if (typeof window !== "undefined") { | ||
16 | |||
17 | var ChangeTypes = exports.ChangeTypes = Enum.create().initWithMembers("MODIFICATION", "ADDITION", "REMOVAL"); | ||
18 | |||
19 | var _eventConstructorsByType = {}; | ||
20 | var _changeEventConstructor = null; | ||
21 | var nullDescriptor = {value: null}; | ||
22 | |||
23 | var wrapProperty = function(obj, key) { | ||
24 | |||
25 | var storageKey = "_" + key; | ||
26 | |||
27 | Montage.defineProperty(obj, storageKey, {value: undefined}); | ||
28 | |||
29 | Montage.defineProperty(obj, key, { | ||
30 | get:(function(key, storageKey) { | ||
31 | return function() { | ||
32 | return this.hasOwnProperty(storageKey) ? this[storageKey] : (this._event ? this._event[key] : undefined); | ||
33 | }; | ||
34 | })(key, storageKey), | ||
35 | |||
36 | set: (function(storageKey) { | ||
37 | return function(value) { | ||
38 | this[storageKey] = value; | ||
39 | }; | ||
40 | })(storageKey) | ||
41 | }); | ||
42 | }; | ||
43 | /** | ||
44 | @class module:montage/core/event/mutable-event.MutableEvent | ||
45 | */ | ||
46 | var MutableEvent = exports.MutableEvent = Montage.create(Montage,/** @lends module:montage/core/event/mutable-event.MutableEvent# */ { | ||
47 | /** | ||
48 | @function | ||
49 | @param {Event} event The original event. | ||
50 | @returns newEvent | ||
51 | */ | ||
52 | fromEvent: { | ||
53 | value: function(event) { | ||
54 | var type = event.type, | ||
55 | constructor = _eventConstructorsByType[type], | ||
56 | newEvent; | ||
57 | if (!constructor) { | ||
58 | constructor = function() { | ||
59 | }; | ||
60 | constructor.prototype = MutableEvent.create()._initPrototypeWithEvent(event); | ||
61 | _eventConstructorsByType[type] = constructor; | ||
62 | } | ||
63 | newEvent = new constructor(); | ||
64 | newEvent._initWithEvent(event); | ||
65 | return newEvent; | ||
66 | } | ||
67 | }, | ||
68 | |||
69 | // Same arguments as initEvent & initCustomEvent | ||
70 | |||
71 | /** | ||
72 | @function | ||
73 | @param {Event} type TODO | ||
74 | @param {Event} canBubbleArg TODO | ||
75 | @param {Event} cancelableArg TODO | ||
76 | @param {Event} data TODO | ||
77 | @returns this.fromEvent(anEvent) | ||
78 | */ | ||
79 | fromType: { | ||
80 | value: function(type, canBubbleArg, cancelableArg, data) { | ||
81 | var anEvent = document.createEvent("CustomEvent"); | ||
82 | anEvent.initEvent(type, canBubbleArg, cancelableArg, data); | ||
83 | return this.fromEvent(anEvent); | ||
84 | } | ||
85 | }, | ||
86 | |||
87 | /** | ||
88 | @function | ||
89 | @returns new _changeEventConstructor() | ||
90 | */ | ||
91 | changeEvent: { | ||
92 | value: function() { | ||
93 | return new _changeEventConstructor(); | ||
94 | } | ||
95 | }, | ||
96 | |||
97 | /** | ||
98 | @function | ||
99 | @param {Event} key TODO | ||
100 | @param {Event} minus TODO | ||
101 | @returns changeEvent | ||
102 | */ | ||
103 | changeEventForKeyAndValue: { | ||
104 | value: function(key, minus) { | ||
105 | var changeEvent = new _changeEventConstructor(); | ||
106 | changeEvent.type = "change@" + key; | ||
107 | changeEvent.minus = minus; | ||
108 | changeEvent.propertyChange = ChangeTypes.MODIFICATION; | ||
109 | return changeEvent; | ||
110 | } | ||
111 | }, | ||
112 | |||
113 | /** | ||
114 | @function | ||
115 | @param {String} plus TODO | ||
116 | @returns itself | ||
117 | */ | ||
118 | withPlusValue: { | ||
119 | value: function(plus) { | ||
120 | this.plus = plus; | ||
121 | return this; | ||
122 | } | ||
123 | }, | ||
124 | /** | ||
125 | @private | ||
126 | */ | ||
127 | _initPrototypeWithEvent: { | ||
128 | value: function(event) { | ||
129 | var key; | ||
130 | |||
131 | for (key in event) { | ||
132 | |||
133 | // Don't overwrite keys we have installed | ||
134 | if (this[key]) { | ||
135 | continue; | ||
136 | } | ||
137 | |||
138 | // Skip methods, the ones we care about have been wrapped already | ||
139 | // TODO actually wrap all known functions generically | ||
140 | //if (typeof this[key] === "function") { | ||
141 | // continue; | ||
142 | //} | ||
143 | |||
144 | // TODO ok, maybe it would be quicker to not make this a function, but I really hate duplicated code | ||
145 | wrapProperty(this, key); | ||
146 | } | ||
147 | |||
148 | wrapProperty(this, "replayed"); | ||
149 | |||
150 | return this; | ||
151 | } | ||
152 | }, | ||
153 | /** | ||
154 | @private | ||
155 | */ | ||
156 | _initWithEvent: { | ||
157 | value: function(event) { | ||
158 | this._event = event; | ||
159 | return this; | ||
160 | } | ||
161 | }, | ||
162 | /** | ||
163 | @function | ||
164 | */ | ||
165 | preventDefault: { | ||
166 | value: function() { | ||
167 | this._event.preventDefault(); | ||
168 | } | ||
169 | }, | ||
170 | /** | ||
171 | @function | ||
172 | */ | ||
173 | stopImmediatePropagation: { | ||
174 | value: function() { | ||
175 | this._event.stopImmediatePropagation(); | ||
176 | // TODO only if the event is cancellable? | ||
177 | this.propagationStopped = true; | ||
178 | } | ||
179 | }, | ||
180 | /** | ||
181 | @type {Property} | ||
182 | @default {Boolean} false | ||
183 | */ | ||
184 | propagationStopped: { | ||
185 | value: false | ||
186 | }, | ||
187 | /** | ||
188 | @type {Property} | ||
189 | @default {Boolean} true | ||
190 | */ | ||
191 | mutable: { | ||
192 | value: true | ||
193 | }, | ||
194 | /** | ||
195 | @type {Property} | ||
196 | @default {Element} null | ||
197 | */ | ||
198 | target: { | ||
199 | value: null | ||
200 | }, | ||
201 | /** | ||
202 | @function | ||
203 | */ | ||
204 | stopPropagation: { | ||
205 | value: function() { | ||
206 | this._event.stopPropagation(); | ||
207 | // TODO only if the event is cancellable? | ||
208 | this.propagationStopped = true; | ||
209 | } | ||
210 | }, | ||
211 | /** | ||
212 | @function | ||
213 | */ | ||
214 | stop: { | ||
215 | value: function() { | ||
216 | this.preventDefault(); | ||
217 | this.stopPropagation(); | ||
218 | } | ||