QuestionFolder.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <div class="question-folder">
  3. <div v-if="isEdit" class="folder-action box-justify margin-bottom-15">
  4. <el-button
  5. type="primary"
  6. :disabled="curNodeData.level >= MAX_FOLDER_LEVEL"
  7. @click="toAddFolder"
  8. >新建文件夹</el-button
  9. >
  10. <el-button
  11. type="danger"
  12. :disabled="curNodeData.level >= MAX_FOLDER_LEVEL"
  13. @click="toDeleteFolder"
  14. >删除文件夹</el-button
  15. >
  16. </div>
  17. <el-tree
  18. class="folder-tree"
  19. :data="classifyTree"
  20. node-key="questionClassifyId"
  21. default-expand-all
  22. :expand-on-click-node="false"
  23. :props="defaultProps"
  24. @node-click="nodeClick"
  25. >
  26. <span slot-scope="{ node, data }">
  27. <i class="icon icon-files-act node-icon"></i>
  28. <span v-if="data.id === null" class="node-form">
  29. <el-form
  30. ref="modalFormComp"
  31. :model="modalForm"
  32. :rules="rules"
  33. size="mini"
  34. :show-message="false"
  35. inline
  36. >
  37. <el-form-item prop="name">
  38. <el-input
  39. v-model="modalForm.name"
  40. placeholder="请输入文件夹名称"
  41. clearable
  42. ></el-input>
  43. </el-form-item>
  44. <el-form-item>
  45. <el-button
  46. type="primary"
  47. icon="el-icon-check"
  48. :disabled="loading"
  49. @click="toCreateFolder"
  50. ></el-button>
  51. <el-button
  52. type="danger"
  53. icon="el-icon-close"
  54. :disabled="loading"
  55. style="margin-left: 6px"
  56. @click="toRemoveFolder(node, data)"
  57. ></el-button>
  58. </el-form-item>
  59. </el-form>
  60. </span>
  61. <span
  62. v-else
  63. :class="['node-cont', { 'is-active': curNodeData.id === data.id }]"
  64. >{{ data.name }}</span
  65. >
  66. </span>
  67. </el-tree>
  68. </div>
  69. </template>
  70. <script>
  71. import { classifyTreeApi, updateClassifyApi, deleteClassifyApi } from "../api";
  72. const initModalForm = {
  73. id: null,
  74. parentId: null,
  75. name: "",
  76. remark: "",
  77. };
  78. export default {
  79. name: "QuestionFolder",
  80. props: {
  81. isEdit: {
  82. type: Boolean,
  83. default: true,
  84. },
  85. folderId: {
  86. type: [Number, String],
  87. default: "",
  88. },
  89. },
  90. data() {
  91. return {
  92. MAX_FOLDER_LEVEL: 3,
  93. classifyTree: [
  94. {
  95. id: 0,
  96. parent: null,
  97. name: "根目录",
  98. children: [],
  99. },
  100. ],
  101. defaultProps: {
  102. label: "name",
  103. },
  104. curNodeData: {},
  105. loading: false,
  106. modalForm: {
  107. ...initModalForm,
  108. },
  109. rules: {
  110. name: [
  111. {
  112. required: true,
  113. message: "请输入文件夹名称",
  114. trigger: "change",
  115. },
  116. ],
  117. },
  118. };
  119. },
  120. mounted() {
  121. this.initData();
  122. },
  123. methods: {
  124. async initData() {
  125. await this.getClassifyTree();
  126. if (this.folderId && !this.isEdit) {
  127. this.curNodeData = this.findNodeById(this.folderId);
  128. }
  129. },
  130. async getClassifyTree() {
  131. const res = await classifyTreeApi();
  132. this.classifyTree[0].children = res.data || [];
  133. },
  134. nodeClick(data) {
  135. if (!data.id && data.id !== 0) {
  136. this.$emit("selected", null);
  137. return;
  138. } else {
  139. this.$emit("selected", data);
  140. }
  141. if (data.id === null || data.id === this.curNodeData.id) return;
  142. this.clearPreNewNode();
  143. this.$nextTick(() => {
  144. this.curNodeData = this.findNodeById(data.id);
  145. });
  146. },
  147. findNodeById(id) {
  148. let curNode = null;
  149. const findNode = (data, level) => {
  150. if (curNode) return;
  151. level++;
  152. data.forEach((item) => {
  153. if (curNode) return;
  154. if (item.id === id) {
  155. curNode = item;
  156. curNode.level = level;
  157. return;
  158. }
  159. if (item.children && item.children.length)
  160. findNode(item.children, level);
  161. });
  162. };
  163. findNode(this.classifyTree, -1);
  164. return curNode || {};
  165. },
  166. clearPreNewNode() {
  167. const removePreNewChild = (data) => {
  168. data = data.filter((item) => item.id !== null);
  169. return data.map((item) => {
  170. if (item.children && item.children.length)
  171. item.children = removePreNewChild(item.children);
  172. return item;
  173. });
  174. };
  175. this.classifyTree = removePreNewChild(this.classifyTree);
  176. },
  177. toAddFolder() {
  178. if (!this.curNodeData.id && this.curNodeData.id !== 0) {
  179. this.$message.error("请先选择文件夹!");
  180. return;
  181. }
  182. if (this.curNodeData.level >= this.MAX_FOLDER_LEVEL) {
  183. this.$message.error(`最多允许${this.MAX_FOLDER_LEVEL}级目录`);
  184. return;
  185. }
  186. if (
  187. this.curNodeData.children &&
  188. this.curNodeData.children.find((item) => item.id === null)
  189. ) {
  190. this.$message.closeAll();
  191. this.$message.error("有未创建完成的目录!");
  192. return;
  193. }
  194. const newChild = {
  195. ...initModalForm,
  196. parentId: this.curNodeData.id,
  197. };
  198. if (!this.curNodeData.children) {
  199. this.$set(this.curNodeData, "children", []);
  200. }
  201. this.curNodeData.children.push(newChild);
  202. this.modalForm = { ...newChild };
  203. },
  204. async toCreateFolder() {
  205. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  206. if (!valid) return;
  207. if (this.loading) return;
  208. this.loading = true;
  209. const res = await updateClassifyApi({ ...this.modalForm }).catch(
  210. () => {}
  211. );
  212. this.loading = false;
  213. if (!res) return;
  214. this.$message.success("操作成功");
  215. const newChild = this.curNodeData.children.find(
  216. (item) => item.id === null
  217. );
  218. newChild.id = res.data;
  219. newChild.name = this.modalForm.name;
  220. },
  221. toRemoveFolder(node, data) {
  222. const parent = node.parent;
  223. const children = parent.data.children || parent.data;
  224. const index = children.findIndex((d) => d.id === data.id);
  225. children.splice(index, 1);
  226. },
  227. async toDeleteFolder() {
  228. if (!this.curNodeData.id) return;
  229. const confirm = await this.$confirm(`确定要删除选中的文件夹吗?`, "提示", {
  230. type: "warning",
  231. }).catch(() => {});
  232. if (confirm !== "confirm") return;
  233. if (this.loading) return;
  234. this.loading = true;
  235. const res = await deleteClassifyApi(this.curNodeData.id).catch(() => {});
  236. this.loading = false;
  237. if (!res) return;
  238. this.$message.success("操作成功");
  239. await this.getClassifyTree();
  240. },
  241. getSelectedNode() {
  242. return this.curNodeData;
  243. },
  244. },
  245. };
  246. </script>