choosedept.ui.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. window.buildTreeItems = function(deptTree, treeItems)
  2. {
  3. if(typeof(treeItems) == 'undefined') treeItems = [];
  4. for(i in deptTree)
  5. {
  6. let dept = deptTree[i];
  7. let treeItem = {key: dept.id, text: dept.name};
  8. treeItems = appendItems(treeItems, treeItem, dept.pId);
  9. }
  10. return treeItems;
  11. };
  12. window.appendItems = function(treeItems, treeItem, pid)
  13. {
  14. if(pid == 0)
  15. {
  16. treeItems.push(treeItem);
  17. return treeItems;
  18. }
  19. for(i in treeItems)
  20. {
  21. hasChild = typeof(treeItems[i].items) != 'undefined';
  22. if(treeItems[i].key == pid)
  23. {
  24. parentItem = treeItems[i];
  25. if(!hasChild) parentItem.items = [];
  26. hasCurrentItem = false;
  27. for(j in parentItem.items)
  28. {
  29. if(parentItem.items[j].key == treeItem.key) hasCurrentItem = true;
  30. }
  31. if(parentItem.checked != undefined) treeItem.checked = parentItem.checked;
  32. if(!hasCurrentItem) parentItem.items.push(treeItem);
  33. }
  34. else if(hasChild)
  35. {
  36. treeItems[i].items = appendItems(treeItems[i].items, treeItem, pid);
  37. }
  38. }
  39. return treeItems;
  40. };
  41. window.getSelectedDepts = function()
  42. {
  43. var selectedDepts = [];
  44. $('#deptList').find('.item-checkbox.checked').each(function()
  45. {
  46. id = $(this).closest('.tree-item').attr('z-key');
  47. selectedDepts.push(id);
  48. });
  49. return selectedDepts;
  50. };
  51. window.submitSelectedDepts = function()
  52. {
  53. const selectedDepts = getSelectedDepts();
  54. if(selectedDepts.length == 0) return zui.Modal.alert(noDeptError);
  55. let link = $.createLink('webhook', 'bind', "id=" + webhookID);
  56. link += link.indexOf('?') >= 0 ? '&' : '?';
  57. link += "selectedDepts=" + selectedDepts.join(',');
  58. $('.actions .save').attr('disabled', 'disabled');
  59. loadPage(link);
  60. };
  61. window.setItemChecked = function(clickDept)
  62. {
  63. const $startItem = $('#deptList li[z-key="' + clickDept + '"]');
  64. const $checkbox = $($startItem.children('[key="item"]'));
  65. const itemChecked = $checkbox.hasClass('checked') && $checkbox.find('.item-checkbox').hasClass('checked');
  66. const noAllChecked = $checkbox.hasClass('checked') && !$checkbox.find('.item-checkbox').hasClass('checked');
  67. $startItem.find('[key="item"]').toggleClass('checked', !itemChecked && !noAllChecked);
  68. $startItem.find('.item-checkbox').toggleClass('checked', !itemChecked && !noAllChecked);
  69. $startItem.find('.item-checkbox').find('input[type=checkbox]').prop('checked', !itemChecked && !noAllChecked);
  70. };
  71. const childrenLoaded = [];
  72. window.loadChildrenTree = function(e)
  73. {
  74. if(webhookType != 'feishuuser') return;
  75. const $deptList = $('#deptList');
  76. const $this = $(e.target);
  77. const $li = $this.closest('li');
  78. const deptID = $li.attr('z-key');
  79. let $checkbox = null;
  80. if($this.hasClass('item-checkbox')) $checkbox = $this;
  81. if($this.parent().hasClass('item-checkbox')) $checkbox = $this.parent();
  82. if($checkbox) return setItemChecked(deptID);
  83. if(childrenLoaded.includes(deptID) || deptID == 1) return;
  84. childrenLoaded.push(deptID);
  85. $deptList.addClass('loading');
  86. $.post(feishuUrl, {departmentID: deptID}, function(deptTree)
  87. {
  88. const tree = $deptList.zui('tree');
  89. deptTree = JSON.parse(deptTree);
  90. if(deptTree.length) tree.render(buildTreeItems(deptTree, tree.options.items));
  91. $deptList.removeClass('loading');
  92. });
  93. };
  94. window.loadDeptTree = function()
  95. {
  96. if(webhookType == 'feishuuser')
  97. {
  98. $.getJSON(feishuUrl, function(deptTree)
  99. {
  100. $('#loadPrompt').remove();
  101. tree = new zui.Tree('#deptList', {checkbox: true, checkOnClick: true, defaultNestedShow: true, onClickItem: loadChildrenTree, items: buildTreeItems(deptTree)});
  102. });
  103. }
  104. else
  105. {
  106. tree = new zui.Tree('#deptList', {checkbox: true, checkOnClick: true, defaultNestedShow: true, items: buildTreeItems(deptTree)});
  107. $('#loadPrompt').remove();
  108. }
  109. };