PageRenderTime 54ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/components/bitrix/catalog.section/templates/bootstrap_v4/script.js

https://gitlab.com/alexprowars/bitrix
JavaScript | 360 lines | 310 code | 50 blank | 0 comment | 58 complexity | ffad2c42a25a76ccc3340a12e1de0129 MD5 | raw file
  1. (function() {
  2. 'use strict';
  3. if (!!window.JCCatalogSectionComponent)
  4. return;
  5. window.JCCatalogSectionComponent = function(params) {
  6. this.formPosting = false;
  7. this.siteId = params.siteId || '';
  8. this.ajaxId = params.ajaxId || '';
  9. this.template = params.template || '';
  10. this.componentPath = params.componentPath || '';
  11. this.parameters = params.parameters || '';
  12. if (params.navParams)
  13. {
  14. this.navParams = {
  15. NavNum: params.navParams.NavNum || 1,
  16. NavPageNomer: parseInt(params.navParams.NavPageNomer) || 1,
  17. NavPageCount: parseInt(params.navParams.NavPageCount) || 1
  18. };
  19. }
  20. this.bigData = params.bigData || {enabled: false};
  21. this.container = document.querySelector('[data-entity="' + params.container + '"]');
  22. this.lazyLoadContainer = document.querySelector('[data-entity="lazy-' + params.container + '"]');
  23. this.showMoreButton = null;
  24. this.showMoreButtonMessage = null;
  25. if (this.bigData.enabled && BX.util.object_keys(this.bigData.rows).length > 0)
  26. {
  27. BX.cookie_prefix = this.bigData.js.cookiePrefix || '';
  28. BX.cookie_domain = this.bigData.js.cookieDomain || '';
  29. BX.current_server_time = this.bigData.js.serverTime;
  30. BX.ready(BX.delegate(this.bigDataLoad, this));
  31. }
  32. if (params.initiallyShowHeader)
  33. {
  34. BX.ready(BX.delegate(this.showHeader, this));
  35. }
  36. if (params.deferredLoad)
  37. {
  38. BX.ready(BX.delegate(this.deferredLoad, this));
  39. }
  40. if (params.lazyLoad)
  41. {
  42. this.showMoreButton = document.querySelector('[data-use="show-more-' + this.navParams.NavNum + '"]');
  43. this.showMoreButtonMessage = this.showMoreButton.innerHTML;
  44. BX.bind(this.showMoreButton, 'click', BX.proxy(this.showMore, this));
  45. }
  46. if (params.loadOnScroll)
  47. {
  48. BX.bind(window, 'scroll', BX.proxy(this.loadOnScroll, this));
  49. }
  50. };
  51. window.JCCatalogSectionComponent.prototype =
  52. {
  53. checkButton: function()
  54. {
  55. if (this.showMoreButton)
  56. {
  57. if (this.navParams.NavPageNomer == this.navParams.NavPageCount)
  58. {
  59. BX.remove(this.showMoreButton);
  60. }
  61. else
  62. {
  63. this.lazyLoadContainer.appendChild(this.showMoreButton);
  64. }
  65. }
  66. },
  67. enableButton: function()
  68. {
  69. if (this.showMoreButton)
  70. {
  71. BX.removeClass(this.showMoreButton, 'disabled');
  72. this.showMoreButton.innerHTML = this.showMoreButtonMessage;
  73. }
  74. },
  75. disableButton: function()
  76. {
  77. if (this.showMoreButton)
  78. {
  79. BX.addClass(this.showMoreButton, 'disabled');
  80. this.showMoreButton.innerHTML = BX.message('BTN_MESSAGE_LAZY_LOAD_WAITER');
  81. }
  82. },
  83. loadOnScroll: function()
  84. {
  85. var scrollTop = BX.GetWindowScrollPos().scrollTop,
  86. containerBottom = BX.pos(this.container).bottom;
  87. if (scrollTop + window.innerHeight > containerBottom)
  88. {
  89. this.showMore();
  90. }
  91. },
  92. showMore: function()
  93. {
  94. if (this.navParams.NavPageNomer < this.navParams.NavPageCount)
  95. {
  96. var data = {};
  97. data['action'] = 'showMore';
  98. data['PAGEN_' + this.navParams.NavNum] = this.navParams.NavPageNomer + 1;
  99. if (!this.formPosting)
  100. {
  101. this.formPosting = true;
  102. this.disableButton();
  103. this.sendRequest(data);
  104. }
  105. }
  106. },
  107. bigDataLoad: function()
  108. {
  109. var url = 'https://analytics.bitrix.info/crecoms/v1_0/recoms.php',
  110. data = BX.ajax.prepareData(this.bigData.params);
  111. if (data)
  112. {
  113. url += (url.indexOf('?') !== -1 ? '&' : '?') + data;
  114. }
  115. var onReady = BX.delegate(function(result){
  116. this.sendRequest({
  117. action: 'deferredLoad',
  118. bigData: 'Y',
  119. items: result && result.items || [],
  120. rid: result && result.id,
  121. count: this.bigData.count,
  122. rowsRange: this.bigData.rowsRange,
  123. shownIds: this.bigData.shownIds
  124. });
  125. }, this);
  126. BX.ajax({
  127. method: 'GET',
  128. dataType: 'json',
  129. url: url,
  130. timeout: 3,
  131. onsuccess: onReady,
  132. onfailure: onReady
  133. });
  134. },
  135. deferredLoad: function()
  136. {
  137. this.sendRequest({action: 'deferredLoad'});
  138. },
  139. sendRequest: function(data)
  140. {
  141. var defaultData = {
  142. siteId: this.siteId,
  143. template: this.template,
  144. parameters: this.parameters
  145. };
  146. if (this.ajaxId)
  147. {
  148. defaultData.AJAX_ID = this.ajaxId;
  149. }
  150. BX.ajax({
  151. url: this.componentPath + '/ajax.php' + (document.location.href.indexOf('clear_cache=Y') !== -1 ? '?clear_cache=Y' : ''),
  152. method: 'POST',
  153. dataType: 'json',
  154. timeout: 60,
  155. data: BX.merge(defaultData, data),
  156. onsuccess: BX.delegate(function(result){
  157. if (!result || !result.JS)
  158. return;
  159. BX.ajax.processScripts(
  160. BX.processHTML(result.JS).SCRIPT,
  161. false,
  162. BX.delegate(function(){this.showAction(result, data);}, this)
  163. );
  164. }, this)
  165. });
  166. },
  167. showAction: function(result, data)
  168. {
  169. if (!data)
  170. return;
  171. switch (data.action)
  172. {
  173. case 'showMore':
  174. this.processShowMoreAction(result);
  175. break;
  176. case 'deferredLoad':
  177. this.processDeferredLoadAction(result, data.bigData === 'Y');
  178. break;
  179. }
  180. },
  181. processShowMoreAction: function(result)
  182. {
  183. this.formPosting = false;
  184. this.enableButton();
  185. if (result)
  186. {
  187. this.navParams.NavPageNomer++;
  188. this.processItems(result.items);
  189. this.processPagination(result.pagination);
  190. this.processEpilogue(result.epilogue);
  191. this.checkButton();
  192. }
  193. },
  194. processDeferredLoadAction: function(result, bigData)
  195. {
  196. if (!result)
  197. return;
  198. var position = bigData ? this.bigData.rows : {};
  199. this.processItems(result.items, BX.util.array_keys(position));
  200. },
  201. processItems: function(itemsHtml, position)
  202. {
  203. if (!itemsHtml)
  204. return;
  205. var processed = BX.processHTML(itemsHtml, false),
  206. temporaryNode = BX.create('DIV');
  207. var items, k, origRows;
  208. temporaryNode.innerHTML = processed.HTML;
  209. items = temporaryNode.querySelectorAll('[data-entity="items-row"]');
  210. if (items.length)
  211. {
  212. this.showHeader(true);
  213. for (k in items)
  214. {
  215. if (items.hasOwnProperty(k))
  216. {
  217. origRows = position ? this.container.querySelectorAll('[data-entity="items-row"]') : false;
  218. items[k].style.opacity = 0;
  219. if (origRows && BX.type.isDomNode(origRows[position[k]]))
  220. {
  221. origRows[position[k]].parentNode.insertBefore(items[k], origRows[position[k]]);
  222. }
  223. else
  224. {
  225. this.container.appendChild(items[k]);
  226. }
  227. }
  228. }
  229. new BX.easing({
  230. duration: 2000,
  231. start: {opacity: 0},
  232. finish: {opacity: 100},
  233. transition: BX.easing.makeEaseOut(BX.easing.transitions.quad),
  234. step: function(state){
  235. for (var k in items)
  236. {
  237. if (items.hasOwnProperty(k))
  238. {
  239. items[k].style.opacity = state.opacity / 100;
  240. }
  241. }
  242. },
  243. complete: function(){
  244. for (var k in items)
  245. {
  246. if (items.hasOwnProperty(k))
  247. {
  248. items[k].removeAttribute('style');
  249. }
  250. }
  251. }
  252. }).animate();
  253. }
  254. BX.ajax.processScripts(processed.SCRIPT);
  255. },
  256. processPagination: function(paginationHtml)
  257. {
  258. if (!paginationHtml)
  259. return;
  260. var pagination = document.querySelectorAll('[data-pagination-num="' + this.navParams.NavNum + '"]');
  261. for (var k in pagination)
  262. {
  263. if (pagination.hasOwnProperty(k))
  264. {
  265. pagination[k].innerHTML = paginationHtml;
  266. }
  267. }
  268. },
  269. processEpilogue: function(epilogueHtml)
  270. {
  271. if (!epilogueHtml)
  272. return;
  273. var processed = BX.processHTML(epilogueHtml, false);
  274. BX.ajax.processScripts(processed.SCRIPT);
  275. },
  276. showHeader: function(animate)
  277. {
  278. var parentNode = BX.findParent(this.container, {attr: {'data-entity': 'parent-container'}}),
  279. header;
  280. if (parentNode && BX.type.isDomNode(parentNode))
  281. {
  282. header = parentNode.querySelector('[data-entity="header"]');
  283. if (header && header.getAttribute('data-showed') != 'true')
  284. {
  285. header.style.display = '';
  286. if (animate)
  287. {
  288. new BX.easing({
  289. duration: 2000,
  290. start: {opacity: 0},
  291. finish: {opacity: 100},
  292. transition: BX.easing.makeEaseOut(BX.easing.transitions.quad),
  293. step: function(state){
  294. header.style.opacity = state.opacity / 100;
  295. },
  296. complete: function(){
  297. header.removeAttribute('style');
  298. header.setAttribute('data-showed', 'true');
  299. }
  300. }).animate();
  301. }
  302. else
  303. {
  304. header.style.opacity = 100;
  305. }
  306. }
  307. }
  308. }
  309. };
  310. })();