janus
Version:
the two-faced application library-framework
97 lines (81 loc) • 2.97 kB
JavaScript
// Generated by CoffeeScript 1.12.2
(function() {
var Base, IndexOfFold, Varying,
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;
Varying = require('../../core/varying').Varying;
Base = require('../../core/base').Base;
IndexOfFold = (function(superClass) {
extend(IndexOfFold, superClass);
function IndexOfFold(list, x) {
IndexOfFold.__super__.constructor.call(this);
this._list = list;
this._varying = new Varying(-1);
this.listenTo(this._list, 'added', (function(_this) {
return function(obj, idx) {
return _this._addObj(obj, idx);
};
})(this));
this.listenTo(this._list, 'moved', (function(_this) {
return function(obj, idx, oldIdx) {
return _this._moveObj(obj, idx, oldIdx);
};
})(this));
this.listenTo(this._list, 'removed', (function(_this) {
return function(obj, idx) {
return _this._removeObj(obj, idx);
};
})(this));
this._targetObservation = Varying.of(x).react((function(_this) {
return function(value) {
return _this._handleValue(value);
};
})(this));
}
IndexOfFold.prototype._handleValue = function(value) {
this._value = value;
this._set(this._list.list.indexOf(value));
};
IndexOfFold.prototype._addObj = function(obj, idx) {
if ((obj === this._value) && ((idx < this._lastIdx) || (this._lastIdx === -1))) {
this._set(idx);
} else if (idx <= this._lastIdx) {
this._set(this._lastIdx + 1);
}
};
IndexOfFold.prototype._moveObj = function(obj, idx, oldIdx) {
if (oldIdx === this._lastIdx) {
this._set(idx);
} else if ((idx <= this._lastIdx) && (oldIdx > this._lastIdx)) {
this._set(this._lastIdx + 1);
} else if ((idx >= this._lastIdx) && (oldIdx < this._lastIdx)) {
this._set(this._lastIdx - 1);
}
};
IndexOfFold.prototype._removeObj = function(obj, idx) {
if (idx === this._lastIdx) {
this._set(this._list.list.indexOf(this._value));
} else if (idx < this._lastIdx) {
this._set(this._lastIdx - 1);
}
};
IndexOfFold.prototype._set = function(idx) {
this._lastIdx = idx;
this._varying.set(idx);
};
IndexOfFold.prototype.__destroy = function() {
this._targetObservation.stop();
};
IndexOfFold.indexOf = function(list, x) {
return Varying.managed((function() {
return new IndexOfFold(list, x);
}), (function(iof) {
return iof._varying;
}));
};
return IndexOfFold;
})(Base);
module.exports = {
IndexOfFold: IndexOfFold
};
}).call(this);