Lungo.View.Scroll.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 SCROLL_TIMEFRAME = 250;
  25. /**
  26. * Creates a new iScroll element.
  27. *
  28. * @method init
  29. *
  30. * @param {string} Id of the container scroll.
  31. * @param {object} [OPTIONAL] Properties
  32. */
  33. var init = function(id, properties) {
  34. if (id) {
  35. _render(id, properties);
  36. } else {
  37. lng.Core.log(3, 'ERROR: Impossible to create a <scroll> without ID');
  38. }
  39. };
  40. /**
  41. * Update iScroll element with new <markup> content.
  42. *
  43. * @method html
  44. *
  45. * @param {string} Id of the container scroll.
  46. * @param {string} Markup content
  47. */
  48. var html = function(id, content) {
  49. var container = _getContainer(id);
  50. container.html(content);
  51. _render(id);
  52. };
  53. /**
  54. * Add <markup> content to iScroll instance
  55. *
  56. * @method append
  57. *
  58. * @param {string} Id of the container scroll.
  59. * @param {string} Markup content
  60. */
  61. var append = function(id, content) {
  62. var container = _getContainer(id);
  63. container.append(content);
  64. _render(id);
  65. };
  66. /**
  67. * Removes iScroll instance.
  68. *
  69. * @method refresh
  70. *
  71. * @param {Object} Id of the <section>
  72. */
  73. var refresh = function(id, properties) {
  74. _render(id, properties);
  75. };
  76. /**
  77. * Removes iScroll instance.
  78. *
  79. * @method remove
  80. *
  81. * @param {string} Id of the <section>
  82. */
  83. var remove = function(id) {
  84. if (lng.Data.Cache.exists(CACHE_KEY) && lng.Data.Cache.get(CACHE_KEY, id)) {
  85. lng.Data.Cache.get(CACHE_KEY, id).destroy();
  86. lng.Data.Cache.remove(CACHE_KEY, id);
  87. }
  88. };
  89. /**
  90. * Scrolls the wrapper contents to the minimum x/y coordinates
  91. *
  92. * @method first
  93. *
  94. * @param {string} Id of the <section>
  95. */
  96. var first = function(id) {
  97. var scroll = lng.Data.Cache.get(CACHE_KEY);
  98. if (scroll[id]) {
  99. scroll[id].scrollTo(0, 0, SCROLL_TIMEFRAME);
  100. }
  101. };
  102. /**
  103. * Scrolls the wrapper contents to the maximum x/y coordinate
  104. *
  105. * @method down
  106. *
  107. * @param {string} Id of the <section>
  108. */
  109. var last = function(id) {
  110. var scroll = lng.Data.Cache.get(CACHE_KEY, id);
  111. if (scroll) {
  112. var element = lng.dom('#' + id).first();
  113. var content_width = 0;
  114. var content_height = 0;
  115. if (_isHorizontal(element)) {
  116. content_width = -(_sizeProperty(element, 'width'));
  117. } else {
  118. content_height = -(_sizeProperty(element, 'height'));
  119. }
  120. scroll.scrollTo(content_width, content_height, SCROLL_TIMEFRAME);
  121. }
  122. };
  123. var _getContainer = function(id) {
  124. var scroll = lng.dom('#' + id);
  125. var container = scroll.children().first();
  126. if (container.length === 0) {
  127. scroll.html('<div></div>');
  128. container = scroll.children().first();
  129. }
  130. return container;
  131. };
  132. var _sizeProperty = function(element, property) {
  133. var element_content = element.children().first();
  134. return element_content[property]() - element[property]();
  135. };
  136. var _render = function(id, properties) {
  137. var scroll = lng.dom('#' + id);
  138. if (_needScroll(scroll, properties)) {
  139. properties = _mixProperties(scroll, properties);
  140. _saveScrollInCache(id, properties);
  141. } else {
  142. remove(id);
  143. }
  144. };
  145. var _needScroll = function(scroll, properties) {
  146. var element = scroll[0];
  147. var is_horizontal = _isHorizontal(lng.dom(element));
  148. if (is_horizontal) {
  149. return (element.clientWidth < element.scrollWidth);
  150. } else {
  151. return (element.clientHeight < element.scrollHeight);
  152. }
  153. };
  154. var _saveScrollInCache = function(id, properties) {
  155. _createScrollIndexInCache();
  156. var scroll = lng.Data.Cache.get(CACHE_KEY);
  157. if (!scroll[id]) {
  158. scroll[id] = new iScroll(id, properties);
  159. } else {
  160. scroll[id].refresh();
  161. }
  162. lng.Data.Cache.set(CACHE_KEY, scroll);
  163. };
  164. var _createScrollIndexInCache = function() {
  165. if (!lng.Data.Cache.exists(CACHE_KEY)) {
  166. lng.Data.Cache.set(CACHE_KEY, {});
  167. }
  168. };
  169. var _mixProperties = function(scroll, properties) {
  170. var scroll_type = _isHorizontal(scroll) ? 'hScroll' : 'vScroll';
  171. properties || (properties = {});
  172. properties[scroll_type] = true;
  173. properties = lng.Core.mix(DEFAULT_PROPERTIES, properties);
  174. return properties;
  175. };
  176. var _isHorizontal = function(scroll) {
  177. return ( scroll.hasClass(HORIZONTAL_CLASS)) ? true : false;
  178. };
  179. return {
  180. init: init,
  181. remove: remove,
  182. refresh: refresh,
  183. html: html,
  184. append: append,
  185. first: first,
  186. last: last
  187. };
  188. })(LUNGO);