Introduced AppShall

This commit is contained in:
Borys Levytskyi
2015-04-04 16:10:17 +03:00
parent a3ca46174b
commit 17fc0bad97
13 changed files with 80 additions and 75 deletions

View File

@@ -1,54 +1,16 @@
(function (should, Container) { (function (should, Container, AppShell) {
var app = { var di = new Container();
views: {}, var app = new AppShell(di);
models: {},
debugMode: false
};
var appModules = []; app.debugMode = false;
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.bootstrap = function(rootViewElement) { app.bootstrap = function(rootViewElement) {
this.rootViewElement = rootViewElement; this.rootViewElement = rootViewElement;
initializeModules(); this.initialize();
invokeRunObservers();
}; };
function initializeModules() {
appModules.forEach(function(m) {
if(is.aFunction(m)) {
m();
}
});
}
function invokeRunObservers() {
runObservers.forEach(function(o){ o(); });
}
window.app = app; window.app = app;
})(window.should, window.Container); })(window.should, window.Container, window.AppShell);

View File

@@ -1,5 +1,5 @@
(function(app, should){ (function(app, should){
app.component('calc', { app.set('calc', {
numberOfBits: function (num) { numberOfBits: function (num) {
should.bePositiveInteger(num); should.bePositiveInteger(num);

View File

@@ -2,7 +2,7 @@
var twoOperandsRegex = /^(\d+)\s*(<<|>>|\||\&|\^)\s*(\d+)$/; var twoOperandsRegex = /^(\d+)\s*(<<|>>|\||\&|\^)\s*(\d+)$/;
var numbersList = /^((\d*)+\s?)+$/; var numbersList = /^((\d*)+\s?)+$/;
app.component('expression', { app.set('expression', {
canParse: function(string) { canParse: function(string) {
return twoOperandsRegex.test(string) || numbersList.test(string); return twoOperandsRegex.test(string) || numbersList.test(string);
}, },

View File

@@ -1,27 +1,31 @@
(function(should, app){ (function(should, app){
app.component("formatter", { app.compose(function() {
toBinaryString: function(num, totalLength) {
var binaryStr = num.toString(2), app.set("formatter", {
formatted = [], toBinaryString: function(num, totalLength) {
i;
if(totalLength != null) { var binaryStr = num.toString(2),
should.bePositiveInteger(totalLength); formatted = [],
} i;
for(i = 0; i<binaryStr.length; i++) { if(totalLength != null) {
formatted.push(binaryStr[i]); should.bePositiveInteger(totalLength);
} }
while(totalLength > formatted.length) { for(i = 0; i<binaryStr.length; i++) {
formatted.unshift('0'); formatted.push(binaryStr[i]);
} }
while(totalLength > formatted.length) {
formatted.unshift('0');
}
return formatted.join('');
}
});
})
return formatted.join('');
}
});
})(window.should, window.app); })(window.should, window.app);

View File

@@ -1,7 +1,7 @@
app.compose(function() { app.compose(function() {
app.controller('expressionInputCtrl', function (){ app.controller('expressionInputCtrl', function (){
var dispatcher = app.component('dispatcher'); var dispatcher = app.get('dispatcher');
return { return {
onViewAttached: function () { onViewAttached: function () {
@@ -54,7 +54,7 @@ app.compose(function() {
}); });
app.controller('resultViewCtrl', function() { app.controller('resultViewCtrl', function() {
var html = app.component('html'); var html = app.get('html');
var resultViewController = { var resultViewController = {
$html: null, $html: null,

View File

@@ -1,7 +1,7 @@
app.compose(function() { app.compose(function() {
app.component('dispatcher', function() { app.component('dispatcher', function() {
var handlers = []; var handlers = [];
var is = app.component('is'); var is = app.get('is');
var resultView = app.controller('resultViewCtrl'); var resultView = app.controller('resultViewCtrl');
var dispatcher = { var dispatcher = {

View File

@@ -1,7 +1,7 @@
(function(app, HtmlBuilder){ (function(app, HtmlBuilder){
app.component('html', HtmlBuilder); app.set('html', HtmlBuilder);
app.component('is', is); app.set('is', is);
/* /*
var template = { var template = {
compile: function (template) { compile: function (template) {

View File

@@ -1,9 +1,9 @@
// Expression View // Expression View
app.compose(function () { app.compose(function () {
var formatter = app.component('formatter'); var formatter = app.get('formatter');
var calc = app.component('calc'); var calc = app.get('calc');
var html = app.component('html'); var html = app.get('html');
app.modelView(app.models.BitwiseOperation, { app.modelView(app.models.BitwiseOperation, {
renderView: function(expr) { renderView: function(expr) {

36
core/appShell.js Normal file
View File

@@ -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;
})();

View File

@@ -5,10 +5,11 @@
<title></title> <title></title>
<script type="text/javascript" src="components/is.js"></script> <script type="text/javascript" src="core/is.js"></script>
<script type="text/javascript" src="components/should.js"></script> <script type="text/javascript" src="core/should.js"></script>
<script type="text/javascript" src="core/di.js"></script>
<script type="text/javascript" src="core/appShell.js"></script>
<script type="text/javascript" src="components/htmlBuilder.js"></script> <script type="text/javascript" src="components/htmlBuilder.js"></script>
<script type="text/javascript" src="components/di.js"></script>
<script type="text/javascript" src="app/app.js"></script> <script type="text/javascript" src="app/app.js"></script>
@@ -146,7 +147,9 @@
}); });
app.bootstrap(document.getElementById('rootView')); 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');
</script> </script>
</body> </body>