store.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. switch(parseInt(oData.code)) {
  173. case 1:
  174. $(oButton).removeClass('bx-btn-disabled');
  175. break;
  176. case 2:
  177. $(oButton).val(_t('_adm_btn_queued_submit'));
  178. break;
  179. }
  180. if(oData.message)
  181. this.showNotification(oData.message);
  182. };
  183. BxDolStudioStore.prototype.info = function(sModuleName, oLink) {
  184. var oDate = new Date();
  185. var $this = this;
  186. bx_loading(this.sIdPageContent, true);
  187. $.get(
  188. this.sActionsUrl,
  189. {
  190. str_action: 'get-product',
  191. str_id: sModuleName,
  192. _t:oDate.getTime()
  193. },
  194. function(oData) {
  195. bx_loading($this.sIdPageContent, false);
  196. var sId = $this.sIdPopupProduct;
  197. if(oData.code == 0 && oData.popup.length > 0) {
  198. $('#' + sId).remove();
  199. $(oData.popup).appendTo('body').bxProcessHtml();
  200. $('#' + sId).dolPopup({
  201. onShow: function() {
  202. $this.initScreenshots(oData.screenshots);
  203. }
  204. });
  205. }
  206. else
  207. $this.showNotification(oData.message);
  208. },
  209. 'json'
  210. );
  211. };
  212. BxDolStudioStore.prototype.initScreenshots = function(iCount) {
  213. var iWidth = 202;
  214. var iPadding = 20;
  215. var iWidthOuter = iWidth + iPadding;
  216. var bBusy = false;
  217. $(".bx-std-pv-screenshots a[rel=group]").fancybox({
  218. transitionIn: 'elastic',
  219. transitionOut: 'elastic',
  220. speedIn: 600,
  221. speedOut: 200
  222. });
  223. if(iCount <= 2)
  224. return;
  225. $(".bx-std-pvs-left").bind('click', function() {
  226. if(bBusy || parseInt($(".bx-std-pvs-cnt").css('left')) >= 0)
  227. return;
  228. bBusy = true;
  229. $(".bx-std-pvs-cnt").animate({left: '+=' + iWidthOuter}, 500, function() {
  230. bBusy = false;
  231. });
  232. });
  233. $(".bx-std-pvs-right").bind('click', function() {
  234. if(bBusy || parseInt($(".bx-std-pvs-cnt").css('left')) <= 460 - $(".bx-std-pvs-cnt").width())
  235. return;
  236. bBusy = true;
  237. $(".bx-std-pvs-cnt").animate({left: '-=' + iWidthOuter}, 500, function() {
  238. bBusy = false;
  239. });
  240. });
  241. $(".bx-std-pv-screenshots").hover(function() {
  242. $(".bx-std-pvs-left, .bx-std-pvs-right").bx_anim('show', 'fade', 'fast');
  243. }, function() {
  244. $(".bx-std-pvs-left, .bx-std-pvs-right").bx_anim('hide', 'fade', 'fast');;
  245. });
  246. };
  247. BxDolStudioStore.prototype.install = function(sValue, oInput) {
  248. var $this = this;
  249. var onSuccess = function(oData) {
  250. $(oInput).parent('.bx-std-pc-buttons:first').hide(0, function() {
  251. $(this).siblings('.bx-std-pcb-installed:hidden').show(0);
  252. });
  253. };
  254. return this.perform('install', sValue, onSuccess, oInput);
  255. };
  256. BxDolStudioStore.prototype.update = function(sValue, oInput) {
  257. var $this = this;
  258. var onSuccess = function(oData) {
  259. $(oInput).parents('.bx-std-product:first').hide();
  260. };
  261. return this.perform('update', sValue, onSuccess, oInput);
  262. };
  263. BxDolStudioStore.prototype.remove = function(sValue, oInput) {
  264. var $this = this;
  265. bx_confirm('', function() {
  266. $this.perform('delete', sValue, function(oData) {
  267. switch(parseInt(oData.code)) {
  268. case 0:
  269. $(oInput).parents('.bx-std-product:first').hide();
  270. break;
  271. case 2:
  272. $(oInput).parent('.bx-std-pc-buttons:first').hide(0, function() {
  273. $(this).siblings('.bx-std-pcb-queued:hidden').show(0);
  274. });
  275. break;
  276. }
  277. }, oInput);
  278. });
  279. return false;
  280. };
  281. BxDolStudioStore.prototype.perform = function(sType, sValue, onSuccess, oInput) {
  282. var oDate = new Date();
  283. var $this = this;
  284. if(!sValue)
  285. return false;
  286. bx_loading_btn(oInput, true);
  287. $.post(
  288. this.sActionsUrl,
  289. {
  290. str_action: sType,
  291. str_value: sValue,
  292. _t:oDate.getTime()
  293. },
  294. function (oData) {
  295. bx_loading_btn(oInput, false);
  296. if(oData.message.length > 0)
  297. $this.showNotification(oData.message);
  298. switch(parseInt(oData.code)) {
  299. case 0:
  300. case 2:
  301. if(typeof onSuccess == 'function')
  302. onSuccess(oData);
  303. break;
  304. }
  305. },
  306. 'json'
  307. );
  308. };
  309. /**
  310. * Is needed if AJAX is used to change (reload) pages.
  311. */
  312. BxDolStudioStore.prototype.changePage = function(sType) {
  313. var oDate = new Date();
  314. var $this = this;
  315. $.get(
  316. this.sActionsUrl,
  317. {
  318. str_action: 'get-products-by-type',
  319. str_value: sType,
  320. _t:oDate.getTime()
  321. },
  322. function(oData) {
  323. if(oData.code != 0) {
  324. $this.showNotification(oData.message);
  325. return;
  326. }
  327. $('#bx-std-pc-menu > .bx-std-pmi-active').removeClass('bx-std-pmi-active');
  328. $('#bx-std-pmi-' + sType).addClass('bx-std-pmi-active');
  329. $('#' + $this.sIdPageContent).bx_anim('hide', $this.sAnimationEffect, $this.iAnimationSpeed, function() {
  330. $(this).html(oData.content).bx_anim('show', $this.sAnimationEffect, $this.iAnimationSpeed);
  331. });
  332. },
  333. 'json'
  334. );
  335. return true;
  336. };
  337. BxDolStudioStore.prototype.changePagePaginate = function(oButton, sType, mixedValue, iStart, iPerPage) {
  338. var oDate = new Date();
  339. var $this = this;
  340. $.get(
  341. this.sActionsUrl,
  342. {
  343. str_action: 'get-products-by-page',
  344. str_type: sType,
  345. str_value: mixedValue,
  346. str_start: iStart,
  347. str_per_page: iPerPage,
  348. _t:oDate.getTime()
  349. },
  350. function(oData) {
  351. if(oData.code != 0) {
  352. $this.showNotification(oData.message);
  353. return;
  354. }
  355. $(oButton).parents('.bx-std-block-content:first').bx_anim('hide', $this.sAnimationEffect, $this.iAnimationSpeed, function() {
  356. $(this).html(oData.content).bx_anim('show', $this.sAnimationEffect, $this.iAnimationSpeed);
  357. });
  358. },
  359. 'json'
  360. );
  361. return true;
  362. };
  363. BxDolStudioStore.prototype.showNotification = function(sContent) {
  364. $(sContent).appendTo('body').dolPopup({
  365. position: 'fixed',
  366. left: '1rem',
  367. top: 'auto',
  368. bottom: '1rem',
  369. fog: false,
  370. removeOnClose: true
  371. });
  372. };
  373. /**
  374. * It isn't used anywhere for now.
  375. */
  376. BxDolStudioStore.prototype.showPopup = function(sId, sContent, mixedPointer) {
  377. $('#' + sId).remove();
  378. $('<div id="' + sId + '" style="display: none;"></div>').appendTo('body').html(sContent);
  379. var oParams = {};
  380. if(mixedPointer)
  381. oParams.pointer = {
  382. el:$(mixedPointer)
  383. };
  384. $('#' + sId).dolPopup(oParams);
  385. };
  386. /** @} */