Comparing version 1.0.6 to 1.0.7
(function(global) { | ||
global.App.Controller('about', '/about/', function($scope, _update) { | ||
$scope.buttonClicked = function(pageName) { | ||
global.App.Router().navigateTo(pageName); | ||
$scope.arr = []; | ||
var arr = $scope.arr, i, random; | ||
var interval = function() { | ||
random = Math.random()*100; | ||
for(i = 0; i < parseInt(random); i++) | ||
arr[i] = Math.random(); | ||
if(i < arr.length) | ||
arr.splice(i, arr.length); | ||
_update(); | ||
setTimeout(interval, 0); | ||
}; | ||
interval(); | ||
}); | ||
})(Function('return this')()); |
@@ -27,5 +27,17 @@ (function(global) { | ||
global.App.Controller('about', '/about/', function($scope, _update) { | ||
$scope.buttonClicked = function(pageName) { | ||
global.App.Router().navigateTo(pageName); | ||
$scope.arr = []; | ||
var arr = $scope.arr, i, random; | ||
var interval = function() { | ||
random = Math.random()*100; | ||
for(i = 0; i < parseInt(random); i++) | ||
arr[i] = Math.random(); | ||
if(i < arr.length) | ||
arr.splice(i, arr.length); | ||
_update(); | ||
setTimeout(interval, 0); | ||
}; | ||
interval(); | ||
}); | ||
@@ -44,2 +56,9 @@ | ||
})(Function('return this')()); | ||
(function(global) { | ||
global.App.Pipe('split', function(value, data) { | ||
return value.split(data); | ||
}); | ||
})(Function('return this')()); |
@@ -1,1 +0,1 @@ | ||
Function("return this")().App.Controller("app","./",function(t,o){t.title="App works!",setTimeout(function(){t.title="Hello World!",o()},3e3)}),new(Function("return this")().App.Router)([{path:"",redirect:"home"},{path:"home",controller:"main-layout"},{path:"about",controller:"about"}]),function(t){t.App.Controller("about","/about/",function(o,n){o.buttonClicked=function(o){t.App.Router().navigateTo(o)}})}(Function("return this")()),function(t){t.App.Controller("main-layout","/main-layout/",function(o,n){o.title="Main Layout",o.buttonClicked=function(o){t.App.Router().navigateTo(o)}})}(Function("return this")()); | ||
Function("return this")().App.Controller("app","./",function(t,n){t.title="App works!",setTimeout(function(){t.title="Hello World!",n()},3e3)}),new(Function("return this")().App.Router)([{path:"",redirect:"home"},{path:"home",controller:"main-layout"},{path:"about",controller:"about"}]),Function("return this")().App.Controller("about","/about/",function(t,n){t.arr=[];var o,r,e=t.arr,i=function(){for(r=100*Math.random(),o=0;o<parseInt(r);o++)e[o]=Math.random();o<e.length&&e.splice(o,e.length),n(),setTimeout(i,0)};i()}),function(t){t.App.Controller("main-layout","/main-layout/",function(n,o){n.title="Main Layout",n.buttonClicked=function(n){t.App.Router().navigateTo(n)}})}(Function("return this")()),Function("return this")().App.Pipe("split",function(t,n){return t.split(n)}); |
@@ -12,2 +12,23 @@ (function(global) { | ||
var ArrayFunctions = { | ||
clean: function(arr) { | ||
if(!Array.isArray(arr)) | ||
throw { message: "[Array:Clean] Cannot work with a non-array!" }; | ||
for(var i = 0; i < arr.length; i++) { | ||
if(arr[i] === undefined) { | ||
arr.splice(i, 1); | ||
i--; | ||
} else if(Array.isArray(arr[i])) | ||
this.clean(arr[i]); | ||
} | ||
} | ||
}; | ||
global.Utils = global.Utils || {}; | ||
global.Utils.Array = ArrayFunctions; | ||
})(Function('return this')()); | ||
(function(global) { | ||
var Http = { | ||
@@ -25,3 +46,7 @@ get: function(url, data, callback) { | ||
for(var param in data.params) if(data.params.hasOwnProperty(param)) { | ||
urlParams += param + '=' + data.params[param] + '&'; | ||
if(Array.isArray(data.params[param])) { | ||
for(var i = 0; i < data.params[param].length; i++) | ||
urlParams += param + '=' + data.params[param][i] + '&'; | ||
} else | ||
urlParams += param + '=' + data.params[param] + '&'; | ||
} | ||
@@ -53,2 +78,3 @@ url += urlParams; | ||
data = data.body; | ||
xhr.setRequestHeader('Content-Type', 'application/json'); | ||
} | ||
@@ -225,5 +251,7 @@ | ||
} | ||
} else if(getterValue && Array.isArray(getterValue.array)) { | ||
} | ||
else if(getterValue && Array.isArray(getterValue.array)) | ||
break; | ||
} | ||
else if(getterValue === false && typeof this.self === 'undefined') | ||
break; | ||
} else | ||
@@ -260,4 +288,14 @@ skipped++; | ||
this.appendChild(newCompNode); | ||
if(newCompNode.isComponent()) | ||
newCompNode.bootstrap(); | ||
} | ||
} | ||
// Clearing all irrelevant children | ||
var fromChild = i; | ||
for(i; i < this.children.length; i++) { | ||
this.removeChild(this.children[i]); | ||
} | ||
this.children.splice(fromChild, i - fromChild); | ||
// Reassigning values | ||
@@ -299,4 +337,3 @@ $scope[iterator.varName] = tempVal; | ||
if(newNode.self && child.self) { | ||
this.self.replaceChild(newNode.self, child.self); | ||
this.children.splice(this.children.indexOf(child), 1, newNode); | ||
this.removeChild(child, newNode) | ||
} else if(newNode.self && !child.self) { | ||
@@ -315,3 +352,3 @@ var childIndex = this.children.indexOf(child); | ||
insertBefore.parentNode.insertBefore(newNode.self, insertBefore); | ||
else | ||
else if(this.self) | ||
this.self.appendChild(newNode.self); | ||
@@ -325,6 +362,19 @@ } else | ||
CompNode.prototype.removeChild = function(child) { | ||
this.self.removeChild(child.self); | ||
child.self = undefined; | ||
child.children.splice(0, child.children.length); | ||
CompNode.prototype.removeChild = function(child, replace) { | ||
if(child.comp instanceof global.Base.Component) { | ||
child.comp.onDestroy(); | ||
delete child.comp; | ||
} else if(Array.isArray(child.children)) { | ||
for(var i = 0; i < child.children.length; i++) | ||
child.removeChild(child.children[i]); | ||
child.children.splice(0, child.children.length); | ||
} | ||
if(replace) { | ||
this.children.splice(this.children.indexOf(child), 1, replace); | ||
this.self.replaceChild(replace.self, child.self); | ||
} else if(child.self) { | ||
this.self.removeChild(child.self); | ||
child.self = undefined; | ||
} | ||
}; | ||
@@ -370,10 +420,36 @@ | ||
Component.prototype.getInput = function(name) { | ||
Component.prototype.getInput = function(name, defaultValue) { | ||
if(this.inputs && this.inputs.hasOwnProperty(name)) | ||
return this.inputs[name]; | ||
else { | ||
throw { message: "Input " + name + " doesn't exist." }; | ||
return defaultValue; | ||
} | ||
}; | ||
Component.prototype.evalWithScope = function(statement) { | ||
var result; | ||
try { | ||
with(this.$scope) { | ||
result = eval(statement); | ||
} | ||
} catch(err) { | ||
console.log(err.message); | ||
} | ||
return result; | ||
}; | ||
Component.prototype.onDestroy = function() { | ||
if(Array.isArray(this.children)) | ||
while(this.children.length > 0) | ||
this.children[0].onDestroy(); | ||
if(Array.isArray(this.subscriptions)) | ||
this.subscriptions.forEach(function(subscription) { | ||
subscription.unsubscribe(); | ||
}); | ||
if(this.parent instanceof Component) | ||
this.parent.children.splice(this.parent.children.indexOf(this), 1); | ||
}; | ||
global.Base = global.Base || {}; | ||
@@ -408,6 +484,13 @@ global.Base.Component = Component; | ||
// Provide the $scope with a function to retrieve inputs | ||
$scope.getInput = function(name) { | ||
$scope[name] = this.getInput(name); | ||
$scope.getInput = function(name, defaultValue) { | ||
$scope[name] = this.getInput(name, defaultValue); | ||
}.bind(comp); | ||
// Provide the $scope with option to hold component subscriptions | ||
$scope.addSubscription = function() { | ||
this.subscriptions = this.subscriptions || []; | ||
for(var i = 0; i < arguments.length; i++) | ||
this.subscriptions.push(arguments[i]); | ||
}.bind(comp); | ||
// Running the constructor | ||
@@ -484,7 +567,9 @@ this.constructor.call(this, $scope, comp.update.bind(comp)); | ||
Model.prototype.init = function() { | ||
this.data = []; | ||
if(typeof this.initFunc !== 'function') { | ||
throw { message: this.name + ": Couldn't initialize the model (initFunc err)" }; | ||
} else { | ||
this.initFunc(function(data) { | ||
this.setData(data); | ||
this.initFunc(function(data, merge) { | ||
this.setData(data, !!merge); | ||
}.bind(this)); | ||
@@ -494,7 +579,13 @@ } | ||
Model.prototype.setData = function(data, /*optional:*/ eventName) { | ||
this.data = data; | ||
Model.prototype.setData = function(data, merge, eventName) { | ||
merge = !!merge; | ||
if(!merge || !this.data) | ||
this.data = data; | ||
else { | ||
for(var i = 0; i < data.length; i++) | ||
this.data.push(data[i]); | ||
} | ||
// If eventName isn't specified default to 'setData'. | ||
this.emit(eventName || 'setData', data); | ||
this.emit(eventName || 'setData', this.data); | ||
}; | ||
@@ -525,5 +616,6 @@ | ||
return { | ||
unsubscribe: function(listener) { | ||
this.events.splice(this.events.indexOf(listener), 1); | ||
}.bind(this, listener) | ||
unsubscribe: function(event, listener) { | ||
var index = this.events[event].indexOf(listener); | ||
this.events[event].splice(index, 1); | ||
}.bind(this, event, listener) | ||
}; | ||
@@ -538,2 +630,17 @@ }; | ||
var Pipe = function(name, func) { | ||
this.name = name; | ||
this.func = func; | ||
}; | ||
Pipe.prototype.transform = function(value, data) { | ||
return this.func(value, data); | ||
}; | ||
global.Base = global.Base || {}; | ||
global.Base.Pipe = Pipe; | ||
})(Function('return this')()); | ||
(function(global) { | ||
var TAG = "[Route]"; | ||
@@ -694,10 +801,33 @@ | ||
ViewNode.prototype.parseNode = function() { | ||
var attrArr = this.self.attributes; | ||
var attrArr = this.self.attributes, | ||
attrName, attrValue; | ||
if(attrArr && attrArr.length > 0) for(var a = 0; a < attrArr.length; a++) { | ||
if(attrArr[a].name === 'controller') { | ||
this.controller = attrArr[a].value; | ||
attrName = attrArr[a].name; | ||
attrValue = attrArr[a].value; | ||
if(attrName === 'controller') { | ||
this.controller = attrValue; | ||
} else { | ||
// Checking for injectables and saving their statements. | ||
var injectable = global.App.getInjectable(attrArr[a].name); | ||
// Checking for injectables and saving their pipes & statements. | ||
var pipeSplit = attrValue.split('|'), | ||
injectable = global.App.getInjectable(attrName), | ||
pipes; | ||
if(injectable instanceof global.Base.Injectable) { | ||
attrValue = pipeSplit[0]; | ||
pipeSplit.splice(0, 1); | ||
if(pipeSplit.length > 0) { | ||
pipes = []; | ||
for(var i = 0; i < pipeSplit.length; i++) { | ||
var pipeParts = pipeSplit[i].trim().split(/:(.+)/g); | ||
pipeParts = pipeParts.filter(function(p) { return p.length > 0 }); | ||
pipes.push({ | ||
name: pipeParts[0], | ||
dataStatement: pipeParts[1] | ||
}); | ||
} | ||
} | ||
if(!Array.isArray(this.directives)) | ||
@@ -707,6 +837,7 @@ this.directives = []; | ||
injectable: injectable, | ||
statement: attrArr[a].value | ||
statement: attrValue, | ||
pipes: pipes | ||
}); | ||
if(!injectable.keepAttribute) { | ||
this.self.removeAttribute(attrArr[a].name); | ||
this.self.removeAttribute(attrName); | ||
// Removing attribute will lower 'attrArr.length' by 1. | ||
@@ -734,2 +865,18 @@ a--; | ||
if(Array.isArray(directive.pipes)) { | ||
var p, pipeObj, pipe; | ||
for(p = 0; p < directive.pipes.length; p++) { | ||
pipeObj = directive.pipes[p]; | ||
pipe = global.App.getPipe(pipeObj.name); | ||
if(pipe instanceof global.Base.Pipe) { | ||
var data; | ||
if(pipeObj.dataStatement) | ||
data = comp.evalWithScope(pipeObj.dataStatement); | ||
// Finally transform the value and apply it | ||
value = pipe.transform(value, data); | ||
} | ||
} | ||
} | ||
// Running modifier on created compNode with getter value | ||
@@ -760,3 +907,2 @@ directive.injectable.modifier(compNode, value); | ||
var childCount = 0; | ||
for(i = 0; i < arr.length; i++) { | ||
@@ -766,5 +912,5 @@ $scope[compNode.iterator.varName] = arr[i]; | ||
childNode.iteratorValue = arr[i]; | ||
if(childNode.self) childCount++; | ||
compNode.appendChild(childNode); | ||
if(childNode.isComponent()) childNode.bootstrap(comp); | ||
if(childNode.isComponent()) | ||
childNode.bootstrap(comp); | ||
} | ||
@@ -840,6 +986,3 @@ | ||
window.onhashchange = function() { | ||
if(!this.navigating) { | ||
this.navigateTo(location.hash); | ||
} | ||
this.navigating = false; | ||
this.navigateTo(location.hash, true); | ||
}.bind(this); | ||
@@ -854,4 +997,4 @@ // Initializing Router | ||
} | ||
this.navigations = 0; | ||
routerInstance = this; | ||
this.navigateTo(location.hash); | ||
@@ -862,3 +1005,3 @@ } else | ||
Router.prototype.navigateTo = function(url) { | ||
Router.prototype.navigateTo = function(url, fromWindow) { | ||
// First validate url | ||
@@ -872,2 +1015,8 @@ if(url[0] === '#') { | ||
if(url !== this.currentPath) { | ||
// An identifier for Router state. | ||
this.navigating = true; | ||
// Updating Router variables | ||
this.navigations++; | ||
this.lastPath = this.currentPath || url; | ||
this.currentPath = url; | ||
@@ -884,3 +1033,6 @@ | ||
} | ||
history.pushState(null, '', '#/' + url); | ||
if(fromWindow) | ||
history.replaceState(null, '', '#/' + url); | ||
else | ||
history.pushState(null, '', '#/' + url); | ||
this.stateChange(resultsObj) | ||
@@ -890,2 +1042,4 @@ } | ||
console.error("UNKNOWN ROUTE " + url); | ||
this.navigating = false; | ||
} | ||
@@ -1006,4 +1160,5 @@ }; | ||
} | ||
// Only set the router data if the system is not already in routing process | ||
// if(changes > 0 && !this.navigating) { | ||
if(changes > 0) { | ||
this.navigating = true; | ||
var currUrl = location.hash; | ||
@@ -1014,4 +1169,10 @@ currUrl = currUrl.split('?')[0] + '?'; | ||
} | ||
location.hash = currUrl.substr(0, currUrl.length - 1); | ||
this.currentPath = location.hash.substr(2, location.hash.length); | ||
currUrl = currUrl.substr(2, currUrl.length - 3); | ||
if(!this.navigating) | ||
history.pushState(null, '', '#/' + currUrl); | ||
else | ||
history.replaceState(null, '', '#/' + currUrl); | ||
this.currentPath = currUrl; | ||
} | ||
@@ -1034,2 +1195,7 @@ if(this.state) this.state.params = params; | ||
Router.prototype.isLandingPage = function() { | ||
return this.lastPath === this.currentPath | ||
&& this.navigations === 1; | ||
}; | ||
Router.prototype.params = function() { | ||
@@ -1048,4 +1214,5 @@ return (this.state && typeof this.state.params === 'object') ? | ||
var Controllers = {}, | ||
Models = {}, | ||
Injectables = {}, | ||
Models = {}, | ||
Pipes = {}, | ||
routerInstance; | ||
@@ -1058,7 +1225,10 @@ | ||
}, | ||
getModel: function(name) { | ||
return Models[name]; | ||
}, | ||
getInjectable: function(name) { | ||
return Injectables[name]; | ||
}, | ||
getModel: function(name) { | ||
return Models[name]; | ||
getPipe: function(name) { | ||
return Pipes[name]; | ||
}, | ||
@@ -1069,4 +1239,5 @@ | ||
Controller: generateController, | ||
Model: createModel, | ||
Injectable: generateInjectable, | ||
Model: createModel, | ||
Pipe: generatePipe, | ||
Router: getRouter | ||
@@ -1083,3 +1254,3 @@ }; | ||
} catch(err) { | ||
console.error(TAG, err.message); | ||
console.error(TAG, err.message || err); | ||
} | ||
@@ -1123,2 +1294,11 @@ return result; | ||
function generatePipe(name, func) { | ||
var TAG = "[Pipe]"; | ||
try { | ||
Pipes[name] = new global.Base.Pipe(name, func); | ||
} catch(err) { | ||
console.error(TAG, err.message); | ||
} | ||
} | ||
function getRouter(routes) { | ||
@@ -1155,7 +1335,12 @@ if(!routerInstance) | ||
modifier: function(compNode, value) { | ||
var total = 0; | ||
for(var className in value) if(value.hasOwnProperty(className)) { | ||
value[className] ? | ||
compNode.self.classList.add(className) : | ||
if(value[className]) { | ||
compNode.self.classList.add(className); | ||
total++; | ||
} else | ||
compNode.self.classList.remove(className); | ||
} | ||
if(total === 0 && compNode.self.classList.length === 0) | ||
compNode.self.removeAttribute('class'); | ||
} | ||
@@ -1247,3 +1432,3 @@ }); | ||
try { | ||
callback(e.target); | ||
callback(e.currentTarget); | ||
} catch(err) { | ||
@@ -1313,5 +1498,2 @@ console.error('[Injectable]', err); | ||
}, | ||
compare: function(oldVal, newVal) { | ||
return oldVal.array === newVal.array; | ||
}, | ||
modifier: function(compNode, value) { | ||
@@ -1321,2 +1503,5 @@ compNode.self = document.createElement('iterator'); | ||
compNode.iterator = value; | ||
}, | ||
compare: function(oldVal, newVal) { | ||
return oldVal.array === newVal.array; | ||
} | ||
@@ -1354,2 +1539,4 @@ }); | ||
modifier: function(compNode, value) { | ||
if(compNode.self instanceof HTMLInputElement) | ||
compNode.self.value = value; | ||
compNode.self.innerHTML = value; | ||
@@ -1356,0 +1543,0 @@ } |
@@ -1,1 +0,1 @@ | ||
Function("return this")().Config={viewOptions:{templatesFolder:"app"}},function(t){var e={get:function(t,e,r){var n=new XMLHttpRequest;if("object"==typeof e.headers)for(var i in e.headers)e.headers.hasOwnProperty(i)&&n.setRequestHeader(i,e.headers[i]);if("object"==typeof e.params){var s="?";for(var o in e.params)e.params.hasOwnProperty(o)&&(s+=o+"="+e.params[o]+"&");t+=s}n.open("GET",t,!0),n.onreadystatechange=function(){200===n.status&&4===n.readyState&&r(JSON.parse(n.responseText))},n.send()},post:function(t,e,r){var n=new XMLHttpRequest;if(n.open("POST",t,!0),"object"==typeof e.headers)for(var i in e.headers)e.headers.hasOwnProperty(i)&&n.setRequestHeader(i,e.headers[i]);"object"==typeof e.body&&(e=e.body),n.onreadystatechange=function(){200===n.status&&4===n.readyState&&r(JSON.parse(n.responseText))},n.send(JSON.stringify(e))}};t.Utils=t.Utils||{},t.Utils.Http=e}(Function("return this")()),function(t){var e={updateObject:function(t,r,n){if(t&&r){var i=0;if("object"==typeof t&&"object"==typeof r)for(var s in r)if(r.hasOwnProperty(s)&&!function(t){if(Array.isArray(n)){for(var e=0;e<n.length;e++)if(t===n[e])return!0}else if(t===n)return!0;return!1}(s))switch(typeof r[s]){case"object":Array.isArray(r[s])?console.warning('[Utils:Object] "updateObject" does not support merging arrays.'):i+=e.updateObject(t[s],r[s]);break;default:t[s]!==r[s]&&(t[s]=r[s],i++)}}return i}};t.Utils=t.Utils||{},t.Utils.Object=e}(Function("return this")()),function(t){var e=function(){this.listeners=[],this.lastMessage=void 0};e.prototype={subscribe:function(t){if("function"==typeof t)return this.listeners.push(t),{unsubscribe:this.unsubscribe.bind(this,t)};console.error(t,"is not a function. Cannot subscribe to Observable.")},unsubscribe:function(t){for(var e=this.listeners.length-1;e>=0;e--)this.listeners[e]===t&&this.listeners.splice(e,1)},next:function(t){this.listeners.forEach(function(e){e(t)}),this.lastMessage=t}},t.Utils=t.Utils||{},t.Utils.Observable=e}(Function("return this")()),function(t){var e={toKeyValueArray:function(t,e){"string"!=typeof e&&(e=",");var r,n=[];return t.split(e).forEach(function(t){r=/([\w-]+): *(.+)/g.exec(t.trim()),n.push({key:r[1],value:r[2]})}),n},toDictionary:function(t,e,r){"string"!=typeof e&&(e=","),"string"!=typeof r&&(r=":");var n,i={};return t.split(e).forEach(function(t){var e=new RegExp("([\\w-]+)"+r+" *(.+)");n=e.exec(t.trim()),Array.isArray(n)&&3===n.length&&(i[n[1]]=n[2])}),i}};t.Utils=t.Utils||{},t.Utils.String=e}(Function("return this")()),function(t){var e=function(t,e){this.viewNode=t,this.self=t.self.cloneNode(!1),Array.isArray(e)&&(this.values=e),this.children=[]};e.prototype.compare=function(t){var r=!1,n=t.$scope;if(Array.isArray(this.viewNode.directives))for(var i,s,o=this.viewNode.directives,a=0,l=0;l<o.length;l++)if(o[l])if(i=o[l].injectable,s=i.getter(o[l].statement,t),i.compare(this.values[l-a],s)){if(s&&Array.isArray(s.array))break}else{if(this.values[l-a]=s,!i.justModify||!this.self){r=!0;break}i.modifier(this,s)}else a++;if(r){var c=this.viewNode.generate(t);this.iteratorValue&&(c.iteratorValue=this.iteratorValue),this.parent.replaceChild(c,this),c.isComponent()&&c.bootstrap(t)}else if(this.multipleNodes){var h,u=this.viewNode,p=this.iterator,f=n[p.varName],d=u.directives[l],m=l;for(u.directives[m]=void 0,l=0;l<p.array.length;l++)n[p.varName]=p.array[l],this.children[l]instanceof e?(this.children[l].compare(t),this.children[l].iteratorValue=p.array[l]):((h=u.generate(t)).iteratorValue=p.array[l],this.appendChild(h));n[p.varName]=f,u.directives[m]=d}else this.children.forEach(function(e){e.compare(t)})},e.prototype.appendChild=function(t){this.children.push(t),t.parent=this,t.self&&this.self.appendChild(t.self)},e.prototype.replaceChild=function(t,e){if(t.parent=e.parent,t.self&&e.self)this.self.replaceChild(t.self,e.self),this.children.splice(this.children.indexOf(e),1,t);else if(t.self&&!e.self){var r=this.children.indexOf(e);if(r>=0){this.children.splice(r,1,t);for(var n;!n&&r<this.children.length-1;)this.children[++r].self&&(n=this.children[r].self);n?n.parentNode.insertBefore(t.self,n):this.self.appendChild(t.self)}else console.error("CompNode: replaceChild failed, 2nd parameter is not a child of this node.")}else!t.self&&e.self&&this.removeChild(e)},e.prototype.removeChild=function(t){this.self.removeChild(t.self),t.self=void 0,t.children.splice(0,t.children.length)},e.prototype.isComponent=function(){return this.self&&1===this.self.nodeType&&(this.viewNode.controller||"string"==typeof this.self.getAttribute("controller"))&&!this.iterator},e.prototype.bootstrap=function(e){this.comp=t.Core.Bootstrap(this.self,e,this.inputs),this.self=this.comp.nodeTree.self},t.Base=t.Base||{},t.Base.CompNode=e}(Function("return this")()),function(t){var e=function(t,r,n){this.el=t,r instanceof e&&(this.parent=r,r.children=r.children||[],r.children.push(this)),this.$scope=n};e.prototype.setView=function(t){this.el.parentNode.replaceChild(t.self,this.el),this.nodeTree=t,this.el=t.self},e.prototype.update=function(){this.nodeTree instanceof t.Base.CompNode&&this.nodeTree.compare(this)},e.prototype.getInput=function(t){if(this.inputs&&this.inputs.hasOwnProperty(t))return this.inputs[t];throw{message:"Input "+t+" doesn't exist."}},t.Base=t.Base||{},t.Base.Component=e}(Function("return this")()),function(t){var e=function(t,e,r){this.name=t,this.view=e,this.constructor=r};e.prototype.generateComponent=function(e,r,n){var i={},s=new t.Base.Component(e,r,i);return e.innerHTML.length>0&&(s.subView=new t.Base.View("content-outlet",null,e.innerHTML)),"object"==typeof n&&(s.inputs=n),i.getInput=function(t){i[t]=this.getInput(t)}.bind(s),this.constructor.call(this,i,s.update.bind(s)),s.setView(this.view.generate(s)),s},t.Base=t.Base||{},t.Base.Controller=e}(Function("return this")()),function(global){var TAG="[Injectable]",Injectable=function(t,e){this.name=t;try{this.modifier=e.modifier.bind(this)}catch(t){console.error(TAG,this.name+": No modifier set.")}"function"==typeof e.getter&&(this.getter=e.getter.bind(this)),"function"==typeof e.compare&&(this.compare=e.compare.bind(this)),this.keepAttribute=!!e.keepAttribute,this.justModify=!!e.justModify,this.useComponentInGetter=!!e.useComponentInGetter};Injectable.prototype.getter=function(statement,comp){var result;try{with(comp.$scope)result=eval(statement)}catch(t){console.error(TAG,this.name+":","Couldn't evaluate '"+statement+"'.")}return result},Injectable.prototype.compare=function(t,e){if(t===e)return!0},global.Base=global.Base||{},global.Base.Injectable=Injectable}(Function("return this")()),function(t){var e=function(t,e){this.name=t,this.initFunc=e,this.events={},this.init()};e.prototype.init=function(){if("function"!=typeof this.initFunc)throw{message:this.name+": Couldn't initialize the model (initFunc err)"};this.initFunc(function(t){this.setData(t)}.bind(this))},e.prototype.setData=function(t,e){this.data=t,this.emit(e||"setData",t)},e.prototype.emit=function(){for(var t,e=[],r=0;r<arguments.length;r++)0===r?t=arguments[r]:e.push(arguments[r]);if(Array.isArray(this.events[t]))for(var n=0;n<this.events[t].length;n++)this.events[t][n].apply(void 0,e)},e.prototype.subscribe=function(t,e){return this.events[t]||(this.events[t]=[]),this.events[t].push(e),{unsubscribe:function(t){this.events.splice(this.events.indexOf(t),1)}.bind(this,e)}},t.Base=t.Base||{},t.Base.Model=e}(Function("return this")()),function(t){var e=function(t){if(!t.hasOwnProperty("path"))throw{message:"[Route] Route must have a path."};if(this.path=t.path.split("/"),t.hasOwnProperty("controller"))this.controller=t.controller;else{if(!t.hasOwnProperty("redirect"))throw{message:"[Route] Route must have a controller."};this.redirect=t.redirect}Array.isArray(t.children)&&(this.children=t.children.map(function(t){return new e(t)}))};e.prototype.checkUrl=function(t,e){var r=0;if(this.path.length>t.length)return!1;for(var n=0;n<this.path.length;n++)this.path[n]===t[n]&&r++;if(this.path.length===r){if(this.redirect)return{redirect:this.redirect};if(Array.isArray(e)||(e=[]),e.push(this.controller),this.path.length<t.length){if(!Array.isArray(this.children))return!1;var i=!1;for(t.splice(0,r),n=0;n<this.children.length;n++)if(this.children[n].checkUrl(t,e)){i=e;break}return i}if(Array.isArray(this.children))for(var s=this.children.filter(function(t){return 1===t.path.length&&""===t.path[0]});Array.isArray(s)&&s.length>0;)s=s[0],e.push(s.controller),Array.isArray(s.children)&&(s=s.children.filter(function(t){return 0===t.path.length}));return{controllers:e}}return!1},t.Base=t.Base||{},t.Base.Route=e}(Function("return this")()),function(t){var e=t.Config.viewOptions,r=function(t,e,r){this.name=t,r?this.templateSrc=r:this.loadTemplate(e),this.buildNodeTree()};r.prototype.loadTemplate=function(t){var r=e.templatesFolder+"/"+t+this.name+".html";r=r.replace(/\/\//g,"/"),this.templateSrc=function(t){var e=new XMLHttpRequest;return e.open("GET",t,!1),e.send(),e.responseText}(r)},r.prototype.buildNodeTree=function(){function e(r){for(var n,i=new t.Base.ViewNode(r),s=0;s<r.childNodes.length;s++)(n=e(r.childNodes[s])).parent=i,i.children.push(n);return i}var r=document.createElement("temp");r.innerHTML=this.templateSrc;for(var n=new t.Base.ViewNode(document.createElement(this.name)),i=0;i<r.childNodes.length;i++)n.children.push(e(r.childNodes[i]));this.nodeTree=n},r.prototype.generate=function(e){for(var r=new t.Base.CompNode(this.nodeTree),n=0;n<this.nodeTree.children.length;n++)r.appendChild(this.nodeTree.children[n].generate(e));return r},t.Base=t.Base||{},t.Base.View=r}(Function("return this")()),function(t){var e=function(t){this.self=t,this.children=[],this.parseNode()};e.prototype.parseNode=function(){var e=this.self.attributes;if(e&&e.length>0)for(var r=0;r<e.length;r++)if("controller"===e[r].name)this.controller=e[r].value;else{var n=t.App.getInjectable(e[r].name);n instanceof t.Base.Injectable&&(Array.isArray(this.directives)||(this.directives=[]),this.directives.push({injectable:n,statement:e[r].value}),n.keepAttribute||(this.self.removeAttribute(e[r].name),r--))}},e.prototype.generate=function(e){var r=new t.Base.CompNode(this),n=e.$scope;if(Array.isArray(this.directives)){var i,s;for(r.values=[],s=0;s<this.directives.length&&r.self;s++)if(this.directives[s]){var o=(i=this.directives[s]).injectable.getter(i.statement,e);if(i.injectable.modifier(r,o),r.values.push(o),r.multipleNodes)break}}if(r.self)if(r.multipleNodes){var a,l=r.iterator.array,c=n[r.iterator.varName],h=this.directives[s],u=s;this.directives[s]=void 0;for(s=0;s<l.length;s++)n[r.iterator.varName]=l[s],(a=this.generate(e)).iteratorValue=l[s],a.self&&0,r.appendChild(a),a.isComponent()&&a.bootstrap(e);n[r.iterator.varName]=c,this.directives[u]=h}else!function(t,r){for(var n,i=0;i<t.children.length;i++)n=t.children[i].generate(e),r.appendChild(n),n.replaceSelfWith&&(n.self.parentNode.replaceChild(n.replaceSelfWith.self,n.self),delete n.replaceSelfWith),n.isComponent()&&n.bootstrap(e)}(this,r);return r},t.Base=t.Base||{},t.Base.ViewNode=e}(Function("return this")()),function(t){function e(e){var r=e.getAttribute("controller");if("string"==typeof r){var n=t.App.getController(r);if(n instanceof t.Base.Controller)return n}}t.Core=t.Core||{},t.Core.Bootstrap=function(r,n,i){if(1===r.nodeType){var s=e(r);if(s instanceof t.Base.Controller)return s.generateComponent(r,n,i);throw{message:"Controller "+r.getAttribute("controller")+" not found."}}throw{message:"Cannot bootstrap a non-element object."}}}(Function("return this")()),function(t){var e,r=function(r){if(e)return e;if(!Array.isArray(r))throw{message:"[Router] 'routes' should be an array of routes."};window.onhashchange=function(){this.navigating||this.navigateTo(location.hash),this.navigating=!1}.bind(this);try{this.routes=r.map(function(e){return new t.Base.Route(e)})}catch(t){throw t}e=this,this.navigateTo(location.hash)};r.prototype.navigateTo=function(e){if("#"===e[0]&&(e="/"!==e[1]?e.slice(1,e.length):e.slice(2,e.length)),e!==this.currentPath){this.currentPath=e;var r=this.parseUrl(e);if(r){var n=r.results;n.redirect?this.navigateTo(n.redirect):("string"==typeof r.params&&r.params.length>0&&(r.params=t.Utils.String.toDictionary(r.params,"&","=")),history.pushState(null,"","#/"+e),this.stateChange(r))}else console.error("UNKNOWN ROUTE "+e)}},r.prototype.parseUrl=function(t){var e=t.split("?"),r=e[1];e=e[0].split("/").filter(function(t){return"#"!==t});for(var n,i=0;i<this.routes.length&&!n;i++)n=this.routes[i].checkUrl(e);return!!n&&{results:n,params:r}},r.prototype.stateChange=function(e){var r=e.results;if(this.state){for(var n,i=0;i<r.controllers.length;i++)if(r.controllers[i]!==this.state.controllers[i]){for(var s=i;s<r.controllers.length;s++)this.state.controllers[s]=r.controllers[s];s<this.state.controllers.length&&this.state.controllers.splice(s,this.state.controllers.length),this.state.nextController=r.controllers[i],n=this.state.compNodes[i];break}if("object"==typeof e.params){for(var o in e.params)e.params.hasOwnProperty(o)&&this.updateParam(o,e.params[o]);for(o in this.state.params)this.state.params.hasOwnProperty(o)&&(e.params[o]||this.updateParam(o))}else{var a=this.state.params;if("object"==typeof a)for(o in a)a.hasOwnProperty(o)&&this.updateParam(o)}if(n instanceof t.Base.CompNode){var l=n.comp,c=n.viewNode.generate(l);n.parent.replaceChild(c,n),c.bootstrap(l.parent)}}else this.state={controllers:r.controllers,params:e.params,nextController:r.controllers[0]}},r.prototype.nextController=function(t){if(this.state&&this.state.nextController){var e=this.state.nextController;this.state.nextController=void 0;for(var r=0;r<this.state.controllers.length;r++)if(e===this.state.controllers[r]){Array.isArray(this.state.compNodes)||(this.state.compNodes=[]),this.state.nextController=this.state.controllers[r+1],this.state.compNodes[r]=t;break}return e}console.error("[Router]","Failed to load next controller, end of list.")},r.prototype.updateParam=function(e,r){var n=this.params(),i=0;if(n[e]!==r&&(r&&0!==r.length?n[e]=r:delete n[e],i++,this.paramSubscriptions)){var s=this.paramSubscriptions[e];s instanceof t.Utils.Observable&&s.next(r)}if(i>0){this.navigating=!0;var o=location.hash;o=o.split("?")[0]+"?";for(var a in n)n.hasOwnProperty(a)&&(o+=a+"="+n[a]+"&");location.hash=o.substr(0,o.length-1),this.currentPath=location.hash.substr(2,location.hash.length)}this.state&&(this.state.params=n)},r.prototype.subscribeToParam=function(e,r){"object"!=typeof this.paramSubscriptions&&(this.paramSubscriptions={});var n=this.paramSubscriptions;n[e]instanceof t.Utils.Observable||(n[e]=new t.Utils.Observable);var i=n[e].subscribe(r);return this.state&&this.state.params&&r(this.state.params[e]),i},r.prototype.params=function(){return this.state&&"object"==typeof this.state.params?this.state.params:{}},t.Core=t.Core||{},t.Core.Router=r}(Function("return this")()),function(t){var e,r={},n={},i={};t.App={getController:function(t){return r[t]},getInjectable:function(t){return n[t]},getModel:function(t){return i[t]},Bootstrap:function(e){var r="[Bootstrap]",n=document.querySelector('*[controller="'+e+'"]');if(n&&1===n.nodeType){try{var i=t.Core.Bootstrap(n)}catch(t){console.error(r,t.message)}return i}console.error(r,"Could not find placeholder for '"+e+"'.")},Controller:function(e,n,i){try{var s=new t.Base.View(e,n);r[e]=new t.Base.Controller(e,s,i)}catch(t){console.error("[Controller]",t.message)}},Injectable:function(e,r){try{n[e]=new t.Base.Injectable(e,r)}catch(t){console.error("[Injectable]",t.message)}},Model:function(e,r){try{i[e]=new t.Base.Model(e,r)}catch(t){console.error("[Model]",t.message)}},Router:function(r){return e||(e=new t.Core.Router(r)),e}}}(Function("return this")()),function(global){global.App.Injectable("bind-class",{justModify:!0,getter:function(statement,comp){var classes=global.Utils.String.toDictionary(statement),value;try{with(comp.$scope)for(var className in classes)classes.hasOwnProperty(className)&&(value=eval(classes[className]),classes[className]=!!value)}catch(t){throw{message:this.name+": "+t.message}}return classes},modifier:function(t,e){for(var r in e)e.hasOwnProperty(r)&&(e[r]?t.self.classList.add(r):t.self.classList.remove(r))}})}(Function("return this")()),function(global){global.App.Injectable("bind-if",{getter:function(statement,comp){var result;try{with(comp.$scope)result=eval(statement)}catch(t){throw this.name+": Couldn't evaluate '"+statement+"'."}return!!result},modifier:function(t,e){e||(t.self=void 0)}})}(Function("return this")()),Function("return this")().App.Injectable("content-outlet",{getter:function(t,e){return e},modifier:function(t,e){e.subView&&(t.replaceSelfWith=e.subView.generate(e.parent))}}),function(global){global.App.Injectable("bind-events",{getter:function(statement,comp){var events=global.Utils.String.toDictionary(statement),matches,funcName,variables;for(var event in events)if(events.hasOwnProperty(event)){var regEx=new RegExp("^(.+)\\((.*)\\)$");if(matches=events[event].match(regEx),!Array.isArray(matches)||matches.length<2)throw this.name+": Invalid statement, expecting: [event: func()]";funcName=matches[1],variables=matches[2].split(",");for(var i=0;i<variables.length;i++)try{with(comp.$scope)variables[i]=eval(variables[i])}catch(t){throw this.name+": "+t.message}events[event]=function(funcName,variables,el){var func;try{with(comp.$scope)func=eval(funcName)}catch(t){throw this.name+": "+t.message}"function"==typeof func&&func.apply(el,variables)}.bind(this,funcName,variables)}return events},modifier:function(t,e){for(var r in e)e.hasOwnProperty(r)&&t.self.addEventListener(r,function(t,e){e&&e.preventDefault&&e.preventDefault();try{t(e.target)}catch(t){console.error("[Injectable]",t)}}.bind(this,e[r]))},compare:function(t,e){return!0}})}(Function("return this")()),function(global){global.App.Injectable("input",{getter:function(statement,comp){var inputs=global.Utils.String.toDictionary(statement);try{for(var input in inputs)if(inputs.hasOwnProperty(input))with(comp.$scope)inputs[input]=eval(inputs[input])}catch(t){throw{message:this.name+": "+t.message}}return inputs},modifier:function(t,e){t.inputs=e},compare:function(t,e){var r=!0;for(var n in e)if(e.hasOwnProperty(n)&&e[n]!==t[n]){r=!1;break}return r}})}(Function("return this")()),function(global){global.App.Injectable("bind-for",{getter:function(statement,comp){for(var words=statement.split(" "),result={},w=0;w<words.length;w++)if("in"===words[w]&&w>0&&w<words.length-1){try{with(comp.$scope)result.array=eval(words[w+1])}catch(t){throw t}result.varName=words[w-1]}return result},compare:function(t,e){return t.array===e.array},modifier:function(t,e){t.self=document.createElement("iterator"),t.multipleNodes=!0,t.iterator=e}})}(Function("return this")()),function(t){t.App.Injectable("router-outlet",{getter:function(t,e){return!0},modifier:function(e,r){var n=t.App.Router().nextController(e);t.App.getController(n)instanceof t.Base.Controller&&e.self.setAttribute("controller",n)}})}(Function("return this")()),Function("return this")().App.Injectable("bind-value",{justModify:!0,modifier:function(t,e){t.self.innerHTML=e}}); | ||
Function("return this")().Config={viewOptions:{templatesFolder:"app"}},function(t){var e={clean:function(t){if(!Array.isArray(t))throw{message:"[Array:Clean] Cannot work with a non-array!"};for(var e=0;e<t.length;e++)void 0===t[e]?(t.splice(e,1),e--):Array.isArray(t[e])&&this.clean(t[e])}};t.Utils=t.Utils||{},t.Utils.Array=e}(Function("return this")()),function(t){var e={get:function(t,e,r){var n=new XMLHttpRequest;if("object"==typeof e.headers)for(var i in e.headers)e.headers.hasOwnProperty(i)&&n.setRequestHeader(i,e.headers[i]);if("object"==typeof e.params){var s="?";for(var o in e.params)if(e.params.hasOwnProperty(o))if(Array.isArray(e.params[o]))for(var a=0;a<e.params[o].length;a++)s+=o+"="+e.params[o][a]+"&";else s+=o+"="+e.params[o]+"&";t+=s}n.open("GET",t,!0),n.onreadystatechange=function(){200===n.status&&4===n.readyState&&r(JSON.parse(n.responseText))},n.send()},post:function(t,e,r){var n=new XMLHttpRequest;if(n.open("POST",t,!0),"object"==typeof e.headers)for(var i in e.headers)e.headers.hasOwnProperty(i)&&n.setRequestHeader(i,e.headers[i]);"object"==typeof e.body&&(e=e.body,n.setRequestHeader("Content-Type","application/json")),n.onreadystatechange=function(){200===n.status&&4===n.readyState&&r(JSON.parse(n.responseText))},n.send(JSON.stringify(e))}};t.Utils=t.Utils||{},t.Utils.Http=e}(Function("return this")()),function(t){var e={updateObject:function(t,r,n){if(t&&r){var i=0;if("object"==typeof t&&"object"==typeof r)for(var s in r)if(r.hasOwnProperty(s)&&!function(t){if(Array.isArray(n)){for(var e=0;e<n.length;e++)if(t===n[e])return!0}else if(t===n)return!0;return!1}(s))switch(typeof r[s]){case"object":Array.isArray(r[s])?console.warning('[Utils:Object] "updateObject" does not support merging arrays.'):i+=e.updateObject(t[s],r[s]);break;default:t[s]!==r[s]&&(t[s]=r[s],i++)}}return i}};t.Utils=t.Utils||{},t.Utils.Object=e}(Function("return this")()),function(t){var e=function(){this.listeners=[],this.lastMessage=void 0};e.prototype={subscribe:function(t){if("function"==typeof t)return this.listeners.push(t),{unsubscribe:this.unsubscribe.bind(this,t)};console.error(t,"is not a function. Cannot subscribe to Observable.")},unsubscribe:function(t){for(var e=this.listeners.length-1;e>=0;e--)this.listeners[e]===t&&this.listeners.splice(e,1)},next:function(t){this.listeners.forEach(function(e){e(t)}),this.lastMessage=t}},t.Utils=t.Utils||{},t.Utils.Observable=e}(Function("return this")()),function(t){var e={toKeyValueArray:function(t,e){"string"!=typeof e&&(e=",");var r,n=[];return t.split(e).forEach(function(t){r=/([\w-]+): *(.+)/g.exec(t.trim()),n.push({key:r[1],value:r[2]})}),n},toDictionary:function(t,e,r){"string"!=typeof e&&(e=","),"string"!=typeof r&&(r=":");var n,i={};return t.split(e).forEach(function(t){var e=new RegExp("([\\w-]+)"+r+" *(.+)");n=e.exec(t.trim()),Array.isArray(n)&&3===n.length&&(i[n[1]]=n[2])}),i}};t.Utils=t.Utils||{},t.Utils.String=e}(Function("return this")()),function(t){var e=function(t,e){this.viewNode=t,this.self=t.self.cloneNode(!1),Array.isArray(e)&&(this.values=e),this.children=[]};e.prototype.compare=function(t){var r=!1,n=t.$scope;if(Array.isArray(this.viewNode.directives))for(var i,s,o=this.viewNode.directives,a=0,l=0;l<o.length;l++)if(o[l])if(i=o[l].injectable,s=i.getter(o[l].statement,t),i.compare(this.values[l-a],s)){if(s&&Array.isArray(s.array))break;if(!1===s&&void 0===this.self)break}else{if(this.values[l-a]=s,!i.justModify||!this.self){r=!0;break}i.modifier(this,s)}else a++;if(r){var c=this.viewNode.generate(t);this.iteratorValue&&(c.iteratorValue=this.iteratorValue),this.parent.replaceChild(c,this),c.isComponent()&&c.bootstrap(t)}else if(this.multipleNodes){var h,p=this.viewNode,u=this.iterator,f=n[u.varName],d=p.directives[l],m=l;for(p.directives[m]=void 0,l=0;l<u.array.length;l++)n[u.varName]=u.array[l],this.children[l]instanceof e?(this.children[l].compare(t),this.children[l].iteratorValue=u.array[l]):((h=p.generate(t)).iteratorValue=u.array[l],this.appendChild(h),h.isComponent()&&h.bootstrap());var v=l;for(l;l<this.children.length;l++)this.removeChild(this.children[l]);this.children.splice(v,l-v),n[u.varName]=f,p.directives[m]=d}else this.children.forEach(function(e){e.compare(t)})},e.prototype.appendChild=function(t){this.children.push(t),t.parent=this,t.self&&this.self.appendChild(t.self)},e.prototype.replaceChild=function(t,e){if(t.parent=e.parent,t.self&&e.self)this.removeChild(e,t);else if(t.self&&!e.self){var r=this.children.indexOf(e);if(r>=0){this.children.splice(r,1,t);for(var n;!n&&r<this.children.length-1;)this.children[++r].self&&(n=this.children[r].self);n?n.parentNode.insertBefore(t.self,n):this.self&&this.self.appendChild(t.self)}else console.error("CompNode: replaceChild failed, 2nd parameter is not a child of this node.")}else!t.self&&e.self&&this.removeChild(e)},e.prototype.removeChild=function(e,r){if(e.comp instanceof t.Base.Component)e.comp.onDestroy(),delete e.comp;else if(Array.isArray(e.children)){for(var n=0;n<e.children.length;n++)e.removeChild(e.children[n]);e.children.splice(0,e.children.length)}r?(this.children.splice(this.children.indexOf(e),1,r),this.self.replaceChild(r.self,e.self)):e.self&&(this.self.removeChild(e.self),e.self=void 0)},e.prototype.isComponent=function(){return this.self&&1===this.self.nodeType&&(this.viewNode.controller||"string"==typeof this.self.getAttribute("controller"))&&!this.iterator},e.prototype.bootstrap=function(e){this.comp=t.Core.Bootstrap(this.self,e,this.inputs),this.self=this.comp.nodeTree.self},t.Base=t.Base||{},t.Base.CompNode=e}(Function("return this")()),function(global){var Component=function(t,e,r){this.el=t,e instanceof Component&&(this.parent=e,e.children=e.children||[],e.children.push(this)),this.$scope=r};Component.prototype.setView=function(t){this.el.parentNode.replaceChild(t.self,this.el),this.nodeTree=t,this.el=t.self},Component.prototype.update=function(){this.nodeTree instanceof global.Base.CompNode&&this.nodeTree.compare(this)},Component.prototype.getInput=function(t,e){return this.inputs&&this.inputs.hasOwnProperty(t)?this.inputs[t]:e},Component.prototype.evalWithScope=function(statement){var result;try{with(this.$scope)result=eval(statement)}catch(t){console.log(t.message)}return result},Component.prototype.onDestroy=function(){if(Array.isArray(this.children))for(;this.children.length>0;)this.children[0].onDestroy();Array.isArray(this.subscriptions)&&this.subscriptions.forEach(function(t){t.unsubscribe()}),this.parent instanceof Component&&this.parent.children.splice(this.parent.children.indexOf(this),1)},global.Base=global.Base||{},global.Base.Component=Component}(Function("return this")()),function(t){var e=function(t,e,r){this.name=t,this.view=e,this.constructor=r};e.prototype.generateComponent=function(e,r,n){var i={},s=new t.Base.Component(e,r,i);return e.innerHTML.length>0&&(s.subView=new t.Base.View("content-outlet",null,e.innerHTML)),"object"==typeof n&&(s.inputs=n),i.getInput=function(t,e){i[t]=this.getInput(t,e)}.bind(s),i.addSubscription=function(){this.subscriptions=this.subscriptions||[];for(var t=0;t<arguments.length;t++)this.subscriptions.push(arguments[t])}.bind(s),this.constructor.call(this,i,s.update.bind(s)),s.setView(this.view.generate(s)),s},t.Base=t.Base||{},t.Base.Controller=e}(Function("return this")()),function(global){var TAG="[Injectable]",Injectable=function(t,e){this.name=t;try{this.modifier=e.modifier.bind(this)}catch(t){console.error(TAG,this.name+": No modifier set.")}"function"==typeof e.getter&&(this.getter=e.getter.bind(this)),"function"==typeof e.compare&&(this.compare=e.compare.bind(this)),this.keepAttribute=!!e.keepAttribute,this.justModify=!!e.justModify,this.useComponentInGetter=!!e.useComponentInGetter};Injectable.prototype.getter=function(statement,comp){var result;try{with(comp.$scope)result=eval(statement)}catch(t){console.error(TAG,this.name+":","Couldn't evaluate '"+statement+"'.")}return result},Injectable.prototype.compare=function(t,e){if(t===e)return!0},global.Base=global.Base||{},global.Base.Injectable=Injectable}(Function("return this")()),function(t){var e=function(t,e){this.name=t,this.initFunc=e,this.events={},this.init()};e.prototype.init=function(){if(this.data=[],"function"!=typeof this.initFunc)throw{message:this.name+": Couldn't initialize the model (initFunc err)"};this.initFunc(function(t,e){this.setData(t,!!e)}.bind(this))},e.prototype.setData=function(t,e,r){if((e=!!e)&&this.data)for(var n=0;n<t.length;n++)this.data.push(t[n]);else this.data=t;this.emit(r||"setData",this.data)},e.prototype.emit=function(){for(var t,e=[],r=0;r<arguments.length;r++)0===r?t=arguments[r]:e.push(arguments[r]);if(Array.isArray(this.events[t]))for(var n=0;n<this.events[t].length;n++)this.events[t][n].apply(void 0,e)},e.prototype.subscribe=function(t,e){return this.events[t]||(this.events[t]=[]),this.events[t].push(e),{unsubscribe:function(t,e){var r=this.events[t].indexOf(e);this.events[t].splice(r,1)}.bind(this,t,e)}},t.Base=t.Base||{},t.Base.Model=e}(Function("return this")()),function(t){var e=function(t,e){this.name=t,this.func=e};e.prototype.transform=function(t,e){return this.func(t,e)},t.Base=t.Base||{},t.Base.Pipe=e}(Function("return this")()),function(t){var e=function(t){if(!t.hasOwnProperty("path"))throw{message:"[Route] Route must have a path."};if(this.path=t.path.split("/"),t.hasOwnProperty("controller"))this.controller=t.controller;else{if(!t.hasOwnProperty("redirect"))throw{message:"[Route] Route must have a controller."};this.redirect=t.redirect}Array.isArray(t.children)&&(this.children=t.children.map(function(t){return new e(t)}))};e.prototype.checkUrl=function(t,e){var r=0;if(this.path.length>t.length)return!1;for(var n=0;n<this.path.length;n++)this.path[n]===t[n]&&r++;if(this.path.length===r){if(this.redirect)return{redirect:this.redirect};if(Array.isArray(e)||(e=[]),e.push(this.controller),this.path.length<t.length){if(!Array.isArray(this.children))return!1;var i=!1;for(t.splice(0,r),n=0;n<this.children.length;n++)if(this.children[n].checkUrl(t,e)){i=e;break}return i}if(Array.isArray(this.children))for(var s=this.children.filter(function(t){return 1===t.path.length&&""===t.path[0]});Array.isArray(s)&&s.length>0;)s=s[0],e.push(s.controller),Array.isArray(s.children)&&(s=s.children.filter(function(t){return 0===t.path.length}));return{controllers:e}}return!1},t.Base=t.Base||{},t.Base.Route=e}(Function("return this")()),function(t){var e=t.Config.viewOptions,r=function(t,e,r){this.name=t,r?this.templateSrc=r:this.loadTemplate(e),this.buildNodeTree()};r.prototype.loadTemplate=function(t){var r=e.templatesFolder+"/"+t+this.name+".html";r=r.replace(/\/\//g,"/"),this.templateSrc=function(t){var e=new XMLHttpRequest;return e.open("GET",t,!1),e.send(),e.responseText}(r)},r.prototype.buildNodeTree=function(){function e(r){for(var n,i=new t.Base.ViewNode(r),s=0;s<r.childNodes.length;s++)(n=e(r.childNodes[s])).parent=i,i.children.push(n);return i}var r=document.createElement("temp");r.innerHTML=this.templateSrc;for(var n=new t.Base.ViewNode(document.createElement(this.name)),i=0;i<r.childNodes.length;i++)n.children.push(e(r.childNodes[i]));this.nodeTree=n},r.prototype.generate=function(e){for(var r=new t.Base.CompNode(this.nodeTree),n=0;n<this.nodeTree.children.length;n++)r.appendChild(this.nodeTree.children[n].generate(e));return r},t.Base=t.Base||{},t.Base.View=r}(Function("return this")()),function(t){var e=function(t){this.self=t,this.children=[],this.parseNode()};e.prototype.parseNode=function(){var e,r,n=this.self.attributes;if(n&&n.length>0)for(var i=0;i<n.length;i++)if(e=n[i].name,r=n[i].value,"controller"===e)this.controller=r;else{var s,o=r.split("|"),a=t.App.getInjectable(e);if(a instanceof t.Base.Injectable){if(r=o[0],o.splice(0,1),o.length>0){s=[];for(var l=0;l<o.length;l++){var c=o[l].trim().split(/:(.+)/g);c=c.filter(function(t){return t.length>0}),s.push({name:c[0],dataStatement:c[1]})}}Array.isArray(this.directives)||(this.directives=[]),this.directives.push({injectable:a,statement:r,pipes:s}),a.keepAttribute||(this.self.removeAttribute(e),i--)}}},e.prototype.generate=function(e){var r=new t.Base.CompNode(this),n=e.$scope;if(Array.isArray(this.directives)){var i,s;for(r.values=[],s=0;s<this.directives.length&&r.self;s++)if(this.directives[s]){var o=(i=this.directives[s]).injectable.getter(i.statement,e);if(Array.isArray(i.pipes)){var a,l,c;for(a=0;a<i.pipes.length;a++)if(l=i.pipes[a],(c=t.App.getPipe(l.name))instanceof t.Base.Pipe){var h;l.dataStatement&&(h=e.evalWithScope(l.dataStatement)),o=c.transform(o,h)}}if(i.injectable.modifier(r,o),r.values.push(o),r.multipleNodes)break}}if(r.self)if(r.multipleNodes){var p,u=r.iterator.array,f=n[r.iterator.varName],d=this.directives[s],m=s;for(this.directives[s]=void 0,s=0;s<u.length;s++)n[r.iterator.varName]=u[s],(p=this.generate(e)).iteratorValue=u[s],r.appendChild(p),p.isComponent()&&p.bootstrap(e);n[r.iterator.varName]=f,this.directives[m]=d}else!function(t,r){for(var n,i=0;i<t.children.length;i++)n=t.children[i].generate(e),r.appendChild(n),n.replaceSelfWith&&(n.self.parentNode.replaceChild(n.replaceSelfWith.self,n.self),delete n.replaceSelfWith),n.isComponent()&&n.bootstrap(e)}(this,r);return r},t.Base=t.Base||{},t.Base.ViewNode=e}(Function("return this")()),function(t){function e(e){var r=e.getAttribute("controller");if("string"==typeof r){var n=t.App.getController(r);if(n instanceof t.Base.Controller)return n}}t.Core=t.Core||{},t.Core.Bootstrap=function(r,n,i){if(1===r.nodeType){var s=e(r);if(s instanceof t.Base.Controller)return s.generateComponent(r,n,i);throw{message:"Controller "+r.getAttribute("controller")+" not found."}}throw{message:"Cannot bootstrap a non-element object."}}}(Function("return this")()),function(t){var e,r=function(r){if(e)return e;if(!Array.isArray(r))throw{message:"[Router] 'routes' should be an array of routes."};window.onhashchange=function(){this.navigateTo(location.hash,!0)}.bind(this);try{this.routes=r.map(function(e){return new t.Base.Route(e)})}catch(t){throw t}this.navigations=0,e=this,this.navigateTo(location.hash)};r.prototype.navigateTo=function(e,r){if("#"===e[0]&&(e="/"!==e[1]?e.slice(1,e.length):e.slice(2,e.length)),e!==this.currentPath){this.navigating=!0,this.navigations++,this.lastPath=this.currentPath||e,this.currentPath=e;var n=this.parseUrl(e);if(n){var i=n.results;i.redirect?this.navigateTo(i.redirect):("string"==typeof n.params&&n.params.length>0&&(n.params=t.Utils.String.toDictionary(n.params,"&","=")),r?history.replaceState(null,"","#/"+e):history.pushState(null,"","#/"+e),this.stateChange(n))}else console.error("UNKNOWN ROUTE "+e);this.navigating=!1}},r.prototype.parseUrl=function(t){var e=t.split("?"),r=e[1];e=e[0].split("/").filter(function(t){return"#"!==t});for(var n,i=0;i<this.routes.length&&!n;i++)n=this.routes[i].checkUrl(e);return!!n&&{results:n,params:r}},r.prototype.stateChange=function(e){var r=e.results;if(this.state){for(var n,i=0;i<r.controllers.length;i++)if(r.controllers[i]!==this.state.controllers[i]){for(var s=i;s<r.controllers.length;s++)this.state.controllers[s]=r.controllers[s];s<this.state.controllers.length&&this.state.controllers.splice(s,this.state.controllers.length),this.state.nextController=r.controllers[i],n=this.state.compNodes[i];break}if("object"==typeof e.params){for(var o in e.params)e.params.hasOwnProperty(o)&&this.updateParam(o,e.params[o]);for(o in this.state.params)this.state.params.hasOwnProperty(o)&&(e.params[o]||this.updateParam(o))}else{var a=this.state.params;if("object"==typeof a)for(o in a)a.hasOwnProperty(o)&&this.updateParam(o)}if(n instanceof t.Base.CompNode){var l=n.comp,c=n.viewNode.generate(l);n.parent.replaceChild(c,n),c.bootstrap(l.parent)}}else this.state={controllers:r.controllers,params:e.params,nextController:r.controllers[0]}},r.prototype.nextController=function(t){if(this.state&&this.state.nextController){var e=this.state.nextController;this.state.nextController=void 0;for(var r=0;r<this.state.controllers.length;r++)if(e===this.state.controllers[r]){Array.isArray(this.state.compNodes)||(this.state.compNodes=[]),this.state.nextController=this.state.controllers[r+1],this.state.compNodes[r]=t;break}return e}console.error("[Router]","Failed to load next controller, end of list.")},r.prototype.updateParam=function(e,r){var n=this.params(),i=0;if(n[e]!==r&&(r&&0!==r.length?n[e]=r:delete n[e],i++,this.paramSubscriptions)){var s=this.paramSubscriptions[e];s instanceof t.Utils.Observable&&s.next(r)}if(i>0){var o=location.hash;o=o.split("?")[0]+"?";for(var a in n)n.hasOwnProperty(a)&&(o+=a+"="+n[a]+"&");o=o.substr(2,o.length-3),this.navigating?history.replaceState(null,"","#/"+o):history.pushState(null,"","#/"+o),this.currentPath=o}this.state&&(this.state.params=n)},r.prototype.subscribeToParam=function(e,r){"object"!=typeof this.paramSubscriptions&&(this.paramSubscriptions={});var n=this.paramSubscriptions;n[e]instanceof t.Utils.Observable||(n[e]=new t.Utils.Observable);var i=n[e].subscribe(r);return this.state&&this.state.params&&r(this.state.params[e]),i},r.prototype.isLandingPage=function(){return this.lastPath===this.currentPath&&1===this.navigations},r.prototype.params=function(){return this.state&&"object"==typeof this.state.params?this.state.params:{}},t.Core=t.Core||{},t.Core.Router=r}(Function("return this")()),function(t){var e,r={},n={},i={},s={};t.App={getController:function(t){return r[t]},getModel:function(t){return n[t]},getInjectable:function(t){return i[t]},getPipe:function(t){return s[t]},Bootstrap:function(e){var r="[Bootstrap]",n=document.querySelector('*[controller="'+e+'"]');if(n&&1===n.nodeType){try{var i=t.Core.Bootstrap(n)}catch(t){console.error(r,t.message||t)}return i}console.error(r,"Could not find placeholder for '"+e+"'.")},Controller:function(e,n,i){try{var s=new t.Base.View(e,n);r[e]=new t.Base.Controller(e,s,i)}catch(t){console.error("[Controller]",t.message)}},Model:function(e,r){try{n[e]=new t.Base.Model(e,r)}catch(t){console.error("[Model]",t.message)}},Injectable:function(e,r){try{i[e]=new t.Base.Injectable(e,r)}catch(t){console.error("[Injectable]",t.message)}},Pipe:function(e,r){try{s[e]=new t.Base.Pipe(e,r)}catch(t){console.error("[Pipe]",t.message)}},Router:function(r){return e||(e=new t.Core.Router(r)),e}}}(Function("return this")()),function(global){global.App.Injectable("bind-class",{justModify:!0,getter:function(statement,comp){var classes=global.Utils.String.toDictionary(statement),value;try{with(comp.$scope)for(var className in classes)classes.hasOwnProperty(className)&&(value=eval(classes[className]),classes[className]=!!value)}catch(t){throw{message:this.name+": "+t.message}}return classes},modifier:function(t,e){var r=0;for(var n in e)e.hasOwnProperty(n)&&(e[n]?(t.self.classList.add(n),r++):t.self.classList.remove(n));0===r&&0===t.self.classList.length&&t.self.removeAttribute("class")}})}(Function("return this")()),function(global){global.App.Injectable("bind-if",{getter:function(statement,comp){var result;try{with(comp.$scope)result=eval(statement)}catch(t){throw this.name+": Couldn't evaluate '"+statement+"'."}return!!result},modifier:function(t,e){e||(t.self=void 0)}})}(Function("return this")()),Function("return this")().App.Injectable("content-outlet",{getter:function(t,e){return e},modifier:function(t,e){e.subView&&(t.replaceSelfWith=e.subView.generate(e.parent))}}),function(global){global.App.Injectable("bind-events",{getter:function(statement,comp){var events=global.Utils.String.toDictionary(statement),matches,funcName,variables;for(var event in events)if(events.hasOwnProperty(event)){var regEx=new RegExp("^(.+)\\((.*)\\)$");if(matches=events[event].match(regEx),!Array.isArray(matches)||matches.length<2)throw this.name+": Invalid statement, expecting: [event: func()]";funcName=matches[1],variables=matches[2].split(",");for(var i=0;i<variables.length;i++)try{with(comp.$scope)variables[i]=eval(variables[i])}catch(t){throw this.name+": "+t.message}events[event]=function(funcName,variables,el){var func;try{with(comp.$scope)func=eval(funcName)}catch(t){throw this.name+": "+t.message}"function"==typeof func&&func.apply(el,variables)}.bind(this,funcName,variables)}return events},modifier:function(t,e){for(var r in e)e.hasOwnProperty(r)&&t.self.addEventListener(r,function(t,e){e&&e.preventDefault&&e.preventDefault();try{t(e.currentTarget)}catch(t){console.error("[Injectable]",t)}}.bind(this,e[r]))},compare:function(t,e){return!0}})}(Function("return this")()),function(global){global.App.Injectable("input",{getter:function(statement,comp){var inputs=global.Utils.String.toDictionary(statement);try{for(var input in inputs)if(inputs.hasOwnProperty(input))with(comp.$scope)inputs[input]=eval(inputs[input])}catch(t){throw{message:this.name+": "+t.message}}return inputs},modifier:function(t,e){t.inputs=e},compare:function(t,e){var r=!0;for(var n in e)if(e.hasOwnProperty(n)&&e[n]!==t[n]){r=!1;break}return r}})}(Function("return this")()),function(global){global.App.Injectable("bind-for",{getter:function(statement,comp){for(var words=statement.split(" "),result={},w=0;w<words.length;w++)if("in"===words[w]&&w>0&&w<words.length-1){try{with(comp.$scope)result.array=eval(words[w+1])}catch(t){throw t}result.varName=words[w-1]}return result},modifier:function(t,e){t.self=document.createElement("iterator"),t.multipleNodes=!0,t.iterator=e},compare:function(t,e){return t.array===e.array}})}(Function("return this")()),function(t){t.App.Injectable("router-outlet",{getter:function(t,e){return!0},modifier:function(e,r){var n=t.App.Router().nextController(e);t.App.getController(n)instanceof t.Base.Controller&&e.self.setAttribute("controller",n)}})}(Function("return this")()),Function("return this")().App.Injectable("bind-value",{justModify:!0,modifier:function(t,e){t.self instanceof HTMLInputElement&&(t.self.value=e),t.self.innerHTML=e}}); |
(function(global) { | ||
var Controllers = {}, | ||
Models = {}, | ||
Injectables = {}, | ||
Models = {}, | ||
Pipes = {}, | ||
routerInstance; | ||
@@ -13,7 +14,10 @@ | ||
}, | ||
getModel: function(name) { | ||
return Models[name]; | ||
}, | ||
getInjectable: function(name) { | ||
return Injectables[name]; | ||
}, | ||
getModel: function(name) { | ||
return Models[name]; | ||
getPipe: function(name) { | ||
return Pipes[name]; | ||
}, | ||
@@ -24,4 +28,5 @@ | ||
Controller: generateController, | ||
Model: createModel, | ||
Injectable: generateInjectable, | ||
Model: createModel, | ||
Pipe: generatePipe, | ||
Router: getRouter | ||
@@ -38,3 +43,3 @@ }; | ||
} catch(err) { | ||
console.error(TAG, err.message); | ||
console.error(TAG, err.message || err); | ||
} | ||
@@ -78,2 +83,11 @@ return result; | ||
function generatePipe(name, func) { | ||
var TAG = "[Pipe]"; | ||
try { | ||
Pipes[name] = new global.Base.Pipe(name, func); | ||
} catch(err) { | ||
console.error(TAG, err.message); | ||
} | ||
} | ||
function getRouter(routes) { | ||
@@ -80,0 +94,0 @@ if(!routerInstance) |
@@ -67,9 +67,13 @@ (function(global) { | ||
this.appendChild(newCompNode); | ||
if(newCompNode.isComponent()) | ||
newCompNode.bootstrap(); | ||
} | ||
} | ||
while(i < this.children.length) { | ||
// Clearing all irrelevant children | ||
var fromChild = i; | ||
for(i; i < this.children.length; i++) { | ||
this.removeChild(this.children[i]); | ||
this.children.splice(i, 1); | ||
} | ||
this.children.splice(fromChild, i - fromChild); | ||
@@ -126,3 +130,3 @@ // Reassigning values | ||
insertBefore.parentNode.insertBefore(newNode.self, insertBefore); | ||
else | ||
else if(this.self) | ||
this.self.appendChild(newNode.self); | ||
@@ -137,9 +141,9 @@ } else | ||
CompNode.prototype.removeChild = function(child, replace) { | ||
if(Array.isArray(child.children)) { | ||
for(var i = 0; i < child.children.length; i++) | ||
child.removeChild(child.children[i]); | ||
} | ||
if(child.comp instanceof global.Base.Component) { | ||
child.comp.onDestroy(); | ||
delete child.comp; | ||
} else if(Array.isArray(child.children)) { | ||
for(var i = 0; i < child.children.length; i++) | ||
child.removeChild(child.children[i]); | ||
child.children.splice(0, child.children.length); | ||
} | ||
@@ -150,6 +154,6 @@ | ||
this.self.replaceChild(replace.self, child.self); | ||
} else { | ||
} else if(child.self) { | ||
this.self.removeChild(child.self); | ||
child.self = undefined; | ||
} | ||
child.self = undefined; | ||
}; | ||
@@ -156,0 +160,0 @@ |
@@ -32,2 +32,14 @@ (function(global) { | ||
Component.prototype.evalWithScope = function(statement) { | ||
var result; | ||
try { | ||
with(this.$scope) { | ||
result = eval(statement); | ||
} | ||
} catch(err) { | ||
console.log(err.message); | ||
} | ||
return result; | ||
}; | ||
Component.prototype.onDestroy = function() { | ||
@@ -34,0 +46,0 @@ if(Array.isArray(this.children)) |
@@ -31,5 +31,6 @@ (function(global) { | ||
// Provide the $scope with option to hold component subscriptions | ||
$scope.addSubscription = function(subscription) { | ||
$scope.addSubscription = function() { | ||
this.subscriptions = this.subscriptions || []; | ||
this.subscriptions.push(subscription); | ||
for(var i = 0; i < arguments.length; i++) | ||
this.subscriptions.push(arguments[i]); | ||
}.bind(comp); | ||
@@ -36,0 +37,0 @@ |
@@ -19,4 +19,4 @@ (function(global) { | ||
} else { | ||
this.initFunc(function(data) { | ||
this.setData(data, true); | ||
this.initFunc(function(data, merge) { | ||
this.setData(data, !!merge); | ||
}.bind(this)); | ||
@@ -23,0 +23,0 @@ } |
@@ -11,10 +11,33 @@ (function(global) { | ||
ViewNode.prototype.parseNode = function() { | ||
var attrArr = this.self.attributes; | ||
var attrArr = this.self.attributes, | ||
attrName, attrValue; | ||
if(attrArr && attrArr.length > 0) for(var a = 0; a < attrArr.length; a++) { | ||
if(attrArr[a].name === 'controller') { | ||
this.controller = attrArr[a].value; | ||
attrName = attrArr[a].name; | ||
attrValue = attrArr[a].value; | ||
if(attrName === 'controller') { | ||
this.controller = attrValue; | ||
} else { | ||
// Checking for injectables and saving their statements. | ||
var injectable = global.App.getInjectable(attrArr[a].name); | ||
// Checking for injectables and saving their pipes & statements. | ||
var pipeSplit = attrValue.split('|'), | ||
injectable = global.App.getInjectable(attrName), | ||
pipes; | ||
if(injectable instanceof global.Base.Injectable) { | ||
attrValue = pipeSplit[0]; | ||
pipeSplit.splice(0, 1); | ||
if(pipeSplit.length > 0) { | ||
pipes = []; | ||
for(var i = 0; i < pipeSplit.length; i++) { | ||
var pipeParts = pipeSplit[i].trim().split(/:(.+)/g); | ||
pipeParts = pipeParts.filter(function(p) { return p.length > 0 }); | ||
pipes.push({ | ||
name: pipeParts[0], | ||
dataStatement: pipeParts[1] | ||
}); | ||
} | ||
} | ||
if(!Array.isArray(this.directives)) | ||
@@ -24,6 +47,7 @@ this.directives = []; | ||
injectable: injectable, | ||
statement: attrArr[a].value | ||
statement: attrValue, | ||
pipes: pipes | ||
}); | ||
if(!injectable.keepAttribute) { | ||
this.self.removeAttribute(attrArr[a].name); | ||
this.self.removeAttribute(attrName); | ||
// Removing attribute will lower 'attrArr.length' by 1. | ||
@@ -51,2 +75,18 @@ a--; | ||
if(Array.isArray(directive.pipes)) { | ||
var p, pipeObj, pipe; | ||
for(p = 0; p < directive.pipes.length; p++) { | ||
pipeObj = directive.pipes[p]; | ||
pipe = global.App.getPipe(pipeObj.name); | ||
if(pipe instanceof global.Base.Pipe) { | ||
var data; | ||
if(pipeObj.dataStatement) | ||
data = comp.evalWithScope(pipeObj.dataStatement); | ||
// Finally transform the value and apply it | ||
value = pipe.transform(value, data); | ||
} | ||
} | ||
} | ||
// Running modifier on created compNode with getter value | ||
@@ -77,3 +117,2 @@ directive.injectable.modifier(compNode, value); | ||
var childCount = 0; | ||
for(i = 0; i < arr.length; i++) { | ||
@@ -83,5 +122,5 @@ $scope[compNode.iterator.varName] = arr[i]; | ||
childNode.iteratorValue = arr[i]; | ||
if(childNode.self) childCount++; | ||
compNode.appendChild(childNode); | ||
if(childNode.isComponent()) childNode.bootstrap(comp); | ||
if(childNode.isComponent()) | ||
childNode.bootstrap(comp); | ||
} | ||
@@ -88,0 +127,0 @@ |
@@ -24,7 +24,12 @@ (function(global) { | ||
modifier: function(compNode, value) { | ||
var total = 0; | ||
for(var className in value) if(value.hasOwnProperty(className)) { | ||
value[className] ? | ||
compNode.self.classList.add(className) : | ||
if(value[className]) { | ||
compNode.self.classList.add(className); | ||
total++; | ||
} else | ||
compNode.self.classList.remove(className); | ||
} | ||
if(total === 0 && compNode.self.classList.length === 0) | ||
compNode.self.removeAttribute('class'); | ||
} | ||
@@ -31,0 +36,0 @@ }); |
@@ -9,2 +9,4 @@ (function(global) { | ||
modifier: function(compNode, value) { | ||
if(compNode.self instanceof HTMLInputElement) | ||
compNode.self.value = value; | ||
compNode.self.innerHTML = value; | ||
@@ -11,0 +13,0 @@ } |
@@ -13,6 +13,3 @@ (function(global) { | ||
window.onhashchange = function() { | ||
if(!this.navigating) { | ||
this.navigateTo(location.hash); | ||
} | ||
this.navigating = false; | ||
this.navigateTo(location.hash, true); | ||
}.bind(this); | ||
@@ -27,4 +24,4 @@ // Initializing Router | ||
} | ||
this.navigations = 0; | ||
routerInstance = this; | ||
this.navigateTo(location.hash); | ||
@@ -35,3 +32,3 @@ } else | ||
Router.prototype.navigateTo = function(url) { | ||
Router.prototype.navigateTo = function(url, fromWindow) { | ||
// First validate url | ||
@@ -45,2 +42,8 @@ if(url[0] === '#') { | ||
if(url !== this.currentPath) { | ||
// An identifier for Router state. | ||
this.navigating = true; | ||
// Updating Router variables | ||
this.navigations++; | ||
this.lastPath = this.currentPath || url; | ||
this.currentPath = url; | ||
@@ -57,3 +60,6 @@ | ||
} | ||
history.pushState(null, '', '#/' + url); | ||
if(fromWindow) | ||
history.replaceState(null, '', '#/' + url); | ||
else | ||
history.pushState(null, '', '#/' + url); | ||
this.stateChange(resultsObj) | ||
@@ -63,2 +69,4 @@ } | ||
console.error("UNKNOWN ROUTE " + url); | ||
this.navigating = false; | ||
} | ||
@@ -179,4 +187,5 @@ }; | ||
} | ||
// Only set the router data if the system is not already in routing process | ||
// if(changes > 0 && !this.navigating) { | ||
if(changes > 0) { | ||
this.navigating = true; | ||
var currUrl = location.hash; | ||
@@ -187,4 +196,10 @@ currUrl = currUrl.split('?')[0] + '?'; | ||
} | ||
location.hash = currUrl.substr(0, currUrl.length - 1); | ||
this.currentPath = location.hash.substr(2, location.hash.length); | ||
currUrl = currUrl.substr(2, currUrl.length - 3); | ||
if(!this.navigating) | ||
history.pushState(null, '', '#/' + currUrl); | ||
else | ||
history.replaceState(null, '', '#/' + currUrl); | ||
this.currentPath = currUrl; | ||
} | ||
@@ -207,2 +222,7 @@ if(this.state) this.state.params = params; | ||
Router.prototype.isLandingPage = function() { | ||
return this.lastPath === this.currentPath | ||
&& this.navigations === 1; | ||
}; | ||
Router.prototype.params = function() { | ||
@@ -209,0 +229,0 @@ return (this.state && typeof this.state.params === 'object') ? |
@@ -15,3 +15,7 @@ (function(global) { | ||
for(var param in data.params) if(data.params.hasOwnProperty(param)) { | ||
urlParams += param + '=' + data.params[param] + '&'; | ||
if(Array.isArray(data.params[param])) { | ||
for(var i = 0; i < data.params[param].length; i++) | ||
urlParams += param + '=' + data.params[param][i] + '&'; | ||
} else | ||
urlParams += param + '=' + data.params[param] + '&'; | ||
} | ||
@@ -43,2 +47,3 @@ url += urlParams; | ||
data = data.body; | ||
xhr.setRequestHeader('Content-Type', 'application/json'); | ||
} | ||
@@ -45,0 +50,0 @@ |
{ | ||
"name": "mvc-lite", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "Lite MVC Library using pure EcmaScript5", | ||
@@ -5,0 +5,0 @@ "main": "main.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
134922
44
2666