janus
Version:
the two-faced application library-framework
118 lines (106 loc) • 4.61 kB
JavaScript
// Generated by CoffeeScript 1.12.2
(function() {
var DerivedList, FilteredList, List, Varying, ref,
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;
ref = require('../list'), List = ref.List, DerivedList = ref.DerivedList;
Varying = require('../../core/varying').Varying;
FilteredList = (function(superClass) {
extend(FilteredList, superClass);
function FilteredList(parent, filterer1) {
var elem, idx, j, len, ref1;
this.parent = parent;
this.filterer = filterer1;
FilteredList.__super__.constructor.call(this);
this._filtereds = [];
this._idxMap = [];
ref1 = this.parent.list;
for (idx = j = 0, len = ref1.length; j < len; idx = ++j) {
elem = ref1[idx];
this._add(elem, idx);
}
this.listenTo(this.parent, 'added', (function(_this) {
return function(elem, idx) {
return _this._add(elem, idx);
};
})(this));
this.listenTo(this.parent, 'moved', (function(_this) {
return function(_, idx, oldIdx) {
return _this._moveAt(oldIdx, idx);
};
})(this));
this.listenTo(this.parent, 'removed', (function(_this) {
return function(elem, idx) {
return _this._removeAt(idx);
};
})(this));
}
FilteredList.prototype._add = function(elem, idx) {
var filtered, lastResult, ref1;
this._idxMap.splice(idx, 0, (ref1 = this._idxMap[idx - 1]) != null ? ref1 : -1);
lastResult = false;
filtered = this.reactTo(Varying.of(this.filterer(elem)), (function(_this) {
return function(result) {
var cidx, i, idxAdj, j, ref2, ref3;
result = result === true;
cidx = _this._filtereds.indexOf(filtered);
if (cidx < 0) {
cidx = idx;
}
if (result !== lastResult) {
lastResult = result === true;
idxAdj = result === true ? 1 : -1;
if (result === false) {
List.prototype.removeAt.call(_this, _this._idxMap[cidx]);
}
for (i = j = ref2 = cidx, ref3 = _this._idxMap.length; ref2 <= ref3 ? j < ref3 : j > ref3; i = ref2 <= ref3 ? ++j : --j) {
_this._idxMap[i] += idxAdj;
}
if (result === true) {
return FilteredList.__super__._add.call(_this, _this.parent.at_(cidx), _this._idxMap[cidx]);
}
}
};
})(this));
this._filtereds.splice(idx, 0, filtered);
};
FilteredList.prototype._removeAt = function(idx) {
var adjIdx, delta, j, mappedIdx, ref1, ref2;
mappedIdx = this._idxMap[idx];
delta = idx === 0 ? mappedIdx + 1 : mappedIdx - this._idxMap[idx - 1];
if (delta === 1) {
FilteredList.__super__._removeAt.call(this, mappedIdx);
for (adjIdx = j = ref1 = idx, ref2 = this._idxMap.length; ref1 <= ref2 ? j <= ref2 : j >= ref2; adjIdx = ref1 <= ref2 ? ++j : --j) {
this._idxMap[adjIdx] -= 1;
}
}
this._filtereds.splice(idx, 1)[0].stop();
this._idxMap.splice(idx, 1);
};
FilteredList.prototype._moveAt = function(oldIdx, newIdx) {
var delta, filterer, i, j, k, oldMappedIdx, ref1, ref2, ref3, ref4, ref5, ref6;
filterer = this._filtereds.splice(oldIdx, 1)[0];
this._filtereds.splice(newIdx, 0, filterer);
oldMappedIdx = this._idxMap[oldIdx];
delta = oldIdx === 0 ? this._idxMap[oldIdx] + 1 : oldMappedIdx - this._idxMap[oldIdx - 1];
if (newIdx > oldIdx) {
for (i = j = ref1 = oldIdx, ref2 = newIdx; ref1 <= ref2 ? j <= ref2 : j >= ref2; i = ref1 <= ref2 ? ++j : --j) {
this._idxMap[i] = this._idxMap[i + 1] - delta;
}
this._idxMap[newIdx] = this._idxMap[newIdx - 1] + delta;
} else if (newIdx < oldIdx) {
for (i = k = ref3 = oldIdx, ref4 = newIdx; k >= ref4; i = k += -1) {
this._idxMap[i] = ((ref5 = this._idxMap[i - 1]) != null ? ref5 : -1) + delta;
}
this._idxMap[newIdx] = ((ref6 = this._idxMap[newIdx - 1]) != null ? ref6 : -1) + delta;
}
if (delta === 1) {
FilteredList.__super__._moveAt.call(this, oldMappedIdx, this._idxMap[newIdx]);
}
};
return FilteredList;
})(DerivedList);
module.exports = {
FilteredList: FilteredList
};
}).call(this);