mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2025-12-23 21:22:48 +01:00
2 lines
13 KiB
JavaScript
2 lines
13 KiB
JavaScript
/*! BitwiseCmd 2015-04-11 */
|
|
window.core={},function(){"use strict";window.core.is={plainObject:function(a){return"object"==typeof a&&a instanceof Object},aFunction:function(a){return"function"==typeof a},string:function(a){return"string"==typeof a},regex:function(a){return"object"==typeof a&&this.constructedFrom(RegExp)},constructedFrom:function(a,b){return a instanceof b},htmlElement:function(a){return a instanceof HtmlElement},array:function(a){return a instanceof Array}}}(),function(){"use strict";function a(a,b){return"string"==typeof a?a+" "+b:b}window.core.should={beNumber:function(b,c){this.check("number"==typeof b&&!isNaN(b),b+" is not a number"),this.check(isFinite(b),a(c,"is an infinite number"))},bePositiveInteger:function(b,c){this.beNumber(b),this.check(b>=0,a(c,"should be positive integer"))},notBeNull:function(b,c){this.check(null!=b,a(c,"is null or undefined"))},beString:function(a,b){this.check("string"==typeof a,"should be a string")},check:function(a,b){if(a!==!0)throw new Error(b)}}}(),function(a){"use strict";function b(a){this.store={},this.resolutionStack=[]}function c(a){if(e(this.resolutionStack,a))throw new Error("Failed to resolve service: "+a+". Circular reference: "+this.resolutionStack.join(" < "));this.resolutionStack.unshift(a);var b=this.store[a];if(null==b)throw new Error(a+" component is not registered");return null==b.resolved&&b.createInstance(),this.resolutionStack.shift(),b.resolved}function d(a){this.def=a,this.resolved=null}function e(a,b){for(var c=a.length;c-->0;)if(a[c]===b)return!0;return!1}var f=a.is;b.prototype.register=function(a,b){var c=this.store[a];return null==c&&(c=b instanceof d?b:new d(b),c.name=a,this.store[a]=c),c},b.prototype.resolve=function(a){return c.call(this,a)},d.prototype.createInstance=function(){var a=this.def;this.resolved="function"==typeof a?a():a,f.aFunction(this.onFirstTimeResolve)&&this.onFirstTimeResolve(this.resolved)},b.Registration=d,a.Container=b}(window.core),function(){"use strict";function a(a){this.models={},this.di=a,this.runList=[],this.compositionList=[]}function b(a){a.forEach(function(a){a()})}a.prototype.get=function(a){return this.di.resolve(a)},a.prototype.set=function(a,b){this.di.register(a,b)},a.prototype.run=function(a){this.runList.push(a)},a.prototype.compose=function(a){this.compositionList.push(a)},a.prototype.initialize=function(){b(this.compositionList),b(this.runList)},window.core.AppShell=a}(),function(a){"use strict";var b={},c=a.should;b.element=function(a,c){var d=document.createElement("div");return d.innerHTML=b.template(a,c),d.children[0]},b.template=function(a,d){c.beString(a,"template");var e,f=/(?:{([^}]+)})/g;return e=null==d?a:a.replace(f,function(a,c){return b.escapeHtml(d[c])})},b.escapeHtml=function(a){return null==a?a:("string"!=typeof a&&(a=a.toString()),a.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'"))},a.HtmlBuilder=b}(window.core),function(a){"use strict";function b(){this.$store={},this.$executionHandlers=[]}var c=a.is;b.create=function(a){var c=new b;for(var d in a)a.hasOwnProperty(d)&&(Object.defineProperty(c,d,{get:b.createGetter(d),set:b.createSetter(d)}),c[d]=a[d]);return Object.seal(c)},b.createGetter=function(a,b){return function(){return this.$store[a]}},b.createSetter=function(a,b){return function(b){this.$store[a]=b,this.notifyPropertyChanged(a,b)}},b.prototype.observe=function(a,b){var d;if(c.aFunction(a))d=a;else{if(!c.string(a)||!c.aFunction(b))return void console.warn("Unsupported set of arguments: ",arguments);d=function(c,d){c===a&&b(d)}}var e=this.$executionHandlers,f=e.push(d);return function(){e.splice(1,f)}},b.prototype.notifyPropertyChanged=function(a,b){this.$executionHandlers.forEach(function(c){c(a,b)})},b.prototype.store=function(){return this.$store},b.prototype.keys=function(){return Object.keys(this.$store)},a.ObservableObject=b}(window.core),function(a){"use strict";var b=new a.Container,c=new a.AppShell(b);c.set("cmdConfig",a.ObservableObject.create({emphasizeBytes:!0,theme:"dark"})),c.debugMode=!1,c.bootstrap=function(a){this.rootViewElement=a,this.set("rootView",a),this.initialize()},window.app=c}(window.core),function(a,b){"use strict";function c(a){a.attachView=function(b){this.viewElement=b,"function"==typeof a.onViewAttached&&a.onViewAttached(b)},a.detachView=function(){this.viewElement=null,"function"==typeof a.onViewDetached&&a.onViewDetached(viewElement)}}function d(b){for(var c,d,e,f=b.querySelectorAll("[data-controller]"),g=0,h=f.length;h>g;g++)e=f[g],c=e.getAttribute("data-controller"),d=a.controller(c),null!=d?(d.attachView(e),"function"==typeof d.detachView&&e.addEventListener("DOMNodeRemoved",function(a){e===a.target&&d.detachView()})):console.warn(c+" controller wasn't found")}var e=b.should;a.controller=function(a,d){if(e.beString(a,"name"),null==d)return this.get(a);var f=new b.Container.Registration(d);f.onFirstTimeResolve=function(a){c(a)},this.set(a,f)},a.run(function(){d(a.get("rootView"),a.di)})}(window.app,window.core),function(a){"use strict";function b(a){this.html=a}function c(c){var d=c.querySelectorAll("[data-template]"),e=a.templates;Array.prototype.forEach.call(d,function(a){var c=a.getAttribute("data-template");return e[c]instanceof b?void console.warn(c+" templates already registered"):void(e[c]=new b(a.innerHTML))})}b.prototype.render=function(b){return a.get("html").element(this.html,b)},a.templates=[],a.template=function(a){var b=this.templates[a];if(null==b)throw new Error(a+" template is not found");return b},a.run(function(){c(a.get("rootView"))})}(window.app),function(a,b){"use strict";function c(a){return d(a)+"ViewBuilder"}function d(a){var b=a.toString();return b.substr(8,b.indexOf("(")-8).trim()}a.modelView=function(b,d){var e=c(b);a.di.register(e,d)},a.buildViewFor=function(a){var b=c(a.constructor),d=this.di.resolve(b);return d.renderView(a)}}(window.app,window.is),app.compose(function(){"use strict";var a=app.get("should");app.set("calc",{numberOfBits:function(b){return a.bePositiveInteger(b),Math.floor(Math.log(b)/Math.log(2))+1},maxNumberOfBits:function(a){for(var b,c=[],d=0;d<a.length;d++)b=a[d],c.push(this.numberOfBits(b));return Math.max.apply(null,c)}})}),app.compose(function(){"use strict";function createCalculableExpression(matches){var o1=parseInt(matches[1],10),o2=parseInt(matches[3],10),m=new app.models.BitwiseOperation;return m.operand1=o1,m.operand2=o2,m.sign=matches[2],m.string=matches.input,m.result=eval(matches.input),m}function createListOfNumbersExpression(a){var b=[];return a.split(" ").forEach(function(a){a.trim().length>0&&b.push(parseInt(a))}),new app.models.BitwiseNumbers(b)}var twoOperandsRegex=/^(\d+)\s*(<<|>>|\||\&|\^)\s*(\d+)$/,numbersList=/^((\d*)+\s?)+$/;app.set("expression",{canParse:function(a){return twoOperandsRegex.test(a)||numbersList.test(a)},parse:function(a){var b=a.replace(/^\s+|\s+$/,""),c=twoOperandsRegex.exec(b);return null!=c?createCalculableExpression(c):(c=numbersList.exec(a),null!=c?createListOfNumbersExpression(a):void 0)}})}),app.compose(function(){"use strict";var a=app.get("should");app.set("formatter",{toBinaryString:function(b,c){var d,e=b.toString(2),f=[];for(null!=c&&a.bePositiveInteger(c),d=0;d<e.length;d++)f.push(e[d]);for(;c>f.length;)f.unshift("0");return f.join("")}})}),app.compose(function(){"use strict";app.set("cmd",function(){function a(a,b){var c=new app.models.ErrorResult(b);g.display(new app.models.DisplayResult(a,c))}function b(a,b){var c=b.handle(a);if(null!=c){var d=new app.models.DisplayResult(a,c);g.display(d)}}function c(a,b){return f.plainObject(a)?a:f.string(a)?{canHandle:function(b){return b===a},handle:b}:null}function d(a){var b=0;for(b;b<e.length;b++)if(e[b].canHandle(a))return e[b]}var e=[],f=app.get("is"),g=app.controller("cmdController");return{execute:function(c){var e=c.trim().toLowerCase(),f=d(e);if(null!=f)if(app.debugMode)b(e,f);else try{b(e,f)}catch(g){a(e,"Error: "+g)}else a(e,"Unsupported expression: "+e.trim())},commands:function(a){for(var b in a)a.hasOwnProperty(b)&&this.command(b,a[b])},command:function(a,b){var d=c(a,b);return null==d?void console.warn("unexpected set of arguments: ",Array.prototype.splice.call(arguments)):f.aFunction(d.canHandle)?f.aFunction(d.handle)?void e.push(d):void console.warn('handler is missing "handle" function. registration denied.'):void console.warn('handler is missing "canHandle" function. registration denied.')},clear:function(){g.clear()}}})}),app.run(function(){"use strict";function a(a){var b=a.parentNode.parentNode;if(b.parentNode.firstChild!=b){var c=b.parentNode;c.removeChild(b),c.insertBefore(b,c.firstChild)}}{var b=app.get("cmd"),c=app.get("cmdConfig");app.get("rootView")}b.commands({help:function(){var b=document.querySelector(".result .helpResultTpl");return null!=b?void a(b):new app.models.ViewResult("helpResultTpl")},clear:function(){b.clear()},em:function(){c.emphasizeBytes=!c.emphasizeBytes},dark:function(){c.theme="dark"},light:function(){c.theme="light"},about:function(){var b=document.querySelector(".result .aboutTpl");return null!=b?void a(b):new app.models.ViewResult("aboutTpl")}}),b.command({canHandle:function(a){return app.get("expression").canParse(a)},handle:function(a){return app.get("expression").parse(a)}})}),app.compose(function(){"use strict";app.controller("expressionInputCtrl",function(){var a=app.get("cmd");return{onViewAttached:function(){var b=this;b.history=[],b.historyIndex=0,this.viewElement.focus(),this.viewElement.addEventListener("keyup",function(c){var d=c.target;13==c.keyCode&&0!=d.value.trim().length&&(a.execute(d.value),b.history.unshift(d.value),b.historyIndex=0,d.value="")}),this.viewElement.addEventListener("keydown",function(a){return 38==a.keyCode?(b.history.length>b.historyIndex&&(a.target.value=b.history[b.historyIndex++]),void a.preventDefault()):void(40==a.keyCode&&(b.historyIndex>0&&(a.target.value=b.history[--b.historyIndex]),a.preventDefault()))})}}}),app.controller("cmdController",function(){app.get("html"),app.get("rootView");return{clear:function(){this.viewElement.innerHTML=""},display:function(a){var b=app.buildViewFor(a),c=this.viewElement;0==c.childNodes.length?c.appendChild(b):c.insertBefore(b,c.childNodes[0])}}}),app.controller("configPanelCtrl",{onViewAttached:function(){var a=this,b=app.get("cmdConfig");a.update(b),b.observe(function(){a.update(b)})},update:function(a){var b=this.viewElement.querySelector("#emphasizeBytes");a.emphasizeBytes?b.classList.add("on"):b.classList.remove("on")}})}),app.compose(function(){"use strict";function a(a){var b=d.maxNumberOfBits(a);if(f.emphasizeBytes&&b%8!=0){if(8>b)return 8;var c=b-b%8;return c+8}return b}function b(a){var b=a.querySelectorAll(".bin");Array.prototype.forEach.call(b,function(a){var b=a.textContent;a.innerHTML=b.replace(/(\d{8})/g,'<span class="byte">$1</span>').replace(/0/g,'<span class="zero">0</span>').replace(/1/g,'<span class="one">1</span>')})}var c=app.get("formatter"),d=app.get("calc"),e=app.get("html"),f=app.get("cmdConfig");app.modelView(app.models.BitwiseOperation,{renderView:function(d){var e=a([d.operand1,d.operand2,d.result]);d.operand1Binary=c.toBinaryString(d.operand1,e),d.operand2Binary=c.toBinaryString(d.operand2,e),d.resultBinary=c.toBinaryString(d.result,e);var f=/<<|>>/.test(d.sign)?"shiftExpressionView":"binaryExpressionView",g=app.template(f),h=g.render(d);return b(h),h}}),app.modelView(app.models.BitwiseNumbers,{renderView:function(d){var f=a(d.numbers),g=e.element('<table class="expression"></table>');return d.numbers.forEach(function(a){var b=g.insertRow(),d=b.insertCell();d.className="label";var e=b.insertCell();e.className="bin",d.textContent=a,e.textContent=c.toBinaryString(a,f)}),b(g),g}}),app.modelView(app.models.ViewResult,{renderView:function(a){var b=app.template(a.template);return b.render()}}),app.modelView(app.models.ErrorResult,{renderView:function(a){return e.element('<div class="error">{message}</div>',a)}}),app.modelView(app.models.DisplayResult,{renderView:function(a){var b=app.template("resultView").render(a),c=app.buildViewFor(a.content);return b.querySelector(".content").appendChild(c),b}})}),function(app){"use strict";function BitwiseOperation(){}function BitwiseNumbers(a){this.numbers=a}function ErrorResult(a){this.message=a}function ViewResult(a){this.template=a}function DisplayResult(a,b){this.input=a,this.content=b}BitwiseOperation.prototype.calculate=function(){return eval(this.string)},app.models.BitwiseOperation=BitwiseOperation,app.models.BitwiseNumbers=BitwiseNumbers,app.models.ErrorResult=ErrorResult,app.models.ViewResult=ViewResult,app.models.DisplayResult=DisplayResult}(window.app),app.run(function(){var a=app.get("rootView"),b=app.get("cmdConfig");b.observe("theme",function(b){console.log("changed theme");var c="dark"==b?"light":"dark";a.classList.contains(b)||(a.classList.remove(c),a.classList.add(b))})}),app.run(function(){function a(){localStorage.setItem(d,JSON.stringify(c.store()))}function b(){var a,b=localStorage.getItem(d);if(core.is.string(b)){a=JSON.parse(b);for(var e in a)c[e]=a[e]}}var c=app.get("cmdConfig"),d="cmdConfig";b(),c.observe(function(b,c){a()})}),app.run(function(){var a=app.get("rootView").querySelectorAll("[data-cmd]");Array.prototype.forEach.call(a,function(a){a.addEventListener("click",function(a){app.get("cmd").execute(a.target.getAttribute("data-cmd"))})})}),function(a,b){"use strict";a.set("html",b.HtmlBuilder),a.set("is",b.is),a.set("should",b.should),a.set("bindr",b.bindr)}(window.app,window.core); |