lungo-1.0.1.js 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  1. /**
  2. *
  3. * /$$
  4. * | $$
  5. * | $$ /$$ /$$ /$$$$$$$ /$$$$$$ /$$$$$$
  6. * | $$ | $$ | $$| $$__ $$ /$$__ $$ /$$__ $$
  7. * | $$ | $$ | $$| $$ \ $$| $$ \ $$| $$ \ $$
  8. * | $$ | $$ | $$| $$ | $$| $$ | $$| $$ | $$
  9. * | $$$$$$$$| $$$$$$/| $$ | $$| $$$$$$$| $$$$$$/
  10. * |________/ \______/ |__/ |__/ \____ $$ \______/
  11. * /$$ \ $$
  12. * | $$$$$$/
  13. * \______/
  14. *
  15. * @copyright 2011 TapQuo Inc (c)
  16. * @license http://www.github.com/tapquo/lungo/blob/master/LICENSE.txt
  17. * @version 1.0.1
  18. * @link https://github.com/TapQuo/Lungo.js
  19. *
  20. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  21. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  22. */
  23. var LUNGO = LUNGO || {};
  24. LUNGO.VERSION = '1.0.1';
  25. LUNGO.Attributes || (LUNGO.Attributes = {});
  26. LUNGO.Data || (LUNGO.Data = {});
  27. LUNGO.Sugar || (LUNGO.Sugar = {});
  28. LUNGO.View || (LUNGO.View = {});/**
  29. * Lungo sandbox APP initialization
  30. *
  31. * @namespace LUNGO
  32. * @class App
  33. *
  34. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  35. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  36. */
  37. LUNGO.App = (function(lng, undefined) {
  38. var default_config = {
  39. id: 1,
  40. name: 'lungo_app',
  41. version: 1.0,
  42. icon: ''
  43. };
  44. /**
  45. * Initializes all LungoJS system: setting properties for the application,
  46. * subscribing to automatic events, initializing sections & articles
  47. * and stating the title.
  48. *
  49. * @method init
  50. *
  51. * @param {object} Application configuration properties
  52. */
  53. var init = function(app_config) {
  54. default_config = lng.Core.mix(default_config, app_config);
  55. lng.Boot();
  56. };
  57. var get = function(property) {
  58. return default_config[property];
  59. };
  60. return {
  61. init: init,
  62. get: get
  63. };
  64. })(LUNGO);/**
  65. * Set environment (Desktop or Mobile) automatically, depending on the
  66. * environment the subscribed events wil be different.
  67. *
  68. * @namespace LUNGO
  69. * @class Environment
  70. *
  71. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  72. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  73. */
  74. LUNGO.Environment = (function(lng, undefined) {
  75. var MOBILE_ENVIRONMENT = 'mobile';
  76. var DESKTOP_ENVIRONMENT = 'desktop';
  77. var _environment = DESKTOP_ENVIRONMENT;
  78. /**
  79. * Analizing if it's run in Mobile Phone and changing the type of event to subscribe.
  80. *
  81. * @method start
  82. */
  83. var start = function() {
  84. if (lng.Core.isMobile()) {
  85. _environment = MOBILE_ENVIRONMENT;
  86. _saveStatsInLungoJS();
  87. }
  88. };
  89. /**
  90. * Gets the current environment for LungoJS
  91. *
  92. * @method init
  93. *
  94. * @return {String} Current environment enumerator
  95. */
  96. var current = function() {
  97. return _environment;
  98. };
  99. /**
  100. * Returns whether the development environment is in desktop mode
  101. *
  102. * @method isDesktop
  103. *
  104. * @return {Boolean} True if is in DESKTOP_ENVIRONMENT
  105. */
  106. var isDesktop = function() {
  107. return (_environment === DESKTOP_ENVIRONMENT) ? true : false;
  108. };
  109. /**
  110. * Save in LungoJS.com the use of the service for further ranking
  111. *
  112. * @method _saveStatsInLungoJS
  113. */
  114. var _saveStatsInLungoJS = function() {
  115. lng.Service.post( 'http://www.lungojs.com/stats/', {
  116. name: lng.App.get('name'),
  117. version: lng.App.get('version'),
  118. icon: lng.App.get('icon')
  119. });
  120. }
  121. return {
  122. start: start,
  123. current: current,
  124. isDesktop: isDesktop
  125. };
  126. })(LUNGO);/**
  127. * Contains all the common functions used in Lungo.
  128. *
  129. * @namespace LUNGO
  130. * @class Core
  131. *
  132. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  133. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  134. */
  135. LUNGO.Core = (function(lng, $, undefined) {
  136. var ARRAY_PROTO = Array.prototype;
  137. var OBJ_PROTO = Object.prototype;
  138. var SUPPORTED_OS = ['ios', 'android', 'blackberry', 'webos'];
  139. /**
  140. * Console system to display messages when you are in debug mode.
  141. *
  142. * @method log
  143. *
  144. * @param {number} Severity based in (1)Log, (2)Warn, (>2)Error
  145. * @param {string} Message to show in console
  146. */
  147. var log = function(severity, message) {
  148. if (lng.Environment.isDesktop()) {
  149. console[(severity === 1) ? 'log' : (severity === 2) ? 'warn' : 'error'](message);
  150. } else {
  151. // @todo : send to the server
  152. }
  153. };
  154. /**
  155. * Executes callbacks based on the parameters received.
  156. *
  157. * @method execute
  158. *
  159. * @param {Function} callback to execute
  160. */
  161. var execute = function() {
  162. var args = toArray(arguments);
  163. var callback = args.shift();
  164. if (toType(callback) === 'function') {
  165. callback.apply(null, args);
  166. }
  167. };
  168. /**
  169. * Creates a new function that, when called, itself calls this function in
  170. * the context of the provided this value, with a given sequence of arguments
  171. * preceding any provided when the new function was called.
  172. *
  173. * @method bind
  174. *
  175. * @param {object} object to which the 'this' can refer in the new function when the new function is called.
  176. * @param {Function} method A function object.
  177. */
  178. var bind = function(object, method) {
  179. return function() {
  180. return method.apply(object, toArray(arguments));
  181. };
  182. };
  183. /**
  184. * Copy from any number of objects and mix them all into a new object.
  185. * The implementation is simple; just loop through arguments and
  186. * copy every property of every object passed to the function.
  187. *
  188. * @method mix
  189. *
  190. * @param {object} arguments to mix them all into a new object.
  191. * @return {object} child a new object with all the objects from the arguments mixed.
  192. */
  193. var mix = function() {
  194. var child = child || {};
  195. for (var arg = 0, len = arguments.length; arg < len; arg++) {
  196. var argument = arguments[arg];
  197. for (var prop in argument) {
  198. if (isOwnProperty(argument, prop)) {
  199. child[prop] = argument[prop];
  200. }
  201. }
  202. }
  203. return child;
  204. };
  205. /**
  206. * Every object descended from Object inherits the hasOwnProperty method.
  207. * This method can be used to determine whether an object has the specified property
  208. * as a direct property of that object.
  209. *
  210. * @param {object} object to test for a property's existence inside itself.
  211. * @param {string} property the name of the property to test.
  212. * @return {boolean} indicating whether the object has the specified property.
  213. */
  214. var isOwnProperty = function(object, property) {
  215. return OBJ_PROTO.hasOwnProperty.call(object, property);
  216. };
  217. /**
  218. * Determine the internal JavaScript [[Class]] of an object.
  219. *
  220. * @param {object} obj to get the real type of itself.
  221. * @return {string} with the internal JavaScript [[Class]] of itself.
  222. */
  223. var toType = function(obj) {
  224. return OBJ_PROTO.toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
  225. };
  226. /**
  227. * Convert an array-like object into a true JavaScript array.
  228. *
  229. * @param {object} obj Any object to turn into a native Array.
  230. * @return {object} The object is now a plain array.
  231. */
  232. var toArray = function(obj) {
  233. return ARRAY_PROTO.slice.call(obj, 0);
  234. };
  235. /**
  236. *
  237. *
  238. */
  239. var isMobile = function() {
  240. var result = false;
  241. for (var i = 0, len = SUPPORTED_OS.length; i < len && !result; i++) {
  242. var mobile_os = SUPPORTED_OS[i];
  243. $.os[mobile_os] && (result = true);
  244. }
  245. return result;
  246. };
  247. return {
  248. log: log,
  249. execute: execute,
  250. bind: bind,
  251. mix: mix,
  252. isOwnProperty: isOwnProperty,
  253. toType: toType,
  254. toArray: toArray,
  255. isMobile: isMobile
  256. };
  257. })(LUNGO, Zepto);/**
  258. * Lungo UI events Manager
  259. *
  260. * @namespace LUNGO
  261. * @class Event
  262. * @requires Zepto
  263. *
  264. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  265. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  266. */
  267. LUNGO.Events = (function(lng, undefined) {
  268. var EVENTS = {
  269. mobile: {
  270. TOUCH_START: 'touchstart',
  271. TOUCH_MOVE: 'touchmove',
  272. TOUCH_END: 'touchend',
  273. TAP: 'tap',
  274. DOUBLE_TAP: 'doubletap',
  275. ORIENTATION_CHANGE: 'orientationchange'
  276. },
  277. desktop: {
  278. TOUCH_START: 'click',
  279. TOUCH_MOVE: 'click',
  280. TOUCH_END: 'click',
  281. TAP: 'click',
  282. DOUBLE_TAP: 'dblclick',
  283. ORIENTATION_CHANGE: 'orientationchange'
  284. }
  285. };
  286. var current_environment = lng.Environment.current();
  287. var current_events = EVENTS[current_environment];
  288. /**
  289. * Returns the touch event based on an enumeration of LungoJS
  290. * and the current environment
  291. *
  292. * @method get
  293. *
  294. * @param {string} Touch enumerator of LungoJS
  295. * @return {string} Touch event based on the current environment
  296. */
  297. var get = function(eventName) {
  298. return current_events[eventName];
  299. };
  300. return {
  301. get: get
  302. };
  303. })(LUNGO);/**
  304. * External Data & Services Manager
  305. *
  306. * @namespace LUNGO
  307. * @class Service
  308. * @requires Zepto
  309. *
  310. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  311. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  312. */
  313. LUNGO.Service = (function(lng, $, undefined) {
  314. /**
  315. * Load data from the server using a HTTP GET request.
  316. *
  317. * @method get
  318. *
  319. * @param {string} Containing the URL to which the request is sent
  320. * @param {object} A map or string that is sent to the server with the request
  321. * @param {Function} [OPTIONAL] Callback function after the request
  322. */
  323. var get = function(url, data, callback) {
  324. var parameters = '?';
  325. for (var parameter in data) {
  326. if (lng.Core.isOwnProperty(data, parameter)) {
  327. if (parameters !== '?') parameters += '&';
  328. parameters += parameter + '=' + data[parameter];
  329. }
  330. }
  331. url = url + parameters;
  332. _ajax('GET', url, null, callback);
  333. };
  334. /**
  335. * Load data from the server using a HTTP POST request.
  336. *
  337. * @method post
  338. *
  339. * @param {string} Containing the URL to which the request is sent
  340. * @param {object} A map or string that is sent to the server with the request
  341. * @param {Function} [OPTIONAL] Callback function after the request
  342. */
  343. var post = function(url, data, callback) {
  344. _ajax('POST', url, data, callback);
  345. };
  346. var _ajax = function(type, url, data, callback, error) {
  347. $.ajax({
  348. type: type,
  349. url: url,
  350. data: data,
  351. dataType: 'json',
  352. success: function(response) {
  353. if (lng.Core.toType(callback) === 'function') {
  354. setTimeout(callback, 100, response);
  355. }
  356. },
  357. error: function(xhr, type) {
  358. if (error) {
  359. setTimeout(error, 100, result);
  360. }
  361. }
  362. });
  363. };
  364. return {
  365. get: get,
  366. post: post
  367. };
  368. })(LUNGO, Zepto);/**
  369. * Handles the <sections> and <articles> to show
  370. *
  371. * @namespace LUNGO
  372. * @class Router
  373. * @requires Zepto
  374. *
  375. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  376. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  377. */
  378. LUNGO.Router = (function(lng, undefined) {
  379. var CSS_CLASSES = {
  380. SHOW: 'show',
  381. HIDE: 'hide'
  382. };
  383. /**
  384. * Navigate to a <section>.
  385. *
  386. * @method section
  387. *
  388. * @param {string} Id of the <section>
  389. */
  390. var section = function(section_id) {
  391. var section_id = (section_id.indexOf('#')) ? '#' + section_id : section_id;
  392. var target = 'section' + section_id;
  393. if (_existsTarget(target)) {
  394. lng.Dom.query(_getHistoryCurrent()).removeClass(CSS_CLASSES.SHOW).addClass(CSS_CLASSES.HIDE);
  395. lng.Dom.query(section_id).addClass(CSS_CLASSES.SHOW);
  396. lng.Router.History.add(section_id);
  397. }
  398. };
  399. /**
  400. * Displays the <article> in a particular <section>.
  401. *
  402. * @method article
  403. *
  404. * @param {string} <section> Id
  405. * @param {string} <article> Id
  406. */
  407. var article = function(section_id, article_id) {
  408. var target = section_id + ' article' + article_id;
  409. if (_existsTarget(target)) {
  410. lng.View.Article.show(section_id, article_id);
  411. }
  412. };
  413. /**
  414. * Return to previous section.
  415. *
  416. * @method back
  417. */
  418. var back = function() {
  419. lng.Dom.query(_getHistoryCurrent()).removeClass(CSS_CLASSES.SHOW);
  420. lng.Router.History.removeLast();
  421. lng.Dom.query(_getHistoryCurrent()).removeClass(CSS_CLASSES.HIDE).addClass(CSS_CLASSES.SHOW);
  422. };
  423. var _existsTarget = function(target) {
  424. var exists = false;
  425. if ($(target).length > 0) {
  426. exists = true;
  427. } else {
  428. lng.Core.log(3, 'Lungo.Router ERROR: The target ' + target + ' does not exists.');
  429. }
  430. return exists;
  431. }
  432. var _getHistoryCurrent = function() {
  433. return lng.Router.History.current();
  434. };
  435. return {
  436. section: section,
  437. article: article,
  438. back: back
  439. };
  440. })(LUNGO);/**
  441. * Stores the displayed <sections> as a historical.
  442. *
  443. * @namespace LUNGO.Router
  444. * @class History
  445. *
  446. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  447. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  448. */
  449. LUNGO.Router.History = (function(undefined) {
  450. var _history = [];
  451. /**
  452. * Create a new element to the browsing history based on the current section id.
  453. *
  454. * @method add
  455. *
  456. * @param {string} Id of the section
  457. */
  458. var add = function(section_id) {
  459. if (section_id !== current()) {
  460. _history.push(section_id);
  461. }
  462. };
  463. /**
  464. * Returns the current browsing history section id.
  465. *
  466. * @method current
  467. *
  468. * @return {string} Current section id
  469. */
  470. var current = function() {
  471. return _history[_history.length - 1];
  472. };
  473. /**
  474. * Removes the current item browsing history.
  475. *
  476. * @method removeLast
  477. */
  478. var removeLast = function() {
  479. _history.length -= 1;
  480. };
  481. return {
  482. add: add,
  483. current: current,
  484. removeLast: removeLast
  485. };
  486. })();/**
  487. * Initialize the <articles> layout of a certain <section>
  488. *
  489. * @namespace LUNGO.View
  490. * @class Article
  491. *
  492. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  493. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  494. */
  495. LUNGO.View.Article = (function(lng, undefined) {
  496. var SELECTORS = {
  497. ARTICLE: 'article',
  498. NAVIGATION_ITEM: 'nav a'
  499. };
  500. var CSS_CLASSES = {
  501. ACTIVE: 'current'
  502. };
  503. var show = function(section_id, article_id) {
  504. var nav_items = section_id + ' ' + SELECTORS.NAVIGATION_ITEM;
  505. _disableNavItems(nav_items);
  506. var current_nav_item = lng.Dom.query(nav_items + '[href="' + article_id + '"]');
  507. current_nav_item.addClass(CSS_CLASSES.ACTIVE);
  508. _setTitle(section_id, current_nav_item);
  509. _showContainer(section_id, article_id);
  510. };
  511. var _disableNavItems = function(items) {
  512. lng.Dom.query(items).removeClass(CSS_CLASSES.ACTIVE);
  513. };
  514. var _showContainer = function(section_id, article_id) {
  515. var section_articles = section_id + ' ' + SELECTORS.ARTICLE;
  516. lng.Dom.query(section_articles).removeClass(CSS_CLASSES.ACTIVE);
  517. lng.Dom.query(article_id).addClass(CSS_CLASSES.ACTIVE);
  518. };
  519. var _setTitle = function(id, item) {
  520. var title = item.data('title');
  521. if (title) {
  522. var section_title = id + ' header .title, ' + id + ' footer .title';
  523. lng.Dom.query(section_title).text(title);
  524. }
  525. };
  526. return {
  527. show: show
  528. };
  529. })(LUNGO);/**
  530. *
  531. *
  532. * @namespace LUNGO.View
  533. * @class Resize
  534. *
  535. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  536. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  537. */
  538. LUNGO.View.Resize = (function(lng, undefined) {
  539. /**
  540. * Rezise a <scroll> element
  541. *
  542. * @method scroll
  543. *
  544. * @param {object} Object reference of a determinated <section>
  545. */
  546. var scroll = function(scroll) {
  547. var container = scroll.children().first();
  548. var child = container.children().first();
  549. if (lng.View.Scroll.isHorizontal(scroll)) {
  550. _resizeScrollContainerWidth(container, child);
  551. } else {
  552. _resizeScrollContainerHeight(scroll, container, child);
  553. }
  554. };
  555. /**
  556. * Resize all <article>s from determinated <section> based on a CSS property.
  557. *
  558. * @method article
  559. *
  560. * @param {object} Object reference of a determinated <section>
  561. * @param {string} Selector that refers to a section element
  562. * @param {string} CSS property
  563. */
  564. var article = function(section, selector, property) {
  565. var element = section.children(selector);
  566. var ARTICLE = 'article'; //@todo >> refactor
  567. if (element) {
  568. section.children(ARTICLE).css(property, element.height() + 'px');
  569. }
  570. };
  571. /**
  572. * Sets toolbars width, using total screen width
  573. *
  574. * @method toolbars
  575. */
  576. var toolbars = function() {
  577. var toolbar = '.toolbar nav';
  578. var all_toolbars = lng.Dom.query(toolbar);
  579. for (var i = 0, len = all_toolbars.length; i < len; i++) {
  580. var toolbar = lng.Dom.query(all_toolbars[i]);
  581. var toolbar_children = toolbar.children();
  582. var toolbar_children_width = (toolbar.width() / toolbar_children.length);
  583. toolbar_children.css('width', toolbar_children_width + 'px');
  584. }
  585. };
  586. var _resizeScrollContainerWidth = function(container, child) {
  587. var scroll_width = (container.children().length * child.width());
  588. container.css('width', scroll_width + 'px');
  589. };
  590. var _resizeScrollContainerHeight = function(scroll, container, child) {
  591. var total_children = container.children().length;
  592. var children_in_scroll_width = Math.floor(scroll.width() / child.width());
  593. var total_rows = Math.ceil(total_children / children_in_scroll_width);
  594. var scroll_height = (total_rows * child.height());
  595. container.css('height', scroll_height + 'px');
  596. };
  597. return {
  598. scroll: scroll,
  599. article: article,
  600. toolbars: toolbars
  601. };
  602. })(LUNGO);/**
  603. * Lungo Template system
  604. *
  605. * @namespace LUNGO.View
  606. * @class Template
  607. * @requires Zepto
  608. *
  609. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  610. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  611. */
  612. LUNGO.View.Template = (function(undefined) {
  613. var _templates = {};
  614. /**
  615. * Create a new databinding template based on a <markup>
  616. *
  617. * @method create
  618. *
  619. * @param {String} Id of the new databinding template
  620. * @param {String} <markup> of the new databinding template
  621. */
  622. var create = function(id, markup) {
  623. _templates[id] = markup;
  624. };
  625. /**
  626. * Returns the existence of a certain Id databinding template
  627. *
  628. * @method exists
  629. *
  630. * @param {String} Id of the databinding template
  631. * @return {Boolean} true if exists, false if not.
  632. */
  633. var exists = function(id) {
  634. return (_templates[id]) ? true : false;
  635. };
  636. /**
  637. * Returns the instance of a certain Id databinding template
  638. *
  639. * @method get
  640. *
  641. * @param {String} Id of the databinding template
  642. * @return {String} Markup of template
  643. */
  644. var get = function(id) {
  645. return _templates[id];
  646. };
  647. return {
  648. create: create,
  649. exists: exists,
  650. get: get
  651. };
  652. })();
  653. /**
  654. * Lungo Data-Binding system
  655. *
  656. * @namespace LUNGO.View.Template
  657. * @class Binding
  658. * @requires Zepto
  659. *
  660. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  661. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  662. */
  663. LUNGO.View.Template.Binding = (function(lng, undefined) {
  664. var BINDING_START = '{{';
  665. var BINDING_END = '}}';
  666. var BINDING_PARSER = /\{{.*?\}}/gi;
  667. /**
  668. * Performs databinding process for a data set and a given template
  669. *
  670. * @method create
  671. *
  672. * @param {String} Id of the container showing the result of databinding
  673. * @param {Number} Databinding Template Id
  674. * @param {Object} Data for binding
  675. * @param {Function} Callback when the process is complete
  676. */
  677. var create = function(container_id, id, data, callback) {
  678. if (lng.View.Template.exists(id)) {
  679. var template = lng.View.Template.get(id);
  680. var markup = _processData(data, template);
  681. _render(container_id, markup);
  682. lng.Core.execute(callback);
  683. } else {
  684. lng.Core.log(3, 'lng.View.Template.binding: id ' + id + ' not exists');
  685. }
  686. };
  687. var dataAttribute = function(element, attribute) {
  688. var data = element.data(attribute.tag);
  689. if (data) {
  690. var html_binded = attribute.html.replace(BINDING_START + 'value' + BINDING_END, data);
  691. element.prepend(html_binded);
  692. }
  693. };
  694. var _processData = function(data, template) {
  695. var data_type = lng.Core.toType(data);
  696. if (data_type === 'array') {
  697. return _bindPropertiesInMultiplesElements(data, template);
  698. } else if (data_type === 'object') {
  699. return _bindProperties(data, template);
  700. } else {
  701. lng.Core.log(3, 'View.Template ERROR >> No type defined.');
  702. }
  703. };
  704. var _bindPropertiesInMultiplesElements = function(elements, template) {
  705. var markup = '';
  706. for (var i = 0, len = elements.length; i < len; i++) {
  707. markup += _bindProperties(elements[i], template);
  708. }
  709. return markup;
  710. };
  711. var _bindProperties = function(element, template) {
  712. for (var property in element) {
  713. if (lng.Core.isOwnProperty(element, property)) {
  714. template = template.replace(BINDING_START + property + BINDING_END, element[property]);
  715. }
  716. }
  717. return _removeNoBindedProperties(template);
  718. };
  719. var _removeNoBindedProperties = function(template) {
  720. return template.replace(BINDING_PARSER, '');
  721. };
  722. var _render = function(container_id, markup) {
  723. var container = lng.Dom.query('#' + container_id);
  724. container.html(markup);
  725. };
  726. return {
  727. create: create,
  728. dataAttribute: dataAttribute
  729. };
  730. })(LUNGO);/**
  731. * Auto generate lists based on Template and Data-Binding system
  732. *
  733. * @namespace LUNGO.View.Template
  734. * @class List
  735. * @requires Zepto
  736. *
  737. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  738. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  739. */
  740. LUNGO.View.Template.List = (function(lng, undefined) {
  741. var _config = null;
  742. /**
  743. * Create a list based DataBind with a configuration object for an element <article>
  744. *
  745. * @method create
  746. *
  747. * @param {object} Id of the container showing the result of databinding
  748. */
  749. var create = function(config) {
  750. _config = config;
  751. _config.container_id += '_list';
  752. if (_validateConfig()) {
  753. _order();
  754. // @ToDo >> _group();
  755. _render();
  756. _createScroll();
  757. }
  758. };
  759. var _validateConfig = function() {
  760. var checked = false;
  761. var container_exists = !! lng.Dom.query(_config.container_id);
  762. if (container_exists) {
  763. //@ToDo >> Refactor to other method
  764. lng.Dom.query("#"+_config.container_id).html('');
  765. var template_exists = lng.View.Template.exists(_config.template_id);
  766. if (template_exists && _config.data.length) {
  767. checked = true;
  768. }
  769. }
  770. return checked;
  771. };
  772. var _order = function() {
  773. var order_field = _config.order_field;
  774. var order_type = (_config.order_type === 'desc') ? -1 : 1;
  775. if (order_field && order_type) {
  776. _config.data.sort(function(a, b) {
  777. return (a[order_field] < b[order_field]) ? - order_type :
  778. (a[order_field] > b[order_field]) ? order_type : 0;
  779. });
  780. }
  781. };
  782. // @ToDo >> group list by property
  783. var _group = function() {
  784. };
  785. var _render = function() {
  786. lng.View.Template.Binding.create(_config.container_id, _config.template_id, _config.data);
  787. };
  788. var _createScroll = function() {
  789. var container_id_for_scroll = lng.Dom.query('#' + _config.container_id).parent().attr('id');
  790. var list_config = {snap:'li'};
  791. lng.View.Scroll.create(container_id_for_scroll, list_config);
  792. };
  793. return {
  794. create: create
  795. };
  796. })(LUNGO);/**
  797. * Wrapper of the third library iScroll
  798. *
  799. * @namespace LUNGO.View
  800. * @class Scroll
  801. * @requires Zepto
  802. * @requires iScroll
  803. *
  804. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  805. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  806. */
  807. LUNGO.View.Scroll = (function(lng, undefined) {
  808. var DEFAULT_PROPERTIES = {
  809. hScroll: false,
  810. vScroll: false,
  811. useTransition: true,
  812. momentum: true,
  813. lockDirection: true,
  814. fixedScrollbar: true,
  815. fadeScrollbar: true,
  816. hideScrollbar: true
  817. };
  818. var HORIZONTAL_CLASS = 'horizontal';
  819. var CACHE_KEY = 'scrolls';
  820. /**
  821. * Creates a new iScroll element.
  822. *
  823. * @method create
  824. *
  825. * @param {string} Id of the container scroll.
  826. * @param {object} [OPTIONAL] Properties
  827. */
  828. var create = function(id, properties) {
  829. if (id) {
  830. var scroll = lng.Dom.query('#' + id);
  831. var scroll_children = scroll.children();
  832. var need_scroll = (scroll_children.height() >= scroll.height());
  833. if (scroll_children.length > 0 && need_scroll) {
  834. properties = _mixProperties(scroll, properties);
  835. _saveScrollInCache(id, properties);
  836. }
  837. } else {
  838. lng.Core.log(3, 'ERROR: Impossible to create a <scroll> without ID');
  839. }
  840. };
  841. /**
  842. * Update iScroll element with new <markup> content.
  843. *
  844. * @method update
  845. *
  846. * @param {string} Id of the container scroll.
  847. * @param {string} Markup content
  848. */
  849. var update = function(id, content) {
  850. var scroll = lng.Dom.query('#' + id);
  851. var container = scroll.children().first();
  852. if (container.length === 0) {
  853. scroll.html('<div id="' + id + '_scrl"></div>');
  854. container = scroll.children().first();
  855. }
  856. container.html(content);
  857. lng.View.Resize.scroll(scroll);
  858. _refresh(id);
  859. };
  860. /**
  861. * Removes iScroll instance.
  862. *
  863. * @method remove
  864. *
  865. * @param {string} Id of the <section>
  866. */
  867. var remove = function(id) {
  868. if (lng.Data.Cache.exists(CACHE_KEY)) {
  869. lng.Data.Cache.get(CACHE_KEY, id).destroy();
  870. lng.Data.Cache.remove(CACHE_KEY, id);
  871. }
  872. };
  873. /**
  874. * Removes iScroll instance.
  875. *
  876. * @method scrollIsHorizontal
  877. *
  878. * @param {Object} Id of the <section>
  879. */
  880. var isHorizontal = function(scroll) {
  881. return (scroll.hasClass(HORIZONTAL_CLASS)) ? true : false;
  882. };
  883. var _saveScrollInCache = function(id, properties) {
  884. _createScrollIndexInCache();
  885. var scroll = lng.Data.Cache.get(CACHE_KEY);
  886. scroll[id] = new iScroll(id, properties);
  887. lng.Data.Cache.set(CACHE_KEY, scroll);
  888. };
  889. var _createScrollIndexInCache = function() {
  890. if (!lng.Data.Cache.exists(CACHE_KEY)) {
  891. lng.Data.Cache.set(CACHE_KEY, {});
  892. }
  893. }
  894. var _mixProperties = function(scroll, properties) {
  895. var scroll_type = isHorizontal(scroll) ? 'hScroll' : 'vScroll';
  896. properties || (properties = {});
  897. properties[scroll_type] = true;
  898. properties = lng.Core.mix(DEFAULT_PROPERTIES, properties);
  899. return properties;
  900. };
  901. var _refresh = function(id, properties) {
  902. !lng.Data.Cache.get(CACHE_KEY, id) && _saveScrollInCache(id);
  903. lng.Data.Cache.get(CACHE_KEY, id).refresh();
  904. };
  905. return {
  906. create: create,
  907. update: update,
  908. remove: remove,
  909. isHorizontal: isHorizontal
  910. };
  911. })(LUNGO);/**
  912. * LungoJS Dom Handler
  913. *
  914. * @namespace LUNGO
  915. * @class Dom
  916. *
  917. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  918. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  919. */
  920. LUNGO.Dom = (function(lng, $, undefined) {
  921. /**
  922. * Add an event listener
  923. *
  924. * @method query
  925. *
  926. * @param {string} <Markup> element selector
  927. * @return {Object} Zepto <element> instance
  928. */
  929. var query = function(selector) {
  930. return $(selector);
  931. };
  932. return {
  933. query: query
  934. };
  935. })(LUNGO, Zepto);/**
  936. * Lungo DOM UI events Manager
  937. *
  938. * @namespace LUNGO.Dom
  939. * @class Event
  940. * @requires Zepto
  941. *
  942. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  943. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  944. */
  945. LUNGO.Dom.Event = (function(lng, undefined) {
  946. /**
  947. * Add an event listener
  948. *
  949. * @method bind
  950. *
  951. * @param {string} Selector that dispatches the event
  952. * @param {string} Touch event name
  953. * @param {Function} Callback function after the request
  954. */
  955. var bind = function(selector, event_name, callback) {
  956. if (_isNotSpecialEvent(selector, event_name, callback)) {
  957. lng.Dom.query(selector).bind(lng.Events.get(event_name), callback);
  958. }
  959. };
  960. /**
  961. * Remove bind event listener
  962. *
  963. * @method unbind
  964. *
  965. * @param {string} Selector that dispatches the event
  966. * @param {string} Touch event name
  967. */
  968. var unbind = function(selector, event_name) {
  969. lng.Dom.query(selector).unbind(lng.Events.get(event_name));
  970. };
  971. /**
  972. * Add an event listener that listens to the selector for current and future elements
  973. *
  974. * @method live
  975. *
  976. * @param {string} Selector that dispatches the event
  977. * @param {string} Touch event name
  978. * @param {Function} Callback function after the request
  979. */
  980. var live = function(selector, event_name, callback) {
  981. if (_isNotSpecialEvent(selector, event_name, callback)) {
  982. lng.Dom.query(selector).live(lng.Events.get(event_name), callback);
  983. }
  984. };
  985. /**
  986. * Remove live listener
  987. *
  988. * @method die
  989. *
  990. * @param {string} Selector that dispatches the event
  991. * @param {string} Event name
  992. */
  993. var die = function(selector, event_name) {
  994. lng.Dom.query(selector).die(lng.Events.get(event_name));
  995. };
  996. /**
  997. * Add an event listener without event delegation
  998. *
  999. * @method delegate
  1000. *
  1001. * @param {string} Selector that dispatches the event
  1002. * @param {string} Children of selector that dispatches the event
  1003. * @param {string} Touch event name
  1004. * @param {Function} Callback function after the request
  1005. */
  1006. var delegate = function(selector, children_selector, event_name, callback) {
  1007. if (_isNotSpecialEvent(selector, event_name, callback)) {
  1008. lng.Dom.query(selector).delegate(children_selector, lng.Events.get(event_name), callback);
  1009. }
  1010. };
  1011. /**
  1012. * Remove delegate event listener
  1013. *
  1014. * @method undelegate
  1015. *
  1016. * @param {string} Selector that dispatches the event
  1017. * @param {string} Children of selector that dispatches the event
  1018. */
  1019. var undelegate = function(selector, children_selector) {
  1020. lng.Dom.query(selector).undelegate(selector);
  1021. };
  1022. /**
  1023. * Listener for DOMelement
  1024. *
  1025. * @method listener
  1026. *
  1027. * @param {object} Selector that dispatches the event
  1028. * @param {string} Touch event name
  1029. * @param {Function} Callback function after the request
  1030. */
  1031. var listener = function(selector, event_name, callback) {
  1032. selector.addEventListener(lng.Events.get(event_name), function(event) {
  1033. setTimeout(callback, 0, event);
  1034. }, false);
  1035. };
  1036. var _isNotSpecialEvent = function(selector, event_name, callback) {
  1037. var is_special_event = false;
  1038. /*
  1039. var SPECIAL_EVENTS = {
  1040. SWIPE: 'swipe',
  1041. SWIPE_LEFT: 'swipeLeft',
  1042. SWIPE_RIGHT: 'swipeRight',
  1043. SWIPE_UP: 'swipeUp',
  1044. SWIPE_DOWN: 'swipeDown',
  1045. DOUBLE_TAP: 'doubleTap'
  1046. };
  1047. var special_event = SPECIAL_EVENTS[event_name];
  1048. lng.Dom.query(selector)[special_event](callback);
  1049. */
  1050. switch(event_name) {
  1051. case 'SWIPE':
  1052. lng.Dom.query(selector).swipe(callback);
  1053. break;
  1054. case 'SWIPE_LEFT':
  1055. lng.Dom.query(selector).swipeLeft(callback);
  1056. break;
  1057. case 'SWIPE_RIGHT':
  1058. lng.Dom.query(selector).swipeRight(callback);
  1059. break;
  1060. case 'SWIPE_UP':
  1061. lng.Dom.query(selector).swipeUp(callback);
  1062. break;
  1063. case 'SWIPE_DOWN':
  1064. lng.Dom.query(selector).swipeDown(callback);
  1065. break;
  1066. case 'DOUBLE_TAP':
  1067. if (lng.Environment.isDesktop()) {
  1068. lng.Dom.query(selector).live(lng.Events.get(event_name), callback);
  1069. } else {
  1070. lng.Dom.query(selector).doubleTap(callback);
  1071. }
  1072. break;
  1073. default:
  1074. is_special_event = true;
  1075. }
  1076. return is_special_event;
  1077. };
  1078. return {
  1079. bind: bind,
  1080. unbind: unbind,
  1081. live: live,
  1082. die: die,
  1083. delegate: delegate,
  1084. undelegate: undelegate,
  1085. listener: listener
  1086. };
  1087. })(LUNGO);/**
  1088. * Object with data-attributes (HTML5) with a special <markup>
  1089. *
  1090. * @namespace LUNGO.Attributes
  1091. * @class Data
  1092. *
  1093. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  1094. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  1095. */
  1096. LUNGO.Attributes.Data = {
  1097. search: {
  1098. tag: 'search',
  1099. selector: '.list',
  1100. html: '<li class="search {{value}}"><input type="search" placeholder="Search..."><a href="#" class="button" data-icon="search"></a></li>'
  1101. },
  1102. icon: {
  1103. tag: 'icon',
  1104. selector: '*',
  1105. html: '<span class="icon {{value}}"></span>'
  1106. },
  1107. title: {
  1108. tag: 'title',
  1109. selector: 'header, footer',
  1110. html: '<h1 class="title">{{value}}</h1>'
  1111. },
  1112. back: {
  1113. tag: 'back',
  1114. selector: 'header, footer',
  1115. html: '<a href="#back" data-target="section" class="back onleft button icon {{value}}"></a>'
  1116. }
  1117. };/**
  1118. * Make an analysis of <elements> in a <section>.
  1119. *
  1120. * @namespace LUNGO.Attributes
  1121. * @class Section
  1122. *
  1123. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  1124. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  1125. */
  1126. LUNGO.Attributes.Section = {
  1127. header: {
  1128. name: 'header',
  1129. bind: 'top'
  1130. },
  1131. footer: {
  1132. name: 'footer',
  1133. bind: 'bottom'
  1134. }
  1135. };/**
  1136. * Temporary cache system
  1137. *
  1138. * @namespace LUNGO.Data
  1139. * @class Cache
  1140. *
  1141. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  1142. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  1143. */
  1144. LUNGO.Data.Cache = (function(lng, undefined) {
  1145. var _cache = {};
  1146. /**
  1147. * Sets in the LungoJS cache system a new key/value
  1148. *
  1149. * @method set
  1150. *
  1151. * @param {string} Key for the new value
  1152. * @param {object} Type of environment: DESKTOP_ENVIRONMENT or MOBILE_ENVIRONMENT
  1153. */
  1154. var set = function(key, value) {
  1155. if (exists(key)) {
  1156. _cache[key] = lng.Core.mix(get(key), value);
  1157. } else {
  1158. _cache[key] = value;
  1159. }
  1160. };
  1161. /**
  1162. * Returns the value of a given key.
  1163. *
  1164. * @method get
  1165. *
  1166. * @param {string} Key in LungoJS Cache System
  1167. * @param {string} [OPTIONAL] Subkey in LungoJS Cache System
  1168. * @return {object} Value
  1169. */
  1170. var get = function(key, value) {
  1171. if (arguments.length === 1) {
  1172. return _cache[key];
  1173. } else {
  1174. return _cache[arguments[0]][arguments[1]];
  1175. }
  1176. };
  1177. /**
  1178. * Removes the instance in LungoJs Cache System of a given key
  1179. *
  1180. * @method remove
  1181. *
  1182. * @param {string} Key in LungoJS Cache System
  1183. * @param {string} [OPTIONAL] Subkey in LungoJS Cache System
  1184. */
  1185. var remove = function(key, value) {
  1186. if (arguments.length === 1) {
  1187. delete _cache[key];
  1188. } else {
  1189. delete _cache[arguments[0]][arguments[1]];
  1190. }
  1191. };
  1192. /**
  1193. * Returns the existence of a key in LungoJs Cache System
  1194. *
  1195. * @method exists
  1196. *
  1197. * @param {String} Key in LungoJS Cache System
  1198. * @return {Boolean} true if exists, false if not
  1199. */
  1200. var exists = function(key) {
  1201. return (_cache[key]) ? true : false;
  1202. };
  1203. return {
  1204. set: set,
  1205. get: get,
  1206. remove: remove,
  1207. exists: exists
  1208. };
  1209. })(LUNGO);/**
  1210. * Wrapper for using WebSql (HTML5 feature)
  1211. *
  1212. * @namespace LUNGO.Data
  1213. * @class Sql
  1214. *
  1215. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  1216. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  1217. */
  1218. LUNGO.Data.Sql = (function(lng, undefined) {
  1219. var CONFIG = {
  1220. name: 'lungo_db',
  1221. version: '1.0',
  1222. size: 65536,
  1223. schema: []
  1224. };
  1225. var db = null;
  1226. /**
  1227. * Initialize the SQLite storage (HTML5 Feature)
  1228. *
  1229. * @method init
  1230. *
  1231. * @param {object} Configuration for the Database
  1232. */
  1233. var init = function(db_config) {
  1234. CONFIG = lng.Core.mix(CONFIG, db_config);
  1235. db = openDatabase(CONFIG.name, CONFIG.version, CONFIG.name, CONFIG.size);
  1236. if (db) {
  1237. _createSchema();
  1238. } else {
  1239. lng.Core.log(3, 'lng.Data.Sql >> Failed to connect to database.');
  1240. }
  1241. };
  1242. /**
  1243. * Select a data set of a given table and based on a selection object
  1244. *
  1245. * @method select
  1246. *
  1247. * @param {string} Name of the table in the database
  1248. * @param {object} [OPTIONAL] Object selection condition
  1249. * @param {Function} Callback when the process is complete
  1250. */
  1251. var select = function(table, where_obj, callback) {
  1252. var where = (where_obj) ? ' WHERE ' + _convertToSql(where_obj, 'AND') : '';
  1253. execute('SELECT * FROM ' + table + where, function(rs) {
  1254. var result = [];
  1255. for (var i = 0, len = rs.rows.length; i < len; i++) {
  1256. result.push(rs.rows.item(i));
  1257. }
  1258. _callbackResponse(callback, result);
  1259. });
  1260. };
  1261. /**
  1262. * Inserts a data set of a given table and based on a data object
  1263. *
  1264. * @method insert
  1265. *
  1266. * @param {string} Name of the table in the database
  1267. * @param {object} Data object to insert in table
  1268. */
  1269. var insert = function(table, data_obj) {
  1270. var fields = '';
  1271. var values = '';
  1272. for (var field in data_obj) {
  1273. if (lng.Core.isOwnProperty(data_obj, field)) {
  1274. var value = data_obj[field];
  1275. fields += (fields) ? ', ' + field : field;
  1276. if (values) values += ', ';
  1277. values += (isNaN(value)) ? '"' + value + '"' : value;
  1278. }
  1279. }
  1280. execute('INSERT INTO ' + table + ' (' + fields + ') VALUES (' + values + ')');
  1281. };
  1282. /**
  1283. * Updates a data set of a given table and based on a data object and
  1284. * an optional selection object
  1285. *
  1286. * @method update
  1287. *
  1288. * @param {string} Name of the table in the database
  1289. * @param {object} Data object to update in table
  1290. * @param {object} [OPTIONAL] Object selection condition
  1291. */
  1292. var update = function(table, data_obj, where_obj) {
  1293. var sql = 'UPDATE ' + table + ' SET ' + _convertToSql(data_obj, ',');
  1294. if (where_obj) sql += ' WHERE ' + _convertToSql(where_obj, 'AND');
  1295. execute(sql);
  1296. };
  1297. /**
  1298. * Delete a data set of a given table and based on a selection object
  1299. *
  1300. * @method drop
  1301. *
  1302. * @param {string} Name of the table in the database
  1303. * @param {object} [OPTIONAL] Object selection condition
  1304. */
  1305. var drop = function(table, where_obj) {
  1306. var where = (where_obj) ? ' WHERE ' + _convertToSql(where_obj, 'AND') : '';
  1307. execute('DELETE FROM ' + table + where + ';');
  1308. };
  1309. /**
  1310. * Executes a SQL statement in the SQLite storage
  1311. *
  1312. * @method execute
  1313. *
  1314. * @param {string} SQL statement
  1315. * @param {Function} Callback when the process is complete
  1316. */
  1317. var execute = function(sql, callback) {
  1318. lng.Core.log(1, 'lng.Data.Sql >> ' + sql);
  1319. db.transaction(function(tx) {
  1320. tx.executeSql(sql, [], function(tx, rs) {
  1321. _callbackResponse(callback, rs);
  1322. }, _throwError);
  1323. });
  1324. };
  1325. var _createSchema = function() {
  1326. var schema = CONFIG.schema;
  1327. var schema_len = schema.length;
  1328. if (!schema_len) return;
  1329. for (var i = 0; i < schema_len; i++) {
  1330. var current = schema[i];
  1331. _regenerateTable(current);
  1332. _createTable(current.name, current.fields);
  1333. }
  1334. };
  1335. var _createTable = function(table, fields) {
  1336. var sql_fields = '';
  1337. for (var field in fields) {
  1338. if (lng.Core.isOwnProperty(fields, field)) {
  1339. if (sql_fields) sql_fields += ', ';
  1340. sql_fields += field + ' ' + fields[field];
  1341. }
  1342. }
  1343. execute('CREATE TABLE IF NOT EXISTS ' + table + ' (' + sql_fields + ');');
  1344. };
  1345. var _regenerateTable = function(table) {
  1346. if (table.drop === true) {
  1347. _dropTable(table.name);
  1348. }
  1349. };
  1350. var _dropTable = function(table) {
  1351. execute('DROP TABLE IF EXISTS ' + table);
  1352. };
  1353. var _convertToSql = function(fields, separator) {
  1354. var sql = '';
  1355. for (var field in fields) {
  1356. if (lng.Core.isOwnProperty(fields, field)) {
  1357. var value = fields[field];
  1358. if (sql) sql += ' ' + separator + ' ';
  1359. sql += field + '=';
  1360. sql += (isNaN(value)) ? '"' + value + '"' : value;
  1361. }
  1362. }
  1363. return sql;
  1364. };
  1365. var _callbackResponse = function(callback, response) {
  1366. if (lng.Core.toType(callback) === 'function') {
  1367. setTimeout(callback, 100, response);
  1368. }
  1369. };
  1370. var _throwError = function(transaction, error) {
  1371. lng.Core.log(3, 'lng.Data.Sql >> ' + error.code + ': ' + error.message);
  1372. };
  1373. return {
  1374. init: init,
  1375. select: select,
  1376. insert: insert,
  1377. update: update,
  1378. drop: drop,
  1379. execute: execute
  1380. };
  1381. })(LUNGO);/**
  1382. * Wrapper for using LocalStorage & SessionStorage (HTML5 Feature)
  1383. *
  1384. * @namespace LUNGO.Data
  1385. * @class Storage
  1386. *
  1387. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  1388. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  1389. */
  1390. LUNGO.Data.Storage = (function(lng, undefined) {
  1391. return {
  1392. };
  1393. })(LUNGO);/**
  1394. * Boot for a new LungoJS Application instance
  1395. *
  1396. * @namespace LUNGO
  1397. * @class App
  1398. *
  1399. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  1400. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  1401. */
  1402. LUNGO.Boot = (function(lng, undefined) {
  1403. return function() {
  1404. lng.Environment.start();
  1405. lng.Boot.Events.start();
  1406. lng.Boot.Data.start();
  1407. lng.Boot.Section.start();
  1408. lng.Boot.Article.start();
  1409. };
  1410. })(LUNGO);/**
  1411. * Initialize the <article> element
  1412. *
  1413. * @namespace LUNGO.Boot
  1414. * @class Article
  1415. * @requires Zepto
  1416. *
  1417. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  1418. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  1419. */
  1420. LUNGO.Boot.Article = (function(lng, undefined) {
  1421. var SELECTORS = {
  1422. LIST_IN_ARTICLE: 'article.list',
  1423. SCROLL_IN_ARTICLE: 'scroll',
  1424. CHECKBOX_IN_ARTICLE: '.checkbox, .radio'
  1425. };
  1426. /**
  1427. * Initializes the markup elements of an article
  1428. *
  1429. * @method init
  1430. */
  1431. var start = function() {
  1432. _initElement(SELECTORS.LIST_IN_ARTICLE, _createListElement);
  1433. _initElement(SELECTORS.SCROLL_IN_ARTICLE, _createScrollElement);
  1434. _initElement(SELECTORS.CHECKBOX_IN_ARTICLE, _b);
  1435. };
  1436. var _initElement = function(selector, callback) {
  1437. var found_elements = lng.Dom.query(selector);
  1438. for (var i = 0, len = found_elements.length; i < len; i++) {
  1439. var element = lng.Dom.query(found_elements[i]);
  1440. lng.Core.execute(callback, element);
  1441. }
  1442. };
  1443. var _createListElement = function(article) {
  1444. if (article.children().length === 0) {
  1445. var article_id = article.attr('id');
  1446. article.append('<ul id="' + article_id + '_list"></ul>');
  1447. }
  1448. };
  1449. var _createScrollElement = function(scroll) {
  1450. var scroll_id = scroll.attr('id');
  1451. lng.View.Scroll.create(scroll_id);
  1452. };
  1453. var _b = function(checkbox) {
  1454. checkbox.append('<span>&nbsp;</span>');
  1455. };
  1456. return {
  1457. start: start
  1458. };
  1459. })(LUNGO);/**
  1460. * Make an analysis of Data attributes in HTML elements and creates a <markup>
  1461. * based on each data type.
  1462. *
  1463. * @namespace LUNGO.Boot
  1464. * @class Data
  1465. * @requires Zepto
  1466. *
  1467. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  1468. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  1469. */
  1470. LUNGO.Boot.Data = (function(lng, undefined) {
  1471. /**
  1472. * Initialize the <markup> data-attributes analisys
  1473. *
  1474. * @method init
  1475. *
  1476. *
  1477. */
  1478. var start = function() {
  1479. var attributes = lng.Attributes.Data;
  1480. for (var attribute in attributes) {
  1481. if (lng.Core.isOwnProperty(attributes, attribute)) {
  1482. _findElements(attributes[attribute]);
  1483. }
  1484. }
  1485. };
  1486. var _findElements = function(attribute) {
  1487. var elements = lng.Dom.query(attribute.selector);
  1488. for (var i = 0, len = elements.length; i < len; i++) {
  1489. var element = lng.Dom.query(elements[i]);
  1490. lng.View.Template.Binding.dataAttribute(element, attribute);
  1491. }
  1492. };
  1493. return {
  1494. start: start
  1495. };
  1496. })(LUNGO);/**
  1497. * Initialize the automatic DOM UI events
  1498. *
  1499. * @namespace LUNGO.Boot
  1500. * @class Events
  1501. *
  1502. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  1503. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  1504. */
  1505. LUNGO.Boot.Events = (function(lng, undefined) {
  1506. /**
  1507. * Initializes the automatic subscription events by markup of the project.
  1508. *
  1509. * @method init
  1510. *
  1511. */
  1512. var start = function() {
  1513. var touch_move_event = 'TOUCH_MOVE';
  1514. var touch_start_event = 'TOUCH_START';
  1515. var orientation_change = 'ORIENTATION_CHANGE';
  1516. var target_selector = 'a[href][data-target]';
  1517. lng.Dom.Event.listener(document, touch_move_event, _iScroll);
  1518. lng.Dom.Event.listener(window, orientation_change, _changeOrientation);
  1519. lng.Dom.Event.live(target_selector, touch_start_event, _loadTarget);
  1520. };
  1521. var _iScroll = function(event_handler) {
  1522. event_handler.preventDefault();
  1523. };
  1524. var _changeOrientation = function(event) {
  1525. lng.View.Resize.toolbars();
  1526. };
  1527. var _loadTarget = function(event) {
  1528. event.preventDefault();
  1529. var link = lng.Dom.query(this);
  1530. var target_id = link.attr('href');
  1531. var target_type = link.data('target');
  1532. (target_type === 'section') ? _goSection(target_id) : _goArticle(link);
  1533. };
  1534. var _goSection = function(id) {
  1535. if (id === '#back') {
  1536. lng.Router.back();
  1537. } else {
  1538. lng.Router.section(id);
  1539. }
  1540. };
  1541. var _goArticle = function(element) {
  1542. var section_id = '#' + element.parents('section').attr('id');
  1543. var article_id = element.attr('href');
  1544. lng.Router.article(section_id, article_id);
  1545. };
  1546. return {
  1547. start: start
  1548. };
  1549. })(LUNGO);/**
  1550. * Initialize the <section> element
  1551. *
  1552. * @namespace LUNGO.Boot
  1553. * @class Section
  1554. * @requires Zepto
  1555. *
  1556. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  1557. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  1558. */
  1559. LUNGO.Boot.Section = (function(lng, undefined) {
  1560. var SELECTORS = {
  1561. ARTICLE: 'article',
  1562. SECTION: 'section'
  1563. };
  1564. var ACTIVE_CLASS = 'current';
  1565. /**
  1566. * Initializes all <section>s of the project
  1567. *
  1568. * @method init
  1569. */
  1570. var start = function() {
  1571. var sections = lng.Dom.query(SELECTORS.SECTION);
  1572. var easing_transition = '-webkit-transform 0.3s ease-in-out';
  1573. _initFirstSection(sections);
  1574. _initAllSections(sections);
  1575. lng.View.Resize.toolbars();
  1576. _allocateEasingTransition(sections, easing_transition);
  1577. };
  1578. var _initFirstSection = function(sections) {
  1579. var first_section = sections.first();
  1580. var first_section_id = '#' + first_section.attr('id');
  1581. first_section.addClass(ACTIVE_CLASS);
  1582. lng.Router.History.add(first_section_id);
  1583. };
  1584. var _initAllSections = function(sections) {
  1585. for (var i = 0, len = sections.length; i < len; i++) {
  1586. var section = lng.Dom.query(sections[i]);
  1587. _initSection(section);
  1588. _initFirstArticle(section);
  1589. }
  1590. };
  1591. var _initSection = function(section) {
  1592. var section_attributes = lng.Attributes.Section;
  1593. for (var attribute in section_attributes) {
  1594. if (lng.Core.isOwnProperty(section_attributes, attribute)) {
  1595. var property = section_attributes[attribute];
  1596. lng.View.Resize.article(section, property.name, property.bind);
  1597. }
  1598. }
  1599. };
  1600. var _allocateEasingTransition = function(sections, easing) {
  1601. var transition_property = { '-webkit-transition': easing };
  1602. sections.css(transition_property);
  1603. };
  1604. var _initFirstArticle = function(section) {
  1605. section.children(SELECTORS.ARTICLE).first().addClass(ACTIVE_CLASS);
  1606. };
  1607. return {
  1608. start: start
  1609. };
  1610. })(LUNGO);