janus
Version:
the two-faced application library-framework
222 lines (193 loc) • 6.21 kB
JavaScript
// Generated by CoffeeScript 1.12.2
(function() {
var Base, DomView, List, View, identity,
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;
View = require('./view').View;
List = require('../collection/list').List;
identity = require('../util/util').identity;
DomView = (function(superClass) {
extend(DomView, superClass);
function DomView(subject, options) {
this.subject = subject;
this.options = options != null ? options : {};
DomView.__super__.constructor.call(this, this.subject, this.options);
}
DomView.prototype.markup = function() {
var node;
return ((function() {
var i, len, ref, results;
ref = this.artifact();
results = [];
for (i = 0, len = ref.length; i < len; i++) {
node = ref[i];
results.push(node.outerHTML);
}
return results;
}).call(this)).join('');
};
DomView.prototype._render = function() {
var dom;
dom = this.dom();
this._bindings = this.preboundTemplate(dom, this.pointer());
this.emit('bound');
return dom;
};
DomView.prototype.attach = function(dom) {
this._artifact = dom;
this._attach(dom);
return dom;
};
DomView.prototype._attach = function(dom) {
this._bindings = this.preboundTemplate(dom, this.pointer(), false);
this.emit('bound');
};
DomView.prototype.wireEvents = function() {
var binding, dom, i, len, ref;
if (this._wired === true) {
return;
}
this._wired = true;
dom = this.artifact();
if (typeof dom.data === "function") {
dom.data('view', this);
}
this._wireEvents();
if (this._bindings != null) {
ref = this._bindings;
for (i = 0, len = ref.length; i < len; i++) {
binding = ref[i];
if (binding.start != null) {
binding.start();
}
}
this._subwires = (function() {
var j, len1, ref1, results;
ref1 = this._bindings;
results = [];
for (j = 0, len1 = ref1.length; j < len1; j++) {
binding = ref1[j];
if (binding.view != null) {
results.push(this.reactTo(binding.view, function(view) {
return view != null ? view.wireEvents() : void 0;
}));
}
}
return results;
}).call(this);
}
};
DomView.prototype._wireEvents = function() {};
DomView.prototype.subviews = function() {
return (this.subviews$ != null ? this.subviews$ : this.subviews$ = Base.managed((function(_this) {
return function() {
var populate, subviews;
subviews = new List();
populate = function() {
var binding;
return subviews.add((function() {
var i, len, ref, results;
ref = this._bindings;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
binding = ref[i];
if (binding.view != null) {
results.push(binding.view);
}
}
return results;
}).call(_this));
};
if (_this._bindings != null) {
populate();
} else {
_this.on('bound', populate);
}
return subviews.flatMap(identity).filter(function(x) {
return x != null;
});
};
})(this)))();
};
DomView.prototype.subviews_ = function() {
var binding, view;
if (this._bindings == null) {
return [];
}
return (function() {
var i, len, ref, ref1, results;
ref = this._bindings;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
binding = ref[i];
if ((view = (ref1 = binding.view) != null ? ref1.get() : void 0) != null) {
results.push(view);
}
}
return results;
}).call(this);
};
DomView.prototype.__destroy = function() {
var base, binding, i, j, len, len1, ref, ref1, ref2, ref3, subwire;
if (this._bindings != null) {
ref = this._bindings;
for (i = 0, len = ref.length; i < len; i++) {
binding = ref[i];
if ((ref1 = binding.view) != null) {
if ((ref2 = ref1.get()) != null) {
ref2.destroy();
}
}
binding.stop();
}
}
if (this._subwires != null) {
ref3 = this._subwires;
for (j = 0, len1 = ref3.length; j < len1; j++) {
subwire = ref3[j];
subwire.stop();
}
}
if (this._artifact != null) {
if (typeof (base = this._artifact).trigger === "function") {
base.trigger('destroying');
}
this._artifact.remove();
} else {
this._artifact = '';
}
};
DomView.build = function(x, y, z) {
var fragment, template, viewModelClass;
if (arguments.length === 3) {
viewModelClass = x;
fragment = y;
template = z;
} else {
fragment = x;
template = y;
}
return (function(superClass1) {
extend(_Class, superClass1);
function _Class() {
return _Class.__super__.constructor.apply(this, arguments);
}
_Class.prototype.dom = function() {
return fragment.clone();
};
_Class.prototype.preboundTemplate = template(fragment);
if (viewModelClass != null) {
_Class.viewModelClass = viewModelClass;
}
_Class.fragment = fragment;
_Class.template = template;
return _Class;
})(this);
};
return DomView;
})(View);
module.exports = {
DomView: DomView
};
}).call(this);