Skip to content

Instantly share code, notes, and snippets.

@ngot
Last active August 29, 2015 14:04
Show Gist options
  • Save ngot/f96fd68227e938a135f6 to your computer and use it in GitHub Desktop.
Save ngot/f96fd68227e938a135f6 to your computer and use it in GitHub Desktop.
dom
function check(n, fn, tm) {
var tm1 = tm || -1;
function _check() {
try {
if (fn()) return;
} catch (e) {
throw e;
return;
}
if (tm > 0 && tm1 <= 0) {
throw "timeout: " + n.name;
} else {
tm1 -= 10;
setTimeout(_check, 10);
}
}
_check();
}
function node(o, cfg) {
var n = {};
for (var k in cfg) {
if (k === 'el') {
n.el = function() {
n.name = cfg.el;
var d = o.el();
if (d) return d.query(cfg.el)[0];
};
} else {
var cfg1 = cfg[k];
n[k] = node(n, typeof(cfg1) == "object" ? cfg1 : {
el: cfg1
});
}
}
n.el.onready = function(fn, tm, checkFn) {
check(n, function() {
if (n.el()) {
if (!checkFn || checkFn(n)) {
fn();
return true;
}
}
}, tm)
};
n.el.onclose = function(fn, tm) {
check(n, function() {
if (!n.el()) {
fn();
return true;
}
}, tm);
}
n.el.click = function(tm, fn) {
n.el.onready(function() {
n.el().click();
if (fn) fn();
}, tm)
}
n.el.fill = function(d, fn) {
n.el.onready(function() {
for (var k in d) {
var el = n.el();
var el1 = el.child(k);
el1.val(d[k]);
el1.trigger("focus");
el1.trigger("blur");
}
if (fn) fn();
})
};
n.el.save = function(o, fn) {
var self = this;
n.el.onready(function() {
if (typeof(o) == "function") fn = o;
else self.fill(o);
n.el().child("save").click();
self.onclose(fn);
})
};
return n;
}
var root = {
el: function() {
return opener.jModo(opener.document);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment