aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/datasource/jsdatasource.js
blob: 78d8e06bc69bec582dc85b65796c78d320cc8aac (plain)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Copyright 2006 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview An implementation of DataNode for wrapping JS data.
 *
 */


goog.provide('goog.ds.JsDataSource');
goog.provide('goog.ds.JsPropertyDataSource');

goog.require('goog.ds.BaseDataNode');
goog.require('goog.ds.BasicNodeList');
goog.require('goog.ds.DataManager');
goog.require('goog.ds.EmptyNodeList');
goog.require('goog.ds.LoadState');


/**
 * Data source whose backing is JavaScript data
 *
 * Names that are reserved for system use and shouldn't be used for data node
 * names: eval, toSource, toString, unwatch, valueOf, watch. Behavior is
 * undefined if these names are used.
 *
 * @param {Object} root The root JS node.
 * @param {string} dataName The name of this node relative to the parent node.
 * @param {Object=} opt_parent Optional parent of this JsDataSource.
 *
 * implements goog.ds.DataNode.
 * @constructor
 * @extends {goog.ds.DataNode}
 */
// TODO(arv): Use interfaces when available.
goog.ds.JsDataSource = function(root, dataName, opt_parent) {
  this.parent_ = opt_parent;
  this.dataName_ = dataName;
  this.setRoot(root);
};


/**
 * The root JS object. Can be null.
 * @type {*}
 * @protected
 * @suppress {underscore}
 */
goog.ds.JsDataSource.prototype.root_;


/**
 * Sets the root JS object
 * @param {Object} root The root JS object. Can be null.
 *
 * @protected
 */
goog.ds.JsDataSource.prototype.setRoot = function(root) {
  this.root_ = root;
  this.childNodeList_ = null;
};


/**
 * Set this data source to use list semantics. List data sources:
 * - Are assumed to have child nodes of all of the same type of data
 * - Fire data changes on the root node of the list whenever children
 *     are added or removed
 * @param {?boolean} isList True to use list semantics.
 * @private
 */
goog.ds.JsDataSource.prototype.setIsList_ = function(isList) {
  this.isList_ = isList;
};


/** @override */
goog.ds.JsDataSource.prototype.get = function() {
  return !goog.isObject(this.root_) ? this.root_ : this.getChildNodes();
};


/**
 * Set the value of the node
 * @param {*} value The new value of the node.
 * @override
 */
goog.ds.JsDataSource.prototype.set = function(value) {
  if (value && goog.isObject(this.root_)) {
    throw Error('Can\'t set group nodes to new values yet');
  }

  if (this.parent_) {
    this.parent_.root_[this.dataName_] = value;
  }
  this.root_ = value;
  this.childNodeList_ = null;

  goog.ds.DataManager.getInstance().fireDataChange(this.getDataPath());
};


/**
 * TODO(user) revisit lazy creation.
 * @override
 */
goog.ds.JsDataSource.prototype.getChildNodes = function(opt_selector) {
  if (!this.root_) {
    return new goog.ds.EmptyNodeList();
  }

  if (!opt_selector || opt_selector == goog.ds.STR_ALL_CHILDREN_SELECTOR) {
    this.createChildNodes_(false);
    return this.childNodeList_;
  } else if (opt_selector.indexOf(goog.ds.STR_WILDCARD) == -1) {
    if (this.root_[opt_selector] != null) {
      return new goog.ds.BasicNodeList([this.getChildNode(opt_selector)]);
    } else {
      return new goog.ds.EmptyNodeList();
    }
  } else {
    throw Error('Selector not supported yet (' + opt_selector + ')');
  }

};


/**
 * Creates the DataNodeList with the child nodes for this element.
 * Allows for only building list as needed.
 *
 * @param {boolean=} opt_force Whether to force recreating child nodes,
 *     defaults to false.
 * @private
 */
