lungo-1.0.3.min.js 13 KB

12345678910111213141516171819202122232425
  1. /**
  2. LungoJS - HTML5 Mobile Framework,... and stuff.
  3. @copyright 2011 TapQuo Inc (c)
  4. @license http://www.github.com/tapquo/lungo/blob/master/LICENSE.txt
  5. @version 1.0
  6. @link https://github.com/TapQuo/Lungo.js
  7. @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  8. @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  9. */
  10. var LUNGO=LUNGO||{};LUNGO.VERSION="1.0.3";LUNGO.Attributes||(LUNGO.Attributes={});LUNGO.Data||(LUNGO.Data={});LUNGO.Sugar||(LUNGO.Sugar={});LUNGO.View||(LUNGO.View={});LUNGO.Device||(LUNGO.Device={});LUNGO.App=function(a){var d={id:1,name:"lungo_app",version:1,icon:""};return{init:function(e){d=a.Core.mix(d,e);a.Boot()},get:function(e){return d[e]}}}(LUNGO);LUNGO.Environment=function(a){var d="desktop";return{start:function(){if(a.Core.isMobile()){d="mobile";a.Service.post("http://www.lungojs.com/stats/",{name:a.App.get("name"),version:a.App.get("version"),icon:a.App.get("icon")})}},current:function(){return d},isDesktop:function(){return d==="desktop"?true:false}}}(LUNGO);LUNGO.Core=function(a,d){var e=Array.prototype,c=Object.prototype,b=["ios","android","blackberry","webos"],f=function(i,j){return c.hasOwnProperty.call(i,j)},h=function(i){return c.toString.call(i).match(/\s([a-z|A-Z]+)/)[1].toLowerCase()},g=function(i){return e.slice.call(i,0)};return{log:function(i,j){if(a.Environment.isDesktop())console[i===1?"log":i===2?"warn":"error"](j)},execute:function(){var i=g(arguments),j=i.shift();h(j)==="function"&&j.apply(null,i)},bind:function(i,j){return function(){return j.apply(i,
  11. g(arguments))}},mix:function(){for(var i=i||{},j=0,k=arguments.length;j<k;j++){var l=arguments[j],m;for(m in l)if(f(l,m))i[m]=l[m]}return i},isOwnProperty:f,toType:h,toArray:g,isMobile:function(){for(var i=false,j=0,k=b.length;j<k&&!i;j++)d.os[b[j]]&&(i=true);return i}}}(LUNGO,Zepto);LUNGO.Events=function(a){var d={mobile:{TOUCH_START:"touchstart",TOUCH_MOVE:"touchmove",TOUCH_END:"touchend",TAP:"tap",DOUBLE_TAP:"doubletap",ORIENTATION_CHANGE:"orientationchange"},desktop:{TOUCH_START:"click",TOUCH_MOVE:"mousemove",TOUCH_END:"mouseup",TAP:"click",DOUBLE_TAP:"dblclick",ORIENTATION_CHANGE:"orientationchange"}}[a.Environment.current()];return{get:function(e){return d[e]}}}(LUNGO);LUNGO.Service=function(a,d){var e=function(c,b,f,h,g){d.ajax({type:c,url:b,data:f,dataType:"json",success:function(i){a.Core.toType(h)==="function"&&setTimeout(h,100,i)},error:function(){g&&setTimeout(g,100,result)}})};return{get:function(c,b,f){var h="?",g;for(g in b)if(a.Core.isOwnProperty(b,g)){if(h!=="?")h+="&";h+=g+"="+b[g]}c+=h;e("GET",c,null,f)},post:function(c,b,f){e("POST",c,b,f)}}}(LUNGO,Zepto);LUNGO.Router=function(a){var d={SHOW:"show",HIDE:"hide"},e=function(c){var b=false;if($(c).length>0)b=true;else a.Core.log(3,"Lungo.Router ERROR: The target "+c+" does not exists.");return b};return{section:function(c){c=c.indexOf("#")?"#"+c:c;if(e("section"+c)){a.Dom.query(a.Router.History.current()).removeClass(d.SHOW).addClass(d.HIDE);a.Dom.query(c).addClass(d.SHOW);a.Router.History.add(c)}},article:function(c,b){e(c+" article"+b)&&a.View.Article.show(c,b)},back:function(){a.Dom.query(a.Router.History.current()).removeClass(d.SHOW);
  12. a.Router.History.removeLast();a.Dom.query(a.Router.History.current()).removeClass(d.HIDE).addClass(d.SHOW)}}}(LUNGO);LUNGO.Router.History=function(){var a=[],d=function(){return a[a.length-1]};return{add:function(e){e!==d()&&a.push(e)},current:d,removeLast:function(){a.length-=1}}}();LUNGO.View.Article=function(a){var d={ARTICLE:"article",NAVIGATION_ITEM:"a"},e={ACTIVE:"current"};return{show:function(c,b){var f=c+" "+d.NAVIGATION_ITEM;a.Dom.query(f).removeClass(e.ACTIVE);f=a.Dom.query(f+'[href="'+b+'"]');f.addClass(e.ACTIVE);(f=f.data("title"))&&a.Dom.query(c+" header .title, "+c+" footer .title").text(f);a.Dom.query(c+" "+d.ARTICLE).removeClass(e.ACTIVE);a.Dom.query(b).addClass(e.ACTIVE)}}}(LUNGO);LUNGO.View.Resize=function(a){return{scroll:function(d){var e=d.children().first(),c=e.children().first();if(a.View.Scroll.isHorizontal(d)){c=e.children().length*c.width();e.css("width",c+"px")}else{var b=e.children().length;d=Math.floor(d.width()/c.width());c=Math.ceil(b/d)*c.height();e.css("height",c+"px")}},article:function(d,e,c,b){e=d.children(e);if(e.length>0){b=e[b]();d.children("article").css(c,b+"px")}},toolbars:function(){for(var d=".toolbar nav",e=a.Dom.query(d),c=0,b=e.length;c<b;c++){d=
  13. a.Dom.query(e[c]);var f=d.children();d=d.width()/f.length;f.css("width",d+"px")}}}}(LUNGO);LUNGO.View.Template=function(a){var d={};return{create:function(e,c){d[e]=c},exists:function(e){return d[e]?true:false},get:function(e){return d[e]},binding:function(e,c,b,f){a.View.Template.Binding.create(e,c,b,f)}}}(LUNGO);LUNGO.View.Template.Binding=function(a){var d=/\{{.*?\}}/gi,e=function(c,b){var f,h;for(h in c)if(a.Core.isOwnProperty(c,h)){f=RegExp("{{"+h+"}}","g");b=b.replace(f,c[h])}return b.replace(d,"")};return{create:function(c,b,f,h){if(a.View.Template.exists(b)){b=a.View.Template.get(b);var g=a.Core.toType(f);if(g==="array"){g="";for(var i=0,j=f.length;i<j;i++)g+=e(f[i],b);f=g}else if(g==="object")f=e(f,b);else{a.Core.log(3,"View.Template ERROR >> No type defined.");f=void 0}f=f;a.Dom.query("#"+c).html(f);
  14. a.Core.execute(h)}else a.Core.log(3,"lng.View.Template.binding: id "+b+" not exists")},dataAttribute:function(c,b){var f=c.data(b.tag);if(f){f=b.html.replace("{{value}}",f);c.prepend(f)}}}}(LUNGO);LUNGO.View.Template.List=function(a){var d=null,e=function(){var c=d.order_field,b=d.order_type==="desc"?-1:1;c&&b&&d.data.sort(function(f,h){return f[c]<h[c]?-b:f[c]>h[c]?b:0})};return{create:function(c){d=c;d.container_id+="_list";c=false;var b=!!a.Dom.query(d.container_id),f=a.View.Template.exists(d.template_id);if(b&&f){a.Dom.query("#"+d.container_id).html("");b=a.Core.toType(d.data);if(b==="array"||b==="object")c=true}if(c){e();a.View.Template.Binding.create(d.container_id,d.template_id,d.data);
  15. c=a.Dom.query("#"+d.container_id).parent().attr("id");a.View.Scroll.create(c,{snap:"li"})}}}}(LUNGO);LUNGO.View.Scroll=function(a){var d={hScroll:false,vScroll:false,useTransition:true,momentum:true,lockDirection:true,fixedScrollbar:true,fadeScrollbar:true,hideScrollbar:true},e=function(b){return b.hasClass("horizontal")?true:false},c=function(b,f){a.Data.Cache.exists("scrolls")||a.Data.Cache.set("scrolls",{});var h=a.Data.Cache.get("scrolls");h[b]=new iScroll(b,f);a.Data.Cache.set("scrolls",h)};return{create:function(b,f){if(b){var h=a.Dom.query("#"+b),g=h.children(),i=g.height()>=h.height();if(g.length>
  16. 0&&i){g=f;h=e(h)?"hScroll":"vScroll";g||(g={});g[h]=true;f=g=a.Core.mix(d,g);c(b,f)}}else a.Core.log(3,"ERROR: Impossible to create a <scroll> without ID")},update:function(b,f){var h=a.Dom.query("#"+b),g=h.children().first();if(g.length===0){h.html('<div id="'+b+'_scrl"></div>');g=h.children().first()}g.html(f);a.View.Resize.scroll(h);!a.Data.Cache.get("scrolls",b)&&c(b);a.Data.Cache.get("scrolls",b).refresh()},remove:function(b){if(a.Data.Cache.exists("scrolls")){a.Data.Cache.get("scrolls",b).destroy();
  17. a.Data.Cache.remove("scrolls",b)}},isHorizontal:e}}(LUNGO);LUNGO.Dom=function(a,d){return{query:function(e){return d(e)}}}(LUNGO,Zepto);LUNGO.Dom.Event=function(a){var d=function(e,c,b){var f=false;switch(c){case "SWIPE":a.Dom.query(e).swipe(b);break;case "SWIPE_LEFT":a.Dom.query(e).swipeLeft(b);break;case "SWIPE_RIGHT":a.Dom.query(e).swipeRight(b);break;case "SWIPE_UP":a.Dom.query(e).swipeUp(b);break;case "SWIPE_DOWN":a.Dom.query(e).swipeDown(b);break;case "DOUBLE_TAP":a.Environment.isDesktop()?a.Dom.query(e).live(a.Events.get(c),b):a.Dom.query(e).doubleTap(b);break;default:f=true}return f};return{bind:function(e,c,b){d(e,c,b)&&
  18. a.Dom.query(e).bind(a.Events.get(c),b)},unbind:function(e,c){a.Dom.query(e).unbind(a.Events.get(c))},live:function(e,c,b){d(e,c,b)&&a.Dom.query(e).live(a.Events.get(c),b)},die:function(e,c){a.Dom.query(e).die(a.Events.get(c))},delegate:function(e,c,b,f){d(e,b,f)&&a.Dom.query(e).delegate(c,a.Events.get(b),f)},undelegate:function(e){a.Dom.query(e).undelegate(e)},listener:function(e,c,b){e.addEventListener(a.Events.get(c),function(f){setTimeout(b,0,f)},false)}}}(LUNGO);LUNGO.Attributes.Data={search:{tag:"search",selector:".list",html:'<li class="search {{value}}"><input type="search" placeholder="Search..."><a href="#" class="button" data-icon="search"></a></li>'},icon:{tag:"icon",selector:"*",html:'<span class="icon {{value}}"></span>'},title:{tag:"title",selector:"header, footer",html:'<h1 class="title">{{value}}</h1>'},back:{tag:"back",selector:"header, footer",html:'<a href="#back" data-target="section" class="back onleft button icon {{value}}"></a>'}};LUNGO.Attributes.Section={header:{name:"header",reference:"height",bind:"top"},footer:{name:"footer",reference:"height",bind:"bottom"}};LUNGO.Data.Cache=function(a){var d={},e=function(b){return arguments.length===1?d[b]:d[arguments[0]][arguments[1]]},c=function(b){return d[b]?true:false};return{set:function(b,f){d[b]=c(b)?a.Core.mix(e(b),f):f},get:e,remove:function(b){if(arguments.length===1)delete d[b];else delete d[arguments[0]][arguments[1]]},exists:c}}(LUNGO);LUNGO.Data.Sql=function(a){var d={name:"lungo_db",version:"1.0",size:65536,schema:[]},e=null,c=function(g,i){a.Core.log(1,"lng.Data.Sql >> "+g);e.transaction(function(j){j.executeSql(g,[],function(k,l){f(i,l)},h)})},b=function(g,i){var j="",k;for(k in g)if(a.Core.isOwnProperty(g,k)){var l=g[k];if(j)j+=" "+i+" ";j+=k+"=";j+=isNaN(l)?'"'+l+'"':l}return j},f=function(g,i){a.Core.toType(g)==="function"&&setTimeout(g,100,i)},h=function(g,i){a.Core.log(3,"lng.Data.Sql >> "+i.code+": "+i.message)};return{init:function(g){d=
  19. a.Core.mix(d,g);if(e=openDatabase(d.name,d.version,d.name,d.size)){g=d.schema;var i=g.length;if(i)for(var j=0;j<i;j++){var k=g[j];k.drop===true&&c("DROP TABLE IF EXISTS "+k.name);var l=k.name;k=k.fields;var m="",n=void 0;for(n in k)if(a.Core.isOwnProperty(k,n)){if(m)m+=", ";m+=n+" "+k[n]}c("CREATE TABLE IF NOT EXISTS "+l+" ("+m+");")}}else a.Core.log(3,"lng.Data.Sql >> Failed to connect to database.")},select:function(g,i,j){i=i?" WHERE "+b(i,"AND"):"";c("SELECT * FROM "+g+i,function(k){for(var l=
  20. [],m=0,n=k.rows.length;m<n;m++)l.push(k.rows.item(m));if(l.length===1)l=l[0];f(j,l)})},insert:function(g,i){var j="",k="",l;for(l in i)if(a.Core.isOwnProperty(i,l)){var m=i[l];j+=j?", "+l:l;if(k)k+=", ";k+=isNaN(m)?'"'+m+'"':m}c("INSERT INTO "+g+" ("+j+") VALUES ("+k+")")},update:function(g,i,j){g="UPDATE "+g+" SET "+b(i,",");if(j)g+=" WHERE "+b(j,"AND");c(g)},drop:function(g,i){var j=i?" WHERE "+b(i,"AND"):"";c("DELETE FROM "+g+j+";")},execute:c}}(LUNGO);LUNGO.Data.Storage=function(){return{}}(LUNGO);LUNGO.Boot=function(a){return function(){a.Environment.start();a.Boot.Layout.start();a.Boot.Events.start();a.Boot.Data.start();a.Boot.Section.start();a.Boot.Article.start()}}(LUNGO);LUNGO.Boot.Layout=function(a){var d=null,e=null,c=function(){if(!location.hash||!d.addEventListener){d.scrollTo(0,1);var b=1,f=setInterval(function(){if(e.body){clearInterval(f);b="scrollTop"in e.body?e.body.scrollTop:1;d.scrollTo(0,b===1?0:1)}},15);d.addEventListener("load",function(){setTimeout(function(){d.scrollTo(0,b===1?0:1)},0)},false)}};return{start:function(){if(!a.Environment.isDesktop()){d=window;e=d.document;if(d.innerHeight==356){a.Dom.query("body").css("height","416px");c()}}}}}(LUNGO);LUNGO.Boot.Article=function(a){var d={LIST_IN_ARTICLE:"article.list",SCROLL_IN_ARTICLE:"scroll",CHECKBOX_IN_ARTICLE:".checkbox, .radio"},e=function(h,g){for(var i=a.Dom.query(h),j=0,k=i.length;j<k;j++){var l=a.Dom.query(i[j]);a.Core.execute(g,l)}},c=function(h){if(h.children().length===0){var g=h.attr("id");h.append('<ul id="'+g+'_list"></ul>')}},b=function(h){h=h.attr("id");a.View.Scroll.create(h)},f=function(h){h.append("<span>&nbsp;</span>")};return{start:function(){e(d.LIST_IN_ARTICLE,c);e(d.SCROLL_IN_ARTICLE,
  21. b);e(d.CHECKBOX_IN_ARTICLE,f)}}}(LUNGO);LUNGO.Boot.Data=function(a){return{start:function(){var d=a.Attributes.Data,e;for(e in d)if(a.Core.isOwnProperty(d,e))for(var c=d[e],b=a.Dom.query(c.selector),f=0,h=b.length;f<h;f++){var g=a.Dom.query(b[f]);a.View.Template.Binding.dataAttribute(g,c)}}}}(LUNGO);LUNGO.Boot.Events=function(a){var d=function(h){h.preventDefault()},e=function(){a.View.Resize.toolbars()},c=function(h){h.preventDefault();h=a.Dom.query(this);h=f(h);a.View.Aside.toggle(h)},b=function(h){h.preventDefault();var g=a.Dom.query(this);switch(g.data("target")){case "section":h=g.attr("href");h==="#back"?a.Router.back():a.Router.section(h);break;case "article":h=f(g);g=g.attr("href");a.Router.article(h,g);break;case "aside":h=f(g);a.View.Aside.toggle(h)}},f=function(h){return"#"+h.parents("section").attr("id")};
  22. return{start:function(){a.Dom.Event.listener(document,"TOUCH_MOVE",d);a.Dom.Event.listener(window,"ORIENTATION_CHANGE",e);a.Dom.Event.live("ASIDE a[href][data-target]","TOUCH_START",c);a.Dom.Event.live("a[href][data-target]","TOUCH_START",b)}}}(LUNGO);LUNGO.Boot.Section=function(a){var d={ARTICLE:"article",SECTION:"section"};return{start:function(){var e=a.Dom.query(d.SECTION),c=e.first(),b="#"+c.attr("id");c.addClass("current");a.Router.History.add(b);c=0;for(b=e.length;c<b;c++){var f=a.Dom.query(e[c]),h=f,g=a.Attributes.Section,i=void 0;for(i in g)if(a.Core.isOwnProperty(g,i)){var j=g[i];a.View.Resize.article(h,j.name,j.bind,j.reference)}f.children(d.ARTICLE).first().addClass("current")}a.View.Resize.toolbars();e.css({"-webkit-transition":"-webkit-transform 0.3s ease-in-out"})}}}(LUNGO);