first commit
This commit is contained in:
79
js/tinymce/classes/data/Binding.js
Normal file
79
js/tinymce/classes/data/Binding.js
Normal file
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Binding.js
|
||||
*
|
||||
* Released under LGPL License.
|
||||
* Copyright (c) 1999-2015 Ephox Corp. All rights reserved
|
||||
*
|
||||
* License: http://www.tinymce.com/license
|
||||
* Contributing: http://www.tinymce.com/contributing
|
||||
*/
|
||||
|
||||
/**
|
||||
* This class gets dynamically extended to provide a binding between two models. This makes it possible to
|
||||
* sync the state of two properties in two models by a layer of abstraction.
|
||||
*
|
||||
* @private
|
||||
* @class tinymce.data.Binding
|
||||
*/
|
||||
define("tinymce/data/Binding", [], function() {
|
||||
/**
|
||||
* Constructs a new bidning.
|
||||
*
|
||||
* @constructor
|
||||
* @method Binding
|
||||
* @param {Object} settings Settings to the binding.
|
||||
*/
|
||||
function Binding(settings) {
|
||||
this.create = settings.create;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a binding for a property on a model.
|
||||
*
|
||||
* @method create
|
||||
* @param {tinymce.data.ObservableObject} model Model to create binding to.
|
||||
* @param {String} name Name of property to bind.
|
||||
* @return {tinymce.data.Binding} Binding instance.
|
||||
*/
|
||||
Binding.create = function(model, name) {
|
||||
return new Binding({
|
||||
create: function(otherModel, otherName) {
|
||||
var bindings;
|
||||
|
||||
function fromSelfToOther(e) {
|
||||
otherModel.set(otherName, e.value);
|
||||
}
|
||||
|
||||
function fromOtherToSelf(e) {
|
||||
model.set(name, e.value);
|
||||
}
|
||||
|
||||
otherModel.on('change:' + otherName, fromOtherToSelf);
|
||||
model.on('change:' + name, fromSelfToOther);
|
||||
|
||||
// Keep track of the bindings
|
||||
bindings = otherModel._bindings;
|
||||
|
||||
if (!bindings) {
|
||||
bindings = otherModel._bindings = [];
|
||||
|
||||
otherModel.on('destroy', function() {
|
||||
var i = bindings.length;
|
||||
|
||||
while (i--) {
|
||||
bindings[i]();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
bindings.push(function() {
|
||||
model.off('change:' + name, fromSelfToOther);
|
||||
});
|
||||
|
||||
return model.get(name);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return Binding;
|
||||
});
|
||||
196
js/tinymce/classes/data/ObservableArray.js
Normal file
196
js/tinymce/classes/data/ObservableArray.js
Normal file
@@ -0,0 +1,196 @@
|
||||
/**
|
||||
* ObservableArray.js
|
||||
*
|
||||
* Released under LGPL License.
|
||||
* Copyright (c) 1999-2015 Ephox Corp. All rights reserved
|
||||
*
|
||||
* License: http://www.tinymce.com/license
|
||||
* Contributing: http://www.tinymce.com/contributing
|
||||
*/
|
||||
|
||||
/**
|
||||
* This class is an array that emmits events when mutation occurs.
|
||||
*
|
||||
* @private
|
||||
* @class tinymce.data.ObservableArray
|
||||
*/
|
||||
define("tinymce/data/ObservableArray", [
|
||||
"tinymce/util/Observable",
|
||||
"tinymce/util/Class"
|
||||
], function(Observable, Class) {
|
||||
var push = Array.prototype.push, slice = Array.prototype.slice, splice = Array.prototype.splice;
|
||||
|
||||
var ObservableArray = Class.extend({
|
||||
Mixins: [Observable],
|
||||
|
||||
/**
|
||||
* Number of items in array.
|
||||
*
|
||||
* @field length
|
||||
* @type Number
|
||||
*/
|
||||
length: 0,
|
||||
|
||||
/**
|
||||
* Constructs a new observable object instance.
|
||||
*
|
||||
* @constructor
|
||||
* @param {Object} data Optional initial data for the object.
|
||||
*/
|
||||
init: function(data) {
|
||||
if (data) {
|
||||
this.push.apply(this, data);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Adds items to the end of array.
|
||||
*
|
||||
* @method push
|
||||
* @param {Object} item... Item or items to add to the end of array.
|
||||
* @return {Number} Number of items that got added.
|
||||
*/
|
||||
push: function() {
|
||||
var args, index = this.length;
|
||||
|
||||
args = Array.prototype.slice.call(arguments);
|
||||
push.apply(this, args);
|
||||
|
||||
this.fire('add', {
|
||||
items: args,
|
||||
index: index
|
||||
});
|
||||
|
||||
return args.length;
|
||||
},
|
||||
|
||||
/**
|
||||
* Pops the last item off the array.
|
||||
*
|
||||
* @method pop
|
||||
* @return {Object} Item that got popped out.
|
||||
*/
|
||||
pop: function() {
|
||||
return this.splice(this.length - 1, 1)[0];
|
||||
},
|
||||
|
||||
/**
|
||||
* Slices out a portion of the array as a new array.
|
||||
*
|
||||
* @method slice
|
||||
* @param {Number} begin Beginning of slice.
|
||||
* @param {Number} end End of slice.
|
||||
* @return {Array} Native array instance with items.
|
||||
*/
|
||||
slice: function(begin, end) {
|
||||
return slice.call(this, begin, end);
|
||||
},
|
||||
|
||||
/**
|
||||
* Removes/replaces/inserts items in the array.
|
||||
*
|
||||
* @method splice
|
||||
* @param {Number} index Index to splice at.
|
||||
* @param {Number} howMany Optional number of items to splice away.
|
||||
* @param {Object} item ... Item or items to insert at the specified index.
|
||||
*/
|
||||
splice: function(index) {
|
||||
var added, removed, args = slice.call(arguments);
|
||||
|
||||
if (args.length === 1) {
|
||||
args[1] = this.length;
|
||||
}
|
||||
|
||||
removed = splice.apply(this, args);
|
||||
added = args.slice(2);
|
||||
|
||||
if (removed.length > 0) {
|
||||
this.fire('remove', {items: removed, index: index});
|
||||
}
|
||||
|
||||
if (added.length > 0) {
|
||||
this.fire('add', {items: added, index: index});
|
||||
}
|
||||
|
||||
return removed;
|
||||
},
|
||||
|
||||
/**
|
||||
* Removes and returns the first item of the array.
|
||||
*
|
||||
* @method shift
|
||||
* @return {Object} First item of the array.
|
||||
*/
|
||||
shift: function() {
|
||||
return this.splice(0, 1)[0];
|
||||
},
|
||||
|
||||
/**
|
||||
* Appends an item to the top of array.
|
||||
*
|
||||
* @method unshift
|
||||
* @param {Object} item... Item or items to prepend to array.
|
||||
* @return {Number} Number of items that got added.
|
||||
*/
|
||||
unshift: function() {
|
||||
var args = slice.call(arguments);
|
||||
this.splice.apply(this, [0, 0].concat(args));
|
||||
return args.length;
|
||||
},
|
||||
|
||||
/**
|
||||
* Executes the callback for each item in the array.
|
||||
*
|
||||
* @method forEach
|
||||
* @param {function} callback Callback to execute for each item in array.
|
||||
* @param {Object} scope Optional scope for this when executing the callback.
|
||||
*/
|
||||
forEach: function(callback, scope) {
|
||||
var i;
|
||||
|
||||
scope = scope || this;
|
||||
for (i = 0; i < this.length; i++) {
|
||||
callback.call(scope, this[i], i, this);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the index of the specified item or -1 if it wasn't found.
|
||||
*
|
||||
* @method indexOf
|
||||
* @return {Number} Index of item or null if it wasn't found.
|
||||
*/
|
||||
indexOf: function(item) {
|
||||
for (var i = 0; i < this.length; i++) {
|
||||
if (this[i] === item) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
},
|
||||
|
||||
/**
|
||||
* Filters the observable array into a new observable array
|
||||
* based on the true/false return value of the specified callback.
|
||||
*
|
||||
* @method filter
|
||||
* @param {function} callback Callback function to execute for each item and filter by.
|
||||
* @param {Object} thisArg Optional scope for this when executing the callback.
|
||||
* @return {tinymce.data.ObservableArray} Filtered observable array instance.
|
||||
*/
|
||||
filter: function(callback, thisArg) {
|
||||
var self = this, out = new ObservableArray();
|
||||
|
||||
this.forEach(function(item, index) {
|
||||
if (callback.call(thisArg || self, item, index, self)) {
|
||||
out.push(item);
|
||||
}
|
||||
});
|
||||
|
||||
return out;
|
||||
}
|
||||
});
|
||||
|
||||
return ObservableArray;
|
||||
});
|
||||
194
js/tinymce/classes/data/ObservableObject.js
Normal file
194
js/tinymce/classes/data/ObservableObject.js
Normal file
@@ -0,0 +1,194 @@
|
||||
/**
|
||||
* ObservableObject.js
|
||||
*
|
||||
* Released under LGPL License.
|
||||
* Copyright (c) 1999-2015 Ephox Corp. All rights reserved
|
||||
*
|
||||
* License: http://www.tinymce.com/license
|
||||
* Contributing: http://www.tinymce.com/contributing
|
||||
*/
|
||||
|
||||
/**
|
||||
* This class is a object that is observable when properties changes a change event gets emitted.
|
||||
*
|
||||
* @private
|
||||
* @class tinymce.data.ObservableObject
|
||||
*/
|
||||
define("tinymce/data/ObservableObject", [
|
||||
"tinymce/data/Binding",
|
||||
"tinymce/util/Observable",
|
||||
"tinymce/util/Class",
|
||||
"tinymce/util/Tools"
|
||||
], function(Binding, Observable, Class, Tools) {
|
||||
function isNode(node) {
|
||||
return node.nodeType > 0;
|
||||
}
|
||||
|
||||
// Todo: Maybe this should be shallow compare since it might be huge object references
|
||||
function isEqual(a, b) {
|
||||
var k, checked;
|
||||
|
||||
// Strict equals
|
||||
if (a === b) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Compare null
|
||||
if (a === null || b === null) {
|
||||
return a === b;
|
||||
}
|
||||
|
||||
// Compare number, boolean, string, undefined
|
||||
if (typeof a !== "object" || typeof b !== "object") {
|
||||
return a === b;
|
||||
}
|
||||
|
||||
// Compare arrays
|
||||
if (Tools.isArray(b)) {
|
||||
if (a.length !== b.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
k = a.length;
|
||||
while (k--) {
|
||||
if (!isEqual(a[k], b[k])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Shallow compare nodes
|
||||
if (isNode(a) || isNode(b)) {
|
||||
return a === b;
|
||||
}
|
||||
|
||||
// Compare objects
|
||||
checked = {};
|
||||
for (k in b) {
|
||||
if (!isEqual(a[k], b[k])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
checked[k] = true;
|
||||
}
|
||||
|
||||
for (k in a) {
|
||||
if (!checked[k] && !isEqual(a[k], b[k])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return Class.extend({
|
||||
Mixins: [Observable],
|
||||
|
||||
/**
|
||||
* Constructs a new observable object instance.
|
||||
*
|
||||
* @constructor
|
||||
* @param {Object} data Initial data for the object.
|
||||
*/
|
||||
init: function(data) {
|
||||
var name, value;
|
||||
|
||||
data = data || {};
|
||||
|
||||
for (name in data) {
|
||||
value = data[name];
|
||||
|
||||
if (value instanceof Binding) {
|
||||
data[name] = value.create(this, name);
|
||||
}
|
||||
}
|
||||
|
||||
this.data = data;
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets a property on the value this will call
|
||||
* observers if the value is a change from the current value.
|
||||
*
|
||||
* @method set
|
||||
* @param {String/object} name Name of the property to set or a object of items to set.
|
||||
* @param {Object} value Value to set for the property.
|
||||
* @return {tinymce.data.ObservableObject} Observable object instance.
|
||||
*/
|
||||
set: function(name, value) {
|
||||
var key, args, oldValue = this.data[name];
|
||||
|
||||
if (value instanceof Binding) {
|
||||
value = value.create(this, name);
|
||||
}
|
||||
|
||||
if (typeof name === "object") {
|
||||
for (key in name) {
|
||||
this.set(key, name[key]);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
if (!isEqual(oldValue, value)) {
|
||||
this.data[name] = value;
|
||||
|
||||
args = {
|
||||
target: this,
|
||||
name: name,
|
||||
value: value,
|
||||
oldValue: oldValue
|
||||
};
|
||||
|
||||
this.fire('change:' + name, args);
|
||||
this.fire('change', args);
|
||||
}
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets a property by name.
|
||||
*
|
||||
* @method get
|
||||
* @param {String} name Name of the property to get.
|
||||
* @return {Object} Object value of propery.
|
||||
*/
|
||||
get: function(name) {
|
||||
return this.data[name];
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns true/false if the specified property exists.
|
||||
*
|
||||
* @method has
|
||||
* @param {String} name Name of the property to check for.
|
||||
* @return {Boolean} true/false if the item exists.
|
||||
*/
|
||||
has: function(name) {
|
||||
return name in this.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns a dynamic property binding for the specified property name. This makes
|
||||
* it possible to sync the state of two properties in two ObservableObject instances.
|
||||
*
|
||||
* @method bind
|
||||
* @param {String} name Name of the property to sync with the property it's inserted to.
|
||||
* @return {tinymce.data.Binding} Data binding instance.
|
||||
*/
|
||||
bind: function(name) {
|
||||
return Binding.create(this, name);
|
||||
},
|
||||
|
||||
/**
|
||||
* Destroys the observable object and fires the "destroy"
|
||||
* event and clean up any internal resources.
|
||||
*
|
||||
* @method destroy
|
||||
*/
|
||||
destroy: function() {
|
||||
this.fire('destroy');
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user