v1.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* 进入页面,树结构为展开状态。*/
  2. /* Entering the page, the step tree is in an expanded state.*/
  3. setTimeout(function()
  4. {
  5. const from = $('.think-node-menu').data('from');
  6. if(from && !localStorage.getItem('thinkTreeFrom')) localStorage.setItem('thinkTreeFrom', from);
  7. }, 15);
  8. setTimeout(function()
  9. {
  10. const thinkTreeFrom = localStorage.getItem('thinkTreeFrom');
  11. const thinkTreeInfo = localStorage.getItem('thinkTreeInfo') ? JSON.parse(localStorage.getItem('thinkTreeInfo')) : null;
  12. if(thinkTreeInfo && thinkTreeFrom != 'createNode')
  13. {
  14. localStorage.removeItem('thinkTreeInfo');
  15. localStorage.removeItem('thinkTreeFrom');
  16. }
  17. }, 60);
  18. var thinkTreeInfo = {id: 0, wizard: 0};
  19. /**
  20. * 添加节点。
  21. * Add a node.
  22. *
  23. * @param int id
  24. * @param string addType
  25. * @access public
  26. * @return void
  27. */
  28. window.addNode = function(id, addType)
  29. {
  30. const $element = $(`div[data-id='${id}']`).not('div[data-type=annex]');
  31. const {wizard, order} = $element.data();
  32. let parentID = $element.data('parent') || 0;
  33. thinkTreeInfo = {id, wizard}
  34. if(addType == 'child') parentID = id;
  35. const level = addType == 'same' ? $element.data('level') : $element.data('level') + 1;
  36. const style = `style="margin-left: calc(${addType == 'same' ? -1 : 0} * var(--tree-indent, 42px))"`;
  37. const className = addType == 'child' ? 'mt-2' : '';
  38. let inputTpl = `<li class="tree-item ${className} tree-item-input not-nested-toggle">`;
  39. inputTpl += `<div class="tree-item-content item-content-thinmory" ${style}>`;
  40. inputTpl += `<input id="stepName" class="form-control" data-id="${id}" data-parent="${parentID}" data-order="${order}" data-add-type="${addType}" data-wizard="${wizard}">`;
  41. inputTpl += '</div></li>';
  42. /* 当添加同级节点且父节点为展开状态时,收起子节点。 */
  43. /* When adding a peer node and the parent node is in an expanded state, collapse the child node. */
  44. if(addType == 'same' && $element.parent().hasClass('is-nested-show')) $element.find('.tree-toggle').trigger('click');
  45. if(addType == 'same')
  46. {
  47. if($element.parent().hasClass('is-nested-show')) $element.find('.tree-toggle').trigger('click');
  48. setTimeout(function()
  49. {
  50. if($element.next('.tree').length == 0) $element.after(`<menu class="tree" level="${level}" data-level="${level}"></menu>`);
  51. $(`div[data-id='${id}']`).next('.tree').prepend(inputTpl);
  52. }, 1);
  53. }
  54. /* 当添加子节点且父节点为收起状态时,展开子节点。 */
  55. /* When adding a child node and the parent node is in an collapsed state, expand the child node. */
  56. if(addType == 'child' && !$element.parent().hasClass('is-nested-show') && $element.children('.icon-caret-right').length) $element.find('.tree-toggle').trigger('click');
  57. if(addType == 'child')
  58. {
  59. if(!$element.parent().hasClass('is-nested-show') && $element.children('.icon-caret-right').length) $element.find('.tree-toggle').trigger('click');
  60. setTimeout(function()
  61. {
  62. if($element.next('.tree').length == 0) $element.after(`<menu class="tree" level="${level}" data-level="${level}"></menu>`);
  63. $(`div[data-id='${id}']`).next('.tree').append(inputTpl);
  64. }, 1)
  65. }
  66. setTimeout(function()
  67. {
  68. $('#stepName').trigger('focus');
  69. $("#stepName").on('blur', saveStep);
  70. $("#stepName").on('keydown', function(e)
  71. {
  72. if(e.keyCode == 13) saveStep.call(this);
  73. });
  74. }, 1);
  75. }
  76. /**
  77. * 添加问题。
  78. * Add a question.
  79. *
  80. * @param int stepID 操作列所在的步骤id
  81. * @param int parentID 父节点id
  82. * @param string type 分析步骤类型 transition|question
  83. * @param string questionType 问题类型 radio|checkbox|input|tableInput
  84. * @access public
  85. * @return void
  86. */
  87. window.addQuestion = function(stepID, parentID, type, questionType = '')
  88. {
  89. $('.listitem').removeClass('selected');
  90. $('.tree-item-uncreated').remove();
  91. const $element = $(`div[data-id='${stepID}']`).not('div[data-type=annex]');
  92. const {wizard, parent = 0, order} = $element.data();
  93. const level = parent == parentID ? $element.data('level') : $element.data('level') + 1;
  94. const style = `style="margin-bottom: 6px; margin-left: calc(${parent == parentID ? 12 : 3} * (-4px)); padding-left: 12px;"`;
  95. const className = parent == parentID ? '' : 'mt-2';
  96. const $title = `<li class="tree-item tree-item-uncreated ${className}"><div class="listitem item-inner tree-item-inner selected text-primary font-bold" ${style}>${untitledLangs[type]}</div></li>`;
  97. const marketID = $('.think-node-menu').data('marketID');
  98. const from = $('.think-node-menu').data('from');
  99. /* 当父节点为收起状态时,展开子节点。 */
  100. /* When the parent node is in a collapsed state, expand the child nodes. */
  101. if(!$(`div[data-id='${parentID}']`).parent().hasClass('is-nested-show')) $(`div[data-id='${parentID}']`).find('.tree-toggle').trigger('click');
  102. setTimeout(function()
  103. {
  104. if($element.next('.tree').length == 0) $element.after(`<menu class="tree" level="${level}" data-type=${type} data-level="${level}"></menu>`);
  105. $(`div[data-id='${stepID}']`).next('.tree').append($title);
  106. loadPartial($.createLink('thinkstep', 'create', `marketID=${marketID}&wizardID=${wizard}&parent=${parentID}&type=${type}&questionType=${questionType}&currentParent=${parent}&currentOrder=${order}&from=${from}`), '.think-step');
  107. /* 去掉节点收起状态。*/
  108. /* Remove the node's collapsed state. */
  109. localStorage.removeItem('thinkTreeInfo');
  110. localStorage.removeItem('thinkTreeFrom');
  111. this.showSelected();
  112. }, 1);
  113. }
  114. /**
  115. * 保存分析步骤。
  116. * Save a step.
  117. *
  118. * @access public
  119. * @return void
  120. */
  121. window.saveStep = function()
  122. {
  123. const name = $(this).val();
  124. if(!name) return $(this).closest('.tree-item').remove();
  125. const {id, addType, parent, order, wizard} = $(this).data();
  126. const currentParent = addType == 'same' ? parent : 0;
  127. const marketID = $('.think-node-menu').data('marketID');
  128. $.ajaxSubmit({
  129. url: $.createLink('thinkstep', 'ajaxCreate', `marketID=${marketID}&currentParent=${currentParent}&currentOrder=${order}`),
  130. data: {
  131. wizard,
  132. title: name,
  133. parent: addType == 'same' ? parent : id,
  134. objectID: id,
  135. createType: addType,
  136. type: 'node',
  137. },
  138. onSuccess: () =>
  139. {
  140. $(this).val('');
  141. localStorage.setItem('thinkTreeFrom', 'createNode');
  142. localStorage.setItem('thinkTreeInfo', JSON.stringify(thinkTreeInfo));
  143. },
  144. onFail: (error) => {
  145. if(error?.message) zui.Modal.alert({message: error?.message.title});
  146. }
  147. });
  148. }
  149. /**
  150. * 切换所有问题步骤的展示。
  151. * Toggle the display of all question steps.
  152. *
  153. * @access public
  154. * @return void
  155. */
  156. var nodeElement = [];
  157. window.toggleQuestionShow = function()
  158. {
  159. $(this).toggleClass('is-collapsed');
  160. const questionElemnt = $('div[data-type=question], div[data-type=transition]');
  161. if(questionElemnt.length)
  162. {
  163. /* 获取所有问题节点的父级节点 */
  164. /* Get the parent nodes of all question nodes. */
  165. questionElemnt.each(function(id, item)
  166. {
  167. const node = $(item).closest('.tree').siblings('.listitem[data-type="node"]');
  168. nodeElement.push(node);
  169. });
  170. }
  171. nodeElement.forEach(function(node)
  172. {
  173. $(node).find('.tree-toggle').trigger('click');
  174. });
  175. }
  176. /**
  177. * 隐藏侧边栏。
  178. * Hide the sidebar.
  179. *
  180. * @access public
  181. * @return void
  182. */
  183. window.hideSidebar = function()
  184. {
  185. localStorage.setItem('hiddenSidebar', true);
  186. $('.think-node-menu').closest(".sidebar").sidebar("toggle");
  187. }
  188. /**
  189. * 展示侧边栏。
  190. * Show the sidebar.
  191. *
  192. * @access public
  193. * @return void
  194. */
  195. $(document).on('click', '.gutter-toggle', function ()
  196. {
  197. localStorage.setItem('hiddenSidebar', false);
  198. })
  199. /**
  200. * 被选中的节点或问题保持始终展示在页面上。
  201. * The selected node or question remains displayed on the page.
  202. *
  203. * @access public
  204. * @return void
  205. */
  206. window.showSelected = function ()
  207. {
  208. const selectedElement = $('#thinkNodeMenu .selected');
  209. if (selectedElement && selectedElement[0]) selectedElement[0].scrollIntoView({ behavior: 'auto', block: 'center' });
  210. }
  211. setTimeout(function()
  212. {
  213. this.showSelected();
  214. }, 100)