huge regactoring

This commit is contained in:
Borys Levytskyi
2015-04-02 22:53:06 +03:00
parent 0b3e0ed4fb
commit cadafab3ec
15 changed files with 418 additions and 291 deletions

98
components/bindr.js Normal file
View File

@@ -0,0 +1,98 @@
(function(){
var bindr = {};
bindr.model = function(definition){
var model = new bindr.Model();
for(var property in definition){
if(!definition.hasOwnProperty(property)){
continue;
}
Object.defineProperty(model, property, {
get:bindr.Model.createGetter(property),
set:bindr.Model.createSetter(property)
});
model[property] = definition[property];
}
return model;
};
bindr.Model = function() {
this.executionHandlers = [];
};
bindr.Model.createGetter = function (propertyName){
return function(){
return this["_" + propertyName];
}
};
bindr.Model.createSetter = function(propertyName){
return function(value){
this["_" + propertyName] = value;
this.notifyPropertyChanged(propertyName, value);
}
};
bindr.Model.prototype.observe = function (handler){
this.executionHandlers.push(handler);
};
bindr.Model.prototype.notifyPropertyChanged = function(propertyName, value){
this.executionHandlers.forEach(function(h){
h(propertyName, value);
});
};
bindr.bindElement = function(element, model, propertyName) {
if(element.bindr != null) {
return;
}
if(element.tagName == "INPUT") {
bindInput(model, element, propertyName);
}
else {
bindHtmlElement(model, element, propertyName);
}
element.bindr = {}; // will be used later
};
bindr.bindView = function(viewElement, model) {
var elements = viewElement.querySelectorAll('[data-bindr]'),
count = elements.length,
i =0, el;
for(;i<count; i++){
el = elements[i];
this.bindElement(el, model, el.getAttribute('data-bindr'))
}
};
function bindInput(model, intput, propertyName) {
intput.addEventListener('keyup', function(e){
model[propertyName] = e.srcElement.value;
});
model.observe(function(property, value){
if(window.event && window.event.srcElement == intput) {
return;
}
intput.value = value;
});
}
function bindHtmlElement(model, el, propertyName) {
model.observe(function(propery, value){
if(propery == propertyName) {
el.innerHTML = value;
}
});
}
window.bindr = bindr;
})();

30
components/commandr.js Normal file
View File

@@ -0,0 +1,30 @@
(function(){
var commandr = {
};
function Command(name) {
this.name = name;
this.executionHandlers = [];
}
Command.prototype.execute = function (cmdArgs) {
cmdArgs.commandHandled = false;
for(var i=0; i<this.executionHandlers.length; i++) {
this.executionHandlers[i](cmdArgs);
if(cmdArgs.commandHandled === true) {
return;
}
}
};
Command.prototype.subscribe = function (handler) {
this.executionHandlers.push(handler);
// TODO: unsubcribe
};
commandr.Command = Command;
window.commandr = commandr;
})();

49
components/html.js Normal file
View File

@@ -0,0 +1,49 @@
(function(){
function HtmlBuilder() {
this.sb = [];
}
HtmlBuilder.prototype.element = function(tagName, arg) {
var attrs = typeof arg == "object" ? arg : { html: arg},
elementContent = attrs.html || '';
this.sb.push('<' + tagName + ' ' + getAttributesStr(attrs) + ">");
if(typeof elementContent == 'function') {
elementContent();
} else if (elementContent != null) {
this.sb.push(elementContent.toString());
}
this.sb.push('</' + tagName + '>');
};
HtmlBuilder.prototype.toString = function () {
return this.sb.join('\r\n');
};
HtmlBuilder.prototype.toHtmlElement = function (){
var el = document.createElement('div');
el.innerHTML = this.toString();
return el.children[0];
};
function getAttributesStr(attr) {
if(attr == null) {
return '';
}
var str = [];
for(var key in attr) {
if(key == 'html')
continue;
str.push(key + '="' + attr[key] + '"');
}
return str.join(' ');
}
window.HtmlBuilder = HtmlBuilder;
})();

28
components/should.js Normal file
View File

@@ -0,0 +1,28 @@
(function(){
window.should = {
beNumber: function (num, name) {
this.check(typeof num == "number" && !isNaN(num), num + " is not a number");
this.check(isFinite(num), num + "is an infinite number")
},
bePositiveInteger: function(num) {
this.beNumber(num);
this.check(num >= 0, "Should be positive integer")
},
notBeNull: function (obj, name) {
this.check(obj != null, name + " is null or undefined");
},
beString: function(obj) {
this.check(typeof obj == "string", "should be a string");
},
check: function(assertion, message) {
if(assertion !== true) {
throw new Error (message);
}
}
};
})();