goog.ds.JsDataSource.prototype.createChildNodes_ = function(opt_force) {
  if (this.childNodeList_ && !opt_force) {
    return;
  }

  if (!goog.isObject(this.root_)) {
    this.childNodeList_ = new goog.ds.EmptyNodeList();
    return;
  }

  var childNodeList = new goog.ds.BasicNodeList();
  var newNode;
  if (goog.isArray(this.root_)) {
    var len = this.root_.length;
    for (var i = 0; i < len; i++) {
      // "id" is reserved node name that will map to a named child node
      // TODO(user) Configurable logic for choosing id node
      var node = this.root_[i];
      var id = node.id;
      var name = id != null ? String(id) : '[' + i + ']';
      newNode = new goog.ds.JsDataSource(node, name, this);
      childNodeList.add(newNode);
    }
  } else {
    for (var name in this.root_) {
      var obj = this.root_[name];
      // If the node is already a datasource, then add it.
      if (obj.getDataName) {
        childNodeList.add(obj);
      } else if (!goog.isFunction(obj)) {
        newNode = new goog.ds.JsDataSource(obj, name, this);
        childNodeList.add(newNode);
      }
    }
  }
  this.childNodeList_ = childNodeList;
};


/**
 * Gets a named child node of the current node
 * @param {string} name The node name.
 * @param {boolean=} opt_canCreate If true, can create child node.
 * @return {goog.ds.DataNode} The child node, or null if no node of
 *     this name exists.
 * @override
 */
goog.ds.JsDataSource.prototype.getChildNode = function(name, opt_canCreate) {
  if (!this.root_) {
    return null;
  }
  var node = /** @type {goog.ds.DataNode} */ (this.getChildNodes().get(name));
  if (!node && opt_canCreate) {
    var newObj = {};
    if (goog.isArray(this.root_)) {
      newObj['id'] = name;
      this.root_.push(newObj);
    } else {
      this.root_[name] = newObj;
    }
    node = new goog.ds.JsDataSource(newObj, name, this);
    if (this.childNodeList_) {
      this.childNodeList_.add(node);
    }
  }
  return node;
};


/**
 * Gets the value of a child node
 * @param {string} name The node name.
 * @return {Object} The value of the node, or null if no value or the child
 *    node doesn't exist.
 * @override
 */
goog.ds.JsDataSource.prototype.getChildNodeValue = function(name) {
  if (this.childNodeList_) {
    var node = this.getChildNodes().get(name);
    return node ? node.get() : null;
  } else if (this.root_) {
    return this.root_[name];
  } else {
    return null;
  }
};


/**
 * Sets a named child node of the current node.
 * If value is null, removes the child node.
 * @param {string} name The node name.
 * @param {Object} value The value to set, can be DataNode, object,
 *     property, or null.
 * @return {Object} The child node, if set.
 * @override
 */
goog.ds.JsDataSource.prototype.setChildNode = function(name, value) {
  var removedPath = null;
  var node = null;
  var addedNode = false;

  // Set node to the DataNode to add - if the value isn't already a DataNode,
  // creates a JsDataSource or JsPropertyDataSource wrapper
  if (value != null) {
    if (value.getDataName) {
      // The value is a DataNode. We must update its parent.
      node = value;
      node.parent_ = this;
    } else {
      if (goog.isArray(value) || goog.isObject(value)) {
        node = new goog.ds.JsDataSource(value, name, this);
      } else {
        node = new goog.ds.JsPropertyDataSource(
            /** @type {goog.ds.DataNode} */ (this.root_), name, this);
      }
    }
  }

  // This logic will get cleaner once we can remove the backing array / object
  // and just rely on the childNodeList_. This is needed until dependent code
  // is cleaned up.
  // TODO(user) Remove backing array / object and just use childNodeList_

  if (goog.isArray(this.root_)) {
    // To remove by name, need to create a map of the child nodes by ID
    this.createChildNodes_();
    var index = this.childNodeList_.indexOf(name);
    if (value == null) {
      // Remove the node
      var nodeToRemove = this.childNodeList_.get(name);
      if (nodeToRemove) {
        removedPath = nodeToRemove.getDataPath();
      }
      this.root_.splice(index, 1);
    } else {
      // Add the node
      if (index) {
        this.root_[index] = value;
      } else {
        this.root_.push(value);
      }
    }
    if (index == null) {
      addedNode = true;
    }
    this.childNodeList_.setNode(name, /** @type {goog.ds.DataNode} */ (node));
  } else if (goog.isObject(this.root_)) {
    if (value == null) {
      // Remove the node
      this.createChildNodes_();
      var nodeToRemove = this.childNodeList_.get(name);
      if (nodeToRemove) {
        removedPath = nodeToRemove.getDataPath();
      }
      delete this.root_[name];
    } else {
      // Add the node
      if (!this.root_[name]) {
        addedNode = true;
      }
      this.root_[name] = value;
    }
    // Only need to update childNodeList_ if has been created already
    if (this.childNodeList_) {
      this.childNodeList_.setNode(name, /** @type {goog.ds.DataNode} */ (node));
    }
  }

  // Fire the event that the node changed
  var dm = goog.ds.DataManager.getInstance();
  if (node) {
    dm.fireDataChange(node.getDataPath());
    if (addedNode && this.isList()) {
      dm.fireDataChange(this.getDataPath());
      dm.fireDataChange(this.getDataPath() + '/count()');
    }
  } else if (removedPath) {
    dm.fireDataChange(removedPath);
    if (this.isList()) {
      dm.fireDataChange(this.getDataPath());
      dm.fireDataChange(this.getDataPath() + '/count()');
    }
  }
  return node;
};


