d2-ui
Version:
205 lines (172 loc) • 8.83 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var _ModelCollection2 = require('./ModelCollection');
var _ModelCollection3 = _interopRequireDefault(_ModelCollection2);
/**
* @class ModelCollectionProperty
*
* @description
* A ModelCollectionProperty instance is a ModelCollection that is a property of
* a model instance. ModelCollectionProperties can be operated on independently of
* the Model that owns them.
*/
var ModelCollectionProperty = (function (_ModelCollection) {
_inherits(ModelCollectionProperty, _ModelCollection);
/**
* @constructor
*
* @param {Model} parentModel The `Model` of the parent of this `ModelCollectionProperty`
* @param {ModelDefinition} modelDefinition The `ModelDefinition` that this `ModelCollection` property is for
* @param {Model[]} values Initial values that should be added to the collection property
*
* @description
*
* Creates a new `ModelCollectionProperty` object. This is a subclass of `ModelCollection`, which adds logic
* for handling adding and removing elements to the collection and saving the changes to the API.
*/
function ModelCollectionProperty(parentModel, modelDefinition, values) {
_classCallCheck(this, ModelCollectionProperty);
_get(Object.getPrototypeOf(ModelCollectionProperty.prototype), 'constructor', this).call(this, modelDefinition, values, undefined);
// Dirty bit
this.dirty = false;
// Keep track of added and removed elements
this.added = new Set();
this.removed = new Set();
// Store the parent model of this collection so we can construct the URI for API calls
this.parentModel = parentModel;
}
/**
* @method add
*
* @param {Model} value Model instance to add to the collection.
* @returns {ModelCollectionProperty} Returns itself for chaining purposes.
*
* @description
* Calls the `add` method on the parent `ModelCollection` class, and then performs checks to keep track of
* what, if any, changes that have been made to the collection.
*/
_createClass(ModelCollectionProperty, [{
key: 'add',
value: function add(value) {
// TODO: Allow adding plain ID's that aren't Model instances maybe?
if (this.valuesContainerMap.has(value.id)) {
return this;
}
_get(Object.getPrototypeOf(ModelCollectionProperty.prototype), 'add', this).call(this, value);
if (this.removed.has(value.id)) {
this.removed['delete'](value.id);
} else {
this.added.add(value.id);
}
this.updateDirty();
return this;
}
/**
* @method remove
*
* @param {Model} value Model instance to remove from the collection.
* @returns {ModelCollectionProperty} Returns itself for chaining purposes.
*
* @description
* If the collection contains an object with the same id as the `value` parameter, that object is removed
* from the collection. Checks are then performed to keep track of what, if any, changes that have been
* made to the collection.
*/
}, {
key: 'remove',
value: function remove(value) {
// TODO: Allow removing plain ID's that aren't Model instances maybe?
_ModelCollection3['default'].throwIfContainsOtherThanModelObjects([value]);
_ModelCollection3['default'].throwIfContainsModelWithoutUid([value]);
if (this['delete'](value.id)) {
if (this.added.has(value.id)) {
this.added['delete'](value.id);
} else {
this.removed.add(value.id);
}
}
this.updateDirty();
return this;
}
/**
* @method updateDirty
*
* @returns {boolean} True if the collection has changed, false otherwise.
*
* @description
* Checks whether any changes have been made to the collection, and updates the dirty flag accordingly.
*/
}, {
key: 'updateDirty',
value: function updateDirty() {
this.dirty = this.added.size > 0 || this.removed.size > 0;
return this.dirty;
}
/**
* @method save
*
* @returns {Promise} A `Promise`
*
* @description
* If any changes have been made to the collection, these changes will be submitted to the API. The returned
* promise will resolve successfully when the changes have been saved to the API, and will be rejected if
* either the changes weren't saved or if there were no changes to save.
*/
}, {
key: 'save',
value: function save() {
var _this = this;
// TODO: Use Promise constructor and call resolve/reject as appropriate
if (!this.dirty) {
return Promise.reject('Nothing to save!');
}
var api = this.modelDefinition.api;
var queries = [];
if (this.added.size) {
Array.from(this.added).forEach(function (id) {
queries.push(api.post([_this.parentModel.href, _this.modelDefinition.plural, id].join('/')));
});
}
if (this.removed.size) {
Array.from(this.removed).forEach(function (id) {
queries.push(api['delete']([_this.parentModel.href, _this.modelDefinition.plural, id].join('/')));
});
}
return Promise.all(queries).then(function () {
_this.added = new Set();
_this.removed = new Set();
_this.updateDirty();
return Promise.resolve();
})['catch'](function (err) {
return Promise.reject('Failed to alter collection:', err);
});
}
/**
* @method create
*
* @param {Model} parentModel
* @param {ModelDefinition} modelDefinition
* @param {Model[]} values
* @returns {ModelCollectionProperty}
*
* @description
* See `ModelCollectionProperty.constructor`.
*/
}], [{
key: 'create',
value: function create(parentModel, modelDefinition, values) {
return new ModelCollectionProperty(parentModel, modelDefinition, values);
}
}]);
return ModelCollectionProperty;
})(_ModelCollection3['default']);
exports['default'] = ModelCollectionProperty;
module.exports = exports['default'];
//# sourceMappingURL=ModelCollectionProperty.js.map