forked from rse/componentjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent-3-patt-G-model.js
312 lines (280 loc) · 13 KB
/
component-3-patt-G-model.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
** ComponentJS -- Component System for JavaScript <http://componentjs.com>
** Copyright (c) 2009-2013 Ralf S. Engelschall <http://engelschall.com>
**
** This Source Code Form is subject to the terms of the Mozilla Public
** License, v. 2.0. If a copy of the MPL was not distributed with this
** file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/* generic pattern for model management */
$cs.pattern.model = $cs.trait({
protos: {
/* define model */
model: function () {
/* determine parameters */
var params = $cs.params("model", arguments, {
model: { pos: 0, def: null }
});
/* simplify further processing */
var model = params.model;
if (model === null)
model = undefined;
/* sanity check model */
var name;
if (_cs.isdefined(model)) {
for (name in model) {
if (typeof model[name].value === "undefined")
model[name].value = "";
if (typeof model[name].valid === "undefined")
model[name].valid = "string";
if (typeof model[name].autoreset === "undefined")
model[name].autoreset = false;
if (typeof model[name].store === "undefined")
model[name].store = false;
for (var key in model[name]) {
if (key !== "value" && key !== "valid" && key !== "autoreset" && key !== "store")
throw _cs.exception("model", "invalid specification key \"" +
key + "\" in specification of model field \"" + name + "\"");
}
if (!$cs.validate(model[name].value, model[name].valid))
throw _cs.exception("model", "model field \"" + name + "\" has " +
"default value " + _cs.json(model[name].value) + ", which does not validate " +
"against validation \"" + model[name].valid + "\"");
}
}
/* try to load stored model values */
var store = this.store("model");
if (store !== null) {
if (_cs.isdefined(model)) {
for (name in model) {
if (model[name].store) {
if (_cs.isdefined(store[name]))
model[name].value = store[name];
}
}
}
}
/* retrieve old model */
var model_old = this.property({ name: "ComponentJS:model", bubbling: false });
/* store model */
if (_cs.isdefined(model)) {
if (_cs.isdefined(model_old)) {
/* merge model into existing one */
var model_new = {};
_cs.extend(model_new, model_old);
_cs.extend(model_new, model);
this.property("ComponentJS:model", model_new);
model = model_new;
}
else {
/* set initial model */
this.property("ComponentJS:model", model);
}
/* optionally save stored model values */
store = {};
var save = false;
for (name in model) {
if (model[name].store) {
store[name] = model[name].value;
save = true;
}
}
if (save)
this.store("model", store);
}
/* return old model */
return model_old;
},
/* get/set model value */
value: function () {
/* determine parameters */
var params = $cs.params("value", arguments, {
name: { pos: 0, req: true },
value: { pos: 1, def: undefined },
force: { pos: 2, def: false },
returnowner: { def: false }
});
/* determine component owning model with requested value */
var owner = null;
var model = null;
var comp = this;
while (comp !== null) {
owner = comp.property({ name: "ComponentJS:model", returnowner: true });
if (!_cs.isdefined(owner))
throw _cs.exception("value", "no model found containing value \"" + params.name + "\"");
model = owner.property("ComponentJS:model");
if (_cs.isdefined(model[params.name]))
break;
comp = owner.parent();
}
if (comp === null)
throw _cs.exception("value", "no model found containing value \"" + params.name + "\"");
/* get new model value */
var value_new = params.value;
/* get old model value */
var ev;
var value_old = model[params.name].value;
var result;
if (typeof value_new === "undefined") {
if (owner.property({ name: "ComponentJS:model:subscribers:get", bubbling: false }) === true) {
/* send event to observers for value get and allow observers
to reject value get operation and/or change old value to get */
ev = owner.publish({
name: "ComponentJS:model:" + params.name + ":get",
args: [ value_old ],
capturing: false,
spreading: false,
bubbling: false,
async: false
});
if (ev.processing()) {
/* re-fetch value from model
(in case the callback set a new value directly) */
value_old = model[params.name].value;
/* allow value to be overridden by event result */
result = ev.result();
if (typeof result !== "undefined")
value_old = result;
}
}
}
/* optionally set new model value */
if ( typeof value_new !== "undefined"
&& (params.force || value_old !== value_new)) {
/* check validity of new value */
if (!$cs.validate(value_new, model[params.name].valid))
throw _cs.exception("value", "model field \"" + params.name + "\" receives " +
"new value " + _cs.json(value_new) + ", which does not validate " +
"against validation \"" + model[params.name].valid + "\"");
/* send event to observers for value set operation and allow observers
to reject value set operation and/or change new value to set */
var cont = true;
if (owner.property({ name: "ComponentJS:model:subscribers:set", bubbling: false }) === true) {
ev = owner.publish({
name: "ComponentJS:model:" + params.name + ":set",
args: [ value_new, value_old ],
capturing: false,
spreading: false,
bubbling: false,
async: false
});
if (!ev.processing())
cont = false;
else {
/* allow value to be overridden */
result = ev.result();
if (typeof result !== "undefined")
value_new = result;
}
}
if (cont && !model[params.name].autoreset) {
/* set value in model */
model[params.name].value = value_new;
/* synchronize model with underlying store */
if (model[params.name].store) {
var store = owner.store("model");
store[params.name] = model[params.name].value;
owner.store("model", store);
}
/* send event to observers after value finally changed */
if (owner.property({ name: "ComponentJS:model:subscribers:changed", bubbling: false }) === true) {
owner.publish({
name: "ComponentJS:model:" + params.name + ":changed",
args: [ value_new, value_old ],
noresult: true,
capturing: false,
spreading: false,
bubbling: false,
async: true
});
}
}
}
/* return old model value */
return (params.returnowner ? owner : value_old);
},
/* touch a model value and trigger event */
touch: function () {
/* determine parameters */
var params = $cs.params("touch", arguments, {
name: { pos: 0, req: true }
});
/* simply force value to same value in order to trigger event */
this.value({
name: params.name,
value: this.value(params.name),
force: true
});
},
/* start observing model value change */
observe: function () {
/* determine parameters */
var params = $cs.params("observe", arguments, {
name: { pos: 0, req: true },
func: { pos: 1, req: true },
touch: { def: false },
operation: { def: "set" },
spool: { def: null }
});
/* determine the actual component owning the model
as we want to subscribe the change event there only */
var owner = null;
var model = null;
var comp = this;
while (comp !== null) {
owner = comp.property({ name: "ComponentJS:model", returnowner: true });
if (!_cs.isdefined(owner))
throw _cs.exception("observe", "no model found containing value \"" + params.name + "\"");
model = owner.property("ComponentJS:model");
if (_cs.isdefined(model[params.name]))
break;
comp = owner.parent();
}
if (comp === null)
throw _cs.exception("observe", "no model found containing value \"" + params.name + "\"");
/* subscribe to model value change event */
var id = owner.subscribe({
name: "ComponentJS:model:" + params.name + ":" + params.operation,
capturing: false,
spreading: false,
bubbling: false,
func: params.func
});
/* mark component for having subscribers of operation
(for performance optimization reasons) */
owner.property("ComponentJS:model:subscribers:" + params.operation, true);
/* optionally spool reverse operation */
if (params.spool !== null) {
var info = _cs.spool_spec_parse(this, params.spool);
info.comp.spool(info.name, this, "unobserve", id);
}
/* if requested, touch the model value once (for an initial observer run) */
if (params.touch)
this.touch(params.name);
return id;
},
/* stop observing model value change */
unobserve: function () {
/* determine parameters */
var params = $cs.params("unobserve", arguments, {
id: { pos: 0, req: true }
});
/* determine the actual component owning the model
as we want to unsubscribe the change event there only */
var owner = null;
var comp = this;
while (comp !== null) {
owner = comp.property({ name: "ComponentJS:model", returnowner: true });
if (!_cs.isdefined(owner))
throw _cs.exception("unobserve", "no model subscription found");
if (owner.subscription(params.id))
break;
comp = owner.parent();
}
if (comp === null)
throw _cs.exception("unobserve", "no model subscription found");
/* subscribe to model value change event */
owner.unsubscribe(params.id);
}
}
});