lungo-1.0.2.min.js 13 KB

12345678910111213141516171819202122232425
  1. /**
  2. *
  3. * LungoJS - HTML5 Mobile Framework... and stuff.
  4. *
  5. * @copyright 2011 TapQuo Inc (c)
  6. * @license http://www.github.com/tapquo/lungo/blob/master/LICENSE.txt
  7. * @version 1.0.2
  8. * @link https://github.com/TapQuo/Lungo.js
  9. *
  10. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  11. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  12. */
  13. var LUNGO=LUNGO||{};LUNGO.VERSION="1.0.1";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(h,j){return c.hasOwnProperty.call(h,j)},i=function(h){return c.toString.call(h).match(/\s([a-z|A-Z]+)/)[1].toLowerCase()},g=function(h){return e.slice.call(h,0)};return{log:function(h,j){if(a.Environment.isDesktop())console[h===1?"log":h===2?"warn":"error"](j)},execute:function(){var h=g(arguments),j=h.shift();i(j)==="function"&&j.apply(null,h)},bind:function(h,j){return function(){return j.apply(h,
  14. g(arguments))}},mix:function(){for(var h=h||{},j=0,k=arguments.length;j<k;j++){var l=arguments[j],m;for(m in l)if(f(l,m))h[m]=l[m]}return h},isOwnProperty:f,toType:i,toArray:g,isMobile:function(){for(var h=false,j=0,k=b.length;j<k&&!h;j++)d.os[b[j]]&&(h=true);return h}}}(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:"click",TOUCH_END:"click",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,i,g){d.ajax({type:c,url:b,data:f,dataType:"json",success:function(h){a.Core.toType(i)==="function"&&setTimeout(i,100,h)},error:function(){g&&setTimeout(g,100,result)}})};return{get:function(c,b,f){var i="?",g;for(g in b)if(a.Core.isOwnProperty(b,g)){if(i!=="?")i+="&";i+=g+"="+b[g]}c+=i;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);
  15. 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:"nav 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){(e=d.children(e))&&d.children("article").css(c,e.height()+"px")},toolbars:function(){for(var d=".toolbar nav",e=a.Dom.query(d),c=0,b=e.length;c<b;c++){d=a.Dom.query(e[c]);
  16. 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,i;for(i in c)if(a.Core.isOwnProperty(c,i)){f=RegExp("{{"+i+"}}","g");b=b.replace(f,c[i])}return b.replace(d,"")};return{create:function(c,b,f,i){if(a.View.Template.exists(b)){b=a.View.Template.get(b);var g=a.Core.toType(f);if(g==="array"){g="";for(var h=0,j=f.length;h<j;h++)g+=e(f[h],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);
  17. a.Core.execute(i)}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,i){return f[c]<i[c]?-b:f[c]>i[c]?b:0})};return{create:function(c){d=c;d.container_id+="_list";c=false;if(a.Dom.query(d.container_id)){a.Dom.query("#"+d.container_id).html("");if(a.View.Template.exists(d.template_id)&&d.data.length)c=true}if(c){e();a.View.Template.Binding.create(d.container_id,d.template_id,d.data);c=a.Dom.query("#"+d.container_id).parent().attr("id");
  18. 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 i=a.Data.Cache.get("scrolls");i[b]=new iScroll(b,f);a.Data.Cache.set("scrolls",i)};return{create:function(b,f){if(b){var i=a.Dom.query("#"+b),g=i.children(),h=g.height()>=i.height();if(g.length>
  19. 0&&h){g=f;i=e(i)?"hScroll":"vScroll";g||(g={});g[i]=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 i=a.Dom.query("#"+b),g=i.children().first();if(g.length===0){i.html('<div id="'+b+'_scrl"></div>');g=i.children().first()}g.html(f);a.View.Resize.scroll(i);!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();
  20. 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)&&
  21. 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",bind:"top"},footer:{name:"footer",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,h){a.Core.log(1,"lng.Data.Sql >> "+g);e.transaction(function(j){j.executeSql(g,[],function(k,l){f(h,l)},i)})},b=function(g,h){var j="",k;for(k in g)if(a.Core.isOwnProperty(g,k)){var l=g[k];if(j)j+=" "+h+" ";j+=k+"=";j+=isNaN(l)?'"'+l+'"':l}return j},f=function(g,h){a.Core.toType(g)==="function"&&setTimeout(g,100,h)},i=function(g,h){a.Core.log(3,"lng.Data.Sql >> "+h.code+": "+h.message)};return{init:function(g){d=
  22. a.Core.mix(d,g);if(e=openDatabase(d.name,d.version,d.name,d.size)){g=d.schema;var h=g.length;if(h)for(var j=0;j<h;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,h,j){h=h?" WHERE "+b(h,"AND"):"";c("SELECT * FROM "+g+h,function(k){for(var l=
  23. [],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,h){var j="",k="",l;for(l in h)if(a.Core.isOwnProperty(h,l)){var m=h[l];j+=j?", "+l:l;if(k)k+=", ";k+=isNaN(m)?'"'+m+'"':m}c("INSERT INTO "+g+" ("+j+") VALUES ("+k+")")},update:function(g,h,j){g="UPDATE "+g+" SET "+b(h,",");if(j)g+=" WHERE "+b(j,"AND");c(g)},drop:function(g,h){var j=h?" WHERE "+b(h,"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(i,g){for(var h=a.Dom.query(i),j=0,k=h.length;j<k;j++){var l=a.Dom.query(h[j]);a.Core.execute(g,l)}},c=function(i){if(i.children().length===0){var g=i.attr("id");i.append('<ul id="'+g+'_list"></ul>')}},b=function(i){i=i.attr("id");a.View.Scroll.create(i)},f=function(i){i.append("<span>&nbsp;</span>")};return{start:function(){e(d.LIST_IN_ARTICLE,c);e(d.SCROLL_IN_ARTICLE,
  24. 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,i=b.length;f<i;f++){var g=a.Dom.query(b[f]);a.View.Template.Binding.dataAttribute(g,c)}}}}(LUNGO);LUNGO.Boot.Events=function(a){var d=function(b){b.preventDefault()},e=function(){a.View.Resize.toolbars()},c=function(b){b.preventDefault();b=a.Dom.query(this);var f=b.attr("href");if(b.data("target")==="section")f==="#back"?a.Router.back():a.Router.section(f);else{f="#"+b.parents("section").attr("id");b=b.attr("href");a.Router.article(f,b)}};return{start:function(){a.Dom.Event.listener(document,"TOUCH_MOVE",d);a.Dom.Event.listener(window,"ORIENTATION_CHANGE",e);a.Dom.Event.live("a[href][data-target]",
  25. "TOUCH_START",c)}}}(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]),i=f,g=a.Attributes.Section,h=void 0;for(h in g)if(a.Core.isOwnProperty(g,h)){var j=g[h];a.View.Resize.article(i,j.name,j.bind)}f.children(d.ARTICLE).first().addClass("current")}a.View.Resize.toolbars();e.css({"-webkit-transition":"-webkit-transform 0.3s ease-in-out"})}}}(LUNGO);