From 022c65635c31cd4caa1d825a112da390d791a9b5 Mon Sep 17 00:00:00 2001 From: Borys Levytskyi Date: Fri, 3 Apr 2015 19:31:57 +0300 Subject: [PATCH] Implemented historical commands --- app/app.js | 37 ++------------ app/bitwise/calc.js | 30 ++++++------ app/bitwise/expression.js | 2 +- app/bitwise/formatter.js | 2 +- app/controllers.js | 79 +++++++++++++++++++++--------- app/dispatcher.js | 5 +- app/services.js | 2 +- app/views.js | 4 +- components/commandr.js | 31 ------------ components/commandsFeature.js | 52 ++++++++++++++++++++ components/{container.js => di.js} | 22 +++++---- index.html | 4 +- 12 files changed, 148 insertions(+), 122 deletions(-) delete mode 100644 components/commandr.js create mode 100644 components/commandsFeature.js rename components/{container.js => di.js} (64%) diff --git a/app/app.js b/app/app.js index 7f4a328..0f60ce4 100644 --- a/app/app.js +++ b/app/app.js @@ -1,8 +1,9 @@ -(function (should, commandr, bindr, Container) { +(function (should, Container) { var app = { views: {}, - models: {} + models: {}, + debugMode: false }; var commandHandlers = {}; @@ -22,35 +23,6 @@ return this.di.resolve(name); }; - app.service = app.component; - - - // TODO: introduce command feature - app.command = function(name, handler) { - var cmd = commandHandlers[name]; - - if(cmd == null) { - cmd = commandHandlers[name] = new commandr.Command(name); - } - - if(typeof handler == "function") { - cmd.subscribe(handler); - } - - if (typeof handler == "object") { - - if(typeof handler.execute != "function"){ - console.warn('Given handler is an object, but doesn\'t have "execute" function'); - return cmd; - } - - this.di.resolveProperties(handler); - cmd.subscribe(handler.execute.bind(handler)); - } - - return cmd; - }; - app.run = function(observer) { runObservers.push(observer); }; @@ -66,5 +38,4 @@ window.app = app; - -})(window.should, window.commandr, window.bindr, window.Container); \ No newline at end of file +})(window.should, window.Container); \ No newline at end of file diff --git a/app/bitwise/calc.js b/app/bitwise/calc.js index f37efa8..40ad8fb 100644 --- a/app/bitwise/calc.js +++ b/app/bitwise/calc.js @@ -1,23 +1,21 @@ (function(app, should){ - var calc = {}; + app.component('calc', { - calc.numberOfBits = function(num) { - should.bePositiveInteger(num); - return Math.floor(Math.log(num) / Math.log(2)) + 1; - }; + numberOfBits: function (num) { + should.bePositiveInteger(num); + return Math.floor(Math.log(num) / Math.log(2)) + 1; + }, - calc.maxNumberOfBits = function (arr) { + maxNumberOfBits: function (arr) { - var counts = [], num; - for(var i=0;i>|\||\&|\^)\s*(\d+)$/; var numbersList = /^((\d*)+\s?)+$/; - app.service('expression', { + app.component('expression', { canParse: function(string) { return twoOperandsRegex.test(string) || numbersList.test(string); }, diff --git a/app/bitwise/formatter.js b/app/bitwise/formatter.js index 4b9bfcf..b1982c9 100644 --- a/app/bitwise/formatter.js +++ b/app/bitwise/formatter.js @@ -1,6 +1,6 @@ (function(should, app){ - app.service("formatter", { + app.component("formatter", { toBinaryString: function(num, totalLength) { var binaryStr = num.toString(2), diff --git a/app/controllers.js b/app/controllers.js index 9b86ff5..46015ad 100644 --- a/app/controllers.js +++ b/app/controllers.js @@ -4,40 +4,75 @@ $dispatcher:null, onViewAttached: function () { var d = this.$dispatcher; + + var self = this; + self.history =[]; + self.historyIndex = 0; + this.viewElement.focus(); + this.viewElement.addEventListener('keyup', function (args) { + var inpt = args.srcElement; + if (args.keyCode != 13) { return; } // Enter - d.dispatch(args.srcElement.value); - args.srcElement.value = ''; + d.dispatch(inpt.value); + self.history.unshift(inpt.value); + self.historyIndex = 0; + inpt.value = ''; }); + + this.viewElement.addEventListener('keydown', function(args){ + console.log(args.keyCode); + if(args.keyCode == 38) { + + if (self.history.length > self.historyIndex) { // up + args.srcElement.value = self.history[self.historyIndex++]; + + } + + args.preventDefault(); + return; + } + + if(args.keyCode == 40) { + + if(self.historyIndex > 0) { // up + args.srcElement.value = self.history[--self.historyIndex]; + } + + args.preventDefault(); + return; + } + }) } }); - app.service('resultView', { - $html: null, - clear: function (){ - this.viewElement.innerHTML = ''; - }, - onViewAttached: function(el) { - var r = 1; - }, - display: function ( model) { - var view = app.buildViewFor(model); + var resultViewController = { + $html: null, + clear: function (){ + this.viewElement.innerHTML = ''; + }, + onViewAttached: function(el) { + var r = 1; + }, + display: function ( model) { + var view = app.buildViewFor(model); - var vw = this.viewElement; - if(vw.childNodes.length == 0) { - vw.appendChild(view); - } - else { - vw.insertBefore(view, vw.childNodes[0]); - } - } - }); + var vw = this.viewElement; + if(vw.childNodes.length == 0) { + vw.appendChild(view); + } + else { + vw.insertBefore(view, vw.childNodes[0]); + } + } + }; - app.controller('resultViewCtrl', app.service('resultView')); + app.component('resultView', resultViewController); + app.controller('resultViewCtrl', resultViewController); })(window.app, window.is); diff --git a/app/dispatcher.js b/app/dispatcher.js index b2a64a0..c1140c1 100644 --- a/app/dispatcher.js +++ b/app/dispatcher.js @@ -4,14 +4,13 @@ var dispatcher = { $resultView:null, - debug:true, dispatch: function(rawInput) { var input = rawInput.trim(); var handler = this.findHandler(input); if(handler != null) { - if(this.debug) { + if(app.debugMode) { this.invokeHandler(input, handler); } else { try { @@ -83,6 +82,6 @@ app.get('resultView').clear(); }); - app.service('dispatcher', dispatcher); + app.component('dispatcher', dispatcher); })(window.app, window.is); diff --git a/app/services.js b/app/services.js index 2b54146..1800eb0 100644 --- a/app/services.js +++ b/app/services.js @@ -1,6 +1,6 @@ (function(app, HtmlBuilder){ - app.service('html', { + app.component('html', { builder: function () { return new HtmlBuilder(); }, diff --git a/app/views.js b/app/views.js index 8bb9a0c..3d07a5c 100644 --- a/app/views.js +++ b/app/views.js @@ -1,8 +1,8 @@ // Expression View (function(app) { - var formatter = app.service('formatter'); - var calc = app.service('calc'); + var formatter = app.component('formatter'); + var calc = app.component('calc'); app.modelView(app.models.BitwiseOperation, { $html:null, diff --git a/components/commandr.js b/components/commandr.js deleted file mode 100644 index f341b1b..0000000 --- a/components/commandr.js +++ /dev/null @@ -1,31 +0,0 @@ -(function(){ - var commandr = { - - }; - - function Command(name) { - this.name = name; - this.executionHandlers = []; - } - - Command.prototype.execute = function (cmdArgs) { - cmdArgs = cmdArgs || {}; - cmdArgs.commandHandled = false; - - for(var i=0; i - - - +