search-group-resources.tsx 8.8 KB

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