folderMixinFactory.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import CreateFolder from "./popup/CreateFolder";
  2. import RenameFolder from "./popup/RenameFolder";
  3. import MoveFolder from "./popup/MoveFolder";
  4. import {
  5. nodeIdList2nodeInfoListByNodeTree,
  6. preOrderTraversalSearch,
  7. } from "@/utils/other.js";
  8. import {
  9. getFolderTree,
  10. createFolder as createFolderApi,
  11. renameFolder as renameFolderApi,
  12. moveToFolder,
  13. delFolder,
  14. } from "@/api";
  15. import { i18n } from "@/lang";
  16. export default function (materialType) {
  17. return {
  18. components: {
  19. CreateFolder,
  20. RenameFolder,
  21. MoveFolder,
  22. },
  23. data() {
  24. return {
  25. selectedList: [],
  26. isShowNewFolder: false,
  27. isShowRenameFolder: false,
  28. isShowMoveFolder: false,
  29. folderTree: null,
  30. folderPath: [
  31. {
  32. name: i18n.t(`gather.${materialType}`),
  33. id: 1,
  34. },
  35. ],
  36. };
  37. },
  38. computed: {
  39. currentFolderId() {
  40. return this.folderPath[this.folderPath.length - 1].id;
  41. },
  42. folderListInPath() {
  43. if (this.list) {
  44. return this.list.filter((item) => {
  45. return item.type === "dir";
  46. });
  47. } else {
  48. return [];
  49. }
  50. },
  51. },
  52. mounted() {},
  53. watch: {
  54. folderPath: {
  55. handler: function () {
  56. this.refreshListDebounced();
  57. },
  58. deep: true,
  59. },
  60. },
  61. methods: {
  62. validateNewFolderName(name) {
  63. const isUnique =
  64. this.folderListInPath.findIndex((item) => {
  65. return item.name === name;
  66. }) === -1;
  67. if (isUnique) {
  68. return {
  69. isValid: true,
  70. tip: "",
  71. };
  72. } else {
  73. return {
  74. isValid: false,
  75. tip: this.$i18n.t("gather.folder_name_already_used"),
  76. };
  77. }
  78. },
  79. onSubmitNewFolder(v) {
  80. this.isShowNewFolder = false;
  81. createFolderApi(
  82. {
  83. ancestors: this.folderPath
  84. .map((item) => {
  85. return item.id;
  86. })
  87. .join(","),
  88. name: v,
  89. parentId: this.currentFolderId,
  90. type: materialType,
  91. },
  92. () => {
  93. this.$msg.success(this.$i18n.t("gather.success"));
  94. this.refreshListDebounced();
  95. },
  96. (error) => {
  97. if (error && error.code !== 3104) {
  98. debugger
  99. this.$msg.error(this.$i18n.t("tips.network_error"));
  100. }
  101. }
  102. );
  103. },
  104. onSubmitRenameFolder(newName) {
  105. this.isShowRenameFolder = false;
  106. renameFolderApi(
  107. {
  108. id: this.popupItem.id,
  109. name: newName,
  110. // parentId: this.currentFolderId,
  111. type: materialType,
  112. },
  113. () => {
  114. this.$msg.success(this.$i18n.t("gather.edit_success"));
  115. const index = this.list.findIndex((eachItem) => {
  116. return eachItem.id === this.popupItem.id;
  117. });
  118. if (index >= 0) {
  119. this.list[index].name = newName;
  120. } else {
  121. console.error("在素材列表里没找到要重命名的那一项!");
  122. }
  123. this.popupItem = null;
  124. },
  125. (error) => {
  126. if (error && error.code !== 3104) {
  127. this.$msg.error(this.$i18n.t("tips.network_error"));
  128. }
  129. this.popupItem = null;
  130. }
  131. );
  132. },
  133. validateRenameFolderName(oldName, newName) {
  134. if (oldName === newName) {
  135. return {
  136. isValid: true,
  137. tip: "",
  138. };
  139. }
  140. const isUnique =
  141. this.folderListInPath.findIndex((item) => {
  142. return item.name === newName;
  143. }) === -1;
  144. if (isUnique) {
  145. return {
  146. isValid: true,
  147. tip: "",
  148. };
  149. } else {
  150. return {
  151. isValid: false,
  152. tip: this.$i18n.t("gather.folder_name_already_used"),
  153. };
  154. }
  155. },
  156. onClickMoveFolder() {
  157. getFolderTree({
  158. type: materialType,
  159. }).then((res) => {
  160. this.folderTree = res.data;
  161. console.log("onClickMoveFolder->>", res.data, this.folderTree);
  162. if (this.folderTree.children.length === 0) {
  163. this.$confirm({
  164. title: this.$i18n.t("gather.move_folder_to"),
  165. content: this.$i18n.t("gather.no_folder_need_create"),
  166. });
  167. } else {
  168. this.isShowMoveFolder = true;
  169. }
  170. });
  171. },
  172. filterAncestors(ancestors) {
  173. if (ancestors && ancestors.length > 0) {
  174. const array = ancestors.split(",").map((item) => item);
  175. return array.join(",");
  176. } else {
  177. return "";
  178. }
  179. },
  180. async onSubmitMoveFolder(targetFolderId) {
  181. // const ancestors = this.folderPath.map((item) => {
  182. // return item.id
  183. // }).join(',')
  184. const target = this.findFolderTreeById(this.folderTree, targetFolderId);
  185. let targetAncestors =
  186. target && target.ancestors && target.ancestors.length > 0
  187. ? this.filterAncestors(target.ancestors) + "," + target.id
  188. : "1";
  189. console.log("filter-targetAncestors", targetAncestors);
  190. const res = await moveToFolder(
  191. this.selectedList,
  192. targetFolderId,
  193. targetAncestors
  194. );
  195. // console.log('res', res.code === 0);
  196. if (res) {
  197. this.$msg.success(this.$i18n.t("gather.success"));
  198. this.$nextTick(() => {
  199. this.isShowMoveFolder = false;
  200. this.selectedList = [];
  201. this.refreshListDebounced();
  202. });
  203. }
  204. },
  205. findFolderTreeById(folderTree, id) {
  206. if (folderTree.id == id) {
  207. return folderTree;
  208. } else if (folderTree.children != null) {
  209. var i;
  210. var result = null;
  211. for (i = 0; result == null && i < folderTree.children.length; i++) {
  212. result = this.findFolderTreeById(folderTree.children[i], id);
  213. }
  214. return result;
  215. }
  216. return null;
  217. },
  218. delFolder(id, onSuccess) {
  219. if (this.uploadListForUI.length > 0) {
  220. this.$confirm({
  221. title: i18n.t("tips.title"),
  222. content: i18n.t("gather.can_not_delete_folder_when_uploading"),
  223. });
  224. } else {
  225. this.$confirm({
  226. title: i18n.t("gather.delete_folder"),
  227. content: i18n.t("gather.comfirm_delete_folder"),
  228. okText: i18n.t("gather.delete"),
  229. ok: () => {
  230. delFolder(id).then(() => {
  231. this.$msg.success(i18n.t("gather.delete_success"));
  232. this.isRequestingMoreData = true;
  233. const lastestUsedSearchKey = this.searchKey;
  234. onSuccess(lastestUsedSearchKey);
  235. });
  236. },
  237. });
  238. }
  239. },
  240. onClickPath(idx) {
  241. this.searchKey = "";
  242. this.folderPath = this.folderPath.slice(0, idx + 1);
  243. this.selectedList = [];
  244. },
  245. onClickFolder(folder) {
  246. getFolderTree({
  247. type: materialType,
  248. }).then((res) => {
  249. this.folderTree = res.data;
  250. const targetPathIdList = folder.ancestors
  251. .split(",")
  252. .map((item) => {
  253. return Number(item);
  254. })
  255. .filter((i) => i);
  256. console.log("targetPathIdList", targetPathIdList);
  257. targetPathIdList.push(folder.id);
  258. this.folderPath = nodeIdList2nodeInfoListByNodeTree(
  259. targetPathIdList,
  260. this.folderTree
  261. );
  262. this.searchKey = "";
  263. this.selectedList = [];
  264. });
  265. },
  266. onClickParentFolder(clickedItem) {
  267. getFolderTree({
  268. type: materialType,
  269. }).then((res) => {
  270. this.folderTree = res.data;
  271. console.log("clickedItem", clickedItem);
  272. if (clickedItem.type === "dir") {
  273. const targetPathIdList = clickedItem.ancestors
  274. .split(",")
  275. .map((item) => {
  276. return Number(item);
  277. });
  278. this.folderPath = nodeIdList2nodeInfoListByNodeTree(
  279. targetPathIdList,
  280. this.folderTree
  281. );
  282. } else {
  283. let targetNodePath = [];
  284. preOrderTraversalSearch(
  285. this.folderTree,
  286. (node) => {
  287. if (node.id === clickedItem.dirId) {
  288. return true;
  289. } else {
  290. return false;
  291. }
  292. },
  293. targetNodePath
  294. );
  295. this.folderPath = targetNodePath.map((item) => {
  296. return {
  297. id: item.id,
  298. name: item.name,
  299. };
  300. });
  301. }
  302. this.searchKey = "";
  303. this.selectedList = [];
  304. });
  305. },
  306. },
  307. };
  308. }