timer.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. * @defgroup Tasks Tasks
  5. * @ingroup UnaModules
  6. *
  7. * @{
  8. */
  9. function BxTasksTimer(oOptions) {
  10. this._sActionsUrl = oOptions.sActionUrl;
  11. this._sObjName = oOptions.sObjName == undefined ? 'oBxTasksTimer' : oOptions.sObjName;
  12. this._sAnimationEffect = oOptions.sAnimationEffect == undefined ? 'fade' : oOptions.sAnimationEffect;
  13. this._iAnimationSpeed = oOptions.iAnimationSpeed == undefined ? 'slow' : oOptions.iAnimationSpeed;
  14. this._aHtmlIds = oOptions.aHtmlIds == undefined ? {} : oOptions.aHtmlIds;
  15. this._oRequestParams = oOptions.oRequestParams == undefined ? {} : oOptions.oRequestParams;
  16. this._bMulti = oOptions.bMulti == undefined ? false : oOptions.bMulti;
  17. this._aTimers = [];
  18. }
  19. BxTasksTimer.prototype.init = function (iContentId, iProfileId, bStarted) {
  20. var $this = this;
  21. $(document).ready(function () {
  22. if(bStarted)
  23. $this._setTimer(iContentId, iProfileId);
  24. });
  25. };
  26. BxTasksTimer.prototype.start = function (oSource, iContentId, iProfileId) {
  27. if(this._bMulti)
  28. this.pauseAll();
  29. this._performAction(oSource, 'start', iContentId, iProfileId);
  30. };
  31. BxTasksTimer.prototype.pause = function (oSource, iContentId, iProfileId) {
  32. this._clearTimer(iContentId, iProfileId);
  33. this._performAction(oSource, 'pause', iContentId, iProfileId);
  34. };
  35. BxTasksTimer.prototype.pauseAll = function () {
  36. if(!this._aTimers)
  37. return;
  38. for(var sKey in this._aTimers) {
  39. var aKey = sKey.split('-');
  40. if(!aKey || aKey.length != 2)
  41. continue;
  42. var oTimer = $('#' + this._aHtmlIds['timer'] + aKey[0] + '-' + aKey[1]);
  43. if(!oTimer || !oTimer.length)
  44. continue;
  45. this.pause(oTimer.get(0), aKey[0],aKey[1]);
  46. }
  47. };
  48. BxTasksTimer.prototype.resume = function (oSource, iContentId, iProfileId) {
  49. if(this._bMulti)
  50. this.pauseAll();
  51. this._performAction(oSource, 'resume', iContentId, iProfileId);
  52. };
  53. BxTasksTimer.prototype.reload = function (oSource, iContentId, iProfileId) {
  54. this._clearTimer(iContentId, iProfileId);
  55. this._performAction(oSource, 'reload', iContentId, iProfileId);
  56. };
  57. BxTasksTimer.prototype.log = function (oSource, iContentId, iProfileId) {
  58. this._clearTimer(iContentId, iProfileId);
  59. this._performAction(oSource, 'log', iContentId, iProfileId);
  60. };
  61. BxTasksTimer.prototype.clear = function (oSource, iContentId, iProfileId) {
  62. var $this = this;
  63. bx_confirm('', function() {
  64. $this._clearTimer(iContentId, iProfileId);
  65. $this._performAction(oSource, 'clear', iContentId, iProfileId);
  66. });
  67. };
  68. BxTasksTimer.prototype.onPerformActionStart = function(oData) {
  69. this._onPerformActionAndSet(oData);
  70. };
  71. BxTasksTimer.prototype.onPerformActionPause = function(oData) {
  72. this._onPerformAction(oData);
  73. };
  74. BxTasksTimer.prototype.onPerformActionResume = function(oData) {
  75. this._onPerformActionAndSet(oData);
  76. };
  77. BxTasksTimer.prototype.onPerformActionReload = function(oData) {
  78. if(oData && oData.started)
  79. this._onPerformActionAndSet(oData);
  80. else
  81. this._onPerformAction(oData);
  82. };
  83. BxTasksTimer.prototype.onPerformActionLog = function(oData) {
  84. this._onPerformAction(oData);
  85. };
  86. BxTasksTimer.prototype.onPerformActionClear = function(oData) {
  87. this._onPerformAction(oData);
  88. };
  89. BxTasksTimer.prototype.loadingInButton = function(e, bShow) {
  90. if($(e).length)
  91. bx_loading_btn($(e), bShow);
  92. else
  93. bx_loading($('body'), bShow);
  94. };
  95. BxTasksTimer.prototype.loadingInTimer = function(e, bShow) {
  96. var oParent = $(e).length ? $(e).parents('.bx-tasks-timer:first') : $('body');
  97. bx_loading(oParent, bShow);
  98. };
  99. BxTasksTimer.prototype.loadingInBlock = function(e, bShow) {
  100. var oParent = $(e).length ? $(e).parents('.bx-db-container:first') : $('body');
  101. bx_loading(oParent, bShow);
  102. };
  103. BxTasksTimer.prototype._performAction = function (oSource, sAction, iContentId, iProfileId) {
  104. var $this = this;
  105. var oDate = new Date();
  106. this.loadingInTimer(oSource, true);
  107. $.get(
  108. this._sActionsUrl + 'process_timer/' + sAction + '/' + iContentId + '/' + iProfileId,
  109. {
  110. _t: oDate.getTime()
  111. },
  112. function(oData) {
  113. $this.loadingInTimer(oSource, false);
  114. processJsonData(oData);
  115. },
  116. 'json'
  117. );
  118. return false;
  119. };
  120. BxTasksTimer.prototype._onPerformActionAndSet = function(oData) {
  121. if(oData && oData.content != undefined) {
  122. var sTimer = '#' + this._aHtmlIds['timer'] + oData.content_id + '-' + oData.profile_id;
  123. $(sTimer).replaceWith(oData.content);
  124. this._setTimer(oData.content_id, oData.profile_id, $(sTimer));
  125. }
  126. };
  127. BxTasksTimer.prototype._onPerformAction = function(oData) {
  128. if(oData && oData.content != undefined)
  129. $('#' + this._aHtmlIds['timer'] + oData.content_id + '-' + oData.profile_id).replaceWith(oData.content);
  130. };
  131. BxTasksTimer.prototype._setTimer = function(iContentId, iProfileId, oTimer) {
  132. var $this = this;
  133. this._clearTimer(iContentId, iProfileId);
  134. if(!oTimer)
  135. oTimer = $('#' + this._aHtmlIds['timer'] + iContentId + '-' + iProfileId);
  136. this._aTimers[iContentId + '-' + iProfileId] = setInterval(function() {
  137. var oH = oTimer.find('.bx-tasks-th');
  138. var oM = oTimer.find('.bx-tasks-tm');
  139. var oS = oTimer.find('.bx-tasks-ts');
  140. var iS = parseInt(oS.html()) + 1;
  141. if(iS == 60) {
  142. iS = 0;
  143. var iM = parseInt(oM.html()) + 1;
  144. if(iM == 60) {
  145. iM = 0;
  146. oH.html($this._padDigits(parseInt(oH.html()) + 1, 2));
  147. }
  148. oM.html($this._padDigits(iM, 2));
  149. }
  150. oS.html($this._padDigits(iS, 2));
  151. }, 1000);
  152. };
  153. BxTasksTimer.prototype._clearTimer = function(iContentId, iProfileId) {
  154. var sTimer = iContentId + '-' + iProfileId;
  155. if(!this._aTimers[sTimer])
  156. return;
  157. clearInterval(this._aTimers[sTimer]);
  158. this._aTimers = this._aTimers.splice(sTimer, 1);
  159. };
  160. BxTasksTimer.prototype._clearTimers = function() {
  161. if(!this._aTimers)
  162. return;
  163. for(var sKey in this._aTimers)
  164. clearInterval(this._aTimers[sKey]);
  165. this._aTimers = [];
  166. };
  167. BxTasksTimer.prototype._padDigits = function(iNumber, iDigits) {
  168. return Array(Math.max(iDigits - String(iNumber).length + 1, 0)).join(0) + iNumber;
  169. };