lungo-1.1.min.js 12 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.1
  6. * @link https://github.com/TapQuo/Lungo.js
  7. *
  8. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  9. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  10. */
  11. var LUNGO=LUNGO||{};LUNGO.VERSION="1.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 b={id:1,name:"lungo_app",version:1,icon:""};return{init:function(e){b=a.Core.mix(b,e);a.Boot()},get:function(e){return b[e]}}}(LUNGO);LUNGO.Core=function(a,b){var e=Array.prototype,c=function(g,i){return b.isOwnProperty(g,i)},d=function(g){return b.toType(g)},f=function(g){return e.slice.call(g,0)};return{log:function(g,i){a.Core.isMobile()||console[g===1?"log":g===2?"warn":"error"](i)},execute:function(){var g=f(arguments),i=g.shift();d(i)==="function"&&i.apply(null,g)},bind:function(g,i){return function(){return i.apply(g,f(arguments))}},mix:function(){for(var g=g||{},i=0,h=arguments.length;i<h;i++){var j=arguments[i],k;for(k in j)if(c(j,
  12. k))g[k]=j[k]}return g},isOwnProperty:c,toType:d,toArray:f,isMobile:function(){return b.isMobile()},environment:function(){return b.environment()}}}(LUNGO,Quo);LUNGO.dom=function(a){return $$(a)};LUNGO.Service=function(a,b){return{get:function(e,c,d,f){return b.get(e,c,d,f)},post:function(e,c,d,f){return b.post(e,c,d,f)},json:function(e,c,d){return b.json(e,c,d)},Settings:b.ajaxSettings}}(LUNGO,Quo);LUNGO.Router=function(a){var b={SHOW:"show",HIDE:"hide"},e=function(c){var d=false;if(a.dom(c).length>0)d=true;else a.Core.log(3,"Lungo.Router ERROR: The target "+c+" does not exists.");return d};return{section:function(c){c=c.indexOf("#")?"#"+c:c;if(e("section"+c)){a.dom(a.Router.History.current()).removeClass(b.SHOW).addClass(b.HIDE);a.dom(c).addClass(b.SHOW);a.Router.History.add(c)}},article:function(c,d){e(c+" article"+d)&&a.View.Article.show(c,d)},back:function(){a.dom(a.Router.History.current()).removeClass(b.SHOW);
  13. a.Router.History.removeLast();a.dom(a.Router.History.current()).removeClass(b.HIDE).addClass(b.SHOW)}}}(LUNGO);LUNGO.Router.History=function(){var a=[],b=function(){return a[a.length-1]};return{add:function(e){e!==b()&&a.push(e)},current:b,removeLast:function(){a.length-=1}}}();LUNGO.View.Article=function(a){var b={ARTICLE:"article",NAVIGATION_ITEM:"a"},e={ACTIVE:"current"};return{show:function(c,d){var f=c+" "+b.NAVIGATION_ITEM;a.dom(f).removeClass(e.ACTIVE);f=a.dom(f+'[href="'+d+'"]');f.addClass(e.ACTIVE);(f=f.data("title"))&&a.dom(c+" header .title, "+c+" footer .title").text(f);a.dom(c+" "+b.ARTICLE).removeClass(e.ACTIVE);a.dom(d).addClass(e.ACTIVE)}}}(LUNGO);LUNGO.View.Resize=function(a){return{scroll:function(b){var e=b.children().first(),c=e.children().first();if(a.View.Scroll.isHorizontal(b)){c=e.children().length*c.width();e.style("width",c+"px")}else{var d=e.children().length;b=Math.floor(b.width()/c.width());c=Math.ceil(d/b)*c.height();e.style("height",c+"px")}},article:function(b,e,c,d){e=b.children(e);if(e.length>0){d=e[d]();b.children("article").style(c,d+"px")}},toolbars:function(){for(var b=".toolbar nav",e=a.dom(b),c=0,d=e.length;c<d;c++){b=
  14. a.dom(e[c]);var f=b.children();b=b.width()/f.length;f.style("width",b+"px")}}}}(LUNGO);LUNGO.View.Template=function(a){var b={};return{create:function(e,c){b[e]=c},exists:function(e){return b[e]?true:false},get:function(e){return b[e]},binding:function(e,c,d,f){a.View.Template.Binding.create(e,c,d,f)}}}(LUNGO);LUNGO.View.Template.Binding=function(a){var b=/\{{.*?\}}/gi,e=function(c,d){var f,g;for(g in c)if(a.Core.isOwnProperty(c,g)){f=RegExp("{{"+g+"}}","g");d=d.replace(f,c[g])}return d.replace(b,"")};return{create:function(c,d,f,g){if(a.View.Template.exists(d)){d=a.View.Template.get(d);var i=a.Core.toType(f);if(i==="array"){i="";for(var h=0,j=f.length;h<j;h++)i+=e(f[h],d);f=i}else if(i==="object")f=e(f,d);else{a.Core.log(3,"View.Template ERROR >> No type defined.");f=void 0}f=f;a.dom("#"+c).html(f);a.Core.execute(g)}else a.Core.log(3,
  15. "lng.View.Template.binding: id "+d+" not exists")},dataAttribute:function(c,d){var f=c.data(d.tag);if(f){f=d.html.replace("{{value}}",f);c.prepend(f)}}}}(LUNGO);LUNGO.View.Template.List=function(a){var b=null,e=function(){var c=b.order_field,d=b.order_type==="desc"?-1:1;c&&d&&b.data.sort(function(f,g){return f[c]<g[c]?-d:f[c]>g[c]?d:0})};return{create:function(c){b=c;b.container_id+="_list";c=false;var d=!!a.dom(b.container_id),f=a.View.Template.exists(b.template_id);if(d&&f){a.dom("#"+b.container_id).html("");d=a.Core.toType(b.data);if(d==="array"||d==="object")c=true}if(c){e();a.View.Template.Binding.create(b.container_id,b.template_id,b.data);c=a.dom("#"+
  16. b.container_id).parent().attr("id");a.View.Scroll.create(c,{snap:"li"})}}}}(LUNGO);LUNGO.View.Scroll=function(a){var b={hScroll:false,vScroll:false,useTransition:true,momentum:true,lockDirection:true,fixedScrollbar:true,fadeScrollbar:true,hideScrollbar:true},e=function(d){return d.hasClass("horizontal")?true:false},c=function(d,f){a.Data.Cache.exists("scrolls")||a.Data.Cache.set("scrolls",{});var g=a.Data.Cache.get("scrolls");g[d]=new iScroll(d,f);a.Data.Cache.set("scrolls",g)};return{create:function(d,f){if(d){var g=a.dom("#"+d);setTimeout(function(){var i=false,h=g[0];if(h.clientHeight<
  17. h.scrollHeight){i=true;var j=h.scrollHeight+90;a.dom(h).children().first().style("height",j+"px")}if(i){i=f;h=e(g)?"hScroll":"vScroll";i||(i={});i[h]=true;f=i=a.Core.mix(b,i);c(d,f)}},100)}else a.Core.log(3,"ERROR: Impossible to create a <scroll> without ID")},update:function(d,f){var g=a.dom("#"+d),i=g.children().first();if(i.length===0){g.html('<div id="'+d+'_scrl"></div>');i=g.children().first()}i.html(f);a.View.Resize.scroll(g);!a.Data.Cache.get("scrolls",d)&&c(d);a.Data.Cache.get("scrolls",d).refresh()},
  18. remove:function(d){if(a.Data.Cache.exists("scrolls")){a.Data.Cache.get("scrolls",d).destroy();a.Data.Cache.remove("scrolls",d)}},isHorizontal:e}}(LUNGO);LUNGO.View.Aside=function(a){return{toggle:function(b){a.dom(b+" article").toggleClass("aside")}}}(LUNGO);LUNGO.View.Element=function(a){var b={BUBBLE:".bubble.count"};return{count:function(e,c){var d=a.dom(e);if(d)if(c>0){var f=d.children(b.BUBBLE);if(f.length>0)f.html(c);else{f=LUNGO.Attributes.Data.Count.html.replace("{{value}}",c);d.append(f)}}else d.children(b.BUBBLE).remove()}}}(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>'},Count:{tag:"count",selector:"*",html:'<span class="bubble count">{{value}}</span>'},Search:{tag:"search",selector:"*",html:'<input type="search" placeholder="{{value}}"/><a href="#" class="button" data-icon="search"></a>'},Icon:{tag:"icon",selector:"*",html:'<span class="icon {{value}}"></span>'},Image:{tag:"image",
  19. selector:"*",html:'<img src="{{value}}" class="icon" />'},Title:{tag:"title",selector:"header, footer, article",html:'<span class="title">{{value}}</span>'},Back:{tag:"back",selector:"header, footer",html:'<a href="#back" data-target="section" class="onleft button"><span class="icon {{value}}"></span></a>'}};LUNGO.Data.Cache=function(a){var b={},e=function(d){return arguments.length===1?b[d]:b[arguments[0]][arguments[1]]},c=function(d){return b[d]?true:false};return{set:function(d,f){b[d]=c(d)?a.Core.mix(e(d),f):f},get:e,remove:function(d){if(arguments.length===1)delete b[d];else delete b[arguments[0]][arguments[1]]},exists:c}}(LUNGO);LUNGO.Data.Sql=function(a){var b={name:"lungo_db",version:"1.0",size:65536,schema:[]},e=null,c=function(h,j){a.Core.log(1,"lng.Data.Sql >> "+h);e.transaction(function(k){k.executeSql(h,[],function(l,m){f(j,m)},i)})},d=function(h,j){var k="",l;for(l in h)if(a.Core.isOwnProperty(h,l)){var m=h[l];if(k)k+=" "+j+" ";k+=l+"=";k+=isNaN(m)?'"'+m+'"':m}return k},f=function(h,j){a.Core.toType(h)==="function"&&setTimeout(h,100,j)},g=function(h,j){var k="",l="",m;for(m in j)if(a.Core.isOwnProperty(j,m)){var n=
  20. j[m];k+=k?", "+m:m;if(l)l+=", ";l+=isNaN(n)?'"'+n+'"':n}c("INSERT INTO "+h+" ("+k+") VALUES ("+l+")")},i=function(h,j){a.Core.log(3,"lng.Data.Sql >> "+j.code+": "+j.message)};return{init:function(h){b=a.Core.mix(b,h);if(e=openDatabase(b.name,b.version,b.name,b.size)){h=b.schema;var j=h.length;if(j)for(var k=0;k<j;k++){var l=h[k];l.drop===true&&c("DROP TABLE IF EXISTS "+l.name);var m=l.name;l=l.fields;var n="",o=void 0;for(o in l)if(a.Core.isOwnProperty(l,o)){if(n)n+=", ";n+=o+" "+l[o]}c("CREATE TABLE IF NOT EXISTS "+
  21. m+" ("+n+");")}}else a.Core.log(3,"lng.Data.Sql >> Failed to connect to database.")},select:function(h,j,k){j=j?" WHERE "+d(j,"AND"):"";c("SELECT * FROM "+h+j,function(l){for(var m=[],n=0,o=l.rows.length;n<o;n++)m.push(l.rows.item(n));f(k,m)})},insert:function(h,j){if(a.Core.toType(j)==="object")g(h,j);else for(row in j)g(h,j[row])},update:function(h,j,k){h="UPDATE "+h+" SET "+d(j,",");if(k)h+=" WHERE "+d(k,"AND");c(h)},drop:function(h,j){var k=j?" WHERE "+d(j,"AND"):"";c("DELETE FROM "+h+k+";")},
  22. execute:c}}(LUNGO);LUNGO.Data.Storage=function(){return{}}(LUNGO);LUNGO.Boot=function(a){return function(){a.Boot.Layout.start();a.Boot.Events.start();a.Boot.Data.start();a.Boot.Section.start();a.Boot.Article.start();a.Boot.Stats.start()}}(LUNGO);LUNGO.Boot.Stats=function(a){var b=function(){a.Service.post("http://www.lungojs.com/stats/",{name:a.App.get("name"),version:a.App.get("version"),icon:a.App.get("icon")},function(){})};return{start:function(){a.Core.isMobile()&&b()}}}(LUNGO);LUNGO.Boot.Layout=function(a){var b=null,e=null,c=function(){if(!location.hash||!b.addEventListener){b.scrollTo(0,1);var d=1,f=setInterval(function(){if(e.body){clearInterval(f);d="scrollTop"in e.body?e.body.scrollTop:1;b.scrollTo(0,d===1?0:1)}},15);b.addEventListener("load",function(){setTimeout(function(){b.scrollTo(0,d===1?0:1)},0)},false)}};return{start:function(){if(a.Core.isMobile()){b=window;e=b.document;if(b.innerHeight==356){a.dom("body").style("height","416px");c()}}}}}(LUNGO);LUNGO.Boot.Article=function(a){var b={LIST_IN_ARTICLE:"article.list",SCROLL_IN_ARTICLE:".scrollable",CHECKBOX_IN_ARTICLE:".checkbox, .radio"},e=function(g,i){for(var h=a.dom(g),j=0,k=h.length;j<k;j++){var l=a.dom(h[j]);a.Core.execute(i,l)}},c=function(g){if(g.children().length===0){var i=g.attr("id");g.append('<ul id="'+i+'_list"></ul>')}},d=function(g){g=g.attr("id");a.View.Scroll.create(g)},f=function(g){g.append("<span>&nbsp;</span>")};return{start:function(){e(b.LIST_IN_ARTICLE,c);e(b.SCROLL_IN_ARTICLE,
  23. d);e(b.CHECKBOX_IN_ARTICLE,f)}}}(LUNGO);LUNGO.Boot.Data=function(a){return{start:function(){var b=a.Attributes.Data,e;for(e in b)if(a.Core.isOwnProperty(b,e))for(var c=b[e],d=a.dom(c.selector),f=0,g=d.length;f<g;f++){var i=a.dom(d[f]);a.View.Template.Binding.dataAttribute(i,c)}}}}(LUNGO);LUNGO.Boot.Events=function(a){var b=function(g){g.preventDefault()},e=function(){a.View.Resize.toolbars()},c=function(g){var i=a.dom(this);i=f(i);a.View.Aside.toggle(i);g.preventDefault()},d=function(g){var i=a.dom(this);switch(i.data("target")){case "section":var h=i.attr("href");h==="#back"?a.Router.back():a.Router.section(h);break;case "article":h=f(i);i=i.attr("href");a.Router.article(h,i);break;case "aside":h=f(i);a.View.Aside.toggle(h)}g.preventDefault()},f=function(g){return"#"+g.parent("section").attr("id")};
  24. return{start:function(){a.dom(document).on("touchmove",b);a.dom(window).on("orientationchange",e);a.dom("aside a[href][data-target]").tap(c);a.dom("a[href][data-target]").tap(d)}}}(LUNGO);LUNGO.Boot.Section=function(a){var b={ARTICLE:"article",SECTION:"section"};return{start:function(){var e=a.dom(b.SECTION),c=e.first(),d="#"+c.attr("id");c.addClass("current");a.Router.History.add(d);if(a.Core.isMobile()){c=a.Core.environment();c.os.name==="ios"&&c.os.version>="5."&&e.style("position","fixed")}c=0;for(d=e.length;c<d;c++)a.dom(e[c]).children(b.ARTICLE).first().addClass("current");a.dom("aside").addClass("show");a.View.Resize.toolbars()}}}(LUNGO);