store.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup UnaStudio UNA Studio
  6. * @{
  7. */
  8. function BxDolStudioStore(oOptions) {
  9. this.sActionsUrl = oOptions.sActionUrl;
  10. this.sObjName = oOptions.sObjName == undefined ? 'oBxDolStudioStore' : oOptions.sObjName;
  11. this.sAnimationEffect = oOptions.sAnimationEffect == undefined ? 'fade' : oOptions.sAnimationEffect;
  12. this.iAnimationSpeed = oOptions.iAnimationSpeed == undefined ? 'slow' : oOptions.iAnimationSpeed;
  13. this.sIdPageContent = 'bx-std-pc-content';
  14. this.sIdPopupProduct = 'bx-std-str-popup-product';
  15. }
  16. BxDolStudioStore.prototype.addToCart = function(iVendor, iProduct, oButton) {
  17. var oDate = new Date();
  18. var $this = this;
  19. bx_loading(this.sIdPageContent, true);
  20. $.get(
  21. this.sActionsUrl,
  22. {
  23. str_action: 'add-to-cart',
  24. str_vendor: iVendor,
  25. str_item: iProduct,
  26. _t:oDate.getTime()
  27. },
  28. function(oData) {
  29. bx_loading($this.sIdPageContent, false);
  30. $this.showNotification(oData.message);
  31. if(parseInt(oData.code) == 0) {
  32. var oCounter = $('#bx-std-pmi-checkout .bx-std-pmen-item-counter span');
  33. oCounter.html(parseInt(oCounter.html()) + 1);
  34. if(parseInt(oCounter.html()) > 0)
  35. oCounter.parent('.bx-std-pmen-item-counter').show();
  36. $(oButton).hide().next('.bx-std-pc-checkout,.bx-std-pva-checkout').show();
  37. }
  38. },
  39. 'json'
  40. );
  41. };
  42. BxDolStudioStore.prototype.deleteFromCart = function(iVendor, iProduct, oButton) {
  43. var oDate = new Date();
  44. var $this = this;
  45. bx_loading(this.sIdPageContent, true);
  46. $.get(
  47. this.sActionsUrl,
  48. {
  49. str_action: 'delete-from-cart',
  50. str_vendor: iVendor,
  51. str_item: iProduct,
  52. _t:oDate.getTime()
  53. },
  54. function(oData) {
  55. bx_loading($this.sIdPageContent, false);
  56. if(oData.message.length > 0)
  57. $this.showNotification(oData.message);
  58. var iCode = parseInt(oData.code);
  59. var oCounter = $('#bx-std-pmi-checkout .bx-std-pmen-item-counter span');
  60. if(iCode == 0 && iProduct != 0) {
  61. var oProduct = $('#bx-std-product-' + iProduct);
  62. if(oProduct.siblings('.bx-std-product').length == 0)
  63. oProduct.parents('.bx-std-block:first').bx_anim('hide', 'fade', 'fast', function() {
  64. $(this).remove();
  65. });
  66. else
  67. oProduct.bx_anim('hide', 'fade', 'fast', function() {
  68. $(this).remove();
  69. });
  70. oCounter.html(parseInt(oCounter.html()) - 1);
  71. }
  72. else if(iCode == 0 && iProduct == 0) {
  73. var oBlock = $(oButton).parents('.bx-std-block:first');
  74. oCounter.html(parseInt(oCounter.html()) - oBlock.find('.bx-std-product').length);
  75. oBlock.bx_anim('hide', 'fade', 'fast', function() {
  76. $(this).remove();
  77. });
  78. }
  79. if(parseInt(oCounter.html()) <= 0)
  80. oCounter.parent('.bx-std-pmen-item-counter').hide();
  81. },
  82. 'json'
  83. );
  84. };
  85. BxDolStudioStore.prototype.deleteAllFromCart = function(iVendor, oButton) {
  86. this.deleteFromCart(iVendor, 0, oButton);
  87. };
  88. BxDolStudioStore.prototype.checkoutCart = function(iVendor, oButton) {
  89. var oDate = new Date();
  90. var $this = this;
  91. bx_loading(this.sIdPageContent, true);
  92. $.get(
  93. this.sActionsUrl,
  94. {
  95. str_action: 'checkout-cart',
  96. str_vendor: iVendor,
  97. _t:oDate.getTime()
  98. },
  99. function(oData) {
  100. bx_loading($this.sIdPageContent, false);
  101. if(oData.message.length > 0)
  102. $this.showNotification(oData.message);
  103. if(parseInt(oData.code) == 0 && oData.redirect.length > 0)
  104. document.location=oData.redirect;
  105. },
  106. 'json'
  107. );
  108. };
  109. BxDolStudioStore.prototype.subscribe = function(iVendor, iProduct, oButton) {
  110. var oDate = new Date();
  111. var $this = this;
  112. bx_loading(this.sIdPageContent, true);
  113. $.get(
  114. this.sActionsUrl,
  115. {
  116. str_action: 'subscribe',
  117. str_vendor: iVendor,
  118. str_item: iProduct,
  119. _t:oDate.getTime()
  120. },
  121. function(oData) {
  122. bx_loading($this.sIdPageContent, false);
  123. if(oData.message.length > 0)
  124. $this.showNotification(oData.message);
  125. if(parseInt(oData.code) == 0 && oData.redirect.length > 0)
  126. document.location=oData.redirect;
  127. },
  128. 'json'
  129. );
  130. };
  131. BxDolStudioStore.prototype.getFile = function(iFileId, oButton) {
  132. this._getFile('get-file', iFileId, '', oButton);
  133. };
  134. BxDolStudioStore.prototype.getUpdate = function(sModuleName, oButton) {
  135. this._getFile('get-update', sModuleName, '', oButton);
  136. };
  137. BxDolStudioStore.prototype.getUpdateAndInstall = function(sModuleName, oButton) {
  138. var $this = this;
  139. var onResult = function(oData, oButton) {
  140. if(oData.code != 0) {
  141. $this._onGetFile(oData, oButton);
  142. return;
  143. }
  144. bx_loading($this.sIdPageContent, false);
  145. $(oButton).parents('.bx-std-product:first').hide();
  146. };
  147. this._getFile('get-update-and-install', sModuleName, onResult, oButton);
  148. };
  149. BxDolStudioStore.prototype._getFile = function(sAction, mixedId, onResult, oButton) {
  150. var oDate = new Date();
  151. var $this = this;
  152. bx_loading_btn(oButton, true);
  153. $(oButton).addClass('bx-btn-disabled');
  154. $.get(
  155. this.sActionsUrl,
  156. {
  157. str_action: sAction,
  158. str_id: mixedId,
  159. _t:oDate.getTime()
  160. },
  161. function(oData) {
  162. if(typeof onResult == 'function')
  163. onResult(oData, oButton);
  164. else
  165. $this._onGetFile(oData, oButton);
  166. },
  167. 'json'
  168. );
  169. };
  170. BxDolStudioStore.prototype._onGetFile = function(oData, oButton) {
  171. bx_loading_btn(oButton, false);
  172. oButton = $(oButton);
  173. switch(parseInt(oData.code)) {
  174. case 1:
  175. oButton.removeClass('bx-btn-disabled');
  176. break;
  177. case 2:
  178. if(oButton.is("input[type = 'button']"))
  179. oButton.val(_t('_adm_btn_queued_submit'));
  180. else
  181. oButton.html(_t('_adm_btn_queued_submit'));
  182. break;
  183. }
  184. if(oData.message)
  185. this.showNotification(oData.message);
  186. if(oData.reload != undefined) {
  187. var iReload = parseInt(Number(oData.reload));
  188. if(iReload != oData.reload)
  189. document.location = document.location;
  190. else
  191. setTimeout(function() {
  192. document.location = document.location;
  193. }, iReload);
  194. }
  195. };
  196. BxDolStudioStore.prototype.info = function(sModuleName, oLink) {
  197. var oDate = new Date();
  198. var $this = this;
  199. bx_loading(this.sIdPageContent, true);
  200. $.get(
  201. this.sActionsUrl,
  202. {
  203. str_action: 'get-product',
  204. str_id: sModuleName,
  205. _t:oDate.getTime()
  206. },
  207. function(oData) {
  208. bx_loading($this.sIdPageContent, false);
  209. var sId = $this.sIdPopupProduct;
  210. if(oData.code == 0 && oData.popup.length > 0) {
  211. $('#' + sId).remove();
  212. $(oData.popup).appendTo('body').bxProcessHtml();
  213. $('#' + sId).dolPopup({
  214. onShow: function() {
  215. $this.initScreenshots(oData.screenshots);
  216. }
  217. });
  218. }
  219. else
  220. $this.showNotification(oData.message);
  221. },
  222. 'json'
  223. );
  224. };
  225. BxDolStudioStore.prototype.initScreenshots = function(iCount) {
  226. var iWidth = 202;
  227. var iPadding = 20;
  228. var iWidthOuter = iWidth + iPadding;
  229. var bBusy = false;
  230. $(".bx-std-pv-screenshots a[rel=group]").fancybox({
  231. type: 'image',
  232. transitionIn: 'elastic',
  233. transitionOut: 'elastic',
  234. speedIn: 600,
  235. speedOut: 200
  236. });
  237. if(iCount <= 2)
  238. return;
  239. $(".bx-std-pvs-left").bind('click', function() {
  240. if(bBusy || parseInt($(".bx-std-pvs-cnt").css('left')) >= 0)
  241. return;
  242. bBusy = true;
  243. $(".bx-std-pvs-cnt").animate({left: '+=' + iWidthOuter}, 500, function() {
  244. bBusy = false;
  245. });
  246. });
  247. $(".bx-std-pvs-right").bind('click', function() {
  248. if(bBusy || parseInt($(".bx-std-pvs-cnt").css('left')) <= 460 - $(".bx-std-pvs-cnt").width())
  249. return;
  250. bBusy = true;
  251. $(".bx-std-pvs-cnt").animate({left: '-=' + iWidthOuter}, 500, function() {
  252. bBusy = false;
  253. });
  254. });
  255. $(".bx-std-pv-screenshots").hover(function() {
  256. $(".bx-std-pvs-left, .bx-std-pvs-right").bx_anim('show', 'fade', 'fast');
  257. }, function() {
  258. $(".bx-std-pvs-left, .bx-std-pvs-right").bx_anim('hide', 'fade', 'fast');;
  259. });
  260. };
  261. BxDolStudioStore.prototype.install = function(sValue, oInput) {
  262. var $this = this;
  263. var onSuccess = function(oData) {
  264. $(oInput).parent('.bx-std-pc-buttons:first').hide(0, function() {
  265. $(this).siblings('.bx-std-pcb-installed:hidden').show(0);
  266. });
  267. };
  268. return this.perform('install', sValue, onSuccess, oInput);
  269. };
  270. BxDolStudioStore.prototype.update = function(sValue, oInput) {
  271. var $this = this;
  272. var onSuccess = function(oData) {
  273. $(oInput).parents('.bx-std-product:first').hide();
  274. };
  275. return this.perform('update', sValue, onSuccess, oInput);
  276. };
  277. BxDolStudioStore.prototype.remove = function(sValue, oInput) {
  278. var $this = this;
  279. bx_confirm('', function() {
  280. $this.perform('delete', sValue, function(oData) {
  281. switch(parseInt(oData.code)) {
  282. case 0:
  283. $(oInput).parents('.bx-std-product:first').hide();
  284. break;
  285. case 2:
  286. $(oInput).parent('.bx-std-pc-buttons:first').hide(0, function() {
  287. $(this).siblings('.bx-std-pcb-queued:hidden').show(0);
  288. });
  289. break;
  290. }
  291. }, oInput);
  292. });
  293. return false;
  294. };
  295. BxDolStudioStore.prototype.perform = function(sType, sValue, onSuccess, oInput) {
  296. var oDate = new Date();
  297. var $this = this;
  298. if(!sValue)
  299. return false;
  300. bx_loading_btn(oInput, true);
  301. $.post(
  302. this.sActionsUrl,
  303. {
  304. str_action: sType,
  305. str_value: sValue,
  306. _t:oDate.getTime()
  307. },
  308. function (oData) {
  309. bx_loading_btn(oInput, false);
  310. if(oData.message.length > 0)
  311. $this.showNotification(oData.message);
  312. switch(parseInt(oData.code)) {
  313. case 0:
  314. case 2:
  315. if(typeof onSuccess == 'function')
  316. onSuccess(oData);
  317. break;
  318. }
  319. },
  320. 'json'
  321. );
  322. };
  323. /**
  324. * Is needed if AJAX is used to change (reload) pages.
  325. */
  326. BxDolStudioStore.prototype.changePage = function(sType) {
  327. var oDate = new Date();
  328. var $this = this;
  329. $.get(
  330. this.sActionsUrl,
  331. {
  332. str_action: 'get-products-by-type',
  333. str_value: sType,
  334. _t:oDate.getTime()
  335. },
  336. function(oData) {
  337. if(oData.code != 0) {
  338. $this.showNotification(oData.message);
  339. return;
  340. }
  341. $('#bx-std-pc-menu > .bx-std-pmi-active').removeClass('bx-std-pmi-active');
  342. $('#bx-std-pmi-' + sType).addClass('bx-std-pmi-active');
  343. $('#' + $this.sIdPageContent).bx_anim('hide', $this.sAnimationEffect, $this.iAnimationSpeed, function() {
  344. $(this).html(oData.content).bx_anim('show', $this.sAnimationEffect, $this.iAnimationSpeed);
  345. });
  346. },
  347. 'json'
  348. );
  349. return true;
  350. };
  351. BxDolStudioStore.prototype.changePagePaginate = function(oButton, sType, mixedValue, iStart, iPerPage) {
  352. var oDate = new Date();
  353. var $this = this;
  354. bx_loading_btn(oButton, true);
  355. $.get(
  356. this.sActionsUrl,
  357. {
  358. str_action: 'get-products-by-page',
  359. str_type: sType,
  360. str_value: mixedValue,
  361. str_start: iStart,
  362. str_per_page: iPerPage,
  363. _t:oDate.getTime()
  364. },
  365. function(oData) {
  366. bx_loading_btn(oButton, false);
  367. if(oData.code != 0) {
  368. $this.showNotification(oData.message);
  369. return;
  370. }
  371. $(oButton).parents('.bx-std-block-content:first').bx_anim('hide', $this.sAnimationEffect, $this.iAnimationSpeed, function() {
  372. $(this).html(oData.content).bx_anim('show', $this.sAnimationEffect, $this.iAnimationSpeed);
  373. });
  374. },
  375. 'json'
  376. );
  377. return true;
  378. };
  379. BxDolStudioStore.prototype.showNotification = function(sContent) {
  380. $(sContent).appendTo('body').dolPopup({
  381. position: 'fixed',
  382. left: '1rem',
  383. top: 'auto',
  384. bottom: '1rem',
  385. fog: false,
  386. removeOnClose: true
  387. });
  388. };
  389. /**
  390. * It isn't used anywhere for now.
  391. */
  392. BxDolStudioStore.prototype.showPopup = function(sId, sContent, mixedPointer) {
  393. $('#' + sId).remove();
  394. $('<div id="' + sId + '" style="display: none;"></div>').appendTo('body').html(sContent);
  395. var oParams = {};
  396. if(mixedPointer)
  397. oParams.pointer = {
  398. el:$(mixedPointer)
  399. };
  400. $('#' + sId).dolPopup(oParams);
  401. };
  402. /** @} */