mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2025-12-23 21:22:48 +01:00
Introduced AppShall
This commit is contained in:
50
app/app.js
50
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);
|
||||
})(window.should, window.Container, window.AppShell);
|
||||
@@ -1,5 +1,5 @@
|
||||
(function(app, should){
|
||||
app.component('calc', {
|
||||
app.set('calc', {
|
||||
|
||||
numberOfBits: function (num) {
|
||||
should.bePositiveInteger(num);
|
||||
|
||||
@@ -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);
|
||||
},
|
||||
|
||||
@@ -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<binaryStr.length; i++) {
|
||||
formatted.push(binaryStr[i]);
|
||||
}
|
||||
if(totalLength != null) {
|
||||
should.bePositiveInteger(totalLength);
|
||||
}
|
||||
|
||||
while(totalLength > formatted.length) {
|
||||
formatted.unshift('0');
|
||||
}
|
||||
for(i = 0; i<binaryStr.length; i++) {
|
||||
formatted.push(binaryStr[i]);
|
||||
}
|
||||
|
||||
while(totalLength > formatted.length) {
|
||||
formatted.unshift('0');
|
||||
}
|
||||
|
||||
return formatted.join('');
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
return formatted.join('');
|
||||
}
|
||||
});
|
||||
|
||||
})(window.should, window.app);
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
36
core/appShell.js
Normal file
36
core/appShell.js
Normal 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;
|
||||
})();
|
||||
11
index.html
11
index.html
@@ -5,10 +5,11 @@
|
||||
|
||||
<title></title>
|
||||
|
||||
<script type="text/javascript" src="components/is.js"></script>
|
||||
<script type="text/javascript" src="components/should.js"></script>
|
||||
<script type="text/javascript" src="core/is.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/di.js"></script>
|
||||
|
||||
<script type="text/javascript" src="app/app.js"></script>
|
||||
|
||||
@@ -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');
|
||||
|
||||
</script>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user