UNPKG

janus

Version:

the two-faced application library-framework

118 lines (103 loc) 3.44 kB
// Generated by CoffeeScript 1.12.2 (function() { var DerivedList, TakenList, Varying, max, min, 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; min = Math.min, max = Math.max; DerivedList = require('../list').DerivedList; Varying = require('../../core/varying').Varying; TakenList = (function(superClass) { extend(TakenList, superClass); function TakenList(parent, number1) { var elem, i, idx, len, ref, take; this.parent = parent; this.number = number1; TakenList.__super__.constructor.call(this); this.number = Varying.of(this.number); this.reactTo(this.number, false, (function(_this) { return function() { return _this._rebalance(); }; })(this)); take = this._take(); ref = this.parent.list; for (idx = i = 0, len = ref.length; i < len; idx = ++i) { elem = ref[idx]; if (idx < take) { 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(elem, idx, oldIdx) { return _this._moveElem(elem, oldIdx, idx); }; })(this)); this.listenTo(this.parent, 'removed', (function(_this) { return function(_, idx) { return _this._removeAt(idx); }; })(this)); } TakenList.prototype._take = function() { var number; number = this.number.get(); if (number < 0) { return max(0, this.parent.list.length + number); } else { return min(this.number.get(), this.parent.list.length); } }; TakenList.prototype._add = function(elem, idx, take) { if (take == null) { take = this._take(); } if (idx < take) { TakenList.__super__._add.call(this, [elem], idx); } if (this.list.length > take) { this._removeAt(take); } }; TakenList.prototype._moveElem = function(elem, oldIdx, newIdx) { var take; take = this._take(); if (oldIdx < take) { if (newIdx < take) { this._moveAt(oldIdx, newIdx); } else { this._removeAt(oldIdx); } } else if (newIdx < take) { this._add(elem, newIdx); } }; TakenList.prototype._removeAt = function(idx) { TakenList.__super__._removeAt.call(this, idx); if (this.list.length < this._take()) { this._add(this.parent.list[this.list.length], this.list.length); } }; TakenList.prototype._rebalance = function() { var take; take = this._take(); if (this.list.length < take) { while (this.list.length !== take) { this._add(this.parent.at_(this.list.length), this.list.length); } } else if (this.list.length > take) { while (this.list.length !== take) { this._removeAt(take); } } }; return TakenList; })(DerivedList); module.exports = { TakenList: TakenList }; }).call(this);