janus
Version:
the two-faced application library-framework
449 lines (391 loc) • 11.6 kB
JavaScript
// Generated by CoffeeScript 1.12.2
(function() {
var Base, DerivedList, List, Sequence, Varying, _added, _moved, _removed, util,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
Base = require('../core/base').Base;
Varying = require('../core/varying').Varying;
Sequence = require('./collection').Sequence;
util = require('../util/util');
_added = function(list, midx, value) {
var _, idx, length, ref, ref1, ref2, reverseThreshold, v;
length = list.length_;
reverseThreshold = midx - length;
ref = list._watches;
for (_ in ref) {
ref1 = ref[_], v = ref1.v, idx = ref1.idx;
if (idx === midx) {
v.set(value);
} else if (idx < 0) {
if (idx >= reverseThreshold) {
v.set(list.list[length + idx]);
}
} else {
if (idx >= midx) {
v.set(list.list[idx]);
}
}
}
if ((ref2 = list.length$) != null) {
ref2.set(length);
}
};
_moved = function(list, oldIdx, newIdx, value) {
var _, cidx, idx, length, ref, ref1, v;
length = list.length_;
ref = list._watches;
for (_ in ref) {
ref1 = ref[_], v = ref1.v, idx = ref1.idx;
cidx = idx < 0 ? length + idx : idx;
if (cidx === newIdx) {
v.set(value);
} else if ((cidx === oldIdx) || (cidx > oldIdx && cidx < newIdx) || (cidx < oldIdx && cidx > newIdx)) {
v.set(list.list[cidx]);
}
}
};
_removed = function(list, midx) {
var _, idx, length, ref, ref1, ref2, reverseThreshold, v;
length = list.length_;
reverseThreshold = midx - length - 1;
ref = list._watches;
for (_ in ref) {
ref1 = ref[_], v = ref1.v, idx = ref1.idx;
if (idx < 0) {
if (idx >= reverseThreshold) {
v.set(list.list[length + idx]);
}
} else {
if (idx >= midx) {
v.set(list.list[idx]);
}
}
}
if ((ref2 = list.length$) != null) {
ref2.set(length);
}
};
List = (function(superClass) {
var _at, _at_, _removeAt;
extend(List, superClass);
List.prototype.isList = true;
function List(list, options) {
if (list == null) {
list = [];
}
if ((options != null ? options.parent : void 0) != null) {
this._parent = options.parent;
}
List.__super__.constructor.call(this, {}, options);
this._watches = {};
if (typeof this._initialize === "function") {
this._initialize();
}
this.list = [];
this.add(list);
}
List.prototype.add = function(elems, idx) {
var elem, i, iidx, len, subidx;
if (idx == null) {
idx = this.list.length;
}
if (!util.isArray(elems)) {
elems = [elems];
}
if (idx < 0) {
idx = this.list.length + idx;
}
if (idx === this.list.length && elems.length === 1) {
this.list.push(elems[0]);
} else {
if (idx > this.list.length) {
this.list[idx - 1] = null;
delete this.list[idx - 1];
}
Array.prototype.splice.apply(this.list, [idx, 0].concat(elems));
}
for (subidx = i = 0, len = elems.length; i < len; subidx = ++i) {
elem = elems[subidx];
iidx = idx + subidx;
this.emit('added', elem, iidx);
if (elem != null) {
if (typeof elem.emit === "function") {
elem.emit('addedTo', this, iidx);
}
}
_added(this, iidx, elem);
if (util.isFunction(elem != null ? elem.destroy : void 0) && (this.isDerivedList !== true)) {
(function(_this) {
return (function(elem) {
return _this.listenTo(elem, 'destroying', function() {
return _this.remove(elem);
});
});
})(this)(elem);
}
}
};
List.prototype.set = function(idx, value) {
var removed;
if (arguments.length === 1) {
return ((function(_this) {
return function(v) {
return _this.set(idx, v);
};
})(this));
}
if (idx < 0) {
idx = this.length_ + idx;
}
if (0 <= idx && idx < this.length_) {
removed = this.list[idx];
this.emit('removed', removed, idx);
if (removed != null) {
if (typeof removed.emit === "function") {
removed.emit('removedFrom', this, idx);
}
}
_removed(this, idx);
}
this.list[idx] = value;
this.emit('added', value, idx);
if (value != null) {
if (typeof value.emit === "function") {
value.emit('addedTo', this, idx);
}
}
_added(this, idx, value);
};
List.prototype.remove = function(which) {
var idx;
idx = this.list.indexOf(which);
if (!(idx >= 0)) {
return void 0;
}
return this.removeAt(idx);
};
_removeAt = function(idx) {
var removed;
if (idx < 0) {
idx = this.list.length + idx;
}
if (idx < 0 || idx >= this.list.length) {
return;
}
removed = idx === 0 ? this.list.shift() : idx === this.list.length - 1 ? this.list.pop() : this.list.splice(idx, 1)[0];
this.emit('removed', removed, idx);
if (removed != null) {
if (typeof removed.emit === "function") {
removed.emit('removedFrom', this, idx);
}
}
_removed(this, idx);
return removed;
};
List.prototype.removeAt = _removeAt;
List.prototype.unset = _removeAt;
List.prototype.move = function(elem, idx) {
var oldIdx;
oldIdx = this.list.indexOf(elem);
if (!(oldIdx >= 0)) {
return;
}
return this.moveAt(oldIdx, idx);
};
List.prototype.moveAt = function(oldIdx, idx) {
var elem;
if (oldIdx < 0) {
oldIdx = this.length_ + oldIdx;
}
if (idx < 0) {
idx = this.length_ + idx;
}
elem = this.list[oldIdx];
this.list.splice(oldIdx, 1);
this.list.splice(idx, 0, elem);
_moved(this, oldIdx, idx, elem);
this.emit('moved', elem, idx, oldIdx);
if (elem != null) {
if (typeof elem.emit === "function") {
elem.emit('movedIn', this, idx, oldIdx);
}
}
return elem;
};
List.prototype.removeAll = function() {
var elem, length, results;
length = this.list.length;
results = [];
while ((length-- !== 0) && (this.list.length > 0)) {
elem = this.list.shift();
this.emit('removed', elem, 0);
if (elem != null) {
if (typeof elem.emit === "function") {
elem.emit('removedFrom', this, 0);
}
}
_removed(this, 0);
results.push(elem);
}
return results;
};
_at_ = function(idx) {
if (idx >= 0) {
return this.list[idx];
} else {
return this.list[this.list.length + idx];
}
};
List.prototype.at_ = _at_;
List.prototype.get_ = _at_;
_at = function(idx) {
var obj, v;
if ((idx != null ? idx.isVarying : void 0) === true) {
return idx.flatMap((function(_this) {
return function(tidx) {
return _this.at(tidx);
};
})(this));
}
if ((obj = this._watches[idx]) != null) {
return obj.v;
} else {
v = new Varying(this.at_(idx));
v.__owner = this;
this._watches[idx] = {
idx: idx,
v: v
};
return v;
}
};
List.prototype.at = _at;
List.prototype.get = _at;
Object.defineProperty(List.prototype, 'length', {
get: function() {
var l;
if ((l = this.length$)) {
return l;
} else {
this.length$ = new Varying(this.list.length);
this.length$.__owner = this;
return this.length$;
}
}
});
Object.defineProperty(List.prototype, 'length_', {
get: function() {
return this.list.length;
}
});
List.prototype.empty_ = function() {
return this.length_ === 0;
};
List.prototype.empty = function() {
return this.length.map(function(length) {
return length === 0;
});
};
List.prototype.nonEmpty_ = function() {
return this.length_ > 0;
};
List.prototype.nonEmpty = function() {
return this.length.map(function(length) {
return length > 0;
});
};
List.prototype.values_ = function() {
return this.list.slice();
};
List.prototype.values = function() {
return this;
};
List.prototype.shadow = function() {
var item, newArray;
newArray = (function() {
var i, len, ref, results;
ref = this.list;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
item = ref[i];
if ((item != null ? item.isEnumerable : void 0) === true) {
results.push(item.shadow());
} else {
results.push(item);
}
}
return results;
}).call(this);
return new this.constructor(newArray, {
parent: this
});
};
if (typeof Symbol !== 'undefined') {
List.prototype[Symbol.iterator] = function() {
return this.list[Symbol.iterator]();
};
}
List.prototype.__destroy = function() {
return this._watches = null;
};
List.deserialize = function(data) {
var datum, items;
items = (function() {
var i, len, results;
if ((this.prototype.modelClass != null) && util.isFunction(this.prototype.modelClass.deserialize)) {
results = [];
for (i = 0, len = data.length; i < len; i++) {
datum = data[i];
results.push(this.prototype.modelClass.deserialize(datum));
}
return results;
} else {
return data;
}
}).call(this);
return new this(items);
};
List.of = function(modelClass) {
return (function(superClass1) {
extend(_Class, superClass1);
function _Class() {
return _Class.__super__.constructor.apply(this, arguments);
}
_Class.prototype.modelClass = modelClass;
return _Class;
})(this);
};
return List;
})(Sequence);
DerivedList = (function(superClass) {
var i, len, method, ref, roError;
extend(DerivedList, superClass);
DerivedList.prototype.isDerivedList = true;
function DerivedList() {
Sequence.call(this);
this.list = [];
this._watches = [];
if (typeof this._initialize === "function") {
this._initialize();
}
}
roError = function() {
throw new Error('this list is read-only');
};
ref = ['add', 'remove', 'removeAt', 'removeAll', 'set', 'move', 'moveAt'];
for (i = 0, len = ref.length; i < len; i++) {
method = ref[i];
DerivedList.prototype["_" + method] = DerivedList.__super__[method];
DerivedList.prototype[method] = roError;
}
DerivedList.prototype.shadow = function() {
return this;
};
return DerivedList;
})(List);
List.Derived = DerivedList;
module.exports = {
List: List,
DerivedList: DerivedList
};
}).call(this);