123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- var _stack = [];
- function seq(element1, element2, etc) {
- return new _seq(arguments);
- }
- function box(element1, element2, etc) {
- return new _box(arguments);
- }
- function lazy(f) {
- return new _lazy(f);
- }
- function data(prop1, prop2, etc) {
- var args = arguments;
- return lazy(function(model) {
- Array.forEach(args, function(p) { model = model[p]; });
- return model;
- });
- }
- function outerdata(prop1, prop2, etc) {
- var args = arguments;
- return lazy(function(model, parent) {
- Array.forEach(args, function(p) { parent = parent[p]; });
- return parent;
- });
- }
- function sub(valueModel, box) {
- return lazy(function() { return box.given(valueModel.valueOf()); });
- }
- function iter(valueModel, box) {
- return lazy(function() {
- var result = seq();
- valueModel.valueOf().forEach(function(element) {
- result.push(lazy(function() { return box.given(element); }));
- });
- return result;
- });
- }
- function _seq(content) {
- this.push.apply(this, content);
- }
- _seq.prototype = object(Array.prototype);
- _seq.prototype.toHTML = function() { return this.map(toHTML).join(""); };
- _seq.prototype.toString = function() { return this.toHTML(); };
- function _box(content) {
- _seq.apply(this, arguments);
- }
- _box.prototype = object(_seq.prototype);
- _box.prototype.model = false;
- _box.prototype.toHTML = function() {
- if (!this.model) { return ""; }
- try { _stack.unshift(this.model); return this.map(toHTML).join(""); }
- finally { _stack.shift(); }
- };
- _box.prototype.given = function(model) {
-
- this.model = model;
- return this;
- };
- function _lazy(f) {
- this.f = f;
- }
- _lazy.prototype.valueOf = function() {
- return this.f.apply(null, _stack);
- };
- _lazy.prototype.toHTML = function() { return toHTML(this.valueOf()); };
- _lazy.prototype.toString = function() { return this.valueOf().toString(); };
- import("lib-app/liveui");
- print(
- box(
- "<div>", data("hello"), "</div> <-- div tags should be written",
- HR(),
- sub(data("innerwld"), box(
- "<div>", outerdata("hello"), ", ", data("foo"), "</div> <-- here as well"
- )),
- UL(iter(data("squares"), box(
- LI(data("n")," squared is ", data("n2"))
- )))
- ).given({
- hello: "hello",
- innerwld: {foo: "world"},
- squares: [1,2,3,4,5].map(function(x) { return { n:x, n2:x*x }; })
- })
- );
|