index.tsx 8.0 KB

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