Comparing version 0.0.1 to 0.0.2
@@ -1,1 +0,1 @@ | ||
"use strict";function Reuq(app){var rq=this;this.templates={};this.app=app;this.app.resources=app.resources||{};this.app.locals=app.locals||{};this.utils=this.getUtils();this._storeTemplates();$("body").on("submit","[rq-form]:not([rq-tmpl] [rq-form]):not([rq-tmpl][rq-form])",function(e){e.preventDefault();rq.utils.submit($(this))});(function(rq){var resources=rq.app.resources;var locals=rq.app.locals||{};Object.keys(locals).forEach(function(name){rq.setLocal(name,rq.getLocal(name))});Object.keys(resources).forEach(function(resourceName){if(resources[resourceName]["autoload"]===undefined||resources[resourceName]["autoload"]){rq.getResource(resourceName,true);rq.render(resourceName,null)}});if(typeof rq.app.onInit==="function"){rq.app.onInit.apply(rq)}})(this)}Reuq.prototype.render=function(templateName,data){var processedTemplate=this._processTemplate(templateName,data);this._render(templateName,processedTemplate)};Reuq.prototype._processTemplate=function(templateName,data){var rq=this;function sanitize(value){var blacklist={"&":"&","<":"<",">":">",'"':""","'":"'"};return value.replace(/[&<>"']/g,function(m){return blacklist[m]})}function isDynamicProperty(property){return property[0]==="@"}function getDynamicPropertyValue(property,data){var expression=property.slice(1);return rq.app.dynamicProperties[expression].apply(rq,[data])}function getPropertyValue(property,data){return isDynamicProperty(property)?getDynamicPropertyValue(property,data):data[property]}function compile(template,data){var $template=$(template);$template.find("[rq-if]").each(function(id,el){var $el=$(el);var condition=$el.attr("rq-if");$el.removeAttr("rq-if");if(!getPropertyValue(condition,data)){$el.remove()}});$template.find("[rq-if-not]").each(function(id,el){var $el=$(el);var condition=$el.attr("rq-if-not");$el.removeAttr("rq-if-not");if(getPropertyValue(condition,data)){$el.remove()}});template=$template.prop("outerHTML");template=template.replace(/\[\[(\w+|@\w+)\]\]/g,function(){var expression=arguments[1];var value=""+getPropertyValue(expression,data);return sanitize(value)});return template}var templateObj=this.templates[templateName];var resourceName=templateObj.resourceName;var template=templateObj.html;var $template=$(template);if(resourceName&&this.app.resources[resourceName].loading){$template.not("[rq-rsrc-loading], [rq-rsrc-loading] *").remove()}else{$template.find("[rq-rsrc-loading], [rq-rsrc-loading] *").remove()}$template.find("[rq-iter], [rq-iter-self]").each(function(id,el){var $el=$(el);if($el.is("[rq-iter]")){var listKey=$el.attr("rq-iter");var list=data[listKey]||[]}else{var list=data||[]}list.forEach(function(listItem){var $compiled=$(compile($el.prop("outerHTML"),listItem));$compiled.removeAttr("rq-iter");$el.after($compiled)});$el.remove()});template=$template.prop("outerHTML");$template=$(compile(template,data));$template.find("[rq-src]").each(function(id,el){var $el=$(el);$el.attr("src",$el.attr("rq-src"));$el.removeAttr("rq-src")});return $template.prop("outerHTML")};Reuq.prototype._render=function(templateName,processedTemplate){$("[from-tmpl="+templateName+"]").remove();var $template=$("[rq-tmpl][rq-tmpl="+templateName+"]");var $newTemplate=$(processedTemplate).removeAttr("hidden").removeAttr("rq-tmpl");$newTemplate.attr("from-tmpl",templateName);$template.after($newTemplate);this.addEvents($newTemplate)};Reuq.prototype.cacheIsValid=function(resourceName){var resource=this.app.resources[resourceName];if(resource.shouldReload){return false}else{var cacheTimeout=resource.cacheTimeout||10;var timeoutDate=new Date(resource.updatedAt.getTime()+cacheTimeout*6e4);return timeoutDate>new Date}};Reuq.prototype.invalidateResourceCache=function(resourceName){this.app.resources[resourceName].shouldReload=true};Reuq.prototype.getResource=function(resourceName,force,cb){if(arguments.length<3&&typeof force==="function"){cb=force;force=false}var rq=this;var resource=this.app.resources[resourceName];if(resource.data&&!force&&this.cacheIsValid(resourceName)){cb(resource.data)}else{resource.loading=true;var url=typeof resource.url==="function"?resource.url(this):resource.url;$.ajax({url:url,dataType:"json",beforeSend:function(xhr){Object.keys(resource.headers||{}).forEach(function(header){xhr.setRequestHeader(header,resource.headers[header])})},success:function(resp){resource.loading=false;resource.loaded=true;rq.setResource(resourceName,resource.dataKey?resp[resource.dataKey]:resp);if(typeof cb==="function"){cb(resource.data)}}})}};Reuq.prototype.setResource=function(resourceName,data){var rq=this;var resource=this.app.resources[resourceName];resource.data=data;resource.shouldReload=false;resource.updatedAt=new Date;this.runResourceSubscribers(resourceName,data);$("[rq-tmpl][rq-rsrc="+resourceName+"]:not([manual-render])").each(function(id,el){rq.render($(el).attr("rq-tmpl"),data)})};Reuq.prototype.runResourceSubscribers=function(resourceName,data){var resource=this.app.resources[resourceName];data=data||resource.data;this.runSubscribers(resource.subscribers,data)};Reuq.prototype.updateResource=function(resourceName,cb){var rq=this;this.getResource(resourceName,function(data){rq.setResource(resourceName,cb(data))})};Reuq.prototype.setLocal=function(name,data){var rq=this;var local=this.app.locals[name]=this.app.locals[name]||{};local.data=data;this.runLocalSubscribers(name,data);$("[rq-tmpl][rq-local="+name+"]:not([manual-render])").each(function(id,el){rq.render($(el).attr("rq-tmpl"),data)})};Reuq.prototype.getLocal=function(name){return this.app.locals[name].data};Reuq.prototype.updateLocal=function(name,cb){var data=this.getLocal(name);this.setLocal(name,cb(data))};Reuq.prototype.runLocalSubscribers=function(name,data){var local=this.app.locals[name];data=data||local.data;this.runSubscribers(local.subscribers,data)};Reuq.prototype.runSubscribers=function(subscribers,data){var rq=this;if(subscribers){subscribers.forEach(function(subscriber){var fn=rq.app.subscribers[subscriber];fn.apply(rq,[data])})}};Reuq.prototype._storeTemplates=function(){var rq=this;$("[rq-tmpl]").each(function(id,el){var $el=$(el);rq.templates[$el.attr("rq-tmpl")]={html:$el.prop("outerHTML"),dom:$el,localDataName:$el.attr("rq-local"),resourceName:$el.attr("rq-rsrc")}})};Reuq.prototype.addEvents=function($dom){$dom=$dom||$("body");var rq=this;var evtSelector="[rq-evt]:not([rq-tmpl] [rq-evt]):not([rq-tmpl][rq-evt])";$dom.find(evtSelector).addBack(evtSelector).each(function(id,el){var $el=$(el);var evtConfig=$el.attr("rq-evt").split(" ");var evtType=evtConfig[0];var evtHandler=rq.app.eventHandlers[evtConfig[1]];$el.on(evtType,function(e){var handlerArgs=[{event:e,target:$el}].concat(evtConfig.slice(2));evtHandler.apply(rq,handlerArgs)})})};Reuq.prototype.getUtils=function(){var rq=this;return{submit:function(form){var data=form.serialize();var url=form.attr("action");var type=form.attr("method");var beforeSend=function(xhr){var headersKey=form.attr("rq-form-headers");if(headersKey){var headers=rq.app.fn[headersKey].apply(rq);Object.keys(headers||{}).forEach(function(header){xhr.setRequestHeader(header,headers[header])})}};$.ajax({type:type,url:url,data:data,beforeSend:beforeSend}).done(function(data,status,jqXHR){var cb=form.attr("rq-cb-done");if(cb){rq.app.callbacks[cb].apply(rq,[data,status,form])}}).fail(function(jqXHR,status,error){var cb=form.attr("rq-cb-fail");if(cb){rq.app.callbacks[cb](error,status,form,jqXHR)}})}}};$("head").append('<style type="text/css">[rq-tmpl] {display: none !important;}</style>');window.Rq=window.Reuq=Reuq; | ||
"use strict";function Reuq(app){var rq=this;this.templates={};this.app=app;this.app.resources=app.resources||{};this.app.locals=app.locals||{};this.utils=this.getUtils();this._storeTemplates();$("body").on("submit","[rq-form]:not([rq-tmpl] [rq-form]):not([rq-tmpl][rq-form])",function(e){e.preventDefault();rq.utils.submit($(this))});(function(rq){var resources=rq.app.resources;var locals=rq.app.locals||{};Object.keys(locals).forEach(function(name){var data=rq.getLocal(name);if(typeof data!=="undefined"){rq.setLocal(name,data)}});Object.keys(resources).forEach(function(resourceName){if(typeof resources[resourceName].autoload==="undefined"||resources[resourceName].autoload){rq.getResource(resourceName,true);rq.setResource(resourceName,null,true)}});if(typeof rq.app.onInit==="function"){rq.app.onInit.apply(rq)}rq.addEvents(null,true)})(this)}Reuq.prototype.render=function(templateName,data){var processedTemplate=this._processTemplate(templateName,data);this._render(templateName,processedTemplate)};Reuq.prototype._processTemplate=function(templateName,data){var rq=this;function sanitize(value){var blacklist={"&":"&","<":"<",">":">",'"':""","'":"'"};return value.replace(/[&<>"']/g,function(m){return blacklist[m]})}function isDynamicProperty(property){return property[0]==="@"}function getDynamicPropertyValue(property,data){var expression=property.slice(1);return rq.app.dynamicProperties[expression].apply(rq,[data])}function getPropertyValue(property,data){if(isDynamicProperty(property)){return getDynamicPropertyValue(property,data)}else{var propertyTree=property.split(".");return propertyTree.reduce(function(accumulator,current){return accumulator[current]},data)}}function compile(template,data){var $template=$(template);$template.find("[rq-if]").each(function(id,el){var $el=$(el);var condition=$el.attr("rq-if");$el.removeAttr("rq-if");if(!getPropertyValue(condition,data)){$el.remove()}});$template.find("[rq-if-not]").each(function(id,el){var $el=$(el);var condition=$el.attr("rq-if-not");$el.removeAttr("rq-if-not");if(getPropertyValue(condition,data)){$el.remove()}});template=$template.prop("outerHTML");template=template.replace(/\[\[(\w+(\.\w+)*|@\w+)\]\]/g,function(){var expression=arguments[1];var value=""+getPropertyValue(expression,data);return sanitize(value)});return template}var templateObj=this.templates[templateName];var resourceName=templateObj.resourceName;var template=templateObj.html;var $template=$(template);if(resourceName&&this.app.resources[resourceName].loading){$template.not("[rq-rsrc-loading], [rq-rsrc-loading] *").remove()}else{$template.find("[rq-rsrc-loading], [rq-rsrc-loading] *").remove()}$template.find("[rq-iter], [rq-iter-self]").each(function(id,el){var $el=$(el);if($el.is("[rq-iter]")){var listKey=$el.attr("rq-iter");var list=data[listKey]||[]}else{var list=data||[]}list.forEach(function(listItem){var $compiled=$(compile($el.prop("outerHTML"),listItem));$compiled.removeAttr("rq-iter");$el.after($compiled)});$el.remove()});template=$template.prop("outerHTML");$template=$(compile(template,data));$template.find("[rq-src]").each(function(id,el){var $el=$(el);$el.attr("src",$el.attr("rq-src"));$el.removeAttr("rq-src")});return $template.prop("outerHTML")};Reuq.prototype._render=function(templateName,processedTemplate){$("[from-tmpl="+templateName+"]").remove();var $template=$("[rq-tmpl][rq-tmpl="+templateName+"]");var $newTemplate=$(processedTemplate).removeAttr("hidden").removeAttr("rq-tmpl");$newTemplate.attr("from-tmpl",templateName);$template.after($newTemplate);this.addEvents($newTemplate)};Reuq.prototype.cacheIsValid=function(resourceName){var resource=this.app.resources[resourceName];if(resource.shouldReload){return false}else{var cacheTimeout=resource.cacheTimeout||10;var timeoutDate=new Date(resource.updatedAt.getTime()+cacheTimeout*6e4);return timeoutDate>new Date}};Reuq.prototype.invalidateResourceCache=function(resourceName){this.app.resources[resourceName].shouldReload=true};Reuq.prototype.getResource=function(resourceName,force,cb){if(arguments.length<3&&typeof force==="function"){cb=force;force=false}var rq=this;var resource=this.app.resources[resourceName];if(resource.data&&!force&&this.cacheIsValid(resourceName)){cb(resource.data)}else{resource.loading=true;var url=typeof resource.url==="function"?resource.url(this):resource.url;$.ajax({url:url,dataType:"json",beforeSend:function(xhr){Object.keys(resource.headers||{}).forEach(function(header){xhr.setRequestHeader(header,resource.headers[header])})},success:function(resp){resource.loading=false;resource.loaded=true;rq.setResource(resourceName,resource.dataKey?resp[resource.dataKey]:resp);if(typeof cb==="function"){cb(resource.data)}}})}};Reuq.prototype.setResource=function(resourceName,data,withoutSubscribers){var rq=this;var resource=this.app.resources[resourceName];resource.data=data;resource.shouldReload=false;resource.updatedAt=new Date;if(!withoutSubscribers){this.runResourceSubscribers(resourceName,data)}$("[rq-tmpl][rq-rsrc="+resourceName+"]:not([manual-render])").each(function(id,el){rq.render($(el).attr("rq-tmpl"),data)})};Reuq.prototype.runResourceSubscribers=function(resourceName,data){var resource=this.app.resources[resourceName];data=data||resource.data;this.runSubscribers(resource.subscribers,data)};Reuq.prototype.updateResource=function(resourceName,cb){var rq=this;this.getResource(resourceName,function(data){rq.setResource(resourceName,cb(data))})};Reuq.prototype.setLocal=function(name,data){var rq=this;var local=this.app.locals[name]=this.app.locals[name]||{};local.data=data;this.runLocalSubscribers(name,data);$("[rq-tmpl][rq-local="+name+"]:not([manual-render])").each(function(id,el){rq.render($(el).attr("rq-tmpl"),data)})};Reuq.prototype.getLocal=function(name){return this.app.locals[name].data};Reuq.prototype.updateLocal=function(name,cb){var data=this.getLocal(name);this.setLocal(name,cb(data))};Reuq.prototype.runLocalSubscribers=function(name,data){var local=this.app.locals[name];data=data||local.data;this.runSubscribers(local.subscribers,data)};Reuq.prototype.runSubscribers=function(subscribers,data){var rq=this;if(subscribers){subscribers.forEach(function(subscriber){var fn=rq.app.subscribers[subscriber];fn.apply(rq,[data])})}};Reuq.prototype._storeTemplates=function(){var rq=this;$("[rq-tmpl]").each(function(id,el){var $el=$(el);rq.templates[$el.attr("rq-tmpl")]={html:$el.prop("outerHTML"),dom:$el,localDataName:$el.attr("rq-local"),resourceName:$el.attr("rq-rsrc")}})};Reuq.prototype.addEvents=function($dom,excludeTemplates){$dom=$dom||$("body");var rq=this;var evtSelector="[rq-evt]:not([rq-tmpl] [rq-evt]):not([rq-tmpl][rq-evt])";if(excludeTemplates){evtSelector+=":not([from-tmpl] [rq-evt]):not([from-tmpl][rq-evt])"}$dom.find(evtSelector).addBack(evtSelector).each(function(id,el){var $el=$(el);var evtConfig=$el.attr("rq-evt").split(" ");var evtType=evtConfig[0];var evtHandler=rq.app.eventHandlers[evtConfig[1]];$el.on(evtType,function(e){var handlerArgs=[{event:e,target:$el}].concat(evtConfig.slice(2));evtHandler.apply(rq,handlerArgs)})})};Reuq.prototype.getUtils=function(){var rq=this;return{submit:function(form){var data=form.serialize();var url=form.attr("action");var type=form.attr("method");var beforeSend=function(xhr){var headersKey=form.attr("rq-form-headers");if(headersKey){var headers=rq.app.fn[headersKey].apply(rq);Object.keys(headers||{}).forEach(function(header){xhr.setRequestHeader(header,headers[header])})}};$.ajax({type:type,url:url,data:data,beforeSend:beforeSend}).done(function(data,status,jqXHR){var cb=form.attr("rq-cb-done");if(cb){rq.app.callbacks[cb].apply(rq,[data,status,form])}}).fail(function(jqXHR,status,error){var cb=form.attr("rq-cb-fail");if(cb){rq.app.callbacks[cb](error,status,form,jqXHR)}})}}};$("head").append('<style type="text/css">[rq-tmpl] {display: none !important;}</style>');window.Rq=window.Reuq=Reuq; |
{ | ||
"name": "reuq", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Frontend Javascript framework built on top of JQuery", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -594,3 +594,3 @@ # Reuq.js | ||
## 6. Accessing the Reuq instance | ||
## 5. Accessing the Reuq instance | ||
@@ -597,0 +597,0 @@ For every function within attributes(i.e `dynamicProperties`, `subscribers`, `eventHandlers` etc) of the reuq `app` object, |
@@ -23,3 +23,6 @@ "use strict"; | ||
Object.keys(locals).forEach(function(name) { | ||
rq.setLocal(name, rq.getLocal(name)); | ||
var data = rq.getLocal(name); | ||
if (typeof data !== 'undefined') { | ||
rq.setLocal(name, data); | ||
} | ||
}); | ||
@@ -29,5 +32,5 @@ //load all resources set to autoload | ||
// if autoload is not set, autoload it by default | ||
if (resources[resourceName]['autoload'] === undefined || resources[resourceName]['autoload']) { | ||
if (typeof resources[resourceName].autoload === 'undefined' || resources[resourceName].autoload) { | ||
rq.getResource(resourceName, true); | ||
rq.render(resourceName, null); | ||
rq.setResource(resourceName, null, true); | ||
} | ||
@@ -40,2 +43,3 @@ }); | ||
} | ||
rq.addEvents(null, true); | ||
})(this); | ||
@@ -77,3 +81,11 @@ } | ||
function getPropertyValue(property, data){ | ||
return isDynamicProperty(property) ? getDynamicPropertyValue(property, data) : data[property] | ||
if (isDynamicProperty(property)) { | ||
return getDynamicPropertyValue(property, data) | ||
} else { | ||
var propertyTree = property.split('.'); | ||
return propertyTree.reduce(function(accumulator, current){ | ||
return accumulator[current] | ||
}, data); | ||
} | ||
} | ||
@@ -108,3 +120,3 @@ | ||
template = template.replace(/\[\[(\w+|@\w+)\]\]/g, function() { | ||
template = template.replace(/\[\[(\w+(\.\w+)*|@\w+)\]\]/g, function() { | ||
var expression = arguments[1]; | ||
@@ -225,3 +237,3 @@ // parse value to string | ||
Reuq.prototype.setResource = function(resourceName, data) { | ||
Reuq.prototype.setResource = function(resourceName, data, withoutSubscribers) { | ||
var rq = this; | ||
@@ -232,4 +244,7 @@ var resource = this.app.resources[resourceName]; | ||
resource.updatedAt = new Date(); | ||
this.runResourceSubscribers(resourceName, data); | ||
if (!withoutSubscribers) { | ||
this.runResourceSubscribers(resourceName, data); | ||
} | ||
$('[rq-tmpl][rq-rsrc=' + resourceName + ']:not([manual-render])').each(function(id, el) { | ||
@@ -304,6 +319,10 @@ rq.render($(el).attr('rq-tmpl'), data); | ||
Reuq.prototype.addEvents = function($dom) { | ||
Reuq.prototype.addEvents = function($dom, excludeTemplates) { | ||
$dom = $dom || $('body'); | ||
var rq = this; | ||
var evtSelector = '[rq-evt]:not([rq-tmpl] [rq-evt]):not([rq-tmpl][rq-evt])'; | ||
if (excludeTemplates) { | ||
evtSelector += ':not([from-tmpl] [rq-evt]):not([from-tmpl][rq-evt])' | ||
} | ||
$dom.find(evtSelector).addBack(evtSelector).each(function(id, el) { | ||
@@ -310,0 +329,0 @@ var $el = $(el); |
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
775877
27
22564