common.ui.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * Compute work days.
  3. *
  4. * @access public
  5. * @return void
  6. */
  7. window.computeWorkDays = function(currentID)
  8. {
  9. isBatchEdit = false;
  10. if(currentID && typeof currentID != 'object')
  11. {
  12. index = currentID.replace(/[a-zA-Z]*\[|\]/g, '');
  13. if(!isNaN(index)) isBatchEdit = true;
  14. }
  15. let beginDate, endDate;
  16. if(isBatchEdit)
  17. {
  18. beginDate = $("input[name=begin\\[" + index + "\\]]").val();
  19. endDate = $("input[name=end\\[" + index + "\\]]").val();
  20. }
  21. else
  22. {
  23. beginDate = $('input[name=begin]').val();
  24. endDate = $('input[name=end]').val();
  25. }
  26. if(beginDate && endDate)
  27. {
  28. if(isBatchEdit) $("input[name=days\\[" + index + "\\]]").val(computeDaysDelta(beginDate, endDate));
  29. if(!isBatchEdit) $('[name=days]').val(computeDaysDelta(beginDate, endDate));
  30. }
  31. else if($('input[checked="true"]').val())
  32. {
  33. computeEndDate();
  34. }
  35. }
  36. /**
  37. * Compute the end date for project.
  38. *
  39. * @param int $delta
  40. * @access public
  41. * @return void
  42. */
  43. function computeEndDate()
  44. {
  45. let delta = $('input[name^=delta]:checked').val();
  46. let beginDate = $('input[name=begin]').val();
  47. if(!beginDate) return;
  48. delta = currentDelta = parseInt(delta);
  49. beginDate = convertStringToDate(beginDate);
  50. if((delta == 7 || delta == 14) && (beginDate.getDay() == 1))
  51. {
  52. delta = (weekend == 2) ? (delta - 2) : (delta - 1);
  53. }
  54. let endDate = formatDate(beginDate, delta - 1);
  55. $('input[name=end]').zui('datePicker').$.setValue(endDate);
  56. computeWorkDays();
  57. setTimeout(function(){$('[name=delta]').val(`${currentDelta}`)}, 0);
  58. }
  59. /**
  60. * 给指定日期加上具体天数,并返回格式化后的日期.
  61. *
  62. * @param string dateString
  63. * @param int days
  64. * @access public
  65. * @return date
  66. */
  67. function formatDate(dateString, days)
  68. {
  69. const date = new Date(dateString);
  70. date.setDate(date.getDate() + days);
  71. const year = date.getFullYear();
  72. const month = String(date.getMonth() + 1).padStart(2, '0');
  73. const day = String(date.getDate()).padStart(2, '0');
  74. return `${year}-${month}-${day}`;
  75. }
  76. /**
  77. * Convert a date string like 2011-11-11 to date object in js.
  78. *
  79. * @param string $date
  80. * @access public
  81. * @return date
  82. */
  83. function convertStringToDate(dateString)
  84. {
  85. dateString = dateString.split('-');
  86. return new Date(dateString[0], dateString[1] - 1, dateString[2]);
  87. }
  88. /**
  89. * Compute delta of two days.
  90. *
  91. * @param string $date1
  92. * @param string $date2
  93. * @access public
  94. * @return int
  95. */
  96. function computeDaysDelta(date1, date2)
  97. {
  98. date1 = convertStringToDate(date1);
  99. date2 = convertStringToDate(date2);
  100. delta = (date2 - date1) / (1000 * 60 * 60 * 24) + 1;
  101. let weekEnds = 0;
  102. for(i = 0; i < delta; i++)
  103. {
  104. if((weekend == 2 && date1.getDay() == 6) || date1.getDay() == 0) weekEnds ++;
  105. date1 = date1.valueOf();
  106. date1 += 1000 * 60 * 60 * 24;
  107. date1 = new Date(date1);
  108. }
  109. return delta - weekEnds;
  110. }
  111. /**
  112. * Hide plan box by stage's attribute.
  113. *
  114. * @param string attribute
  115. * @access public
  116. * @return void
  117. */
  118. function hidePlanBox(attribute)
  119. {
  120. if(attribute == 'request' || attribute == 'review')
  121. {
  122. $('.productsBox .planBox').addClass('hidden');
  123. $('.productsBox .linkProduct').removeClass('w-1/2').removeClass('w-1/4').addClass('w-full');
  124. }
  125. }
  126. /**
  127. * Set white.
  128. *
  129. * @param string $acl
  130. * @access public
  131. * @return void
  132. */
  133. function setWhite()
  134. {
  135. const acl = $("[name^='acl']:checked").val();
  136. acl != 'open' ? $('#whitelistBox').removeClass('hidden') : $('#whitelistBox').addClass('hidden');
  137. }
  138. /**
  139. * Show lifetime tips.
  140. *
  141. * @access public
  142. * @return void
  143. */
  144. function showLifeTimeTips()
  145. {
  146. const lifetime = $('#lifetime').val();
  147. if(lifetime == 'ops')
  148. {
  149. $('#lifeTimeTips').removeClass('hidden');
  150. }
  151. else
  152. {
  153. $('#lifeTimeTips').addClass('hidden');
  154. }
  155. }
  156. /**
  157. * 提示并删除执行。
  158. * Delete execution with tips.
  159. *
  160. * @param int executionID
  161. * @param string executionName
  162. * @access public
  163. * @return void
  164. */
  165. window.confirmDeleteExecution = function(executionID, confirmDeleteTip)
  166. {
  167. zui.Modal.confirm({message: confirmDeleteTip, icon:'icon-exclamation-sign', iconClass: 'warning-pale rounded-full icon-2x'}).then((res) =>
  168. {
  169. if(res) $.ajaxSubmit({url: $.createLink('execution', 'delete', 'executionID=' + executionID + '&comfirm=yes')});
  170. });
  171. }