diff --git a/app/app.js b/app/app.js index d4004cb..c5198ea 100644 --- a/app/app.js +++ b/app/app.js @@ -1,54 +1,16 @@ -(function (should, Container) { +(function (should, Container, AppShell) { - var app = { - views: {}, - models: {}, - debugMode: false - }; + var di = new Container(); + var app = new AppShell(di); - var appModules = []; - var runObservers = []; - - app.di = new Container(); - - app.component = function(name, inst) { - if(arguments.length == 1) { - return this.di.resolve(name); - } - - this.di.register(name, inst); - }; - - app.get = function(name) { - return this.di.resolve(name); - }; - - app.compose = function (module) { - appModules.push(module); - }; - - app.run = function(observer) { - runObservers.push(observer); - }; + app.debugMode = false; app.bootstrap = function(rootViewElement) { this.rootViewElement = rootViewElement; - initializeModules(); - invokeRunObservers(); + this.initialize(); }; - function initializeModules() { - appModules.forEach(function(m) { - if(is.aFunction(m)) { - m(); - } - }); - } - - function invokeRunObservers() { - runObservers.forEach(function(o){ o(); }); - } window.app = app; -})(window.should, window.Container); \ No newline at end of file +})(window.should, window.Container, window.AppShell); \ No newline at end of file diff --git a/app/bitwise/calc.js b/app/bitwise/calc.js index 40ad8fb..f674bd1 100644 --- a/app/bitwise/calc.js +++ b/app/bitwise/calc.js @@ -1,5 +1,5 @@ (function(app, should){ - app.component('calc', { + app.set('calc', { numberOfBits: function (num) { should.bePositiveInteger(num); diff --git a/app/bitwise/expression.js b/app/bitwise/expression.js index c6fb383..85c9b2e 100644 --- a/app/bitwise/expression.js +++ b/app/bitwise/expression.js @@ -2,7 +2,7 @@ var twoOperandsRegex = /^(\d+)\s*(<<|>>|\||\&|\^)\s*(\d+)$/; var numbersList = /^((\d*)+\s?)+$/; - app.component('expression', { + app.set('expression', { canParse: function(string) { return twoOperandsRegex.test(string) || numbersList.test(string); }, diff --git a/app/bitwise/formatter.js b/app/bitwise/formatter.js index b1982c9..c2cee1e 100644 --- a/app/bitwise/formatter.js +++ b/app/bitwise/formatter.js @@ -1,27 +1,31 @@ (function(should, app){ - app.component("formatter", { - toBinaryString: function(num, totalLength) { + app.compose(function() { - var binaryStr = num.toString(2), - formatted = [], - i; + app.set("formatter", { + toBinaryString: function(num, totalLength) { - if(totalLength != null) { - should.bePositiveInteger(totalLength); - } + var binaryStr = num.toString(2), + formatted = [], + i; - for(i = 0; i formatted.length) { - formatted.unshift('0'); - } + for(i = 0; i formatted.length) { + formatted.unshift('0'); + } + + return formatted.join(''); + } + }); + }) - return formatted.join(''); - } - }); })(window.should, window.app); diff --git a/app/controllers.js b/app/controllers.js index be7a327..8e04bb5 100644 --- a/app/controllers.js +++ b/app/controllers.js @@ -1,7 +1,7 @@ app.compose(function() { app.controller('expressionInputCtrl', function (){ - var dispatcher = app.component('dispatcher'); + var dispatcher = app.get('dispatcher'); return { onViewAttached: function () { @@ -54,7 +54,7 @@ app.compose(function() { }); app.controller('resultViewCtrl', function() { - var html = app.component('html'); + var html = app.get('html'); var resultViewController = { $html: null, diff --git a/app/dispatcher.js b/app/dispatcher.js index 4a9aa24..ec411c5 100644 --- a/app/dispatcher.js +++ b/app/dispatcher.js @@ -1,7 +1,7 @@ app.compose(function() { app.component('dispatcher', function() { var handlers = []; - var is = app.component('is'); + var is = app.get('is'); var resultView = app.controller('resultViewCtrl'); var dispatcher = { diff --git a/app/services.js b/app/services.js index 70a791b..c4f6f69 100644 --- a/app/services.js +++ b/app/services.js @@ -1,7 +1,7 @@ (function(app, HtmlBuilder){ - app.component('html', HtmlBuilder); - app.component('is', is); + app.set('html', HtmlBuilder); + app.set('is', is); /* var template = { compile: function (template) { diff --git a/app/views.js b/app/views.js index 11cc18a..be241d1 100644 --- a/app/views.js +++ b/app/views.js @@ -1,9 +1,9 @@ // Expression View app.compose(function () { - var formatter = app.component('formatter'); - var calc = app.component('calc'); - var html = app.component('html'); + var formatter = app.get('formatter'); + var calc = app.get('calc'); + var html = app.get('html'); app.modelView(app.models.BitwiseOperation, { renderView: function(expr) { diff --git a/core/appShell.js b/core/appShell.js new file mode 100644 index 0000000..e4958ac --- /dev/null +++ b/core/appShell.js @@ -0,0 +1,36 @@ +(function() { + + function AppShell(diContainer) { + this.models = {}; + this.di = diContainer; + this.runList = []; + this.compositionList = []; + } + + AppShell.prototype.get = function(name) { + return this.di.resolve(name); + }; + + AppShell.prototype.set = function(name, def) { + this.di.register(name, def); + }; + + AppShell.prototype.run = function(func) { + this.runList.push(func); + }; + + AppShell.prototype.compose = function (func) { + this.compositionList.push(func); + }; + + AppShell.prototype.initialize = function () { + callInvocationList(this.compositionList); + callInvocationList(this.runList); + }; + + function callInvocationList(functions) { + functions.forEach(function(o){ o(); }); + } + + window.AppShell = AppShell; +})(); \ No newline at end of file diff --git a/components/di.js b/core/di.js similarity index 100% rename from components/di.js rename to core/di.js diff --git a/components/is.js b/core/is.js similarity index 100% rename from components/is.js rename to core/is.js diff --git a/components/should.js b/core/should.js similarity index 100% rename from components/should.js rename to core/should.js diff --git a/index.html b/index.html index 2a9ca6b..0890fb2 100644 --- a/index.html +++ b/index.html @@ -5,10 +5,11 @@ - - + + + + - @@ -146,7 +147,9 @@ }); app.bootstrap(document.getElementById('rootView')); - app.component('dispatcher').dispatch('help'); + app.get('dispatcher').dispatch('help'); + app.get('dispatcher').dispatch('1|2'); + app.get('dispatcher').dispatch('1 2');