From 5ab2bd08e1b491e789fd553be991ac8432d5ed75 Mon Sep 17 00:00:00 2001 From: Rob Eisenberg Date: Thu, 30 Apr 2015 20:16:27 -0400 Subject: [PATCH] chore(git): add missed files --- dist/commonjs/element-observation.js | 436 ++++++++++++++++++++++++++ dist/commonjs/map-change-records.js | 33 ++ dist/commonjs/property-observation.js | 303 ++++++++++++++++++ 3 files changed, 772 insertions(+) create mode 100644 dist/commonjs/element-observation.js create mode 100644 dist/commonjs/map-change-records.js create mode 100644 dist/commonjs/property-observation.js diff --git a/dist/commonjs/element-observation.js b/dist/commonjs/element-observation.js new file mode 100644 index 00000000..2647ddca --- /dev/null +++ b/dist/commonjs/element-observation.js @@ -0,0 +1,436 @@ +'use strict'; + +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }; + +exports.__esModule = true; + +var XLinkAttributeObserver = (function () { + function XLinkAttributeObserver(element, propertyName, attributeName) { + _classCallCheck(this, XLinkAttributeObserver); + + this.element = element; + this.propertyName = propertyName; + this.attributeName = attributeName; + } + + XLinkAttributeObserver.prototype.getValue = function getValue() { + return this.element.getAttributeNS('http://www.w3.org/1999/xlink', this.attributeName); + }; + + XLinkAttributeObserver.prototype.setValue = function setValue(newValue) { + return this.element.setAttributeNS('http://www.w3.org/1999/xlink', this.attributeName, newValue); + }; + + XLinkAttributeObserver.prototype.subscribe = function subscribe(callback) { + throw new Error('Observation of an Element\'s "' + this.propertyName + '" property is not supported.'); + }; + + return XLinkAttributeObserver; +})(); + +exports.XLinkAttributeObserver = XLinkAttributeObserver; + +var DataAttributeObserver = (function () { + function DataAttributeObserver(element, propertyName) { + _classCallCheck(this, DataAttributeObserver); + + this.element = element; + this.propertyName = propertyName; + } + + DataAttributeObserver.prototype.getValue = function getValue() { + return this.element.getAttribute(this.propertyName); + }; + + DataAttributeObserver.prototype.setValue = function setValue(newValue) { + return this.element.setAttribute(this.propertyName, newValue); + }; + + DataAttributeObserver.prototype.subscribe = function subscribe(callback) { + throw new Error('Observation of an Element\'s "' + this.propertyName + '" property is not supported.'); + }; + + return DataAttributeObserver; +})(); + +exports.DataAttributeObserver = DataAttributeObserver; + +var StyleObserver = (function () { + function StyleObserver(element, propertyName) { + _classCallCheck(this, StyleObserver); + + this.element = element; + this.propertyName = propertyName; + } + + StyleObserver.prototype.getValue = function getValue() { + return this.element.style.cssText; + }; + + StyleObserver.prototype.setValue = function setValue(newValue) { + if (newValue instanceof Object) { + newValue = this.flattenCss(newValue); + } + this.element.style.cssText = newValue; + }; + + StyleObserver.prototype.subscribe = function subscribe(callback) { + throw new Error('Observation of an Element\'s "' + this.propertyName + '" property is not supported.'); + }; + + StyleObserver.prototype.flattenCss = function flattenCss(object) { + var s = ''; + for (var propertyName in object) { + if (object.hasOwnProperty(propertyName)) { + s += propertyName + ': ' + object[propertyName] + '; '; + } + } + return s; + }; + + return StyleObserver; +})(); + +exports.StyleObserver = StyleObserver; + +var ValueAttributeObserver = (function () { + function ValueAttributeObserver(element, propertyName, handler) { + _classCallCheck(this, ValueAttributeObserver); + + this.element = element; + this.propertyName = propertyName; + this.handler = handler; + this.callbacks = []; + } + + ValueAttributeObserver.prototype.getValue = function getValue() { + return this.element[this.propertyName]; + }; + + ValueAttributeObserver.prototype.setValue = function setValue(newValue) { + this.element[this.propertyName] = newValue; + this.call(); + }; + + ValueAttributeObserver.prototype.call = function call() { + var callbacks = this.callbacks, + i = callbacks.length, + oldValue = this.oldValue, + newValue = this.getValue(); + + while (i--) { + callbacks[i](newValue, oldValue); + } + + this.oldValue = newValue; + }; + + ValueAttributeObserver.prototype.subscribe = function subscribe(callback) { + var that = this; + + if (!this.disposeHandler) { + this.oldValue = this.getValue(); + this.disposeHandler = this.handler.subscribe(this.element, this.call.bind(this)); + } + + this.callbacks.push(callback); + + return this.unsubscribe.bind(this, callback); + }; + + ValueAttributeObserver.prototype.unsubscribe = function unsubscribe(callback) { + var callbacks = this.callbacks; + callbacks.splice(callbacks.indexOf(callback), 1); + if (callbacks.length === 0) { + this.disposeHandler(); + this.disposeHandler = null; + } + }; + + return ValueAttributeObserver; +})(); + +exports.ValueAttributeObserver = ValueAttributeObserver; + +var SelectValueObserver = (function () { + function SelectValueObserver(element, handler, observerLocator) { + _classCallCheck(this, SelectValueObserver); + + this.element = element; + this.handler = handler; + this.observerLocator = observerLocator; + } + + SelectValueObserver.prototype.getValue = function getValue() { + return this.value; + }; + + SelectValueObserver.prototype.setValue = function setValue(newValue) { + var _this = this; + + if (newValue !== null && newValue !== undefined && this.element.multiple && !Array.isArray(newValue)) { + throw new Error('Only null or Array instances can be bound to a multi-select.'); + } + if (this.value === newValue) { + return; + } + + if (this.arraySubscription) { + this.arraySubscription(); + this.arraySubscription = null; + } + + if (Array.isArray(newValue)) { + this.arraySubscription = this.observerLocator.getArrayObserver(newValue).subscribe(this.synchronizeOptions.bind(this)); + } + + this.value = newValue; + this.synchronizeOptions(); + + if (this.element.options.length > 0 && !this.initialSync) { + this.initialSync = true; + this.observerLocator.taskQueue.queueMicroTask({ call: function call() { + return _this.synchronizeOptions(); + } }); + } + }; + + SelectValueObserver.prototype.synchronizeOptions = function synchronizeOptions() { + var value = this.value, + i, + options, + option, + optionValue, + clear, + isArray; + + if (value === null || value === undefined) { + clear = true; + } else if (Array.isArray(value)) { + isArray = true; + } + + options = this.element.options; + i = options.length; + while (i--) { + option = options.item(i); + if (clear) { + option.selected = false; + continue; + } + optionValue = option.hasOwnProperty('model') ? option.model : option.value; + if (isArray) { + option.selected = value.indexOf(optionValue) !== -1; + continue; + } + option.selected = value === optionValue; + } + }; + + SelectValueObserver.prototype.synchronizeValue = function synchronizeValue() { + var options = this.element.options, + option, + i, + ii, + count = 0, + value = []; + + for (i = 0, ii = options.length; i < ii; i++) { + option = options.item(i); + if (!option.selected) { + continue; + } + value[count] = option.hasOwnProperty('model') ? option.model : option.value; + count++; + } + + if (!this.element.multiple) { + if (count === 0) { + value = null; + } else { + value = value[0]; + } + } + + this.oldValue = this.value; + this.value = value; + this.call(); + }; + + SelectValueObserver.prototype.call = function call() { + var callbacks = this.callbacks, + i = callbacks.length, + oldValue = this.oldValue, + newValue = this.value; + + while (i--) { + callbacks[i](newValue, oldValue); + } + }; + + SelectValueObserver.prototype.subscribe = function subscribe(callback) { + if (!this.callbacks) { + this.callbacks = []; + this.disposeHandler = this.handler.subscribe(this.element, this.synchronizeValue.bind(this, false)); + } + + this.callbacks.push(callback); + return this.unsubscribe.bind(this, callback); + }; + + SelectValueObserver.prototype.unsubscribe = function unsubscribe(callback) { + var callbacks = this.callbacks; + callbacks.splice(callbacks.indexOf(callback), 1); + if (callbacks.length === 0) { + this.disposeHandler(); + this.disposeHandler = null; + this.callbacks = null; + } + }; + + SelectValueObserver.prototype.bind = function bind() { + var _this2 = this; + + this.domObserver = new MutationObserver(function () { + _this2.synchronizeOptions(); + _this2.synchronizeValue(); + }); + this.domObserver.observe(this.element, { childList: true, subtree: true }); + }; + + SelectValueObserver.prototype.unbind = function unbind() { + this.domObserver.disconnect(); + this.domObserver = null; + + if (this.arraySubscription) { + this.arraySubscription(); + this.arraySubscription = null; + } + }; + + return SelectValueObserver; +})(); + +exports.SelectValueObserver = SelectValueObserver; + +var CheckedObserver = (function () { + function CheckedObserver(element, handler, observerLocator) { + _classCallCheck(this, CheckedObserver); + + this.element = element; + this.handler = handler; + this.observerLocator = observerLocator; + } + + CheckedObserver.prototype.getValue = function getValue() { + return this.value; + }; + + CheckedObserver.prototype.setValue = function setValue(newValue) { + var _this3 = this; + + if (this.value === newValue) { + return; + } + + if (this.arraySubscription) { + this.arraySubscription(); + this.arraySubscription = null; + } + + if (this.element.type === 'checkbox' && Array.isArray(newValue)) { + this.arraySubscription = this.observerLocator.getArrayObserver(newValue).subscribe(this.synchronizeElement.bind(this)); + } + + this.value = newValue; + this.synchronizeElement(); + + if (!this.element.hasOwnProperty('model') && !this.initialSync) { + this.initialSync = true; + this.observerLocator.taskQueue.queueMicroTask({ call: function call() { + return _this3.synchronizeElement(); + } }); + } + }; + + CheckedObserver.prototype.synchronizeElement = function synchronizeElement() { + var value = this.value, + element = this.element, + elementValue = element.hasOwnProperty('model') ? element.model : element.value, + isRadio = element.type === 'radio'; + + element.checked = isRadio && value === elementValue || !isRadio && value === true || !isRadio && Array.isArray(value) && value.indexOf(elementValue) !== -1; + }; + + CheckedObserver.prototype.synchronizeValue = function synchronizeValue() { + var value = this.value, + element = this.element, + elementValue = element.hasOwnProperty('model') ? element.model : element.value, + index; + + if (element.type === 'checkbox') { + if (Array.isArray(value)) { + index = value.indexOf(elementValue); + if (element.checked && index === -1) { + value.push(elementValue); + } else if (!element.checked && index !== -1) { + value.splice(index, 1); + } + + return; + } else { + value = element.checked; + } + } else if (element.checked) { + value = elementValue; + } else { + return; + } + + this.oldValue = this.value; + this.value = value; + this.call(); + }; + + CheckedObserver.prototype.call = function call() { + var callbacks = this.callbacks, + i = callbacks.length, + oldValue = this.oldValue, + newValue = this.value; + + while (i--) { + callbacks[i](newValue, oldValue); + } + }; + + CheckedObserver.prototype.subscribe = function subscribe(callback) { + if (!this.callbacks) { + this.callbacks = []; + this.disposeHandler = this.handler.subscribe(this.element, this.synchronizeValue.bind(this, false)); + } + + this.callbacks.push(callback); + return this.unsubscribe.bind(this, callback); + }; + + CheckedObserver.prototype.unsubscribe = function unsubscribe(callback) { + var callbacks = this.callbacks; + callbacks.splice(callbacks.indexOf(callback), 1); + if (callbacks.length === 0) { + this.disposeHandler(); + this.disposeHandler = null; + this.callbacks = null; + } + }; + + CheckedObserver.prototype.unbind = function unbind() { + if (this.arraySubscription) { + this.arraySubscription(); + this.arraySubscription = null; + } + }; + + return CheckedObserver; +})(); + +exports.CheckedObserver = CheckedObserver; \ No newline at end of file diff --git a/dist/commonjs/map-change-records.js b/dist/commonjs/map-change-records.js new file mode 100644 index 00000000..55acc24e --- /dev/null +++ b/dist/commonjs/map-change-records.js @@ -0,0 +1,33 @@ +'use strict'; + +exports.__esModule = true; +exports.getChangeRecords = getChangeRecords; +function newRecord(type, object, key, oldValue) { + return { + type: type, + object: object, + key: key, + oldValue: oldValue + }; +} + +function getChangeRecords(map) { + var entries = []; + for (var _iterator = map.keys(), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { + var _ref; + + if (_isArray) { + if (_i >= _iterator.length) break; + _ref = _iterator[_i++]; + } else { + _i = _iterator.next(); + if (_i.done) break; + _ref = _i.value; + } + + var key = _ref; + + entries.push(newRecord('added', map, key)); + } + return entries; +} \ No newline at end of file diff --git a/dist/commonjs/property-observation.js b/dist/commonjs/property-observation.js new file mode 100644 index 00000000..9ec893d2 --- /dev/null +++ b/dist/commonjs/property-observation.js @@ -0,0 +1,303 @@ +'use strict'; + +var _interopRequireDefault = function (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }; + +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }; + +exports.__esModule = true; + +var _core = require('core-js'); + +var _core2 = _interopRequireDefault(_core); + +var SetterObserver = (function () { + function SetterObserver(taskQueue, obj, propertyName) { + _classCallCheck(this, SetterObserver); + + this.taskQueue = taskQueue; + this.obj = obj; + this.propertyName = propertyName; + this.callbacks = []; + this.queued = false; + this.observing = false; + } + + SetterObserver.prototype.getValue = function getValue() { + return this.obj[this.propertyName]; + }; + + SetterObserver.prototype.setValue = function setValue(newValue) { + this.obj[this.propertyName] = newValue; + }; + + SetterObserver.prototype.getterValue = function getterValue() { + return this.currentValue; + }; + + SetterObserver.prototype.setterValue = function setterValue(newValue) { + var oldValue = this.currentValue; + + if (oldValue !== newValue) { + if (!this.queued) { + this.oldValue = oldValue; + this.queued = true; + this.taskQueue.queueMicroTask(this); + } + + this.currentValue = newValue; + } + }; + + SetterObserver.prototype.call = function call() { + var callbacks = this.callbacks, + i = callbacks.length, + oldValue = this.oldValue, + newValue = this.currentValue; + + this.queued = false; + + while (i--) { + callbacks[i](newValue, oldValue); + } + }; + + SetterObserver.prototype.subscribe = function subscribe(callback) { + var callbacks = this.callbacks; + callbacks.push(callback); + + if (!this.observing) { + this.convertProperty(); + } + + return function () { + callbacks.splice(callbacks.indexOf(callback), 1); + }; + }; + + SetterObserver.prototype.convertProperty = function convertProperty() { + this.observing = true; + this.currentValue = this.obj[this.propertyName]; + this.setValue = this.setterValue; + this.getValue = this.getterValue; + + try { + Object.defineProperty(this.obj, this.propertyName, { + configurable: true, + enumerable: true, + get: this.getValue.bind(this), + set: this.setValue.bind(this) + }); + } catch (_) {} + }; + + return SetterObserver; +})(); + +exports.SetterObserver = SetterObserver; + +var OoObjectObserver = (function () { + function OoObjectObserver(obj, observerLocator) { + _classCallCheck(this, OoObjectObserver); + + this.obj = obj; + this.observers = {}; + this.observerLocator = observerLocator; + } + + OoObjectObserver.prototype.subscribe = function subscribe(propertyObserver, callback) { + var _this = this; + + var callbacks = propertyObserver.callbacks; + callbacks.push(callback); + + if (!this.observing) { + this.observing = true; + try { + Object.observe(this.obj, function (changes) { + return _this.handleChanges(changes); + }, ['update', 'add']); + } catch (_) {} + } + + return function () { + callbacks.splice(callbacks.indexOf(callback), 1); + }; + }; + + OoObjectObserver.prototype.getObserver = function getObserver(propertyName, descriptor) { + var propertyObserver = this.observers[propertyName]; + if (!propertyObserver) { + if (descriptor) { + propertyObserver = this.observers[propertyName] = new OoPropertyObserver(this, this.obj, propertyName); + } else { + propertyObserver = this.observers[propertyName] = new UndefinedPropertyObserver(this, this.obj, propertyName); + } + } + return propertyObserver; + }; + + OoObjectObserver.prototype.handleChanges = function handleChanges(changeRecords) { + var updates = {}, + observers = this.observers, + i = changeRecords.length; + + while (i--) { + var change = changeRecords[i], + name = change.name; + + if (!(name in updates)) { + var observer = observers[name]; + updates[name] = true; + if (observer) { + observer.trigger(change.object[name], change.oldValue); + } + } + } + }; + + return OoObjectObserver; +})(); + +exports.OoObjectObserver = OoObjectObserver; + +var OoPropertyObserver = (function () { + function OoPropertyObserver(owner, obj, propertyName) { + _classCallCheck(this, OoPropertyObserver); + + this.owner = owner; + this.obj = obj; + this.propertyName = propertyName; + this.callbacks = []; + } + + OoPropertyObserver.prototype.getValue = function getValue() { + return this.obj[this.propertyName]; + }; + + OoPropertyObserver.prototype.setValue = function setValue(newValue) { + this.obj[this.propertyName] = newValue; + }; + + OoPropertyObserver.prototype.trigger = function trigger(newValue, oldValue) { + var callbacks = this.callbacks, + i = callbacks.length; + + while (i--) { + callbacks[i](newValue, oldValue); + } + }; + + OoPropertyObserver.prototype.subscribe = function subscribe(callback) { + return this.owner.subscribe(this, callback); + }; + + return OoPropertyObserver; +})(); + +exports.OoPropertyObserver = OoPropertyObserver; + +var UndefinedPropertyObserver = (function () { + function UndefinedPropertyObserver(owner, obj, propertyName) { + _classCallCheck(this, UndefinedPropertyObserver); + + this.owner = owner; + this.obj = obj; + this.propertyName = propertyName; + this.callbackMap = new Map(); + this.callbacks = []; + } + + UndefinedPropertyObserver.prototype.getValue = function getValue() { + if (this.actual) { + return this.actual.getValue(); + } + return this.obj[this.propertyName]; + }; + + UndefinedPropertyObserver.prototype.setValue = function setValue(newValue) { + if (this.actual) { + this.actual.setValue(newValue); + return; + } + + this.obj[this.propertyName] = newValue; + this.trigger(newValue, undefined); + }; + + UndefinedPropertyObserver.prototype.trigger = function trigger(newValue, oldValue) { + var callback; + + if (this.subscription) { + this.subscription(); + } + + this.getObserver(); + + for (var _iterator = this.callbackMap.keys(), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { + if (_isArray) { + if (_i >= _iterator.length) break; + callback = _iterator[_i++]; + } else { + _i = _iterator.next(); + if (_i.done) break; + callback = _i.value; + } + + callback(newValue, oldValue); + } + }; + + UndefinedPropertyObserver.prototype.getObserver = function getObserver() { + var callback, observerLocator; + + if (!Object.getOwnPropertyDescriptor(this.obj, this.propertyName)) { + return; + } + + observerLocator = this.owner.observerLocator; + delete this.owner.observers[this.propertyName]; + delete observerLocator.getObserversLookup(this.obj, observerLocator)[this.propertyName]; + this.actual = observerLocator.getObserver(this.obj, this.propertyName); + + for (var _iterator2 = this.callbackMap.keys(), _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { + if (_isArray2) { + if (_i2 >= _iterator2.length) break; + callback = _iterator2[_i2++]; + } else { + _i2 = _iterator2.next(); + if (_i2.done) break; + callback = _i2.value; + } + + this.callbackMap.set(callback, this.actual.subscribe(callback)); + } + }; + + UndefinedPropertyObserver.prototype.subscribe = function subscribe(callback) { + var _this2 = this; + + if (!this.actual) { + this.getObserver(); + } + + if (this.actual) { + return this.actual.subscribe(callback); + } + + if (!this.subscription) { + this.subscription = this.owner.subscribe(this); + } + + this.callbackMap.set(callback, null); + + return function () { + var actualDispose = _this2.callbackMap.get(callback); + if (actualDispose) actualDispose(); + _this2.callbackMap['delete'](callback); + }; + }; + + return UndefinedPropertyObserver; +})(); + +exports.UndefinedPropertyObserver = UndefinedPropertyObserver; \ No newline at end of file