search-group-resources.tsx 7.2 KB

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