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