123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- import defaultValue from "../Core/defaultValue.js";
- import defined from "../Core/defined.js";
- import DeveloperError from "../Core/DeveloperError.js";
- import Event from "../Core/Event.js";
- import ConstantProperty from "./ConstantProperty.js";
- import createPropertyDescriptor from "./createPropertyDescriptor.js";
- import Property from "./Property.js";
- function PropertyBag(value, createPropertyCallback) {
- this._propertyNames = [];
- this._definitionChanged = new Event();
- if (defined(value)) {
- this.merge(value, createPropertyCallback);
- }
- }
- Object.defineProperties(PropertyBag.prototype, {
-
- propertyNames: {
- get: function () {
- return this._propertyNames;
- },
- },
-
- isConstant: {
- get: function () {
- var propertyNames = this._propertyNames;
- for (var i = 0, len = propertyNames.length; i < len; i++) {
- if (!Property.isConstant(this[propertyNames[i]])) {
- return false;
- }
- }
- return true;
- },
- },
-
- definitionChanged: {
- get: function () {
- return this._definitionChanged;
- },
- },
- });
- PropertyBag.prototype.hasProperty = function (propertyName) {
- return this._propertyNames.indexOf(propertyName) !== -1;
- };
- function createConstantProperty(value) {
- return new ConstantProperty(value);
- }
- PropertyBag.prototype.addProperty = function (
- propertyName,
- value,
- createPropertyCallback
- ) {
- var propertyNames = this._propertyNames;
-
- if (!defined(propertyName)) {
- throw new DeveloperError("propertyName is required.");
- }
- if (propertyNames.indexOf(propertyName) !== -1) {
- throw new DeveloperError(
- propertyName + " is already a registered property."
- );
- }
-
- propertyNames.push(propertyName);
- Object.defineProperty(
- this,
- propertyName,
- createPropertyDescriptor(
- propertyName,
- true,
- defaultValue(createPropertyCallback, createConstantProperty)
- )
- );
- if (defined(value)) {
- this[propertyName] = value;
- }
- this._definitionChanged.raiseEvent(this);
- };
- PropertyBag.prototype.removeProperty = function (propertyName) {
- var propertyNames = this._propertyNames;
- var index = propertyNames.indexOf(propertyName);
-
- if (!defined(propertyName)) {
- throw new DeveloperError("propertyName is required.");
- }
- if (index === -1) {
- throw new DeveloperError(propertyName + " is not a registered property.");
- }
-
- this._propertyNames.splice(index, 1);
- delete this[propertyName];
- this._definitionChanged.raiseEvent(this);
- };
- PropertyBag.prototype.getValue = function (time, result) {
-
- if (!defined(time)) {
- throw new DeveloperError("time is required.");
- }
-
- if (!defined(result)) {
- result = {};
- }
- var propertyNames = this._propertyNames;
- for (var i = 0, len = propertyNames.length; i < len; i++) {
- var propertyName = propertyNames[i];
- result[propertyName] = Property.getValueOrUndefined(
- this[propertyName],
- time,
- result[propertyName]
- );
- }
- return result;
- };
- PropertyBag.prototype.merge = function (source, createPropertyCallback) {
-
- if (!defined(source)) {
- throw new DeveloperError("source is required.");
- }
-
- var propertyNames = this._propertyNames;
- var sourcePropertyNames = defined(source._propertyNames)
- ? source._propertyNames
- : Object.keys(source);
- for (var i = 0, len = sourcePropertyNames.length; i < len; i++) {
- var name = sourcePropertyNames[i];
- var targetProperty = this[name];
- var sourceProperty = source[name];
-
- if (targetProperty === undefined && propertyNames.indexOf(name) === -1) {
- this.addProperty(name, undefined, createPropertyCallback);
- }
- if (sourceProperty !== undefined) {
- if (targetProperty !== undefined) {
- if (defined(targetProperty) && defined(targetProperty.merge)) {
- targetProperty.merge(sourceProperty);
- }
- } else if (
- defined(sourceProperty) &&
- defined(sourceProperty.merge) &&
- defined(sourceProperty.clone)
- ) {
- this[name] = sourceProperty.clone();
- } else {
- this[name] = sourceProperty;
- }
- }
- }
- };
- function propertiesEqual(a, b) {
- var aPropertyNames = a._propertyNames;
- var bPropertyNames = b._propertyNames;
- var len = aPropertyNames.length;
- if (len !== bPropertyNames.length) {
- return false;
- }
- for (var aIndex = 0; aIndex < len; ++aIndex) {
- var name = aPropertyNames[aIndex];
- var bIndex = bPropertyNames.indexOf(name);
- if (bIndex === -1) {
- return false;
- }
- if (!Property.equals(a[name], b[name])) {
- return false;
- }
- }
- return true;
- }
- PropertyBag.prototype.equals = function (other) {
- return (
- this === other ||
- (other instanceof PropertyBag &&
- propertiesEqual(this, other))
- );
- };
- export default PropertyBag;
|