intervals.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup Events Events
  6. * @ingroup UnaModules
  7. *
  8. * @{
  9. */
  10. /**
  11. * Event intervals sub-form
  12. */
  13. function BxDolEventsIntervals (options) {
  14. this.init(options);
  15. this._eForm = null;
  16. };
  17. BxDolEventsIntervals.prototype.init = function (options) {
  18. this._sUniqId = options.uniq_id;
  19. this._sJsInstanceName = options.js_instance_name;
  20. this._sResultContainerId = 'bx-form-input-reoccurring-intervals-forms-' + this._sUniqId;
  21. this._sTemplateGhost = options.template_ghost ? options.template_ghost : '<div id="' + this._getIntervalContainerId('{interval_id}') + '"><input type="hidden" name="f[]" value="{interval_id}" />{interval_name} (<a href="javascript:void(0);" onclick="{js_instance_name}.deleteGhost(this, \'{interval_id}\')">delete</a>)</div>';
  22. this._sTemplateErrorGhosts = options.template_error ? options.template_error : '<div class="bx-informer-msg-error bx-def-padding-sec bx-def-round-corners bx-def-margin-sec-top">{error}</div>';
  23. this._iContentId = undefined == options.content_id || '' == options.content_id ? '' : parseInt(options.content_id);
  24. };
  25. BxDolEventsIntervals.prototype._getUrlWithStandardParams = function () {
  26. return sUrlRoot + 'modules/?r=events/intervals&uid=' + this._sUniqId;
  27. };
  28. BxDolEventsIntervals.prototype._getIntervalContainerId = function (iIntervalId) {
  29. return 'bx-events-interval-' + iIntervalId;
  30. };
  31. BxDolEventsIntervals.prototype.getCurrentIntervalsCount = function () {
  32. return $('#' + $this._sResultContainerId + ' .bx-events-interval-ghost').length;
  33. };
  34. BxDolEventsIntervals.prototype.addEmptyGhost = function () {
  35. var oVars = {
  36. js_instance_name: this._sJsInstanceName,
  37. file_id: 0,
  38. interval_id: 0,
  39. repeat_year: '',
  40. repeat_month: '',
  41. repeat_week_of_month: '',
  42. repeat_day_of_month: '',
  43. repeat_day_of_week: '',
  44. repeat_stop: ''
  45. }
  46. var sHTML = this._sTemplateGhost;
  47. for (var i in oVars)
  48. sHTML = sHTML.replace (new RegExp('{'+i+'}', 'g'), oVars[i]);
  49. $('#' + this._sResultContainerId).prepend(sHTML).addWebForms();
  50. };
  51. BxDolEventsIntervals.prototype.restoreGhosts = function () {
  52. var sUrl = this._getUrlWithStandardParams() + '&f=json' + '&a=restore&c=' + this._iContentId + '&_t=' + (new Date());
  53. var $this = this;
  54. bx_loading(this._sResultContainerId, true);
  55. $.getJSON(sUrl, function (aData) {
  56. bx_loading($this._sResultContainerId, false);
  57. if (typeof $this._sTemplateGhost == 'object') {
  58. $.each($this._sTemplateGhost, function(iIntervalId, sHTML) {
  59. var oIntervalsContainer = $('#' + $this._getIntervalContainerId(iIntervalId));
  60. if (oIntervalsContainer.length > 0)
  61. return;
  62. if ('object' === typeof(aData) && 'undefined' !== aData[iIntervalId])
  63. sHTML = $this._replaceVars(sHTML, aData[iIntervalId]);
  64. $('#' + $this._sResultContainerId).prepend(sHTML).addWebForms();
  65. });
  66. }
  67. if ('object' === typeof(aData)) {
  68. $.each(aData, function(iIntervalId, oVars) {
  69. var oIntervalsContainer = $('#' + $this._getIntervalContainerId(iIntervalId));
  70. if (oIntervalsContainer.length > 0)
  71. return;
  72. var sHTML;
  73. if (typeof $this._sTemplateGhost == 'object')
  74. sHTML = $this._sTemplateGhost[iIntervalId];
  75. else
  76. sHTML = $this._sTemplateGhost;
  77. if ("undefined" !== typeof(sHTML)) {
  78. sHTML = $this._replaceVars(sHTML, oVars);
  79. $('#' + $this._sResultContainerId).prepend(sHTML).addWebForms();
  80. }
  81. });
  82. }
  83. });
  84. };
  85. BxDolEventsIntervals.prototype.deleteGhost = function (e, iIntervalId) {
  86. if (0 == parseInt(iIntervalId)) {
  87. $(e).parents().filter('.bx-events-interval-ghost').slideUp(function () {
  88. $(this).remove();
  89. });
  90. }
  91. else {
  92. var sUrl = this._getUrlWithStandardParams() + '&a=delete&id=' + iIntervalId;
  93. var $this = this;
  94. var sIntervalContainerId = $this._getIntervalContainerId(iIntervalId);
  95. bx_loading(sIntervalContainerId, true);
  96. $.post(sUrl, function (sMsg) {
  97. bx_loading(sIntervalContainerId, false);
  98. if ('ok' == sMsg) {
  99. $('#' + sIntervalContainerId).slideUp('slow', function () {
  100. $(this).remove();
  101. });
  102. } else {
  103. $('#' + $this._sResultContainerId).prepend($this._sTemplateErrorGhosts.replace('{error}', sMsg));
  104. }
  105. });
  106. }
  107. };
  108. BxDolEventsIntervals.prototype._replaceVars = function (sHTML, oVars) {
  109. oVars = $.extend({}, oVars, {js_instance_name: this._sJsInstanceName});
  110. // replace vars
  111. for (var i in oVars)
  112. sHTML = sHTML.replace (new RegExp('{'+i+'}', 'g'), oVars[i]);
  113. // set selected values
  114. var oHTML = $(sHTML);
  115. for (var i in oVars)
  116. oHTML.find('select[name="' + i + '[]"] option[value="' + oVars[i] + '"]').attr('selected', 'selected');
  117. sHTML = $('<div></div>').append(oHTML).html();
  118. return sHTML;
  119. };
  120. /** @} */