search-group-resources.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 = '';
  273. data.childSelectId = null;
  274. data.selectParents = {};
  275. onSearch();
  276. nextTick(() => {
  277. if (forms.type === 'MUSIC') {
  278. orginHeight.value = collapseWrapRef.value?.offsetHeight;
  279. // hiddenHeight.value =
  280. // collapseWrapRef.value.offsetHeight / line.value || 0;
  281. // 默认隐藏
  282. getLive();
  283. } else {
  284. divDomList.value = [];
  285. }
  286. });
  287. }}>
  288. {item.label}
  289. </NButton>
  290. ))}
  291. </NSpace>
  292. {/* <NButton
  293. type="primary"
  294. class={styles.addTrain}
  295. focusable={false}
  296. strong
  297. onClick={() => emit('add')}>
  298. <img src={iconAdd} />
  299. 添加自定义教材
  300. </NButton> */}
  301. </div>
  302. <NForm labelAlign="left" labelPlacement="left">
  303. {forms.type === 'MUSIC' && (
  304. <>
  305. {data.tags.length > 0 && (
  306. <NFormItem label={data.tags[0]?.columnName + ':'}>
  307. <NSpace class={styles.spaceSection}>
  308. {data.tags.map((subject: any) => (
  309. <span
  310. class={[
  311. styles.textBtn,
  312. data.tagActiveId === subject.id &&
  313. styles.textBtnActive
  314. ]}
  315. onClick={() => {
  316. changeTag(subject);
  317. }}>
  318. {subject.name}
  319. </span>
  320. ))}
  321. </NSpace>
  322. </NFormItem>
  323. )}
  324. <ChildNodeSearch
  325. activeRow={data.selectParents}
  326. onSelectChildTag={(val: any) => {
  327. data.childSelectId = val;
  328. onSearch();
  329. }}
  330. />
  331. </>
  332. )}
  333. <NFormItem label="乐器:">
  334. <NSpace class={styles.spaceSection}>
  335. {catchStore.getSubjectInstruments.map((subject: any) =>
  336. subject.instruments && subject.instruments.length > 1 ? (
  337. <NPopselect
  338. options={subject.instruments}
  339. trigger="hover"
  340. scrollable
  341. v-model:value={state.tempSubjectId}
  342. onUpdate:value={() => {
  343. forms.subjectId = state.tempSubjectId;
  344. onSearch();
  345. }}
  346. key={subject.value}
  347. class={[styles.popSelect]}>
  348. <span
  349. class={[
  350. styles.textBtn,
  351. selectChildObj(subject.instruments).selected &&
  352. styles.textBtnActive
  353. ]}>
  354. {selectChildObj(subject.instruments).name || subject.name}
  355. <i class={styles.iconArrow}></i>
  356. </span>
  357. </NPopselect>
  358. ) : (
  359. <span
  360. class={[
  361. styles.textBtn,
  362. forms.subjectId === subject.value && styles.textBtnActive
  363. ]}
  364. onClick={() => {
  365. forms.subjectId = subject.value;
  366. state.tempSubjectId = null;
  367. onSearch();
  368. }}>
  369. {subject.name}
  370. </span>
  371. )
  372. )}
  373. </NSpace>
  374. </NFormItem>
  375. <TheSearch
  376. class={styles.inputSearch}
  377. round
  378. onSearch={(val: string) => {
  379. forms.name = val;
  380. onSearch();
  381. }}
  382. />
  383. </NForm>
  384. </div>
  385. );
  386. }
  387. });