Lungo.View.Scroll.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * Wrapper of the third library iScroll
  3. *
  4. * @namespace LUNGO.View
  5. * @class Scroll
  6. * @requires iScroll
  7. *
  8. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  9. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  10. */
  11. LUNGO.View.Scroll = (function(lng, undefined) {
  12. var DEFAULT_PROPERTIES = {
  13. hScroll: false,
  14. vScroll: false,
  15. useTransition: true,
  16. momentum: true,
  17. lockDirection: true,
  18. fixedScrollbar: true,
  19. fadeScrollbar: true,
  20. hideScrollbar: true
  21. };
  22. var HORIZONTAL_CLASS = 'horizontal';
  23. var CACHE_KEY = 'scrolls';
  24. var HEADER_FOOTER_BLEEDING = 90;
  25. /**
  26. * Creates a new iScroll element.
  27. *
  28. * @method create
  29. *
  30. * @param {string} Id of the container scroll.
  31. * @param {object} [OPTIONAL] Properties
  32. */
  33. var create = function(id, properties) {
  34. if (id) {
  35. var scroll = lng.dom('#' + id);
  36. //ToDo >> Refactor
  37. setTimeout(function() {
  38. if (_needScroll(scroll)) {
  39. properties = _mixProperties(scroll, properties);
  40. _saveScrollInCache(id, properties);
  41. }
  42. }, 100);
  43. } else {
  44. lng.Core.log(3, 'ERROR: Impossible to create a <scroll> without ID');
  45. }
  46. };
  47. /**
  48. * Update iScroll element with new <markup> content.
  49. *
  50. * @method update
  51. *
  52. * @param {string} Id of the container scroll.
  53. * @param {string} Markup content
  54. */
  55. var update = function(id, content) {
  56. var scroll = lng.dom('#' + id);
  57. var container = scroll.children().first();
  58. if (container.length === 0) {
  59. scroll.html('<div id="' + id + '_scrl"></div>');
  60. container = scroll.children().first();
  61. }
  62. container.html(content);
  63. lng.View.Resize.scroll(scroll);
  64. _refresh(id);
  65. };
  66. /**
  67. * Removes iScroll instance.
  68. *
  69. * @method remove
  70. *
  71. * @param {string} Id of the <section>
  72. */
  73. var remove = function(id) {
  74. if (lng.Data.Cache.exists(CACHE_KEY)) {
  75. lng.Data.Cache.get(CACHE_KEY, id).destroy();
  76. lng.Data.Cache.remove(CACHE_KEY, id);
  77. }
  78. };
  79. /**
  80. * Removes iScroll instance.
  81. *
  82. * @method scrollIsHorizontal
  83. *
  84. * @param {Object} Id of the <section>
  85. */
  86. var isHorizontal = function(scroll) {
  87. return (scroll.hasClass(HORIZONTAL_CLASS)) ? true : false;
  88. };
  89. var _needScroll = function(scroll) {
  90. var is_necessary = false;
  91. var element = scroll[0];
  92. if (element.clientHeight < element.scrollHeight) {
  93. is_necessary = true;
  94. var child_height = element.scrollHeight + HEADER_FOOTER_BLEEDING;
  95. _resizeChildContainer(element, child_height);
  96. }
  97. return is_necessary;
  98. };
  99. var _resizeChildContainer = function(element, height) {
  100. var child_container = lng.dom(element).children().first();
  101. child_container.style('height', height + 'px');
  102. };
  103. var _saveScrollInCache = function(id, properties) {
  104. _createScrollIndexInCache();
  105. var scroll = lng.Data.Cache.get(CACHE_KEY);
  106. scroll[id] = new iScroll(id, properties);
  107. lng.Data.Cache.set(CACHE_KEY, scroll);
  108. };
  109. var _createScrollIndexInCache = function() {
  110. if (!lng.Data.Cache.exists(CACHE_KEY)) {
  111. lng.Data.Cache.set(CACHE_KEY, {});
  112. }
  113. }
  114. var _mixProperties = function(scroll, properties) {
  115. var scroll_type = isHorizontal(scroll) ? 'hScroll' : 'vScroll';
  116. properties || (properties = {});
  117. properties[scroll_type] = true;
  118. properties = lng.Core.mix(DEFAULT_PROPERTIES, properties);
  119. return properties;
  120. };
  121. var _refresh = function(id, properties) {
  122. !lng.Data.Cache.get(CACHE_KEY, id) && _saveScrollInCache(id);
  123. lng.Data.Cache.get(CACHE_KEY, id).refresh();
  124. };
  125. return {
  126. create: create,
  127. update: update,
  128. remove: remove,
  129. isHorizontal: isHorizontal
  130. };
  131. })(LUNGO);