lungo-1.0.1.js 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886
  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
  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. /**
  648. * Performs databinding process for a data set and a given template
  649. *
  650. * @method binding
  651. *
  652. * @param {String} Id of the container showing the result of databinding
  653. * @param {Number} Databinding Template Id
  654. * @param {Object} Data for binding
  655. * @param {Function} Callback when the process is complete
  656. */
  657. var binding = function(container_id, id, data, callback) {
  658. lng.View.Template.Binding.create(container_id, id, data, callback);
  659. };
  660. return {
  661. create: create,
  662. exists: exists,
  663. get: get,
  664. binding: binding
  665. };
  666. })();
  667. /**
  668. * Lungo Data-Binding system
  669. *
  670. * @namespace LUNGO.View.Template
  671. * @class Binding
  672. * @requires Zepto
  673. *
  674. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  675. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  676. */
  677. LUNGO.View.Template.Binding = (function(lng, undefined) {
  678. var BINDING_START = '{{';
  679. var BINDING_END = '}}';
  680. var BINDING_PARSER = /\{{.*?\}}/gi;
  681. /**
  682. * Performs databinding process for a data set and a given template
  683. *
  684. * @method create
  685. *
  686. * @param {String} Id of the container showing the result of databinding
  687. * @param {Number} Databinding Template Id
  688. * @param {Object} Data for binding
  689. * @param {Function} Callback when the process is complete
  690. */
  691. var create = function(container_id, id, data, callback) {
  692. if (lng.View.Template.exists(id)) {
  693. var template = lng.View.Template.get(id);
  694. var markup = _processData(data, template);
  695. _render(container_id, markup);
  696. lng.Core.execute(callback);
  697. } else {
  698. lng.Core.log(3, 'lng.View.Template.binding: id ' + id + ' not exists');
  699. }
  700. };
  701. var dataAttribute = function(element, attribute) {
  702. var data = element.data(attribute.tag);
  703. if (data) {
  704. var html_binded = attribute.html.replace(BINDING_START + 'value' + BINDING_END, data);
  705. element.prepend(html_binded);
  706. }
  707. };
  708. var _processData = function(data, template) {
  709. var data_type = lng.Core.toType(data);
  710. if (data_type === 'array') {
  711. return _bindPropertiesInMultiplesElements(data, template);
  712. } else if (data_type === 'object') {
  713. return _bindProperties(data, template);
  714. } else {
  715. lng.Core.log(3, 'View.Template ERROR >> No type defined.');
  716. }
  717. };
  718. var _bindPropertiesInMultiplesElements = function(elements, template) {
  719. var markup = '';
  720. for (var i = 0, len = elements.length; i < len; i++) {
  721. markup += _bindProperties(elements[i], template);
  722. }
  723. return markup;
  724. };
  725. var _bindProperties = function(element, template) {
  726. for (var property in element) {
  727. if (lng.Core.isOwnProperty(element, property)) {
  728. template = template.replace(BINDING_START + property + BINDING_END, element[property]);
  729. }
  730. }
  731. return _removeNoBindedProperties(template);
  732. };
  733. var _removeNoBindedProperties = function(template) {
  734. return template.replace(BINDING_PARSER, '');
  735. };
  736. var _render = function(container_id, markup) {
  737. var container = lng.Dom.query('#' + container_id);
  738. container.html(markup);
  739. };
  740. return {
  741. create: create,
  742. dataAttribute: dataAttribute
  743. };
  744. })(LUNGO);/**
  745. * Auto generate lists based on Template and Data-Binding system
  746. *
  747. * @namespace LUNGO.View.Template
  748. * @class List
  749. * @requires Zepto
  750. *
  751. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  752. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  753. */
  754. LUNGO.View.Template.List = (function(lng, undefined) {
  755. var _config = null;
  756. /**
  757. * Create a list based DataBind with a configuration object for an element <article>
  758. *
  759. * @method create
  760. *
  761. * @param {object} Id of the container showing the result of databinding
  762. */
  763. var create = function(config) {
  764. _config = config;
  765. _config.container_id += '_list';
  766. if (_validateConfig()) {
  767. _order();
  768. // @ToDo >> _group();
  769. _render();
  770. _createScroll();
  771. }
  772. };
  773. var _validateConfig = function() {
  774. var checked = false;
  775. var container_exists = !! lng.Dom.query(_config.container_id);
  776. if (container_exists) {
  777. //@ToDo >> Refactor to other method
  778. lng.Dom.query("#"+_config.container_id).html('');
  779. var template_exists = lng.View.Template.exists(_config.template_id);
  780. if (template_exists && _config.data.length) {
  781. checked = true;
  782. }
  783. }
  784. return checked;
  785. };
  786. var _order = function() {
  787. var order_field = _config.order_field;
  788. var order_type = (_config.order_type === 'desc') ? -1 : 1;
  789. if (order_field && order_type) {
  790. _config.data.sort(function(a, b) {
  791. return (a[order_field] < b[order_field]) ? - order_type :
  792. (a[order_field] > b[order_field]) ? order_type : 0;
  793. });
  794. }
  795. };
  796. // @ToDo >> group list by property
  797. var _group = function() {
  798. };
  799. var _render = function() {
  800. lng.View.Template.Binding.create(_config.container_id, _config.template_id, _config.data);
  801. };
  802. var _createScroll = function() {
  803. var container_id_for_scroll = lng.Dom.query('#' + _config.container_id).parent().attr('id');
  804. var list_config = {snap:'li'};
  805. lng.View.Scroll.create(container_id_for_scroll, list_config);
  806. };
  807. return {
  808. create: create
  809. };
  810. })(LUNGO);/**
  811. * Wrapper of the third library iScroll
  812. *
  813. * @namespace LUNGO.View
  814. * @class Scroll
  815. * @requires Zepto
  816. * @requires iScroll
  817. *
  818. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  819. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  820. */
  821. LUNGO.View.Scroll = (function(lng, undefined) {
  822. var DEFAULT_PROPERTIES = {
  823. hScroll: false,
  824. vScroll: false,
  825. useTransition: true,
  826. momentum: true,
  827. lockDirection: true,
  828. fixedScrollbar: true,
  829. fadeScrollbar: true,
  830. hideScrollbar: true
  831. };
  832. var HORIZONTAL_CLASS = 'horizontal';
  833. var CACHE_KEY = 'scrolls';
  834. /**
  835. * Creates a new iScroll element.
  836. *
  837. * @method create
  838. *
  839. * @param {string} Id of the container scroll.
  840. * @param {object} [OPTIONAL] Properties
  841. */
  842. var create = function(id, properties) {
  843. if (id) {
  844. var scroll = lng.Dom.query('#' + id);
  845. var scroll_children = scroll.children();
  846. var need_scroll = (scroll_children.height() >= scroll.height());
  847. if (scroll_children.length > 0 && need_scroll) {
  848. properties = _mixProperties(scroll, properties);
  849. _saveScrollInCache(id, properties);
  850. }
  851. } else {
  852. lng.Core.log(3, 'ERROR: Impossible to create a <scroll> without ID');
  853. }
  854. };
  855. /**
  856. * Update iScroll element with new <markup> content.
  857. *
  858. * @method update
  859. *
  860. * @param {string} Id of the container scroll.
  861. * @param {string} Markup content
  862. */
  863. var update = function(id, content) {
  864. var scroll = lng.Dom.query('#' + id);
  865. var container = scroll.children().first();
  866. if (container.length === 0) {
  867. scroll.html('<div id="' + id + '_scrl"></div>');
  868. container = scroll.children().first();
  869. }
  870. container.html(content);
  871. lng.View.Resize.scroll(scroll);
  872. _refresh(id);
  873. };
  874. /**
  875. * Removes iScroll instance.
  876. *
  877. * @method remove
  878. *
  879. * @param {string} Id of the <section>
  880. */
  881. var remove = function(id) {
  882. if (lng.Data.Cache.exists(CACHE_KEY)) {
  883. lng.Data.Cache.get(CACHE_KEY, id).destroy();
  884. lng.Data.Cache.remove(CACHE_KEY, id);
  885. }
  886. };
  887. /**
  888. * Removes iScroll instance.
  889. *
  890. * @method scrollIsHorizontal
  891. *
  892. * @param {Object} Id of the <section>
  893. */
  894. var isHorizontal = function(scroll) {
  895. return (scroll.hasClass(HORIZONTAL_CLASS)) ? true : false;
  896. };
  897. var _saveScrollInCache = function(id, properties) {
  898. _createScrollIndexInCache();
  899. var scroll = lng.Data.Cache.get(CACHE_KEY);
  900. scroll[id] = new iScroll(id, properties);
  901. lng.Data.Cache.set(CACHE_KEY, scroll);
  902. };
  903. var _createScrollIndexInCache = function() {
  904. if (!lng.Data.Cache.exists(CACHE_KEY)) {
  905. lng.Data.Cache.set(CACHE_KEY, {});
  906. }
  907. }
  908. var _mixProperties = function(scroll, properties) {
  909. var scroll_type = isHorizontal(scroll) ? 'hScroll' : 'vScroll';
  910. properties || (properties = {});
  911. properties[scroll_type] = true;
  912. properties = lng.Core.mix(DEFAULT_PROPERTIES, properties);
  913. return properties;
  914. };
  915. var _refresh = function(id, properties) {
  916. !lng.Data.Cache.get(CACHE_KEY, id) && _saveScrollInCache(id);
  917. lng.Data.Cache.get(CACHE_KEY, id).refresh();
  918. };
  919. return {
  920. create: create,
  921. update: update,
  922. remove: remove,
  923. isHorizontal: isHorizontal
  924. };
  925. })(LUNGO);/**
  926. * LungoJS Dom Handler
  927. *
  928. * @namespace LUNGO
  929. * @class Dom
  930. *
  931. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  932. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  933. */
  934. LUNGO.Dom = (function(lng, $, undefined) {
  935. /**
  936. * Add an event listener
  937. *
  938. * @method query
  939. *
  940. * @param {string} <Markup> element selector
  941. * @return {Object} Zepto <element> instance
  942. */
  943. var query = function(selector) {
  944. return $(selector);
  945. };
  946. return {
  947. query: query
  948. };
  949. })(LUNGO, Zepto);/**
  950. * Lungo DOM UI events Manager
  951. *
  952. * @namespace LUNGO.Dom
  953. * @class Event
  954. * @requires Zepto
  955. *
  956. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  957. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  958. */
  959. LUNGO.Dom.Event = (function(lng, undefined) {
  960. /**
  961. * Add an event listener
  962. *
  963. * @method bind
  964. *
  965. * @param {string} Selector that dispatches the event
  966. * @param {string} Touch event name
  967. * @param {Function} Callback function after the request
  968. */
  969. var bind = function(selector, event_name, callback) {
  970. if (_isNotSpecialEvent(selector, event_name, callback)) {
  971. lng.Dom.query(selector).bind(lng.Events.get(event_name), callback);
  972. }
  973. };
  974. /**
  975. * Remove bind event listener
  976. *
  977. * @method unbind
  978. *
  979. * @param {string} Selector that dispatches the event
  980. * @param {string} Touch event name
  981. */
  982. var unbind = function(selector, event_name) {
  983. lng.Dom.query(selector).unbind(lng.Events.get(event_name));
  984. };
  985. /**
  986. * Add an event listener that listens to the selector for current and future elements
  987. *
  988. * @method live
  989. *
  990. * @param {string} Selector that dispatches the event
  991. * @param {string} Touch event name
  992. * @param {Function} Callback function after the request
  993. */
  994. var live = function(selector, event_name, callback) {
  995. if (_isNotSpecialEvent(selector, event_name, callback)) {
  996. lng.Dom.query(selector).live(lng.Events.get(event_name), callback);
  997. }
  998. };
  999. /**
  1000. * Remove live listener
  1001. *
  1002. * @method die
  1003. *
  1004. * @param {string} Selector that dispatches the event
  1005. * @param {string} Event name
  1006. */
  1007. var die = function(selector, event_name) {
  1008. lng.Dom.query(selector).die(lng.Events.get(event_name));
  1009. };
  1010. /**
  1011. * Add an event listener without event delegation
  1012. *
  1013. * @method delegate
  1014. *
  1015. * @param {string} Selector that dispatches the event
  1016. * @param {string} Children of selector that dispatches the event
  1017. * @param {string} Touch event name
  1018. * @param {Function} Callback function after the request
  1019. */
  1020. var delegate = function(selector, children_selector, event_name, callback) {
  1021. if (_isNotSpecialEvent(selector, event_name, callback)) {
  1022. lng.Dom.query(selector).delegate(children_selector, lng.Events.get(event_name), callback);
  1023. }
  1024. };
  1025. /**
  1026. * Remove delegate event listener
  1027. *
  1028. * @method undelegate
  1029. *
  1030. * @param {string} Selector that dispatches the event
  1031. * @param {string} Children of selector that dispatches the event
  1032. */
  1033. var undelegate = function(selector, children_selector) {
  1034. lng.Dom.query(selector).undelegate(selector);
  1035. };
  1036. /**
  1037. * Listener for DOMelement
  1038. *
  1039. * @method listener
  1040. *
  1041. * @param {object} Selector that dispatches the event
  1042. * @param {string} Touch event name
  1043. * @param {Function} Callback function after the request
  1044. */
  1045. var listener = function(selector, event_name, callback) {
  1046. selector.addEventListener(lng.Events.get(event_name), function(event) {
  1047. setTimeout(callback, 0, event);
  1048. }, false);
  1049. };
  1050. var _isNotSpecialEvent = function(selector, event_name, callback) {
  1051. var is_special_event = false;
  1052. /*
  1053. var SPECIAL_EVENTS = {
  1054. SWIPE: 'swipe',
  1055. SWIPE_LEFT: 'swipeLeft',
  1056. SWIPE_RIGHT: 'swipeRight',
  1057. SWIPE_UP: 'swipeUp',
  1058. SWIPE_DOWN: 'swipeDown',
  1059. DOUBLE_TAP: 'doubleTap'
  1060. };
  1061. var special_event = SPECIAL_EVENTS[event_name];
  1062. lng.Dom.query(selector)[special_event](callback);
  1063. */
  1064. switch(event_name) {
  1065. case 'SWIPE':
  1066. lng.Dom.query(selector).swipe(callback);
  1067. break;
  1068. case 'SWIPE_LEFT':
  1069. lng.Dom.query(selector).swipeLeft(callback);
  1070. break;
  1071. case 'SWIPE_RIGHT':
  1072. lng.Dom.query(selector).swipeRight(callback);
  1073. break;
  1074. case 'SWIPE_UP':
  1075. lng.Dom.query(selector).swipeUp(callback);
  1076. break;
  1077. case 'SWIPE_DOWN':
  1078. lng.Dom.query(selector).swipeDown(callback);
  1079. break;
  1080. case 'DOUBLE_TAP':
  1081. if (lng.Environment.isDesktop()) {
  1082. lng.Dom.query(selector).live(lng.Events.get(event_name), callback);
  1083. } else {
  1084. lng.Dom.query(selector).doubleTap(callback);
  1085. }
  1086. break;
  1087. default:
  1088. is_special_event = true;
  1089. }
  1090. return is_special_event;
  1091. };
  1092. return {
  1093. bind: bind,
  1094. unbind: unbind,
  1095. live: live,
  1096. die: die,
  1097. delegate: delegate,
  1098. undelegate: undelegate,
  1099. listener: listener
  1100. };
  1101. })(LUNGO);/**
  1102. * Object with data-attributes (HTML5) with a special <markup>
  1103. *
  1104. * @namespace LUNGO.Attributes
  1105. * @class Data
  1106. *
  1107. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  1108. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  1109. */
  1110. LUNGO.Attributes.Data = {
  1111. search: {
  1112. tag: 'search',
  1113. selector: '.list',
  1114. html: '<li class="search {{value}}"><input type="search" placeholder="Search..."><a href="#" class="button" data-icon="search"></a></li>'
  1115. },
  1116. icon: {
  1117. tag: 'icon',
  1118. selector: '*',
  1119. html: '<span class="icon {{value}}"></span>'
  1120. },
  1121. title: {
  1122. tag: 'title',
  1123. selector: 'header, footer',
  1124. html: '<h1 class="title">{{value}}</h1>'
  1125. },
  1126. back: {
  1127. tag: 'back',
  1128. selector: 'header, footer',
  1129. html: '<a href="#back" data-target="section" class="back onleft button icon {{value}}"></a>'
  1130. }
  1131. };/**
  1132. * Make an analysis of <elements> in a <section>.
  1133. *
  1134. * @namespace LUNGO.Attributes
  1135. * @class Section
  1136. *
  1137. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  1138. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  1139. */
  1140. LUNGO.Attributes.Section = {
  1141. header: {
  1142. name: 'header',
  1143. bind: 'top'
  1144. },
  1145. footer: {
  1146. name: 'footer',
  1147. bind: 'bottom'
  1148. }
  1149. };/**
  1150. * Temporary cache system
  1151. *
  1152. * @namespace LUNGO.Data
  1153. * @class Cache
  1154. *
  1155. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  1156. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  1157. */
  1158. LUNGO.Data.Cache = (function(lng, undefined) {
  1159. var _cache = {};
  1160. /**
  1161. * Sets in the LungoJS cache system a new key/value
  1162. *
  1163. * @method set
  1164. *
  1165. * @param {string} Key for the new value
  1166. * @param {object} Type of environment: DESKTOP_ENVIRONMENT or MOBILE_ENVIRONMENT
  1167. */
  1168. var set = function(key, value) {
  1169. if (exists(key)) {
  1170. _cache[key] = lng.Core.mix(get(key), value);
  1171. } else {
  1172. _cache[key] = value;
  1173. }
  1174. };
  1175. /**
  1176. * Returns the value of a given key.
  1177. *
  1178. * @method get
  1179. *
  1180. * @param {string} Key in LungoJS Cache System
  1181. * @param {string} [OPTIONAL] Subkey in LungoJS Cache System
  1182. * @return {object} Value
  1183. */
  1184. var get = function(key, value) {
  1185. if (arguments.length === 1) {
  1186. return _cache[key];
  1187. } else {
  1188. return _cache[arguments[0]][arguments[1]];
  1189. }
  1190. };
  1191. /**
  1192. * Removes the instance in LungoJs Cache System of a given key
  1193. *
  1194. * @method remove
  1195. *
  1196. * @param {string} Key in LungoJS Cache System
  1197. * @param {string} [OPTIONAL] Subkey in LungoJS Cache System
  1198. */
  1199. var remove = function(key, value) {
  1200. if (arguments.length === 1) {
  1201. delete _cache[key];
  1202. } else {
  1203. delete _cache[arguments[0]][arguments[1]];
  1204. }
  1205. };
  1206. /**
  1207. * Returns the existence of a key in LungoJs Cache System
  1208. *
  1209. * @method exists
  1210. *
  1211. * @param {String} Key in LungoJS Cache System
  1212. * @return {Boolean} true if exists, false if not
  1213. */
  1214. var exists = function(key) {
  1215. return (_cache[key]) ? true : false;
  1216. };
  1217. return {
  1218. set: set,
  1219. get: get,
  1220. remove: remove,
  1221. exists: exists
  1222. };
  1223. })(LUNGO);/**
  1224. * Wrapper for using WebSql (HTML5 feature)
  1225. *
  1226. * @namespace LUNGO.Data
  1227. * @class Sql
  1228. *
  1229. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  1230. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  1231. */
  1232. LUNGO.Data.Sql = (function(lng, undefined) {
  1233. var CONFIG = {
  1234. name: 'lungo_db',
  1235. version: '1.0',
  1236. size: 65536,
  1237. schema: []
  1238. };
  1239. var db = null;
  1240. /**
  1241. * Initialize the SQLite storage (HTML5 Feature)
  1242. *
  1243. * @method init
  1244. *
  1245. * @param {object} Configuration for the Database
  1246. */
  1247. var init = function(db_config) {
  1248. CONFIG = lng.Core.mix(CONFIG, db_config);
  1249. db = openDatabase(CONFIG.name, CONFIG.version, CONFIG.name, CONFIG.size);
  1250. if (db) {
  1251. _createSchema();
  1252. } else {
  1253. lng.Core.log(3, 'lng.Data.Sql >> Failed to connect to database.');
  1254. }
  1255. };
  1256. /**
  1257. * Select a data set of a given table and based on a selection object
  1258. *
  1259. * @method select
  1260. *
  1261. * @param {string} Name of the table in the database
  1262. * @param {object} [OPTIONAL] Object selection condition
  1263. * @param {Function} Callback when the process is complete
  1264. */
  1265. var select = function(table, where_obj, callback) {
  1266. var where = (where_obj) ? ' WHERE ' + _convertToSql(where_obj, 'AND') : '';
  1267. execute('SELECT * FROM ' + table + where, function(rs) {
  1268. var result = [];
  1269. for (var i = 0, len = rs.rows.length; i < len; i++) {
  1270. result.push(rs.rows.item(i));
  1271. }
  1272. _callbackResponse(callback, result);
  1273. });
  1274. };
  1275. /**
  1276. * Inserts a data set of a given table and based on a data object
  1277. *
  1278. * @method insert
  1279. *
  1280. * @param {string} Name of the table in the database
  1281. * @param {object} Data object to insert in table
  1282. */
  1283. var insert = function(table, data_obj) {
  1284. var fields = '';
  1285. var values = '';
  1286. for (var field in data_obj) {
  1287. if (lng.Core.isOwnProperty(data_obj, field)) {
  1288. var value = data_obj[field];
  1289. fields += (fields) ? ', ' + field : field;
  1290. if (values) values += ', ';
  1291. values += (isNaN(value)) ? '"' + value + '"' : value;
  1292. }
  1293. }
  1294. execute('INSERT INTO ' + table + ' (' + fields + ') VALUES (' + values + ')');
  1295. };
  1296. /**
  1297. * Updates a data set of a given table and based on a data object and
  1298. * an optional selection object
  1299. *
  1300. * @method update
  1301. *
  1302. * @param {string} Name of the table in the database
  1303. * @param {object} Data object to update in table
  1304. * @param {object} [OPTIONAL] Object selection condition
  1305. */
  1306. var update = function(table, data_obj, where_obj) {
  1307. var sql = 'UPDATE ' + table + ' SET ' + _convertToSql(data_obj, ',');
  1308. if (where_obj) sql += ' WHERE ' + _convertToSql(where_obj, 'AND');
  1309. execute(sql);
  1310. };
  1311. /**
  1312. * Delete a data set of a given table and based on a selection object
  1313. *
  1314. * @method drop
  1315. *
  1316. * @param {string} Name of the table in the database
  1317. * @param {object} [OPTIONAL] Object selection condition
  1318. */
  1319. var drop = function(table, where_obj) {
  1320. var where = (where_obj) ? ' WHERE ' + _convertToSql(where_obj, 'AND') : '';
  1321. execute('DELETE FROM ' + table + where + ';');
  1322. };
  1323. /**
  1324. * Executes a SQL statement in the SQLite storage
  1325. *
  1326. * @method execute
  1327. *
  1328. * @param {string} SQL statement
  1329. * @param {Function} Callback when the process is complete
  1330. */
  1331. var execute = function(sql, callback) {
  1332. lng.Core.log(1, 'lng.Data.Sql >> ' + sql);
  1333. db.transaction(function(tx) {
  1334. tx.executeSql(sql, [], function(tx, rs) {
  1335. _callbackResponse(callback, rs);
  1336. }, _throwError);
  1337. });
  1338. };
  1339. var _createSchema = function() {
  1340. var schema = CONFIG.schema;
  1341. var schema_len = schema.length;
  1342. if (!schema_len) return;
  1343. for (var i = 0; i < schema_len; i++) {
  1344. var current = schema[i];
  1345. _regenerateTable(current);
  1346. _createTable(current.name, current.fields);
  1347. }
  1348. };
  1349. var _createTable = function(table, fields) {
  1350. var sql_fields = '';
  1351. for (var field in fields) {
  1352. if (lng.Core.isOwnProperty(fields, field)) {
  1353. if (sql_fields) sql_fields += ', ';
  1354. sql_fields += field + ' ' + fields[field];
  1355. }
  1356. }
  1357. execute('CREATE TABLE IF NOT EXISTS ' + table + ' (' + sql_fields + ');');
  1358. };
  1359. var _regenerateTable = function(table) {
  1360. if (table.drop === true) {
  1361. _dropTable(table.name);
  1362. }
  1363. };
  1364. var _dropTable = function(table) {
  1365. execute('DROP TABLE IF EXISTS ' + table);
  1366. };
  1367. var _convertToSql = function(fields, separator) {
  1368. var sql = '';
  1369. for (var field in fields) {
  1370. if (lng.Core.isOwnProperty(fields, field)) {
  1371. var value = fields[field];
  1372. if (sql) sql += ' ' + separator + ' ';
  1373. sql += field + '=';
  1374. sql += (isNaN(value)) ? '"' + value + '"' : value;
  1375. }
  1376. }
  1377. return sql;
  1378. };
  1379. var _callbackResponse = function(callback, response) {
  1380. if (lng.Core.toType(callback) === 'function') {
  1381. setTimeout(callback, 100, response);
  1382. }
  1383. };
  1384. var _throwError = function(transaction, error) {
  1385. lng.Core.log(3, 'lng.Data.Sql >> ' + error.code + ': ' + error.message);
  1386. };
  1387. return {
  1388. init: init,
  1389. select: select,
  1390. insert: insert,
  1391. update: update,
  1392. drop: drop,
  1393. execute: execute
  1394. };
  1395. })(LUNGO);/**
  1396. * Wrapper for using LocalStorage & SessionStorage (HTML5 Feature)
  1397. *
  1398. * @namespace LUNGO.Data
  1399. * @class Storage
  1400. *
  1401. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  1402. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  1403. */
  1404. LUNGO.Data.Storage = (function(lng, undefined) {
  1405. return {
  1406. };
  1407. })(LUNGO);/**
  1408. * Boot for a new LungoJS Application instance
  1409. *
  1410. * @namespace LUNGO
  1411. * @class App
  1412. *
  1413. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  1414. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  1415. */
  1416. LUNGO.Boot = (function(lng, undefined) {
  1417. return function() {
  1418. lng.Environment.start();
  1419. lng.Boot.Events.start();
  1420. lng.Boot.Data.start();
  1421. lng.Boot.Section.start();
  1422. lng.Boot.Article.start();
  1423. };
  1424. })(LUNGO);/**
  1425. * Initialize the <article> element
  1426. *
  1427. * @namespace LUNGO.Boot
  1428. * @class Article
  1429. * @requires Zepto
  1430. *
  1431. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  1432. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  1433. */
  1434. LUNGO.Boot.Article = (function(lng, undefined) {
  1435. var SELECTORS = {
  1436. LIST_IN_ARTICLE: 'article.list',
  1437. SCROLL_IN_ARTICLE: 'scroll',
  1438. CHECKBOX_IN_ARTICLE: '.checkbox, .radio'
  1439. };
  1440. /**
  1441. * Initializes the markup elements of an article
  1442. *
  1443. * @method init
  1444. */
  1445. var start = function() {
  1446. _initElement(SELECTORS.LIST_IN_ARTICLE, _createListElement);
  1447. _initElement(SELECTORS.SCROLL_IN_ARTICLE, _createScrollElement);
  1448. _initElement(SELECTORS.CHECKBOX_IN_ARTICLE, _b);
  1449. };
  1450. var _initElement = function(selector, callback) {
  1451. var found_elements = lng.Dom.query(selector);
  1452. for (var i = 0, len = found_elements.length; i < len; i++) {
  1453. var element = lng.Dom.query(found_elements[i]);
  1454. lng.Core.execute(callback, element);
  1455. }
  1456. };
  1457. var _createListElement = function(article) {
  1458. if (article.children().length === 0) {
  1459. var article_id = article.attr('id');
  1460. article.append('<ul id="' + article_id + '_list"></ul>');
  1461. }
  1462. };
  1463. var _createScrollElement = function(scroll) {
  1464. var scroll_id = scroll.attr('id');
  1465. lng.View.Scroll.create(scroll_id);
  1466. };
  1467. var _b = function(checkbox) {
  1468. checkbox.append('<span>&nbsp;</span>');
  1469. };
  1470. return {
  1471. start: start
  1472. };
  1473. })(LUNGO);/**
  1474. * Make an analysis of Data attributes in HTML elements and creates a <markup>
  1475. * based on each data type.
  1476. *
  1477. * @namespace LUNGO.Boot
  1478. * @class Data
  1479. * @requires Zepto
  1480. *
  1481. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  1482. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  1483. */
  1484. LUNGO.Boot.Data = (function(lng, undefined) {
  1485. /**
  1486. * Initialize the <markup> data-attributes analisys
  1487. *
  1488. * @method init
  1489. *
  1490. *
  1491. */
  1492. var start = function() {
  1493. var attributes = lng.Attributes.Data;
  1494. for (var attribute in attributes) {
  1495. if (lng.Core.isOwnProperty(attributes, attribute)) {
  1496. _findElements(attributes[attribute]);
  1497. }
  1498. }
  1499. };
  1500. var _findElements = function(attribute) {
  1501. var elements = lng.Dom.query(attribute.selector);
  1502. for (var i = 0, len = elements.length; i < len; i++) {
  1503. var element = lng.Dom.query(elements[i]);
  1504. lng.View.Template.Binding.dataAttribute(element, attribute);
  1505. }
  1506. };
  1507. return {
  1508. start: start
  1509. };
  1510. })(LUNGO);/**
  1511. * Initialize the automatic DOM UI events
  1512. *
  1513. * @namespace LUNGO.Boot
  1514. * @class Events
  1515. *
  1516. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  1517. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  1518. */
  1519. LUNGO.Boot.Events = (function(lng, undefined) {
  1520. /**
  1521. * Initializes the automatic subscription events by markup of the project.
  1522. *
  1523. * @method init
  1524. *
  1525. */
  1526. var start = function() {
  1527. var touch_move_event = 'TOUCH_MOVE';
  1528. var touch_start_event = 'TOUCH_START';
  1529. var orientation_change = 'ORIENTATION_CHANGE';
  1530. var target_selector = 'a[href][data-target]';
  1531. lng.Dom.Event.listener(document, touch_move_event, _iScroll);
  1532. lng.Dom.Event.listener(window, orientation_change, _changeOrientation);
  1533. lng.Dom.Event.live(target_selector, touch_start_event, _loadTarget);
  1534. };
  1535. var _iScroll = function(event_handler) {
  1536. event_handler.preventDefault();
  1537. };
  1538. var _changeOrientation = function(event) {
  1539. lng.View.Resize.toolbars();
  1540. };
  1541. var _loadTarget = function(event) {
  1542. event.preventDefault();
  1543. var link = lng.Dom.query(this);
  1544. var target_id = link.attr('href');
  1545. var target_type = link.data('target');
  1546. (target_type === 'section') ? _goSection(target_id) : _goArticle(link);
  1547. };
  1548. var _goSection = function(id) {
  1549. if (id === '#back') {
  1550. lng.Router.back();
  1551. } else {
  1552. lng.Router.section(id);
  1553. }
  1554. };
  1555. var _goArticle = function(element) {
  1556. var section_id = '#' + element.parents('section').attr('id');
  1557. var article_id = element.attr('href');
  1558. lng.Router.article(section_id, article_id);
  1559. };
  1560. return {
  1561. start: start
  1562. };
  1563. })(LUNGO);/**
  1564. * Initialize the <section> element
  1565. *
  1566. * @namespace LUNGO.Boot
  1567. * @class Section
  1568. * @requires Zepto
  1569. *
  1570. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  1571. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  1572. */
  1573. LUNGO.Boot.Section = (function(lng, undefined) {
  1574. var SELECTORS = {
  1575. ARTICLE: 'article',
  1576. SECTION: 'section'
  1577. };
  1578. var ACTIVE_CLASS = 'current';
  1579. /**
  1580. * Initializes all <section>s of the project
  1581. *
  1582. * @method init
  1583. */
  1584. var start = function() {
  1585. var sections = lng.Dom.query(SELECTORS.SECTION);
  1586. var easing_transition = '-webkit-transform 0.3s ease-in-out';
  1587. _initFirstSection(sections);
  1588. _initAllSections(sections);
  1589. lng.View.Resize.toolbars();
  1590. _allocateEasingTransition(sections, easing_transition);
  1591. };
  1592. var _initFirstSection = function(sections) {
  1593. var first_section = sections.first();
  1594. var first_section_id = '#' + first_section.attr('id');
  1595. first_section.addClass(ACTIVE_CLASS);
  1596. lng.Router.History.add(first_section_id);
  1597. };
  1598. var _initAllSections = function(sections) {
  1599. for (var i = 0, len = sections.length; i < len; i++) {
  1600. var section = lng.Dom.query(sections[i]);
  1601. _initSection(section);
  1602. _initFirstArticle(section);
  1603. }
  1604. };
  1605. var _initSection = function(section) {
  1606. var section_attributes = lng.Attributes.Section;
  1607. for (var attribute in section_attributes) {
  1608. if (lng.Core.isOwnProperty(section_attributes, attribute)) {
  1609. var property = section_attributes[attribute];
  1610. lng.View.Resize.article(section, property.name, property.bind);
  1611. }
  1612. }
  1613. };
  1614. var _allocateEasingTransition = function(sections, easing) {
  1615. var transition_property = { '-webkit-transition': easing };
  1616. sections.css(transition_property);
  1617. };
  1618. var _initFirstArticle = function(section) {
  1619. section.children(SELECTORS.ARTICLE).first().addClass(ACTIVE_CLASS);
  1620. };
  1621. return {
  1622. start: start
  1623. };
  1624. })(LUNGO);