/**
 * Get the name of the node relative to the parent node
 * @return {string} The name of the node.
 * @override
 */
goog.ds.JsDataSource.prototype.getDataName = function() {
  return this.dataName_;
};


/**
 * Setthe name of the node relative to the parent node
 * @param {string} dataName The name of the node.
 * @override
 */
goog.ds.JsDataSource.prototype.setDataName = function(dataName) {
  this.dataName_ = dataName;
};


/**
 * Gets the a qualified data path to this node
 * @return {string} The data path.
 * @override
 */
goog.ds.JsDataSource.prototype.getDataPath = function() {
  var parentPath = '';
  if (this.parent_) {
    parentPath = this.parent_.getDataPath() + goog.ds.STR_PATH_SEPARATOR;
  }

  return parentPath + this.dataName_;
};


/**
 * Load or reload the backing data for this node
 * @override
 */
goog.ds.JsDataSource.prototype.load = function() {
  // Nothing to do
};


/**
 * Gets the state of the backing data for this node
 * TODO(user) Discuss null value handling
 * @return {goog.ds.LoadState} The state.
 * @override
 */
goog.ds.JsDataSource.prototype.getLoadState = function() {
  return (this.root_ == null) ? goog.ds.LoadState.NOT_LOADED :
      goog.ds.LoadState.LOADED;
};


/**
 * Whether the value of this node is a homogeneous list of data
 * @return {boolean} True if a list.
 * @override
 */
goog.ds.JsDataSource.prototype.isList = function() {
  return this.isList_ != null ? this.isList_ : goog.isArray(this.root_);
};



/**
 * Data source for JavaScript properties that arent objects. Contains reference
 * to parent object so that you can set the vaule
 *
 * @param {goog.ds.DataNode} parent Parent object.
 * @param {string} dataName Name of this property.
 * @param {goog.ds.DataNode=} opt_parentDataNode The parent data node. If
 *     omitted, assumes that the parent object is the parent data node.
 *
 * @constructor
 * @extends {goog.ds.BaseDataNode}
 */
goog.ds.JsPropertyDataSource = function(parent, dataName, opt_parentDataNode) {
  goog.ds.BaseDataNode.call(this);
  this.dataName_ = dataName;
  this.parent_ = parent;
  this.parentDataNode_ = opt_parentDataNode || this.parent_;
};
goog.inherits(goog.ds.JsPropertyDataSource, goog.ds.BaseDataNode);


/**
 * Get the value of the node
 * @return {Object} The value of the node, or null if no value.
 */
goog.ds.JsPropertyDataSource.prototype.get = function() {
  return this.parent_[this.dataName_];
};


/**
 * Set the value of the node
 * @param {Object} value The new value of the node.
 * @override
 */
goog.ds.JsPropertyDataSource.prototype.set = function(value) {
  var oldValue = this.parent_[this.dataName_];
  this.parent_[this.dataName_] = value;

  if (oldValue != value) {
    goog.ds.DataManager.getInstance().fireDataChange(this.getDataPath());
  }
};


/**
 * Get the name of the node relative to the parent node
 * @return {string} The name of the node.
 * @override
 */
goog.ds.JsPropertyDataSource.prototype.getDataName = function() {
  return this.dataName_;
};


/** @override */
goog.ds.JsPropertyDataSource.prototype.getParent = function() {
  return this.parentDataNode_;
};