lungo-1.1.js 48 KB

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