-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasil.min.js
More file actions
executable file
·1 lines (1 loc) · 13.6 KB
/
basil.min.js
File metadata and controls
executable file
·1 lines (1 loc) · 13.6 KB
1
var LOG_DASHES="#---------------------------------------------";var LOG_ERROR="#*********************************************";var LOG_OPENER="# ";var Basil={util:{},log:{},core:{event_actions:{},active:{user_idle:false,},settings:{defaults:{},options:{}},run:{stem:{init:function(){}},leaf:{build:function(){},cleanup:function(){}}}},app:{leaves:{},actions:{}},getStemParam:function(a){},config:function(a){},init:function(){},addAction:function(){},addLeaf:function(){},loadLeaf:function(b,a){}};Basil.core.settings={debug:false,displayGradually:true,displaySpeed:250,paths:"/assets/javascript/app/stems/",leaf_path_prefix:"/assets/javascript/app/leaves/"};Basil.prepare=function(a){Basil.util.each(a,function(b,c){Basil.core.settings[b]=c});Basil.core.preflight();Basil.util.domReady(Basil.core.run.stem.init.bind(Basil.core.run.stem))};Basil.core.preflight=function(){if(!window.jQuery&&!window.MooTools){if(typeof console){console.error("Basil currently depends on jQuery or MooTools to run")}return}};Basil.addLeaf=function(a,b){Basil.app.leaves[a]=b};Basil.addAction=function(a,b){Basil.app.actions[a]=b};var basil={updateObject:function(a){for(var b in a){if(a.hasOwnProperty(b)){basil.view_object[b]=a[b]}}},getElementsByAttribute:function(h,j,k,d){var e=(j=="*"&&h.all)?h.all:h.getElementsByTagName(j);var c=new Array();var g=(typeof d!="undefined")?new RegExp("(^|\\s)"+d+"(\\s|$)","i"):null;var a;var f;for(var b=0;b<e.length;b++){a=e[b];f=a.getAttribute&&a.getAttribute(k);if(typeof f=="string"&&f.length>0){if(typeof d=="undefined"||(g&&g.test(f))){c.push(a)}}}return c},updateChangeDetected:function(a){if(a&&a.keyCode&&basil.ignore_keys.indexOf(a.keyCode)>-1){return}basil.change_detected=true;if(basil.section_object.handleChangeDetected){basil.section_object.handleChangeDetected()}},findInputs:function(d){var e=[{type:"input",event:"keyup"},{type:"textarea",event:"keyup"},{type:"select",event:"change"}];for(var b=0;b<e.length;b++){var a=d.getElementsByTagName(e[b].type);for(var c=0;c<a.length;c++){if(a[c].addEventListener){a[c].addEventListener(e[b].event,basil.updateChangeDetected,false)}else{if(a[c].attachEvent){a[c].attachEvent("on"+e[b].event,basil.updateChangeDetected)}}}}},getItemInObject:function(d,c){var a=c.split(".");for(var b=0;b<a.length;b++){d=d[a[b]];if(!d){return d}}return d},getItemInSettings:function(a){return basil.getItemInObject(SETTINGS,a)},getItemInViewObject:function(a){return basil.getItemInObject(basil.view_object,a)},findDataFields:function(d){var a=basil.getElementsByAttribute(d,"*","data-field");for(var b=0;b<a.length;b++){var e=a[b];if(e.getAttribute("data-loop")){continue}var f=e.getAttribute("data-field");var c=e.getAttribute("data-call");if(c&&c=="settings"){var g=basil.getItemInSettings(f)}else{if(c){var g=basil.getItemInObject(window[c],f)}else{var g=basil.getItemInViewObject(f)}}if(g===undefined&&e.nodeName!="INPUT"){consoleLog(LOG_ERROR);consoleLog(" BAD ITEM: "+f);consoleLog(e);consoleLog(g)}switch(e.nodeName){case"INPUT":case"TEXTAREA":case"SELECT":if(g!==undefined){e.value=g}if(e.type.toLowerCase()=="checkbox"){e.checked=g=="1"?true:false}break;case"IFRAME":case"IMG":e.src=g;break;case"FIELDSET":break;default:e.innerHTML=g;break}}},findBehaviors:function(c){var a=basil.getElementsByAttribute(c,"*","data-behavior");for(var b=0;b<a.length;b++){var g=$(a[b]);var f=g.getAttribute("data-behavior").split(":");if(f.length>1){var e=Behavior[f[1]];var d=f[0].match(/^on/)?f[0]:"on"+f[0]}else{var e=Behavior[f[0]];var d="onclick"}if(!e){return}if(g.addEventListener){g.addEventListener(d.substr(2),e,false)}else{if(g.attachEvent){g.attachEvent(d,e)}}}},findTriggers:function(c){var a=basil.getElementsByAttribute(c,"*","data-trigger");for(var b=0;b<a.length;b++){var e=$(a[b]);var d=Trigger[e.getAttribute("data-trigger")];if(!d){return}d(e)}},findViews:function(d){var a=basil.getElementsByAttribute(d,"*","data-view");for(var c=0;c<a.length;c++){var f=$(a[c]);var b=f.getAttribute("data-view");if(!b){consoleLog(LOG_ERROR);consoleLog("View: "+b+" not found!")}var e=b.match(/^[^-]*/);if(e[0]=="shared"){basil.loadView(f,"/basil/shared/views/"+b.replace(/[^-]*-/,"")+basil.view_ext)}else{basil.loadView(f,basil.views_path+e[0]+"/views/"+b.replace(/[^-]*-/,"")+basil.view_ext)}}},findAll:function(a){basil.findViews(a);basil.findDataFields(a);basil.findTriggers(a);basil.findBehaviors(a);basil.findInputs(a)},loadView:function(b,a){$(b).set("html","");basil.load_counter+=1;_ajax.get(a+"?"+basil.random_number,{},function(c){$(b).set("html",c);basil.findAll(b);basil.load_counter-=1;if(basil.load_counter==0){if(basil.section_object&&basil.section_object.basilDone){basil.section_object.basilDone()}}})}};Basil.core.event_actions={leaf:function(d,b,c){if(c){Basil.core.run.stem.ignoreHash=true;window.location.hash=c}var a=Basil.util.getElementBySelector(d);new Basil.core.run.leaf(a,b)},alert:function(a){alert(a)},location:function(a){if(a.search("#")==0){window.location.hash=a}else{window.location=a}}};Basil.core.run.leaf=function(c,b){this.cleanup=function(){};this.element=c;this.file=b;this.data=undefined;this.resource=undefined;this.actions={};this.dataready=false;this.htmlready=false;var d=Basil.util.ajax;if(this.file){var a;if(this.file.search(/\?/)>-1){a=this.file.substr(0,this.file.search(/\?/))}else{a=this.file}if(Basil.app.leaves[a]){Basil.util.each(Basil.app.leaves[a],function(e,f){this[e]=f}.bind(this))}}this.build=function(){if(!this.file){this.process();return}this.hideElement();if(typeof this.resource=="string"){var e=this.resource.replace(/\{\{(.*?)\}\}/g,function(f,g){hashParamBeingRequested=g.trim().split(":").pop();return Basil.core.run.stem.hashParams[hashParamBeingRequested]});d.getJSON(e,{callback:function(f){this.data=f;if(this.htmlready){this.process()}this.dataready=true}.bind(this)})}else{this.dataready=true}Basil.util.ajax.get(Basil.core.settings.leaf_path+b,{callback:function(f){Basil.util.html(this.element,f);if(this.dataready){this.process()}this.htmlready=true}.bind(this)})};this.process=function(){this.findLoops(this.element);this.findHugs(this.element);this.findLeaves(this.element);this.findEvents(this.element);this.showElement()};this.findLoops=function(e){var f=Basil.util.getElementsByAttribute(e,"bsl-loop");if(!f.length){return}Basil.util.each(f,function(h,g){data_key=g.getAttribute("bsl-loop");if(data_key.search("resource:")==0){var i=data_key.split(":").pop();d.getJSON(i,{callback:function(j){if(j){this.processLoops(g,j)}}.bind(this),async:true})}else{if(this.data[data_key]){this.processLoops(g,this.data[data_key])}}}.bind(this))};this.processLoops=function(e,f){templateHTML=e.innerHTML;var g,h="";Basil.util.each(f,function(i,j){h+=this.findHugs(templateHTML,j)}.bind(this));e.innerHTML=h};this.findHugs=function(e,f){if(!f){f=this.data}if(typeof e!="string"){var g=e.innerHTML}else{var g=e}g=g.replace(/\{\{hash:(.*?)\}\}/g,function(h,i){hashParamBeingRequested=i.trim().split(":").pop();return Basil.core.run.stem.hashParams[hashParamBeingRequested]});g=g.replace(/\{\{(.*?)\}\}/ig,function(i,h){return f[h.trim()]}.bind(this));if(typeof e=="string"){return g}else{e.innerHTML=g}};this.findLeaves=function(e){var f=Basil.util.getElementsByAttribute(e,"bsl-leaf");Basil.util.each(f,function(i,g){var h=g.getAttribute("bsl-leaf");if(h!=""){new Basil.core.run.leaf(g,h)}}.bind(this))};this.findEvents=function(k){var m=["focus","blur","change","click","dblclick","error","keydown","keyup","keypress","load","contextmenu","mousedown","mouseup","mouseenter","mouseleave","mousemove","touchstart","touchmove","touchend","touchcancel","resize","scroll","submit","unload"];var l=Basil.util.getElements(k);var f,h,j,i,g,e;Basil.util.each(l,function(o,n){e=n.attributes;if(!e.length){return}Basil.util.each(e,function(q,p){h=p.nodeName.split("-e-")[1];if(h&&(f=m.indexOf(h))>-1){g=p.nodeValue?p.nodeValue.trim():"";funcs=g.match(/[a-zA-Z0-9_]+(\(.*\))?/ig);Basil.util.each(funcs,function(s,t){i=t.match(/^[a-zA-Z0-9_ ]+/)[0];j=t.match(/\((.*?)\)/);if(j&&j[1]){var r=[];Basil.util.each(j[1].split(","),function(u,v){if(v.search("'"==0)){r.push(v.substr(1,v.length-2))}});j=r}if(this.actions[i]){if(m[f]=="load"){this.actions[i].apply(this,j)}Basil.util.addEvent(n,m[f],this.actions[i],j)}else{if(Basil.app.actions[i]){if(m[f]=="load"){Basil.app.actions[i].apply(this,j)}Basil.util.addEvent(n,m[f],Basil.app.actions[i],j)}else{if(Basil.core.event_actions[i]){if(m[f]=="load"){Basil.core.event_actions[i].apply(this,j)}Basil.util.addEvent(n,m[f],Basil.core.event_actions[i],j)}}}}.bind(this))}}.bind(this))}.bind(this))};this.removeBasilAttributes=function(){};this.hideElement=function(){if(Basil.core.settings.displayGradually){if(window.jQuery){$(this.element).hide()}}else{this.element.style.visibility="hidden";this.element.style.opacity=0}};this.showElement=function(){if(Basil.core.settings.displayGradually){if(window.jQuery){$(this.element).fadeIn(Basil.core.settings.displaySpeed)}}else{this.element.style.visibility="visible";this.element.style.opacity=1}};this.build()};Basil.core.run.stem=new function(){this.init=function(){this.appWrapper=Basil.util.getElementsByAttribute(document.body,"bsl-app")[0];if(!this.appWrapper){Basil.log.warn("No bsl-app detected!");return}if(!this.findDefaultStem()){Basil.log.exception("No stems found");return}var a=this.locate();this.setupHashListener();new Basil.core.run.leaf(this.appWrapper,false);this.stemWrapper=Basil.util.getElementsByAttribute(document.body,"bsl-stem")[0];if(!this.stemWrapper){this.appWrapper.innerHTML="<div bsl-stem></div>";this.stemWrapper=Basil.util.getElementsByAttribute(document.body,"bsl-stem")[0];Basil.log.warn("No stem wrapper found, inserting into app wrapper.")}this.loadStem(a)};this.findStemLeaf=function(){if(!Basil.core.settings.stems[this.hashPath]){window.location.hash="#!"+Basil.core.settings.stems[this.defaultStem]}else{return Basil.core.settings.stems[this.hashPath]}};this.setupHashListener=function(){if(("onhashchange" in window)&&!(window.msie)&&!(window.ie)){window.onhashchange=this.hashChangeEvent.bind(this)}else{var a=window.location.hash;window.setInterval(function(){if(window.location.hash!=a){a=window.location.hash;this.hashChangeEvent()}},100)}};this.hashChangeEvent=function(b){if(b.oldURL.search("#")<0){return}if(Basil.core.run.stem.ignoreHash){Basil.core.run.stem.ignoreHash=false;return}var a=this.locate();this.loadStem(a)};this.loadStem=function(a){new Basil.core.run.leaf(this.stemWrapper,a)};this.locate=function(){var b=window.location.hash.substr(2);Basil.log.print(LOG_DASHES,"HASH CHANGE!",window.location.hash,LOG_DASHES);Basil.core.run.stem.hashParams={};if(!b||b==""){window.location.hash="#!"+this.defaultStem;var b=window.location.hash.substr(2)}var a=undefined;var c=b.replace(/(^\/|\/$)/g,"").split("/");a=Basil.util.each(Basil.core.settings.stems,function(e,f){if(e==b){return e}var d=e.replace(/(^\/|\/$)/g,"").split("/");if(c.length==d.length){var g=undefined;var g=Basil.util.each(d,function(h,i){if(i.search(":")!=0){if(i!=c){return undefined}}if(h==d.length-1){return e}});if(g){Basil.util.each(d,function(h,i){if(i.search(":")==0){Basil.core.run.stem.hashParams[i.substr(1)]=c[h]}});return g}}}.bind(this));if(!a){window.location.hash="#!"+this.defaultStem}return Basil.core.settings.stems[a]};this.findDefaultStem=function(){var a=false;this.defaultStem=Basil.util.each(Basil.core.settings.stems,function(b,c){if(!a){return b}});return this.defaultStem?this.defaultStem:false}};Basil.util={each:function(d,c){for(var a in d){if(a=="length"){continue}if(d.hasOwnProperty(a)){var b=d[a];if((response=c(a,b))){return response}}}},getElementBySelector:function(a){if(window.jQuery){return $(a).get(0)}else{if(window.MooTools){return $$(a)[0]}}},getElementsBySelector:function(a){if(window.jQuery){return $(a).get()}else{if(window.MooTools){return $$(a)}}},getElements:function(a){return Basil.util.getElementsByAttribute(a,"")},getElementsByAttribute:function(c,a){var b=a==""?"*":"["+a+"]";if(window.jQuery){return $(c).find(b).get()}else{if(window.MooTools){return c.getElements(b)}}},domReady:function(a){if(window.jQuery){$(a)}else{if(window.MooTools){window.addEvent("domready",a)}}},html:function(b,a){if(window.jQuery){$(b).html(a)}else{if(window.MooTools){b.set("html",a)}}},addEvent:function(a,c,b,d){if(window.jQuery){$(a).on(c,function(f){if(d){b.apply(this,d)}else{b(f)}});return $(a)}else{if(window.MooTools){if(d){return a.addEvent(c,b.apply(this,d))}else{return a.addEvent(c,b)}}}}};Basil.util.ajax={request:function(b,a){defaults={method:"POST",async:false,data:{},dataType:"html",callback:function(c){}};Basil.util.each(a,function(c,d){defaults[c]=d});a=defaults;if(window.jQuery){$.ajax({dataType:a.dataType,url:b,type:a.type,async:!a.async,success:function(d,e,c){Basil.log.print("AJAX SUCCESS",{responseData:d});if(a.callback&&typeof a.callback=="function"){a.callback(d)}},error:function(c,e,d){Basil.log.exception("AJAX ERROR",c,e,d)},beforeSend:function(c){Basil.log.print("AJAX REQUEST",b,a.data)}})}else{if(window.MooTools){new Request({url:b,format:a.dataType,async:!a.async,onRequest:function(c){Basil.log.print(b,data)},onSuccess:function(c,d){Basil.log.print("AJAX SUCCESS",c);if(a.callback&&typeof a.callback=="function"){a.callback(c)}},onError:function(c){Basil.log.exception("AJAX ERROR",c)},onFailure:function(c){Basil.log.exception("AJAX FAILURE",c)}}).post(a.data)}}},get:function(b,a){Basil.util.ajax.request(b,a)},getJSON:function(b,a){a.dataType="JSON";Basil.util.ajax.request(b,a)}};Basil.log={available:function(){return Basil.core.settings.debug&&console!=="undefined"},print:function(){if(Basil.log.available()){for(arg in arguments){console.log(arguments[arg])}}},warn:function(){if(Basil.log.available()){for(arg in arguments){console.warn(arguments[arg])}}},exception:function(){if(Basil.log.available()){for(arg in arguments){console.error(arguments[arg])}}},};