Lungo.View.Article.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * Initialize the <articles> layout of a certain <section>
  3. *
  4. * @namespace LUNGO.View
  5. * @class Article
  6. *
  7. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  8. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  9. */
  10. LUNGO.View.Article = (function(lng, undefined) {
  11. var ELEMENT = lng.Constants.ELEMENT;
  12. var CLASS = lng.Constants.CLASS;
  13. var ATTRIBUTE = lng.Constants.ATTRIBUTE;
  14. var TRIGGER = lng.Constants.TRIGGER;
  15. var SELECTORS = {
  16. NAVIGATION_ITEM: 'a[href][data-target="article"]',
  17. REFERENCE_LINK: ' a[href][data-article]'
  18. };
  19. /**
  20. * ?
  21. *
  22. * @method show
  23. */
  24. var show = function(section_id, article_id) {
  25. _toggleNavItems(section_id, article_id);
  26. showReferenceLinks(section_id, article_id.replace('#', ''));
  27. _showContainer(section_id, article_id);
  28. };
  29. /**
  30. * ?
  31. *
  32. * @method showReferenceLinks
  33. */
  34. var showReferenceLinks = function(section_id, article_id) {
  35. var links = lng.dom(ELEMENT.SECTION + section_id + SELECTORS.REFERENCE_LINK);
  36. for (var i = 0, len = links.length; i < len; i++) {
  37. var link = lng.dom(links[i]);
  38. (link.data(ATTRIBUTE.ARTICLE) === article_id) ? link.show() : link.hide();
  39. }
  40. };
  41. var _toggleNavItems = function(section_id, article_id) {
  42. var nav_items = lng.dom(section_id + ' ' + SELECTORS.NAVIGATION_ITEM);
  43. nav_items.removeClass(CLASS.CURRENT);
  44. for (var i = 0, len = nav_items.length; i < len; i++) {
  45. var nav_item = lng.dom(nav_items[i]);
  46. var nav_item_parsed_url = lng.Core.parseUrl(nav_item.attr(ATTRIBUTE.HREF));
  47. if (nav_item_parsed_url === article_id) {
  48. nav_item.addClass(CLASS.CURRENT);
  49. _setTitle(section_id, nav_item);
  50. }
  51. }
  52. };
  53. var _showContainer = function(section_id, article_id) {
  54. var section_articles = section_id + ' ' + ELEMENT.ARTICLE + '.' + CLASS.CURRENT;
  55. var current_active_article_id = '#' + lng.dom(section_articles).attr(ATTRIBUTE.ID);
  56. lng.dom(section_articles).removeClass(CLASS.CURRENT).trigger(TRIGGER.UNLOAD);
  57. lng.Fallback.androidInputs(current_active_article_id, false);
  58. lng.dom(article_id).addClass(CLASS.CURRENT);
  59. lng.Fallback.androidInputs(article_id, true);
  60. };
  61. var _setTitle = function(id, item) {
  62. var title = item.data(ATTRIBUTE.TITLE);
  63. if (title) {
  64. var section_title = id + ' header .title, ' + id + ' footer .title';
  65. lng.dom(section_title).text(title);
  66. }
  67. };
  68. return {
  69. show: show,
  70. showReferenceLinks: showReferenceLinks
  71. };
  72. })(LUNGO);