common.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. function switchStatus(projectID, status)
  2. {
  3. if(status) location.href = createLink('project', 'task', 'project=' + projectID + '&type=' + status);
  4. }
  5. function setWhite(acl)
  6. {
  7. acl != 'open' ? $('#whitelistBox').removeClass('hidden') : $('#whitelistBox').addClass('hidden');
  8. }
  9. function switchGroup(projectID, groupBy)
  10. {
  11. link = createLink('project', 'groupTask', 'project=' + projectID + '&groupBy=' + groupBy);
  12. location.href=link;
  13. }
  14. /**
  15. * Convert a date string like 2011-11-11 to date object in js.
  16. *
  17. * @param string $date
  18. * @access public
  19. * @return date
  20. */
  21. function convertStringToDate(dateString)
  22. {
  23. dateString = dateString.split('-');
  24. return new Date(dateString[0], dateString[1] - 1, dateString[2]);
  25. }
  26. /**
  27. * Compute delta of two days.
  28. *
  29. * @param string $date1
  30. * @param string $date1
  31. * @access public
  32. * @return int
  33. */
  34. function computeDaysDelta(date1, date2)
  35. {
  36. date1 = convertStringToDate(date1);
  37. date2 = convertStringToDate(date2);
  38. delta = (date2 - date1) / (1000 * 60 * 60 * 24) + 1;
  39. weekEnds = 0;
  40. for(i = 0; i < delta; i++)
  41. {
  42. if((weekend == 2 && date1.getDay() == 6) || date1.getDay() == 0) weekEnds ++;
  43. date1 = date1.valueOf();
  44. date1 += 1000 * 60 * 60 * 24;
  45. date1 = new Date(date1);
  46. }
  47. return delta - weekEnds;
  48. }
  49. /**
  50. * Compute work days.
  51. *
  52. * @access public
  53. * @return void
  54. */
  55. function computeWorkDays(currentID)
  56. {
  57. isBactchEdit = false;
  58. if(currentID)
  59. {
  60. index = currentID.replace(/\w*\[|\]/g, '');
  61. if(!isNaN(index)) isBactchEdit = true;
  62. }
  63. if(isBactchEdit)
  64. {
  65. beginDate = $('#begins\\[' + index + '\\]').val();
  66. endDate = $('#ends\\[' + index + '\\]').val();
  67. }
  68. else
  69. {
  70. beginDate = $('#begin').val();
  71. endDate = $('#end').val();
  72. }
  73. if(beginDate && endDate)
  74. {
  75. if(isBactchEdit) $('#dayses\\[' + index + '\\]').val(computeDaysDelta(beginDate, endDate));
  76. if(!isBactchEdit) $('#days').val(computeDaysDelta(beginDate, endDate));
  77. }
  78. else if($('input[checked="true"]').val())
  79. {
  80. computeEndDate();
  81. }
  82. }
  83. /**
  84. * Compute the end date for project.
  85. *
  86. * @param int $delta
  87. * @access public
  88. * @return void
  89. */
  90. function computeEndDate(delta)
  91. {
  92. beginDate = $('#begin').val();
  93. if(!beginDate) return;
  94. delta = parseInt(delta);
  95. beginDate = convertStringToDate(beginDate);
  96. if((delta == 7 || delta == 14) && (beginDate.getDay() == 1))
  97. {
  98. delta = (weekend == 2) ? (delta - 2) : (delta - 1);
  99. }
  100. endDate = $.zui.formatDate(beginDate.addDays(delta - 1), 'yyyy-MM-dd');
  101. $('#end').val(endDate).datetimepicker('update');
  102. computeWorkDays();
  103. }
  104. /* Auto compute the work days. */
  105. $(function()
  106. {
  107. $(".date").bind('dateSelected', function()
  108. {
  109. computeWorkDays(this.id);
  110. })
  111. });
  112. /**
  113. * Load branches.
  114. *
  115. * @param int $product
  116. * @access public
  117. * @return void
  118. */
  119. function loadBranches(product)
  120. {
  121. /* When selecting a product, delete a plan that is empty by default. */
  122. $("#planDefault").remove();
  123. $(".productsBox select[name^='products']").each(function()
  124. {
  125. var $product = $(product);
  126. var productID = $(this).val();
  127. if($product.val() != 0 && $product.val() == $(this).val() && $product.attr('id') != $(this).attr('id') && !multiBranchProducts[$product.val()])
  128. {
  129. bootbox.alert(errorSameProducts);
  130. $product.val(0);
  131. $product.trigger("chosen:updated");
  132. return false;
  133. }
  134. });
  135. var $tableRow = $(product).closest('.table-row');
  136. var index = $tableRow.find('select:first').attr('id').replace('products' , '');
  137. var oldBranch = $(product).attr('data-branch') !== undefined ? $(product).attr('data-branch') : 0;
  138. if($(product).val() != 0)
  139. {
  140. $(product).closest('tr').find('.newProduct').addClass('hidden')
  141. }
  142. else
  143. {
  144. $(product).closest('tr').find('.newProduct').removeClass('hidden')
  145. }
  146. if(!multiBranchProducts[$(product).val()])
  147. {
  148. $tableRow.find('.table-col:last select').val('').trigger('chosen:updated');
  149. $tableRow.find('.table-col:last').addClass('hidden');
  150. }
  151. $.get(createLink('branch', 'ajaxGetBranches', "productID=" + $(product).val() + "&oldBranch=" + oldBranch + "&param=active&projectID=" + projectID + "&withMainBranch=true"), function(data)
  152. {
  153. if(data)
  154. {
  155. $tableRow.find("select[name^='branch']").replaceWith(data);
  156. $tableRow.find('.table-col:last .chosen-container').remove();
  157. $tableRow.find('.table-col:last').removeClass('hidden');
  158. $tableRow.find("select[name^='branch']").attr('multiple', '').attr('name', 'branch[' + index + '][]').attr('id', 'branch' + index).attr('onchange', "loadPlans('#products" + index + "', this)").chosen();
  159. disableSelectedProduct();
  160. }
  161. if(typeof isStage != 'undefined' && isStage == true)
  162. {
  163. $tableRow.find("select[name^='branch'] option").attr('selected', 'selected');
  164. $tableRow.find("select[name^='branch']").trigger('chosen:updated');
  165. $tableRow.find("div[id^='branch']").addClass('chosen-disabled');
  166. }
  167. });
  168. var branch = $('#branch' + index);
  169. loadPlans(product, branch);
  170. }
  171. /**
  172. * Load plans.
  173. *
  174. * @param obj $product
  175. * @param obj $branchID
  176. * @access public
  177. * @return void
  178. */
  179. function loadPlans(product, branch)
  180. {
  181. var productID = $(product).val();
  182. var branchID = $(branch).val() == null ? 0 : '0,' + $(branch).val();
  183. var planID = $(product).attr('data-plan') !== undefined ? $(product).attr('data-plan') : 0;
  184. var index = $(product).attr('id').replace('products', '');
  185. $.get(createLink('product', 'ajaxGetPlans', "productID=" + productID + '&branch=' + branchID + '&planID=' + planID + '&fieldID&needCreate=&expired=unexpired,noclosed&param=skipParent,multiple'), function(data)
  186. {
  187. if(data)
  188. {
  189. $("div#plan" + index).find("select[name^='plans']").replaceWith(data);
  190. $("div#plan" + index).find('.chosen-container').remove();
  191. $("div#plan" + index).find('select').attr('name', 'plans[' + productID + ']' + '[]').attr('id', 'plans' + productID).chosen();
  192. }
  193. });
  194. }
  195. /**
  196. * Add new line for link product.
  197. *
  198. * @param obj $obj
  199. * @access public
  200. * @return void
  201. */
  202. function addNewLine(obj)
  203. {
  204. var newLine = $(obj).closest('tr').clone();
  205. var index = 0;
  206. $(".productsBox select[name^='products']").each(function()
  207. {
  208. var id = $(this).attr('id').replace('products' , '');
  209. id = parseInt(id);
  210. id ++;
  211. index = id > index ? id : index;
  212. })
  213. newLine.find('.newProduct').remove();
  214. newLine.find('.addProduct').remove();
  215. newLine.addClass('newLine');
  216. newLine.find('th').html('');
  217. newLine.find('.removeLine').css('visibility', 'visible');
  218. newLine.find('.chosen-container').remove();
  219. newLine.find('.productsBox .table-col:last').addClass('hidden');
  220. newLine.find("select[name^='products']").attr('name', 'products[' + index + ']').attr('id', 'products' + index).val('').chosen();
  221. newLine.find("select[name^='plans']").attr('name', 'plans[' + index + '][' + 0 + '][]').chosen();
  222. newLine.find("div[id^='plan']").attr('id', 'plan' + index);
  223. $(obj).closest('tr').after(newLine);
  224. var product = newLine.find("select[name^='products']");
  225. var branch = newLine.find("select[name^='branch']");
  226. loadPlans(product, branch);
  227. disableSelectedProduct();
  228. }
  229. function removeLine(obj)
  230. {
  231. $(obj).closest('tr').remove();
  232. disableSelectedProduct();
  233. }
  234. $(function()
  235. {
  236. $(document).on('click', '.task-toggle', function(e)
  237. {
  238. var $toggle = $(this);
  239. var id = $(this).data('id');
  240. var isCollapsed = $toggle.toggleClass('collapsed').hasClass('collapsed');
  241. $toggle.closest('[data-ride="table"]').find('tr.parent-' + id).toggle(!isCollapsed);
  242. e.stopPropagation();
  243. e.preventDefault();
  244. });
  245. });
  246. /**
  247. * Set card count.
  248. *
  249. * @param string $heightType
  250. * @access public
  251. * @return void
  252. */
  253. function setCardCount(heightType)
  254. {
  255. heightType != 'custom' ? $('#cardBox').addClass('hidden') : $('#cardBox').removeClass('hidden');
  256. }
  257. /**
  258. * Hide plan box by stage's attribute.
  259. *
  260. * @param string attribute
  261. * @access public
  262. * @return void
  263. */
  264. function hidePlanBox(attribute)
  265. {
  266. if(attribute == 'request' || attribute == 'review')
  267. {
  268. $('.productsBox .planBox').addClass('hide');
  269. $('.productsBox .planBox select').attr('disabled', 'disabled');
  270. $('#productTitle').text(manageProductsLang);
  271. $('#plansBox').closest('tr').addClass('hide');
  272. $('#plansBox').attr('disabled', 'disabled');
  273. }
  274. else
  275. {
  276. $('.productsBox .planBox').removeClass('hide');
  277. $('.productsBox .planBox select').attr('disabled', '');
  278. $('#productTitle').text(manageProductPlanLang);
  279. $('#plansBox').closest('tr').removeClass('hide');
  280. $('#plansBox').attr('disabled', '');
  281. }
  282. }