janus
Version:
the two-faced application library-framework
124 lines (107 loc) • 3.97 kB
JavaScript
// Generated by CoffeeScript 1.12.2
(function() {
var DerivedList, FlatMappedList, List, MappedList, 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;
MappedList = (function(superClass) {
extend(MappedList, superClass);
function MappedList(parent, mapper, options) {
var elem, i, len, ref1;
this.parent = parent;
this.mapper = mapper;
this.options = options != null ? options : {};
MappedList.__super__.constructor.call(this);
this.destroyWith(this.parent);
ref1 = this.parent.list;
for (i = 0, len = ref1.length; i < len; i++) {
elem = ref1[i];
this._add(elem);
}
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(_, idx) {
return _this._removeAt(idx);
};
})(this));
}
MappedList.prototype._add = function(elem, idx) {
MappedList.__super__._add.call(this, this.mapper(elem), idx);
};
return MappedList;
})(DerivedList);
FlatMappedList = (function(superClass) {
extend(FlatMappedList, superClass);
function FlatMappedList(parent, mapper, options) {
var elem, i, idx, len, ref1;
this.parent = parent;
this.mapper = mapper;
this.options = options != null ? options : {};
FlatMappedList.__super__.constructor.call(this);
this._bindings = new List();
ref1 = this.parent.list;
for (idx = i = 0, len = ref1.length; i < len; idx = ++i) {
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, 'removed', (function(_this) {
return function(_, idx) {
return _this._removeAt(idx);
};
})(this));
this.listenTo(this.parent, 'moved', (function(_this) {
return function(_, idx, oldIdx) {
return _this._moveAt(oldIdx, idx);
};
})(this));
}
FlatMappedList.prototype._add = function(elem, idx) {
var binding, initial, mapping, wrapped;
wrapped = new Varying(elem);
initial = null;
mapping = wrapped.flatMap(this.mapper);
binding = this.reactTo(mapping, (function(_this) {
return function(newValue) {
var bidx;
if (initial == null) {
initial = newValue;
}
bidx = _this._bindings.list.indexOf(binding);
if (bidx >= 0) {
return _this._set(bidx, newValue);
}
};
})(this));
this._bindings.add(binding, idx);
FlatMappedList.__super__._add.call(this, [initial], idx);
};
FlatMappedList.prototype._removeAt = function(idx) {
this._bindings.removeAt(idx).stop();
FlatMappedList.__super__._removeAt.call(this, idx);
};
FlatMappedList.prototype._moveAt = function(oldIdx, idx) {
this._bindings.moveAt(oldIdx, idx);
FlatMappedList.__super__._moveAt.call(this, oldIdx, idx);
};
return FlatMappedList;
})(DerivedList);
module.exports = {
MappedList: MappedList,
FlatMappedList: FlatMappedList
};
}).call(this);