lettuce
Version:
Lettuce JS, Mini Mobile Framework for Romantic with DSL.
3 lines (2 loc) • 4.03 kB
JavaScript
/*! lettuce 15-01-2015 */
!function(a){"use strict";var b=function(){};b.VERSION="0.0.8",a.lettuce=b,b.isObject=function(a){var b=typeof a;return"function"===b||"object"===b&&!!a},b.extend=function(a){if(!b.isObject(a))return a;for(var c,d,e=1,f=arguments.length;f>e;e++){c=arguments[e];for(d in c)hasOwnProperty.call(c,d)&&(a[d]=c[d])}return a},b.prototype.Class=function(a,b){var c=function d(c,e){function f(){var a=this instanceof f?this:new g;return a.init.apply(a,arguments),a}function g(){}f.Basic=g;var h=g[a]=c[a],i=g[a]=f[a]=new g;i.constructor=f,f.extend=function(a){return new d(f,a)};var j=f.open=function(a){if("function"==typeof a&&(a=a.call(f,i,h,f,c)),"object"==typeof a)for(var d in a)b.call(a,d)&&(i[d]=a[d]);return"init"in i||(i.init=c),f};return j(e)};return c}("prototype",{}.hasOwnProperty);var c=new b.prototype.Class(function(){});c.prototype.init=function(){};var d={run:function(a){this.methods=a,this.execute("first");for(var b in this.methods)"last"!==b&&b.match(/.*Page/)&&this.execute(b);this.execute("last")},execute:function(a){var b=this.methods[a]();return delete this.methods[a],b}};c.prototype=b.extend(c.prototype,d);var e={Parser:c};b.prototype=b.extend(b.prototype,e),b.get=function(a,c){b.send(a,"GET",c)},b.post=function(a,c,d){b.send(a,"POST",d,c)},b.send=function(a,b,c,d){d=d||null;var e=new XMLHttpRequest;c instanceof Function&&(e.onreadystatechange=function(){4!==e.readyState||200!==e.status&&0!==e.status||c(e.responseText)}),e.open(b,a,!0),d instanceof Object&&(d=JSON.stringify(d),e.setRequestHeader("Content-Type","application/json")),e.setRequestHeader("X-Requested-With","XMLHttpRequest"),e.send(d)};var f={on:function(a,b){this._events=this._events||{},this._events[a]=this._events[a]||[],this._events[a].push(b)},off:function(a,b){this._events=this._events||{},a in this._events!=!1&&this._events[a].splice(this._events[a].indexOf(b),1)},trigger:function(a){if(this._events=this._events||{},a in this._events!=!1)for(var b=0;b<this._events[a].length;b++)this._events[a][b].apply(this,Array.prototype.slice.call(arguments,1))}},g={Event:f};b.prototype=b.extend(b.prototype,g);var h=function(a,b){var c=/[^\w\-\.:]/.test(a)?new Function(h.arg+",tmpl","var _e=tmpl.encode"+h.helper+",_s='"+a.replace(h.regexp,h.func)+"';return _s;"):h.cache[a]=h.cache[a]||h(h.load(a));return b?c(b,h):function(a){return c(a,h)}};h.cache={},h.load=function(a){return document.getElementById(a).innerHTML},h.regexp=/([\s'\\])(?!(?:[^{]|\{(?!%))*%\})|(?:\{%(=|#)([\s\S]+?)%\})|(\{%)|(%\})/g,h.func=function(a,b,c,d,e,f){return b?{"\n":"\\n","\r":"\\r"," ":"\\t"," ":" "}[b]||"\\"+b:c?"="===c?"'+_e("+d+")+'":"'+("+d+"==null?'':"+d+")+'":e?"';":f?"_s+='":void 0},h.escapeRegexp=/[<>&"'\x00]/g,h.escapeMap={"<":"<",">":">","&":"&",'"':""","'":"'"},h.encode=function(a){return(null==a?"":""+a).replace(h.escapeRegexp,function(a){return h.escapeMap[a]||""})},h.arg="o",h.helper=",print=function(s,e){_s+=e?(s==null?'':s):_e(s);},include=function(s,d){_s+=tmpl(s,d);}",b.prototype.tmpl=b.tmpl=h;var i=new b.prototype.Class(function(){});i.prototype.init=function(){},i.prototype.render=function(a,b){document.getElementById(b).innerHTML=a};var j={SimpleView:i};b.prototype=b.extend(b.prototype,j);var k={routes:[],mode:null,root:"/",hashStrip:/^#*/,getFragment:function(a){return(a||window.location).hash.replace(this.hashStrip,"")},add:function(a,b){return"function"==typeof a&&(b=a,a=""),this.routes.push({re:a,handler:b}),this},check:function(a,b){for(var c=a||b.getFragment(),d=0;d<b.routes.length;d++){var e="#"+c,f=e.match(b.routes[d].re);f&&(f.shift(),b.routes[d].handler.apply({},f))}},load:function(){var a=this,b=a.getFragment(),c=function(){b!==a.getFragment()?b=a.getFragment():a.check(b,a)};return clearInterval(this.interval),this.interval=setInterval(c,50),this},navigate:function(a){return a=a?a:"",window.location.href.match(/#(.*)$/),window.location.href=window.location.href.replace(/#(.*)$/,"")+"#"+a,this}},l={Router:k};b.prototype=b.extend(b.prototype,l)}(this);