common.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. window.ignoreTips = {
  2. 'beyondBudgetTip' : false,
  3. 'dateTip' : false
  4. };
  5. /**
  6. * Access rights are equal to private, and the white list settings are displayed.
  7. *
  8. * @param string acl
  9. * @access public
  10. * @return void
  11. */
  12. function setWhite(acl)
  13. {
  14. acl != 'open' ? $('#whitelistBox').removeClass('hidden') : $('#whitelistBox').addClass('hidden');
  15. }
  16. /**
  17. * Convert a date string like 2011-11-11 to date object in js.
  18. *
  19. * @param string dateString
  20. * @access public
  21. * @return date
  22. */
  23. function convertStringToDate(dateString)
  24. {
  25. dateString = dateString.split('-');
  26. return new Date(dateString[0], dateString[1] - 1, dateString[2]);
  27. }
  28. /**
  29. * Compute delta of two days.
  30. *
  31. * @param string date1
  32. * @param string date2
  33. * @access public
  34. * @return int
  35. */
  36. function computeDaysDelta(date1, date2)
  37. {
  38. date1 = convertStringToDate(date1);
  39. date2 = convertStringToDate(date2);
  40. delta = (date2 - date1) / (1000 * 60 * 60 * 24) + 1;
  41. weekEnds = 0;
  42. for(i = 0; i < delta; i++)
  43. {
  44. if((weekend == 2 && date1.getDay() == 6) || date1.getDay() == 0) weekEnds ++;
  45. date1 = date1.valueOf();
  46. date1 += 1000 * 60 * 60 * 24;
  47. date1 = new Date(date1);
  48. }
  49. return delta - weekEnds;
  50. }
  51. /**
  52. * Compute work days.
  53. *
  54. * @param string currentID
  55. * @access public
  56. * @return void
  57. */
  58. function computeWorkDays(currentID)
  59. {
  60. isBactchEdit = false;
  61. if(currentID)
  62. {
  63. index = currentID.replace('begins[', '');
  64. index = index.replace('ends[', '');
  65. index = index.replace(']', '');
  66. if(!isNaN(index)) isBactchEdit = true;
  67. }
  68. if(isBactchEdit)
  69. {
  70. beginDate = $('#begins\\[' + index + '\\]').val();
  71. endDate = $('#ends\\[' + index + '\\]').val();
  72. }
  73. else
  74. {
  75. beginDate = $('#begin').val();
  76. endDate = $('#end').val();
  77. var begin = new Date(beginDate.replace(/-/g,"/"));
  78. var end = new Date(endDate.replace(/-/g,"/"));
  79. var time = end.getTime() - begin.getTime();
  80. var days = parseInt(time / (1000 * 60 * 60 * 24)) + 1;
  81. if(days != $("input:radio[name='delta']:checked").val()) $("input:radio[name='delta']:checked").attr('checked', false);
  82. if(endDate == longTime) $("#delta999").prop('checked', true);
  83. }
  84. if(beginDate && endDate)
  85. {
  86. if(isBactchEdit) $('#dayses\\[' + index + '\\]').val(computeDaysDelta(beginDate, endDate));
  87. if(!isBactchEdit) $('#days').val(computeDaysDelta(beginDate, endDate));
  88. }
  89. else if($('input[checked="true"]').val())
  90. {
  91. computeEndDate();
  92. }
  93. outOfDateTip();
  94. }
  95. /**
  96. * Compute the end date for project.
  97. *
  98. * @param int $delta
  99. * @access public
  100. * @return void
  101. */
  102. function computeEndDate(delta)
  103. {
  104. beginDate = $('#begin').val();
  105. if(!beginDate) return;
  106. delta = parseInt(delta);
  107. if(delta == 999)
  108. {
  109. $('#end').val(longTime);
  110. outOfDateTip();
  111. return false;
  112. }
  113. beginDate = convertStringToDate(beginDate);
  114. if((delta == 7 || delta == 14) && (beginDate.getDay() == 1))
  115. {
  116. delta = (weekend == 2) ? (delta - 2) : (delta - 1);
  117. }
  118. endDate = $.zui.formatDate(beginDate.addDays(delta - 1), 'yyyy-MM-dd');
  119. $('#end').val(endDate).datetimepicker('update');
  120. computeWorkDays();
  121. }
  122. /**
  123. * Load branches.
  124. *
  125. * @param object product
  126. * @access public
  127. * @return void
  128. */
  129. function loadBranches(product)
  130. {
  131. /* When selecting a product, delete a plan that is empty by default. */
  132. $("#planDefault").remove();
  133. $('#productsBox select').each(function()
  134. {
  135. var $product = $(product);
  136. if($product.val() != 0 && $product.val() == $(this).val() && $product.attr('id') != $(this).attr('id'))
  137. {
  138. alert(errorSameProducts);
  139. $product.val(0);
  140. $product.trigger("chosen:updated");
  141. return false;
  142. }
  143. });
  144. if($('#productsBox .input-group:last select:first').val() != 0)
  145. {
  146. var length = $('#productsBox .input-group').size();
  147. $('#productsBox .row').append('<div class="col-sm-4">' + $('#productsBox .col-sm-4:last').html() + '</div>');
  148. if($('#productsBox .input-group:last select').size() >= 2) $('#productsBox .input-group:last select:last').remove();
  149. $('#productsBox .input-group:last .chosen-container').remove();
  150. $('#productsBox .input-group:last select:first').attr('name', 'products[' + length + ']').attr('id', 'products' + length);
  151. $('#productsBox .input-group:last .chosen').chosen();
  152. adjustProductBoxMargin();
  153. }
  154. var $inputgroup = $(product).closest('.input-group');
  155. if($inputgroup.find('select').size() >= 2) $inputgroup.removeClass('has-branch').find('select:last').remove();
  156. if($inputgroup.find('.chosen-container').size() >= 2) $inputgroup.find('.chosen-container:last').remove();
  157. var index = $inputgroup.find('select:first').attr('id').replace('products' , '');
  158. $.get(createLink('branch', 'ajaxGetBranches', "productID=" + $(product).val()), function(data)
  159. {
  160. if(data)
  161. {
  162. $inputgroup.addClass('has-branch').append(data);
  163. $inputgroup.find('select:last').attr('name', 'branch[' + index + ']').attr('id', 'branch' + index).attr('onchange', "loadPlans('#products" + index + "', this.value)").chosen();
  164. }
  165. });
  166. loadPlans(product);
  167. }
  168. function loadPlans(product, branchID)
  169. {
  170. if($('#plansBox').size() == 0) return false;
  171. var productID = $(product).val();
  172. var branchID = typeof(branchID) == 'undefined' ? 0 : branchID;
  173. var index = $(product).attr('id').replace('products', '');
  174. if(productID != 0)
  175. {
  176. if(typeof(planID) == 'undefined') planID = 0;
  177. planID = $("select#plans" + productID).val() != '' ? $("select#plans" + productID).val() : planID;
  178. $.get(createLink('product', 'ajaxGetPlans', "productID=" + productID + '&branch=' + branchID + '&planID=' + planID + '&fieldID&needCreate=&expired=' + ((config.currentMethod == 'create' || config.currentMethod == 'edit') ? 'unexpired' : '')), function(data)
  179. {
  180. if(data)
  181. {
  182. if($("div#plan" + index).size() == 0) $("#plansBox .row").append('<div class="col-sm-4" id="plan' + index + '"></div>');
  183. $("div#plan" + index).html(data).find('select').attr('name', 'plans[' + productID + ']').attr('id', 'plans' + productID).chosen();
  184. adjustPlanBoxMargin();
  185. }
  186. });
  187. }
  188. }
  189. /**
  190. * Adjust the layout of product selection.
  191. *
  192. * @access public
  193. * @return void
  194. */
  195. function adjustProductBoxMargin()
  196. {
  197. var productRows = Math.ceil($('#productsBox > .row > .col-sm-4').length / 3);
  198. if(productRows > 1)
  199. {
  200. for(i = 1; i <= productRows - 1; i++)
  201. {
  202. $('#productsBox .col-sm-4:lt(' + (i * 3) + ')').css('margin-bottom', '10px');
  203. }
  204. }
  205. }
  206. /**
  207. * Adjust the layout of the plan selection.
  208. *
  209. * @access public
  210. * @return void
  211. */
  212. function adjustPlanBoxMargin()
  213. {
  214. var planRows = Math.ceil($('#plansBox > .row > .col-sm-4').length / 3);
  215. if(planRows > 1)
  216. {
  217. for(j = 1; j <= planRows - 1; j++)
  218. {
  219. $('#plansBox .col-sm-4:lt(' + (j * 3) + ')').css('margin-bottom', '10px');
  220. }
  221. }
  222. }
  223. /**
  224. * Initialization operation.
  225. *
  226. * @access public
  227. * @return void
  228. */
  229. $(function()
  230. {
  231. $('#privList > tbody > tr > th input[type=checkbox]').change(function()
  232. {
  233. var id = $(this).attr('id');
  234. var checked = $(this).prop('checked');
  235. if(id == 'allChecker')
  236. {
  237. $('input[type=checkbox]').prop('checked', checked);
  238. }
  239. else
  240. {
  241. $(this).parents('tr').find('input[type=checkbox]').prop('checked', checked);
  242. }
  243. });
  244. })
  245. /**
  246. * Change budget input.
  247. *
  248. * @access public
  249. * @return void
  250. */
  251. $(function()
  252. {
  253. $('#future').on('change', function()
  254. {
  255. if($(this).prop('checked'))
  256. {
  257. $('#budget').val('').attr('disabled', 'disabled');
  258. if($('#beyondBudgetTip').length > 0) $('#beyondBudgetTip').parent().parent().remove();
  259. }
  260. else
  261. {
  262. $('#budget').removeAttr('disabled');
  263. }
  264. });
  265. })
  266. /**
  267. * Set budget tips and acl list.
  268. *
  269. * @param int $programID
  270. * @access public
  271. * @return void
  272. */
  273. function setBudgetTipsAndAclList(parentID)
  274. {
  275. var selectedProgramID = $('#parent').val();
  276. if(parentID != 0)
  277. {
  278. $.get(createLink('project', 'ajaxGetProjectFormInfo', "objectType=program&objectID=" + parentID + "&selectedProgramID=" + selectedProgramID), function(data)
  279. {
  280. var data = JSON.parse(data);
  281. parentProgram = programList[parentID];
  282. programBudget = parentProgram.budget;
  283. PGMBudgetUnit = currencySymbol[parentProgram.budgetUnit];
  284. budgetNotes = programBudget != 0 ? (PGMParentBudget + PGMBudgetUnit + data.availableBudget) : '';
  285. $('#budget').attr('placeholder', budgetNotes);
  286. refreshBudgetUnit(data);
  287. });
  288. $('.aclBox').html($('#subPGMAcl').html());
  289. }
  290. else
  291. {
  292. $('#budget').removeAttr('placeholder');
  293. $('.aclBox').html($('#PGMAcl').html());
  294. }
  295. if(typeof(programID) == 'undefined') programID = 0;
  296. budgetOverrunTips();
  297. outOfDateTip();
  298. }
  299. /**
  300. * compare childlish date.
  301. *
  302. * @access public
  303. * @return void
  304. */
  305. function compareChildDate()
  306. {
  307. if(window.ignoreTips['dateTip']) return;
  308. if(page == 'create') return;
  309. var end = $('#end').val();
  310. var begin = $('#begin').val();
  311. var selectedProgramID = $('#parent').val();
  312. if($('#dateTip').length > 0) $('#dateTip').remove();
  313. if(end == longTime) end = LONG_TIME;
  314. if(end.length > 0 && begin.length > 0)
  315. {
  316. var programEnd = new Date(end);
  317. var programBegin = new Date(begin);
  318. $.get(createLink('project', 'ajaxGetProjectFormInfo', 'objectType=program&objectID=' + programID + '&selectedProgramID=' + selectedProgramID), function(data)
  319. {
  320. var childInfo = JSON.parse(data);
  321. if(childInfo.maxChildEnd == '' || childInfo.minChildBegin == '') return;
  322. var childBegin = new Date(childInfo.minChildBegin);
  323. var childEnd = new Date(childInfo.maxChildEnd);
  324. if(programBegin <= childBegin && programEnd >= childEnd) return;
  325. var dateTip = '';
  326. if(programBegin > childBegin)
  327. {
  328. dateTip = "<tr><td></td><td colspan='2'><span id='dateTip' class='text-remind'><p>" + beginGreatEqualChild + childInfo.minChildBegin + "</p><p id='ignore' onclick='ignoreTip(this)'>" + ignore + "</p></span></td></tr>";
  329. }
  330. else if(programEnd < childEnd)
  331. {
  332. dateTip = "<tr><td></td><td colspan='2'><span id='dateTip' class='text-remind'><p>" + endLessThanChild + childInfo.maxChildEnd + "</p><p id='ignore' onclick='ignoreTip(this)'>" + ignore + "</p></span></td></tr>";
  333. }
  334. $('#dateBox').parent().parent().after(dateTip);
  335. $('#dateTip').parent().css('line-height', '0');
  336. });
  337. }
  338. }
  339. /**
  340. * The date is out of the range of the parent project set, and a prompt is given.
  341. *
  342. * @access public
  343. * @return void
  344. */
  345. function outOfDateTip()
  346. {
  347. if(window.ignoreTips['dateTip']) return;
  348. var end = $('#end').val();
  349. var begin = $('#begin').val();
  350. if($('#dateTip').length > 0) $('#dateTip').parent().parent().remove();
  351. if(end == longTime) end = LONG_TIME;
  352. if(end.length > 0 && begin.length > 0)
  353. {
  354. var selectedProgramID = $('#parent').val();
  355. var programEnd = new Date(end);
  356. var programBegin = new Date(begin);
  357. if(selectedProgramID == 0)
  358. {
  359. compareChildDate();
  360. return;
  361. }
  362. if(typeof(programID) == 'undefined') programID = 0;
  363. $.get(createLink('project', 'ajaxGetProjectFormInfo', 'objectType=program&objectID=' + programID + "&selectedProgramID=" + selectedProgramID), function(data)
  364. {
  365. var dateTip = '';
  366. var data = JSON.parse(data);
  367. var parentEnd = new Date(data.selectedProgramEnd);
  368. var parentBegin = new Date(data.selectedProgramBegin);
  369. if(programBegin >= parentBegin && programEnd <= parentEnd)
  370. {
  371. compareChildDate();
  372. return;
  373. }
  374. if(programBegin < parentBegin)
  375. {
  376. dateTip = "<tr><td></td><td colspan='2'><span id='dateTip' class='text-remind'><p>" + beginLessThanParent + data.selectedProgramBegin + "</p><p id='ignore' onclick='ignoreTip(this)'>" + ignore + "</p></span></td></tr>";
  377. }
  378. else if(programEnd > parentEnd)
  379. {
  380. dateTip = "<tr><td></td><td colspan='2'><span id='dateTip' class='text-remind'><p>" + endGreatThanParent + data.selectedProgramEnd + "</p><p id='ignore' onclick='ignoreTip(this)'>" + ignore + "</p></span></td></tr>";
  381. }
  382. $('#dateBox').parent().parent().after(dateTip);
  383. $('#dateTip').parent().css('line-height', '0');
  384. });
  385. }
  386. }
  387. /**
  388. * Append prompt when the budget exceeds the parent project set.
  389. *
  390. * @access public
  391. * @return void
  392. */
  393. function budgetOverrunTips()
  394. {
  395. if(window.ignoreTips['beyondBudgetTip']) return;
  396. var selectedProgramID = $('#parent').val();
  397. var budget = $('#budget').val();
  398. if(selectedProgramID == 0)
  399. {
  400. if($('#beyondBudgetTip').length > 0) $('#beyondBudgetTip').parent().parent().remove();
  401. return false;
  402. }
  403. if(typeof(programID) == 'undefined') programID = 0;
  404. $.get(createLink('project', 'ajaxGetProjectFormInfo', 'objectType=program&objectID=' + programID + "&selectedProgramID=" + selectedProgramID), function(data)
  405. {
  406. var data = JSON.parse(data);
  407. var tip = "";
  408. if(budget !=0 && budget !== null && budget > data.availableBudget) var tip = "<tr><td></td><td colspan='2'><span id='beyondBudgetTip' class='text-remind'><p>" + budgetOverrun + currencySymbol[data.budgetUnit] + data.availableBudget + "</p><p id='ignore' onclick='ignoreTip(this)'>" + ignore + "</p></span></td></tr>"
  409. if($('#beyondBudgetTip').length > 0) $('#beyondBudgetTip').parent().parent().remove();
  410. $('#budgetBox').parent().parent().after(tip);
  411. $('#beyondBudgetTip').parent().css('line-height', '0');
  412. });
  413. }
  414. /**
  415. * Make this prompt no longer appear.
  416. *
  417. * @param string $obj
  418. * @access public
  419. * @return void
  420. */
  421. function ignoreTip(obj)
  422. {
  423. var parentID = obj.parentNode.id;
  424. $('#' + parentID).addClass('hidden');
  425. if(parentID == 'dateTip') window.ignoreTips['dateTip'] = true;
  426. if(parentID == 'beyondBudgetTip') window.ignoreTips['beyondBudgetTip'] = true;
  427. }