search-group-resources.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. import {
  2. defineComponent,
  3. nextTick,
  4. onMounted,
  5. reactive,
  6. ref,
  7. watch,
  8. toRefs
  9. } from 'vue';
  10. import styles from './index.module.less';
  11. import {
  12. NButton,
  13. NForm,
  14. NFormItem,
  15. NImage,
  16. NPopselect,
  17. NSpace
  18. } from 'naive-ui';
  19. // import iconAdd from '../../images/icon-add.png';
  20. import TheSearch from '/src/components/TheSearch';
  21. import { resourceTypeArray } from '/src/utils/searchArray';
  22. import { useCatchStore } from '/src/store/modules/catchData';
  23. // import isCollaose from '../../images/isCollaose.png';
  24. const ChildNodeSearch = defineComponent({
  25. name: 'ChildNodeSearch',
  26. props: {
  27. activeRow: {
  28. type: Object,
  29. default: () => ({})
  30. },
  31. list: {
  32. type: Array,
  33. default: () => []
  34. }
  35. },
  36. emits: ['selectChildTag'],
  37. setup(props, { emit }) {
  38. const { activeRow } = toRefs(props);
  39. const selectItem = ref({});
  40. watch(
  41. () => props.activeRow,
  42. () => {
  43. activeRow.value = props.activeRow;
  44. selectItem.value = {};
  45. }
  46. );
  47. return () => (
  48. <>
  49. {activeRow.value?.id && (
  50. <>
  51. <NFormItem label={activeRow.value.columnName + ':'}>
  52. <NSpace class={styles.spaceSection}>
  53. {activeRow.value?.children.map((subject: any) => (
  54. <span
  55. class={[
  56. styles.textBtn,
  57. (activeRow.value.activeIndex || '') == subject.id &&
  58. styles.textBtnActive
  59. ]}
  60. onClick={() => {
  61. activeRow.value.activeIndex = subject.id;
  62. let children: any;
  63. let columnName = '';
  64. if (subject.children) {
  65. children = [
  66. {
  67. columnName: subject.children[0].columnName,
  68. name: '全部',
  69. id: ''
  70. },
  71. ...subject.children
  72. ];
  73. columnName = subject.children[0].columnName;
  74. selectItem.value = {
  75. ...subject,
  76. columnName,
  77. activeIndex: '',
  78. children
  79. };
  80. } else {
  81. selectItem.value = {};
  82. }
  83. emit('selectChildTag', activeRow.value.activeIndex);
  84. }}>
  85. {subject.name}
  86. </span>
  87. ))}
  88. </NSpace>
  89. </NFormItem>
  90. <ChildNodeSearch
  91. activeRow={selectItem.value}
  92. onSelectChildTag={(item: any) => {
  93. // console.log(
  94. // activeRow.value.activeIndex,
  95. // 'activeRow.value.activeIndex'
  96. // );
  97. emit('selectChildTag', item || activeRow.value.activeIndex);
  98. }}
  99. />
  100. </>
  101. )}
  102. </>
  103. );
  104. }
  105. });
  106. export default defineComponent({
  107. name: 'search-group',
  108. emits: ['search', 'add'],
  109. expose: ['init'],
  110. setup(props, { emit }) {
  111. const catchStore = useCatchStore();
  112. const forms = reactive({
  113. type: 'MUSIC', //
  114. name: '',
  115. // grade: null as any,
  116. bookVersionId: null as any,
  117. // musicSheetCategoriesId: null as any,
  118. subjectId: null
  119. });
  120. const state = reactive({
  121. tempSubjectId: null
  122. });
  123. const data = reactive({
  124. selectParents: {}, // 选中的数据
  125. tags: [] as any[],
  126. tagActiveId: '' as any,
  127. tagActive: {} as any,
  128. childSelectId: null as any
  129. });
  130. const onSearch = () => {
  131. emit('search', {
  132. ...forms,
  133. bookVersionId: data.childSelectId || data.tagActiveId
  134. });
  135. };
  136. const collapseWrapRef = ref();
  137. const divDomList = ref([] as any);
  138. const orginHeight = ref(0);
  139. const line = ref(0);
  140. const isCollapse = ref(false);
  141. const loadingCollapse = ref(false); // 是否加载完成
  142. // const musicCateRef = (el: any) => {
  143. // if (el?.selfElRef) {
  144. // divDomList.value.push(el.selfElRef.parentNode);
  145. // }
  146. // };
  147. // const setCollapse = (flag: boolean) => {
  148. // isCollapse.value = flag;
  149. // getLive();
  150. // };
  151. const getLive = () => {
  152. try {
  153. divDomList.value = [...new Set(divDomList.value)];
  154. let offsetLeft = -1;
  155. divDomList.value.forEach((item: any, index: number) => {
  156. if (index === 0) {
  157. line.value = 1;
  158. offsetLeft = item.offsetLeft;
  159. } else if (item.offsetLeft === offsetLeft && index != 0) {
  160. // 如果某个标签的offsetLeft和第一个标签的offsetLeft相等 说明增加了一行
  161. line.value++;
  162. }
  163. if (!isCollapse.value) {
  164. if (line.value > 1) {
  165. //从第3行开始 隐藏标签
  166. item.style.display = 'none';
  167. // 显示展开按钮 class名chu是在前面动态添加的
  168. } else {
  169. item.style.display = 'block';
  170. }
  171. } else {
  172. item.style.display = 'block';
  173. }
  174. });
  175. loadingCollapse.value = true;
  176. } catch {
  177. //
  178. }
  179. };
  180. const selectChildObj = (item: any) => {
  181. const obj: any = {};
  182. item?.forEach((child: any) => {
  183. if (child.id === forms.subjectId) {
  184. obj.selected = true;
  185. obj.name = child.name;
  186. }
  187. });
  188. return obj;
  189. };
  190. const _initTags = () => {
  191. const tags = catchStore.getMusicTagTree;
  192. data.tags = [
  193. {
  194. columnName: tags[0].columnName,
  195. name: '全部',
  196. id: ''
  197. },
  198. ...tags
  199. ];
  200. data.tagActiveId = data.tags[0].id;
  201. };
  202. const changeTag = (item: any) => {
  203. data.tagActiveId = item.id;
  204. data.childSelectId = null;
  205. let children: any;
  206. let columnName = '';
  207. if (item.children) {
  208. children = [
  209. {
  210. columnName: item.children[0].columnName,
  211. name: '全部',
  212. id: ''
  213. },
  214. ...item.children
  215. ];
  216. columnName = item.children[0].columnName;
  217. data.selectParents = {
  218. ...item,
  219. columnName,
  220. activeIndex: '',
  221. children
  222. };
  223. } else {
  224. data.selectParents = {};
  225. }
  226. onSearch();
  227. };
  228. onMounted(async () => {
  229. // console.log('加载');
  230. // 获取教材分类列表
  231. // await catchStore.getMusicSheetCategory();
  232. await catchStore.getMusicTagTreeApi();
  233. _initTags();
  234. // console.log(data, 'data');
  235. // 获取声部列表
  236. await catchStore.getSubjects();
  237. if (forms.type === 'MUSIC') {
  238. orginHeight.value = collapseWrapRef.value?.offsetHeight;
  239. // hiddenHeight.value = collapseWrapRef.value.offsetHeight / line.value;
  240. // 默认隐藏
  241. getLive();
  242. // const musicTagTreeList = catchStore.getMusicTagTree;
  243. // if (musicTagTreeList.length > 0) {
  244. // forms.bookVersionId = musicTagTreeList[0].id;
  245. // state.gradeList = musicTagTreeList[0].children || [];
  246. // if (state.gradeList.length > 0) {
  247. // forms.grade = state.gradeList[0].id;
  248. // state.musicCategory = state.gradeList[0].children || [];
  249. // if (state.musicCategory.length > 0) {
  250. // forms.musicSheetCategoriesId = state.musicCategory[0].id;
  251. // }
  252. // }
  253. // }
  254. }
  255. onSearch();
  256. });
  257. return () => (
  258. <div class={styles.searchGroup}>
  259. <div class={[styles.searchCatatory]}>
  260. <NSpace size="small" class={styles.btnType}>
  261. {resourceTypeArray.map((item: any) => (
  262. <NButton
  263. type={forms.type === item.value ? 'primary' : 'default'}
  264. secondary={forms.type === item.value ? false : true}
  265. round
  266. size="small"
  267. focusable={false}
  268. onClick={() => {
  269. forms.type = item.value;
  270. forms.subjectId = null;
  271. state.tempSubjectId = null;
  272. data.tagActiveId = null;
  273. data.childSelectId = null;
  274. onSearch();
  275. nextTick(() => {
  276. if (forms.type === 'MUSIC') {
  277. orginHeight.value = collapseWrapRef.value?.offsetHeight;
  278. // hiddenHeight.value =
  279. // collapseWrapRef.value.offsetHeight / line.value || 0;
  280. // 默认隐藏
  281. getLive();
  282. } else {
  283. divDomList.value = [];
  284. }
  285. });
  286. }}>
  287. {item.label}
  288. </NButton>
  289. ))}
  290. </NSpace>
  291. {/* <NButton
  292. type="primary"
  293. class={styles.addTrain}
  294. focusable={false}
  295. strong
  296. onClick={() => emit('add')}>
  297. <img src={iconAdd} />
  298. 添加自定义教材
  299. </NButton> */}
  300. </div>
  301. <NForm labelAlign="left" labelPlacement="left">
  302. {forms.type === 'MUSIC' && (
  303. <>
  304. {data.tags.length > 0 && (
  305. <NFormItem label={data.tags[0]?.columnName + ':'}>
  306. <NSpace class={styles.spaceSection}>
  307. {data.tags.map((subject: any) => (
  308. <span
  309. class={[
  310. styles.textBtn,
  311. data.tagActiveId === subject.id &&
  312. styles.textBtnActive
  313. ]}
  314. onClick={() => {
  315. changeTag(subject);
  316. }}>
  317. {subject.name}
  318. </span>
  319. ))}
  320. </NSpace>
  321. </NFormItem>
  322. )}
  323. <ChildNodeSearch
  324. activeRow={data.selectParents}
  325. onSelectChildTag={(val: any) => {
  326. data.childSelectId = val;
  327. onSearch();
  328. }}
  329. />
  330. </>
  331. )}
  332. <NFormItem label="乐器:">
  333. <NSpace class={styles.spaceSection}>
  334. {catchStore.getSubjectInstruments.map((subject: any) =>
  335. subject.instruments && subject.instruments.length > 1 ? (
  336. <NPopselect
  337. options={subject.instruments}
  338. trigger="hover"
  339. scrollable
  340. v-model:value={state.tempSubjectId}
  341. onUpdate:value={() => {
  342. forms.subjectId = state.tempSubjectId;
  343. onSearch();
  344. }}
  345. key={subject.value}
  346. class={[styles.popSelect]}>
  347. <span
  348. class={[
  349. styles.textBtn,
  350. selectChildObj(subject.instruments).selected &&
  351. styles.textBtnActive
  352. ]}>
  353. {selectChildObj(subject.instruments).name || subject.name}
  354. <i class={styles.iconArrow}></i>
  355. </span>
  356. </NPopselect>
  357. ) : (
  358. <span
  359. class={[
  360. styles.textBtn,
  361. forms.subjectId === subject.value && styles.textBtnActive
  362. ]}
  363. onClick={() => {
  364. forms.subjectId = subject.value;
  365. state.tempSubjectId = null;
  366. onSearch();
  367. }}>
  368. {subject.name}
  369. </span>
  370. )
  371. )}
  372. </NSpace>
  373. </NFormItem>
  374. <TheSearch
  375. class={styles.inputSearch}
  376. round
  377. onSearch={(val: string) => {
  378. forms.name = val;
  379. onSearch();
  380. }}
  381. />
  382. </NForm>
  383. </div>
  384. );
  385. }
  386. });