/*! demo-x - v0.1.6 - 2014-12-04 * http://esha.github.io/demo-x/ * Copyright (c) 2014 ESHA Research; Licensed MIT */ (function(window, D) { "use strict"; var DemoXProto, DemoX; if (D.registerElement) { DemoXProto = Object.create(HTMLElement.prototype); DemoX = {}; // wait to register until all is ready } else { DemoXProto = {}; DemoX = window.DemoX = function DemoX(el) { if (!el.createdCallback) { for (var prop in DemoXProto) { Object.defineProperty(el, prop, Object.getOwnPropertyDescriptor(DemoXProto, prop)); } el.createdCallback(); } }; DemoX.prototype = DemoXProto; DemoX.load = function() { D.queryAll('demo-x').each(DemoX); }; DemoX.load();// early availability D.addEventListener('DOMContentLoaded', DemoX.load);// eventual consistency } DemoXProto.timing = { intent: 1000, backspace: 25, comment: 10, code: 50, tick: 250, minTicks: 8 }; DemoXProto.createdCallback = function() { var self = this; self.display = self.query('demo-dom'); self.input = self.query('demo-in'); self.output = self.query('demo-out'); self.intent(self.input); self._exec = function() { self.execute(); }; self.input.setAttribute('style', 'white-space: pre;'); if (self.input.children.length) { self.initStory(); } if (self.display) { self.doc = DemoX.docify(self.display.children); for (var i=0; i' + log; if (result instanceof Error) { console.error(result); } } else { console.log(code); console.log(result); } } }; DemoXProto.animate = function(text, next, update, finish) { var i = text.length, self = this, action = 'code'; (function _step() { if (!self.stopped) { if (next.indexOf(text) < 0) { action = 'backspace'; text = text.substr(0, --i); } else if (i < next.length) { action = 'code'; text = next.substr(0, ++i); } else { return finish(); } if (text.indexOf('\n') < text.indexOf('//') || text.indexOf('*/') < text.indexOf('/*')) { action = 'comment'; } update(text); setTimeout(_step, self.timing[action]); } })(); }; DemoXProto.index = 0; DemoX.docify = function(dom) { var d = D.createDocumentFragment(); d.html = d.documentElement = D.createElement('html'); d.appendChild(d.html); d.html.appendChild(d.body = D.createElement('body')); dom.each(function(el) { el.remove(); d.body.append(el); }); d.html.dot(); try { delete d.parentNode; d.parentNode = window; } catch (e) {} return d; }; DemoX.describe = function(el) { if (D.x._.isList(el) && el.each) { return el.each(DemoX.describe).join(', '); } if (el instanceof HTMLElement) { var id = el.getAttribute('id'), classes = el.getAttribute('class'); return '<'+ el.tagName.toLowerCase()+ (id ? '#'+id : '')+ (classes ? '.'+classes.split(' ').join('.') : '')+ '>'; } if (el instanceof Node) { return el.value; } if (typeof el === "object") { if (el instanceof Error) { return 'Error: "'+el.message + (el.code ? '" from "'+el.code : '') + '"'; } return JSON.stringify(el); } return el && el.value || (el+''); }; // this all hitches on css animations and domx-stringify's _attr support DemoX.highlight = function(el) { if (el.setAttribute) { el.setAttribute('_highlight', 'true'); } }; DemoX.unhighlight = function(el) { if (el.removeAttribute) { el.removeAttribute('_highlight'); } }; var flashTimeout; DemoX.flash = function(el) { if (el && el.each) { if (flashTimeout){ clearTimeout(flashTimeout); } flashTimeout = setTimeout(function() { el.each(DemoX.highlight); setTimeout(function() { el.each(DemoX.unhighlight); }, 1000); }, DemoX.flash.time || 100); } }; // register only after everything is ready if (D.registerElement) { var utils = DemoX; DemoX = window.DemoX = D.registerElement('demo-x', { prototype: DemoXProto }); for (var prop in utils) { DemoX[prop] = utils[prop]; } } })(window, document);