-
Notifications
You must be signed in to change notification settings - Fork 0
/
jot.min.js
1 lines (1 loc) · 3.07 KB
/
jot.min.js
1
(function(root){var Jot={};if(typeof module!=="undefined"&&module.exports)module.exports=Jot;else root.Jot=Jot;var Task=Jot.Task=function(t){this.context=t||{};this.props=this.props||{i:undefined,o:undefined,into:null,outo:null};return this};Task.prototype.i=function(t){if(typeof t==="undefined")return this.props.i;if(typeof t==="function"){this.props.i=t;this.props.iString=cleanInput(t)}else if(typeof t==="string"){var e=this.eval;this.props.iString=t;this.props.i=function(){return e(t)}}return this};Task.prototype.iString=function(){return this.props.iString};Task.prototype.o=function(t){if(typeof t==="undefined")return this.props.o;this.props.o=t;return this};Task.prototype.into=function(t){if(typeof t==="undefined")return this.props.into;this.props.into=t;return this};Task.prototype.outo=function(t){if(typeof t==="undefined")return this.props.outo;this.props.outo=t;return this};Task.prototype.eval=function(str){return eval(str)};Task.prototype.invade=function(t){var e=getElement(t);if(e){var n=document.createElement("code");var r=document.createElement("code");var i=document.createElement("pre");var o=document.createElement("pre");e.appendChild(i).appendChild(n);e.appendChild(o).appendChild(r);this.into(n).outo(r)}return this};Task.prototype.render=function(){var t=this.iString(),e=this.o(),n=this.into(),r=this.outo();if(r)r.innerHTML="";if(typeof t!=="undefined"&&n)n.innerHTML=t;if(typeof e!=="undefined"&&r){if(e instanceof Element){r.appendChild(e)}else{if(e instanceof Error)e=e.toString();else if(!detectTags(e))e=JSON.stringify(e);r.innerHTML=e}}return this};Task.prototype.run=function(t){if(t)this.i(t);var e=this.i();if(e){var n=tryCatch(e,this.context);if(n&&(n instanceof Promise||n.then)){return n.then(this.o.bind(this)).then(this.render.bind(this))}this.o(n)}this.render();return this};Task.prototype.reset=function(){this.props.o=undefined;return this};var TaskSet=Jot.TaskSet=function(){this.context={};this.tasks=[];return this};TaskSet.prototype.addTask=function(t){Jot.Task.call(t,this.context);this.tasks.push(t);return this};TaskSet.prototype.createTask=function(t,e){var n=new Task(this.context);if(!e)n.invade(t);else n.into(t).outo(e);this.tasks.push(n);return n};TaskSet.prototype.render=function(){this.tasks.forEach(function(t){t.render()});return this};TaskSet.prototype.run=function(){this.tasks.forEach(function(t){t.run()});return this};TaskSet.prototype.reset=function(){this.tasks.forEach(function(t){t.reset()});return this};function getElement(t){if(t instanceof Element)return t;if(t instanceof Array)return t[0];if(typeof t==="string")return document.querySelector(t)}function detectTags(t){var e=false;if(typeof t==="string")e=escapeTags(t).length!==t.length;return e}function escapeTags(t){t=t.replace(/</g,"<");t=t.replace(/>/g,">");return t}function cleanInput(t){var e=/^function[A-z\s]*\([\s]*\)[\s]*\{\n*/,n=/[\n\s]*\}$/,r=/^[\s]+/,i=escapeTags(t.toString());i=i.replace(e,"");i=i.replace(n,"");var o=i.match(r);if(o){var s=new RegExp("^"+o[0],"gm");i=i.replace(s,"")}return i}function tryCatch(t,e){try{var n=t.call(e);return n}catch(r){return r}}return})(this);