search-group-resources.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import { defineComponent, nextTick, onMounted, reactive, ref } from 'vue';
  2. import styles from './index.module.less';
  3. import {
  4. NButton,
  5. NForm,
  6. NFormItem,
  7. NImage,
  8. NPopselect,
  9. NSpace
  10. } from 'naive-ui';
  11. import TheSearch from '/src/components/TheSearch';
  12. import { resourceTypeArray } from '/src/utils/searchArray';
  13. import { useCatchStore } from '/src/store/modules/catchData';
  14. import isCollaose from '../../images/isCollaose.png';
  15. export default defineComponent({
  16. name: 'search-group',
  17. emits: ['search'],
  18. setup(props, { emit }) {
  19. const catchStore = useCatchStore();
  20. const resourceList = ref([] as any[]);
  21. const forms = reactive({
  22. type: 'MUSIC', //
  23. name: '',
  24. bookVersionId: null,
  25. subjectId: null
  26. });
  27. const state = reactive({
  28. tempSubjectId: null
  29. });
  30. const onSearch = () => {
  31. emit('search', forms);
  32. };
  33. const collapseWrapRef = ref();
  34. const divDomList = ref([] as any);
  35. const orginHeight = ref(0);
  36. const line = ref(0);
  37. const isCollapse = ref(false);
  38. const loadingCollapse = ref(false); // 是否加载完成
  39. const musicCateRef = (el: any) => {
  40. if (el?.selfElRef) {
  41. divDomList.value.push(el.selfElRef.parentNode);
  42. }
  43. };
  44. const setCollapse = (flag: boolean) => {
  45. isCollapse.value = flag;
  46. getLive();
  47. };
  48. const getLive = () => {
  49. try {
  50. divDomList.value = [...new Set(divDomList.value)];
  51. let offsetLeft = -1;
  52. divDomList.value.forEach((item: any, index: number) => {
  53. if (index === 0) {
  54. line.value = 1;
  55. offsetLeft = item.offsetLeft;
  56. } else if (item.offsetLeft === offsetLeft && index != 0) {
  57. // 如果某个标签的offsetLeft和第一个标签的offsetLeft相等 说明增加了一行
  58. line.value++;
  59. }
  60. if (!isCollapse.value) {
  61. if (line.value > 1) {
  62. //从第3行开始 隐藏标签
  63. item.style.display = 'none';
  64. // 显示展开按钮 class名chu是在前面动态添加的
  65. } else {
  66. item.style.display = 'block';
  67. }
  68. } else {
  69. item.style.display = 'block';
  70. }
  71. });
  72. loadingCollapse.value = true;
  73. } catch {
  74. //
  75. }
  76. };
  77. const selectChildObj = (item: any) => {
  78. const obj: any = {};
  79. item?.forEach((child: any) => {
  80. if (child.id === forms.subjectId) {
  81. obj.selected = true;
  82. obj.name = child.name;
  83. }
  84. });
  85. return obj;
  86. };
  87. onMounted(async () => {
  88. resourceList.value = [
  89. // {
  90. // label: '全部',
  91. // value: ''
  92. // },
  93. ...resourceTypeArray
  94. ];
  95. // 获取教材分类列表
  96. await catchStore.getMusicSheetCategory();
  97. // 获取声部列表
  98. await catchStore.getSubjects();
  99. // 这里开始
  100. // if (forms.type === 'MUSIC') {
  101. // orginHeight.value = collapseWrapRef.value.offsetHeight;
  102. // // 默认隐藏
  103. // getLive();
  104. // }
  105. });
  106. return () => (
  107. <div class={styles.searchGroup}>
  108. <div class={styles.searchCatatory}>
  109. <NSpace size="small" class={styles.btnType}>
  110. {resourceList.value.map((item: any) => (
  111. <NButton
  112. type={forms.type === item.value ? 'primary' : 'default'}
  113. secondary={forms.type === item.value ? false : true}
  114. round
  115. size="small"
  116. focusable={false}
  117. onClick={() => {
  118. forms.type = item.value;
  119. forms.subjectId = null;
  120. state.tempSubjectId = null;
  121. onSearch();
  122. nextTick(() => {
  123. if (forms.type === 'MUSIC') {
  124. orginHeight.value = collapseWrapRef.value?.offsetHeight;
  125. // hiddenHeight.value =
  126. // collapseWrapRef.value.offsetHeight / line.value || 0;
  127. // 默认隐藏
  128. getLive();
  129. } else {
  130. divDomList.value = [];
  131. }
  132. });
  133. }}>
  134. {item.label}
  135. </NButton>
  136. ))}
  137. </NSpace>
  138. </div>
  139. <NForm labelAlign="left" labelPlacement="left">
  140. {/* {forms.type === 'MUSIC' && (
  141. <div class={[styles.collapsSection]}>
  142. <NFormItem label="教材:">
  143. <div
  144. class={[
  145. styles.collapseWrap,
  146. loadingCollapse.value ? '' : styles.hideButton,
  147. isCollapse.value ? '' : styles.isHidden
  148. ]}
  149. ref={collapseWrapRef}>
  150. <NSpace class={[styles.spaceSection]}>
  151. {catchStore.getAllMusicCategories.map((music: any) => (
  152. <NButton
  153. data-id={music.id}
  154. ref={musicCateRef}
  155. secondary={forms.bookVersionId === music.id}
  156. quaternary={forms.bookVersionId !== music.id}
  157. strong
  158. focusable={false}
  159. type={
  160. forms.bookVersionId === music.id
  161. ? 'primary'
  162. : 'default'
  163. }
  164. onClick={() => {
  165. forms.bookVersionId = music.id;
  166. onSearch();
  167. }}>
  168. {music.name}
  169. </NButton>
  170. ))}
  171. {line.value > 1 && (
  172. <div
  173. class={styles.collaoseGroup}
  174. onClick={() => {
  175. setCollapse(!isCollapse.value);
  176. }}>
  177. <NImage
  178. previewDisabled
  179. src={isCollaose}
  180. class={[
  181. styles.collaoseBtn,
  182. isCollapse.value ? styles.isStart : ''
  183. ]}></NImage>
  184. </div>
  185. )}
  186. </NSpace>
  187. </div>
  188. </NFormItem>
  189. </div>
  190. )} */}
  191. <NFormItem label="乐器:">
  192. <NSpace class={styles.spaceSection2}>
  193. {catchStore.getSubjectInstruments.map((subject: any) =>
  194. subject.instruments && subject.instruments.length > 1 ? (
  195. <NPopselect
  196. options={subject.instruments}
  197. trigger="hover"
  198. scrollable
  199. v-model:value={state.tempSubjectId}
  200. onUpdate:value={() => {
  201. forms.subjectId = state.tempSubjectId;
  202. onSearch();
  203. }}
  204. key={subject.value}
  205. class={[styles.popSelect]}>
  206. <span
  207. class={[
  208. styles.textBtn,
  209. selectChildObj(subject.instruments).selected &&
  210. styles.textBtnActive
  211. ]}>
  212. {selectChildObj(subject.instruments).name || subject.name}
  213. <i class={styles.iconArrow}></i>
  214. </span>
  215. </NPopselect>
  216. ) : (
  217. <span
  218. class={[
  219. styles.textBtn,
  220. forms.subjectId === subject.value && styles.textBtnActive
  221. ]}
  222. onClick={() => {
  223. forms.subjectId = subject.value;
  224. state.tempSubjectId = null;
  225. onSearch();
  226. }}>
  227. {subject.name}
  228. </span>
  229. )
  230. )}
  231. </NSpace>
  232. </NFormItem>
  233. <TheSearch
  234. class={styles.inputSearch}
  235. round
  236. onSearch={(val: string) => {
  237. forms.name = val;
  238. onSearch();
  239. }}
  240. />
  241. </NForm>
  242. </div>
  243. );
  244. }
  245. });