-
Notifications
You must be signed in to change notification settings - Fork 40
/
mobile-console.min.js
23 lines (23 loc) · 6.03 KB
/
mobile-console.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
!function(e,n){"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?module.exports=n():e.mobileConsole=n()}(this,function(){var n='<div id="jsmc-collapse"></div><div id="jsmc-clear">×</div><div id="jsmc-commands">≡</div><div id="jsmc-commands-container"></div><div id="jsmc-content">\t<input id="jsmc-button" type="button" value="Run"/>\t<div id="jsmc-log">\t</div>\t<div id="jsmc-input-container">\t\t<input id="jsmc-input" type="text" placeholder="type your js here"/>\t</div></div>',s='\t<div class="jsmc-log-text"></div>\t<div class="jsmc-log-target"></div>',e={props:{showOnError:!1,proxyConsole:!0,isCollapsed:!1,catchErrors:!0},init:function(){this.commandsHash=[],this.initialized||(this.props.catchErrors&&this.bindErrorListener(),this.initializeContainers(),this.bindListeners(),this.initialized=!0,this.props.proxyConsole?this.decorateConsole():this.undecorateConsole())},options:function(e){for(var n in e)void 0!==this.props[n]&&(this.props[n]=e[n])
this.init()},show:function(e){document.getElementById("js-mobile-console")||this.init(),this.$el.container.style.display="block",e&&e.expand&&this.toggleCollapsed(!1)},hide:function(){this.$el&&this.$el.container&&(this.$el.container.style.display="none")},commands:function(e){if("object"!=typeof e)throw new Error("mobileConsole: commands method accepts object, not "+typeof e)
for(var n in this.commandsHash=e,this.commandsHashLength=0,e)e.hasOwnProperty(n)&&this.commandsHashLength++
this.commandsHashLength&&this.populateCommandsContainer_()},populateCommandsContainer_:function(){var e,n,t,o=this
for(e in this.commandsHash)this.commandsHash.hasOwnProperty(e)&&((n=document.createElement("div")).className="jsmc-command",n.innerHTML=e,n.command=this.commandsHash[e],(t=document.createElement("div")).className="jsmc-command-wrapper",t.appendChild(n),this.$el.commandsContainer.appendChild(t))
this.commandsPopulated||this.$el.commandsContainer.addEventListener("click",function(e){var n,t
"jsmc-command"===e.target.className&&(n=e.target.command,t=o.eval(n),o.logValue(n,!1,!0),o.logValue(t.text,t.error))}),this.commandsPopulated=!0},destroy:function(){var e=document.getElementById("js-mobile-console")
e.parentNode.removeChild(e)},initializeContainers:function(e){this.$el={},el=this.$el.container=document.createElement("div"),el.id="js-mobile-console",el.innerHTML=n,el.style.display="none",document.body.appendChild(el),this.$el.input=document.getElementById("jsmc-input"),this.$el.button=document.getElementById("jsmc-button"),this.$el.log=document.getElementById("jsmc-log"),this.$el.content=document.getElementById("jsmc-content"),this.$el.collapseControl=document.getElementById("jsmc-collapse"),this.$el.clearControl=document.getElementById("jsmc-clear"),this.$el.commandsControl=document.getElementById("jsmc-commands"),this.$el.commandsContainer=document.getElementById("jsmc-commands-container"),this.props.isCollapsed?(this.$el.content.style.display="none",this.$el.clearControl.style.display="none",this.$el.commandsControl.style.display="none",this.isCollapsed=!0,this.$el.collapseControl.innerHTML="▲"):this.$el.collapseControl.innerHTML="▼"},toggleCollapsed:function(e){this.isCollapsed="boolean"==typeof e?e:!this.isCollapsed
var n=this.isCollapsed?"none":"block"
this.$el.content.style.display=n,this.$el.collapseControl.innerHTML=this.isCollapsed?"▲":"▼",this.isCollapsed?(this.$el.clearControl.style.display="none",this.$el.commandsControl.style.display="none"):(this.$el.clearControl.style.display="inline-block",this.commandsHashLength&&(this.$el.commandsControl.style.display="inline-block"))},bindListeners:function(){var t=this
function n(){var e=t.$el.input.value,n=t.eval(e)
t.logValue(n.text,n.error)}this.$el.collapseControl.addEventListener("click",function(){t.toggleCollapsed()}),this.$el.clearControl.addEventListener("click",function(){t.clearLogs()}),this.$el.button.addEventListener("click",function(){n()}),this.$el.input.addEventListener("keyup",function(e){13===e.which&&n()}),this.$el.commandsControl.addEventListener("click",function(){t.toggleCommands()})},toggleCommands:function(){this.commandsShown=!this.commandsShown,this.$el.commandsContainer.style.display=this.commandsShown?"inline-block":"none"},eval:function(e){var n,t
try{n=window.eval(e)}catch(e){n=e.message,t=!0}if(JSON&&JSON.stringify)try{n=JSON.stringify(n)}catch(e){n=e.message,t=!0}return{text:n,error:t}},clearLogs:function(){this.$el.log.innerHTML=""},bindErrorListener:function(){var i=this
window.onerror=function(e,n,t,o){i.props.showOnError&&i.show({expand:!0})
var s=n+":"+t+(o?":"+o:"")
i.logValue(e,s)}},appendLogEl:function(e){this.$el.log.appendChild(e),this.$el.log.scrollTop=this.$el.log.scrollHeight},decorateConsole:function(){var o=this
function e(t){window.console[t]&&(o.nativeConsole[t]=window.console[t],window.console[t]=function(){var e,n=[].slice.call(arguments)
o.nativeConsole[t].apply(window.console,n),e=function(e){var n,t,o,s,i=[].slice.call(e)
if(JSON&&JSON.stringify)try{for(n=0;n<i.length;n++)"string"!=typeof i[n]?(o=!1,i[n]=JSON.stringify(i[n],function(e,n){return void 0===n?(o=!0,"$$_|_undefined_|_$$"):n}),o&&(i[n]=i[n].replace('"$$_|_undefined_|_$$"',"undefined"))):(i[n]=i[n].replace("\n","<br>"),t=l(n,i),i[n]=t.string,i.splice(n+1,t.nextElement-1))}catch(e){i=[e.message],s=!0}function l(e,n){var t,o,s,i=n[e].split("%c"),l=n[e],a=e+1
if(1<i.length){for(t=1;t<i.length;)o=n[a]||"",s='<span style="'+o+'">',i.splice(t,0,s),i.splice(t+2,0,"</span>"),a++,t+=3
l=i.join("")}return{string:l,nextElement:a}}return{text:i.join(" "),error:s}}(n),o.logValue(e.text,e.error)})}this.consoleDecorated||(o.nativeConsole={},this.consoleDecorated=!0,window.console&&(e("log"),e("info"),e("warn"),e("error")))},undecorateConsole:function(){this.consoleDecorated},logValue:function(e,n,t){var o=document.createElement("div")
o.className="jsmc-log-el",o.innerHTML=s,n&&(o.className+=" jsmc-log-error"),t&&(o.className+=" jsmc-log-command"),o.getElementsByClassName("jsmc-log-text")[0].innerHTML=e,"string"==typeof n&&(o.getElementsByClassName("jsmc-log-target")[0].innerHTML=n),this.appendLogEl(o)}}
return e})