leftFolder.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <!-- 左侧文件夹结构 -->
  2. <template>
  3. <div class="left-folder" v-loading="loading">
  4. <el-scrollbar style="height: 100%">
  5. <el-row style="padding: 6px 12px; margin-top: 10px">
  6. <span style="font-size: 12px; color: #646c73">文件夹</span>
  7. <i class="el-icon-plus add-folder-icon" title="添加文件夹" @click="handleShowAddFolder"></i>
  8. </el-row>
  9. <ul class="folder-list">
  10. <li class="folder-item" style="cursor: auto" v-show="showAddFolder">
  11. <el-input
  12. style="width: 157px"
  13. ref="folderName"
  14. size="small"
  15. clearable
  16. v-model="folderName"
  17. @keyup.native.enter="handleClickAddFolder"
  18. placeholder="请输入文件夹名称"
  19. ></el-input>
  20. <i class="el-icon-refresh-left revoke-icon" title="撤销" @click="handleRevokeAddFolder"></i>
  21. <i class="el-icon-check confirm-icon" title="确认" @click="handleClickAddFolder"></i>
  22. </li>
  23. <li
  24. class="folder-item"
  25. :class="{ 'folder-select': folder.checked }"
  26. v-for="folder in folderData"
  27. :key="folder.id"
  28. :title="folder.name"
  29. @contextmenu.prevent="openMenu($event, folder)"
  30. @click="handleCheckFolder(folder.id)"
  31. >
  32. <span v-show="!folder.showInput" class="folder-item-text">{{ folder.name }}</span>
  33. <!-- <el-badge is-dot class="folder-item-number">103</el-badge> -->
  34. <el-input
  35. v-show="folder.showInput"
  36. style="width: 157px"
  37. :ref="`folderName${folder.id}`"
  38. size="small"
  39. clearable
  40. v-model="folder.updateName"
  41. @keyup.native.enter="handleUpdateFolderName(folder)"
  42. placeholder="请输入文件夹名称"
  43. ></el-input>
  44. <i
  45. v-show="folder.showInput"
  46. class="el-icon-refresh-left revoke-icon"
  47. title="撤销"
  48. @click.stop="handleRevokeUpdateFolder(folder)"
  49. ></i>
  50. <i v-show="folder.showInput" class="el-icon-check confirm-icon" title="确认" @click.stop="handleUpdateFolderName(folder)"></i>
  51. </li>
  52. <!-- 右键菜单部分 -->
  53. <ul v-show="visible" :style="{ left: left + 'px', top: top + 'px' }" class="contextmenu">
  54. <li @click="showUpdateFolderName">重命名</li>
  55. <li @click="handleDeleteFolder">删除文件夹</li>
  56. </ul>
  57. </ul>
  58. </el-scrollbar>
  59. </div>
  60. </template>
  61. <script>
  62. import { folderCreate, folderDelete, folderUpdate, folderQuery } from "@/api/labsl";
  63. export default {
  64. data() {
  65. return {
  66. showAddFolder: false,
  67. loading: false,
  68. folderName: "未命名文件夹",
  69. folderData: [],
  70. visible: false,
  71. top: 0,
  72. left: 0,
  73. rightClickFolder: null,
  74. };
  75. },
  76. created() {
  77. this.getFolderData();
  78. },
  79. methods: {
  80. // 获取文件夹数据
  81. async getFolderData(folderId) {
  82. this.loading = true;
  83. const params = {
  84. pageNumber: 1,
  85. pageSize: 1000,
  86. orders: "name asc, createTime desc"
  87. }
  88. const res = await folderQuery(params);
  89. this.folderData = res.content.map((folder) => {
  90. folder.updateName = folder.name;
  91. folder.checked = false;
  92. folder.showInput = false;
  93. return folder;
  94. });
  95. // 如果参数传入文件夹,则选中对应的文件夹,否则默认选中第一个文件夹
  96. if (folderId) {
  97. this.handleCheckFolder(folderId);
  98. } else {
  99. this.handleCheckFolder(this.folderData[0].id);
  100. }
  101. this.loading = false;
  102. },
  103. // 点击添加文件夹icon打开弹窗
  104. handleShowAddFolder() {
  105. this.showAddFolder = true;
  106. this.$nextTick(() => {
  107. this.$refs.folderName.focus();
  108. });
  109. },
  110. // 创建文件夹
  111. async handleClickAddFolder() {
  112. if (!this.folderName) {
  113. this.$message({ showClose: true, message: "请输入文件夹名称!" });
  114. } else if (
  115. this.folderData.find((folder) => {
  116. return folder.name === this.folderName;
  117. })
  118. ) {
  119. this.$message({ showClose: true, message: "您输入的文件夹已存在!" });
  120. } else {
  121. const res = await folderCreate({ content: [{ name: this.folderName }] });
  122. if (res.result === "success" && res.entityList.length) {
  123. res.entityList.forEach((item) => {
  124. this.folderData.unshift({
  125. id: item.id,
  126. name: item.name,
  127. updateName: item.name,
  128. checked: false,
  129. showInput: false,
  130. });
  131. });
  132. this.$message.success("添加文件夹成功!");
  133. this.handleRevokeAddFolder();
  134. this.handleCheckFolder(res.entityList[res.entityList.length - 1].id);
  135. } else {
  136. this.$message.error(res.message);
  137. }
  138. }
  139. },
  140. // 撤销添加文件夹
  141. handleRevokeAddFolder() {
  142. // 初始化添加文件夹状态
  143. this.showAddFolder = false;
  144. this.folderName = "未命名文件夹";
  145. },
  146. // 点击文件夹
  147. handleCheckFolder(folderId) {
  148. this.folderData.forEach((item) => {
  149. if (item.id === folderId) {
  150. item.checked = true;
  151. this.$emit("changeFolder", item);
  152. } else {
  153. item.checked = false;
  154. }
  155. });
  156. },
  157. // 右键文件夹
  158. openMenu(e, folder) {
  159. this.visible = true;
  160. this.top = e.pageY;
  161. this.left = e.pageX;
  162. this.rightClickFolder = folder;
  163. },
  164. // 关闭右键菜单
  165. closeMenu() {
  166. this.visible = false;
  167. },
  168. // 显示要修改的文件夹输入框
  169. showUpdateFolderName() {
  170. if (this.rightClickFolder) {
  171. this.rightClickFolder.showInput = true;
  172. this.$nextTick(() => {
  173. this.$refs[`folderName${this.rightClickFolder.id}`][0].focus();
  174. });
  175. }
  176. },
  177. // 文件夹重命名
  178. async handleUpdateFolderName(folder) {
  179. if (!folder.updateName) {
  180. this.$message({ showClose: true, message: "请输入文件夹名称!" });
  181. } else if (
  182. this.folderData.find((item) => {
  183. return item.name === folder.updateName;
  184. })
  185. ) {
  186. this.$message({ showClose: true, message: "您输入的文件夹已存在!" });
  187. } else {
  188. const res = await folderUpdate({
  189. content: [
  190. {
  191. id: folder.id,
  192. name: folder.updateName,
  193. },
  194. ],
  195. });
  196. if (res.result === "success") {
  197. folder.name = folder.updateName;
  198. this.$message.success("文件夹重命名成功!");
  199. folder.showInput = false;
  200. } else {
  201. this.$message.error(res.message);
  202. }
  203. }
  204. },
  205. // 撤销重命名
  206. handleRevokeUpdateFolder(folder) {
  207. folder.updateName = folder.name;
  208. folder.showInput = false;
  209. },
  210. // 删除文件夹
  211. handleDeleteFolder() {
  212. if (this.rightClickFolder) {
  213. this.$confirm("此操作将永久删除该文件夹, 是否继续?", "提示", {
  214. confirmButtonText: "确定",
  215. cancelButtonText: "取消",
  216. type: "warning",
  217. }).then(async () => {
  218. const res = await folderDelete([{ id: this.rightClickFolder.id }]);
  219. if (res.result === "success") {
  220. this.folderData.splice(
  221. this.folderData.findIndex((item) => {
  222. return item.id === this.rightClickFolder.id;
  223. }),
  224. 1
  225. );
  226. this.$message.success("删除成功!");
  227. } else {
  228. this.$message.error(res.message);
  229. }
  230. });
  231. }
  232. },
  233. },
  234. watch: {
  235. // 监听 visible,来触发关闭右键菜单,调用关闭菜单的方法
  236. visible(value) {
  237. if (value) {
  238. document.body.addEventListener("click", this.closeMenu);
  239. } else {
  240. document.body.removeEventListener("click", this.closeMenu);
  241. }
  242. },
  243. },
  244. };
  245. </script>
  246. <style lang="less" scoped>
  247. .left-folder {
  248. background: #f7f9fa;
  249. color: #1f2429;
  250. width: 100%;
  251. padding: 0 8px;
  252. height: 100%;
  253. padding-left: 6px;
  254. .add-folder-icon {
  255. float: right;
  256. margin-top: 3px;
  257. cursor: pointer;
  258. }
  259. .add-folder-icon:hover {
  260. color: #0091ff;
  261. }
  262. .folder-list {
  263. width: 100%;
  264. .folder-item {
  265. width: 100%;
  266. cursor: pointer;
  267. height: 40px;
  268. line-height: 40px;
  269. padding: 0 12px;
  270. .folder-item-text {
  271. display: inline-block;
  272. width: calc(100% - 35px);
  273. overflow: hidden;
  274. white-space: nowrap;
  275. text-overflow: ellipsis;
  276. }
  277. .folder-item-number {
  278. height: 18px;
  279. line-height: 18px;
  280. font-size: 12px;
  281. color: #646c73;
  282. margin-top: 11px;
  283. float: right;
  284. }
  285. .revoke-icon {
  286. cursor: pointer;
  287. margin-left: 8px;
  288. }
  289. .confirm-icon {
  290. cursor: pointer;
  291. color: #67c23a;
  292. margin-left: 8px;
  293. }
  294. }
  295. .folder-select {
  296. color: #0091ff;
  297. background-color: #e1f2ff;
  298. border-radius: 4px;
  299. }
  300. .folder-item:hover {
  301. color: #0091ff;
  302. background-color: #e1f2ff;
  303. border-radius: 4px;
  304. }
  305. .contextmenu {
  306. width: 120px;
  307. background: #fff;
  308. z-index: 3000;
  309. position: fixed;
  310. list-style-type: none;
  311. padding: 8px 0;
  312. border-radius: 4px;
  313. font-size: 12px;
  314. box-shadow: 0px 2px 10px 0px rgba(31, 35, 41, 0.1);
  315. li {
  316. margin: 0;
  317. padding: 8px 12px;
  318. cursor: pointer;
  319. }
  320. li:hover {
  321. color: #0091ff;
  322. background: #e1f2ff;
  323. }
  324. }
  325. }
  326. }
  327. </style>