lib/terminus/public/terminus.js in terminus-0.5.0 vs lib/terminus/public/terminus.js in terminus-0.6.0

- old
+ new

@@ -1,5 +1,11 @@ +Faye.random = function() { + var s = '', i = 40; + while (i--) s += Math.floor(Math.random() * 16).toString(16); + return s; +}; + Terminus = { isIE: /\bMSIE\b/.test(navigator.userAgent), connect: function(host, port) { if (this._bayeux) return; @@ -151,28 +157,28 @@ }, this); }, getAttribute: function(node, name) { return Terminus.isIE ? (node.getAttributeNode(name) || {}).nodeValue || false - : node.getAttribute(name); + : node.getAttribute(name) || false; }, - hideNodes: function(root, list) { + hideNodes: function(root, hide, list) { if (!root) return list; list = list || []; var isScript = (root.tagName || '').toLowerCase() === 'script', - isHidden = (root.style || {}).display === 'none'; + isHidden = (root.style || {}).display === 'none' || root.hidden; - if (isScript || isHidden) { + if (isScript || (hide && isHidden)) { var parent = root.parentNode, next = root.nextSibling; if (!isScript) list.push([root, parent, next]); if (parent) parent.removeChild(root); } else { var children = root.childNodes || []; for (var i = 0, n = children.length; i < n; i++) { - this.hideNodes(children[i], list); + this.hideNodes(children[i], hide, list); } } return list; }, @@ -240,14 +246,14 @@ if (!element) return callback(true); Syn.trigger('click', {}, element); - if (options.resynchronize === false) return callback(true); + if (!options.resynchronize) return callback(true); if (timeout) - Terminus.Worker._setTimeout.call(window, function() { + Terminus.Worker.timeout.call(window, function() { callback('failed to resynchronize, ajax request timed out'); }, 1000 * timeout); Terminus.Worker.callback(function() { callback(true); @@ -278,30 +284,55 @@ execute: function(expression, callback) { eval(expression); callback(true); }, - find: function(xpath, nodeId, callback) { + find_css: function(css, nodeId, callback) { var root = nodeId ? this._node(nodeId) : document; + if (!root || !root.querySelectorAll) return callback([]); + + var result = root.querySelectorAll(css), + list = []; + + for (var i = 0, n = result.length; i < n; i++) { + list[i] = Terminus.Registry.put(result[i]); + } + + callback(list); + }, + + find_xpath: function(xpath, nodeId, callback) { + var root = nodeId ? this._node(nodeId) : document; if (!root) return callback([]); var result = document.evaluate(xpath, root, null, XPathResult.ANY_TYPE, null), list = [], element; while (element = result.iterateNext()) list.push(Terminus.Registry.put(element)); - return callback(list); + callback(list); }, + is_disabled: function(nodeId, callback) { + var node = this._node(nodeId); + if (!node) return callback(null); + + while (node.tagName && node.tagName.toLowerCase() !== 'body') { + if (Terminus.getAttribute(node, 'disabled')) return callback(true); + node = node.parentNode; + } + callback(false); + }, + is_visible: function(nodeId, callback) { var node = this._node(nodeId); if (!node) return callback(null); while (node.tagName && node.tagName.toLowerCase() !== 'body') { - if (node.style.display === 'none' || node.type === 'hidden') + if (node.hidden || node.style.display === 'none' || node.type === 'hidden') return callback(false); node = node.parentNode; } callback(true); }, @@ -318,14 +349,20 @@ var field = this._node(nodeId), max = Terminus.getAttribute(field, 'maxlength'); if (!field) return callback(null); if (field.type === 'file') return callback('not_allowed'); + if (Terminus.getAttribute(field, 'readonly')) return callback(true); Syn.trigger('focus', {}, field); Syn.trigger('click', {}, field); + if (typeof value === 'number') value = value.toString(); + + if (String(field.contentEditable) === 'true') + field.innerHTML = value; + switch (typeof value) { case 'string': if (max) value = value.substr(0, parseInt(max)); field.value = value; break; @@ -341,15 +378,15 @@ var node = this._node(nodeId); if (!node) return callback(null); callback(node.tagName.toLowerCase()); }, - text: function(nodeId, callback) { + text: function(nodeId, hide, callback) { var node = this._node(nodeId); if (!node) return callback(null); - var hidden = Terminus.hideNodes(node), + var hidden = Terminus.hideNodes(node, hide), title = document.title; document.title = ''; var text = node.textContent || node.innerText || ''; @@ -359,10 +396,14 @@ text = text.replace(/^\s*|\s*$/g, '').replace(/\s+/g, ' '); callback(text); }, + title: function(callback) { + callback(document.title); + }, + trigger: function(nodeId, eventType, callback) { var node = this._node(nodeId); if (!node) return callback(null); Syn.trigger(eventType, {}, node); callback(true); @@ -432,14 +473,11 @@ if (!Terminus.isIE) this._wrapTimeouts(); }, callback: function(callback, scope) { if (this._pending === 0) { - if (this._setTimeout) - this._setTimeout.call(window, function() { callback.call(scope) }, 0); - else - setTimeout(function() { callback.call(scope) }, 0); + Terminus.Worker.timeout(function() { callback.call(scope) }, 0); } else { this._callbacks.push([callback, scope]); } }, @@ -458,10 +496,17 @@ callback[0].call(callback[1]); } this._callbacks = []; }, + timeout: function(fn, time) { + if (this._setTimeout) + this._setTimeout.call(window, fn, time); + else + setTimeout(fn, time); + }, + _wrapTimeouts: function() { var timeout = window.setTimeout, clear = window.clearTimeout, timeouts = {}, self = this; @@ -513,10 +558,10 @@ monitor = Terminus.Worker.monitor; options.complete = function() { var result; try { - result = complete.apply(this, arguments); + if (complete) result = complete.apply(this, arguments); } finally { if (monitor) Terminus.Worker.resume(); } return result; };