timer.js 6.8 KB

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