SelectGenreDialog.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <Dialog ref="dialog" v-model="dialogVisible">
  3. <template #header>
  4. <div class="row items-center">
  5. <div style="font-size: 110%">
  6. Выбрать жанры
  7. </div>
  8. </div>
  9. </template>
  10. <div ref="box" class="column q-mt-xs overflow-auto no-wrap" style="width: 370px; padding: 0px 10px 10px 10px;">
  11. <div class="row items-center top-panel bg-grey-3">
  12. <q-input ref="search" v-model="search" class="col" outlined dense bg-color="white" placeholder="Найти" clearable />
  13. </div>
  14. <div v-show="nodes.length" class="checkbox-tick-all">
  15. <q-checkbox v-model="tickAll" size="36px" label="Выбрать/снять все" toggle-order="ft" @update:model-value="makeTickAll" />
  16. </div>
  17. <q-tree
  18. v-model:ticked="ticked"
  19. v-model:expanded="expanded"
  20. class="q-my-xs"
  21. :nodes="nodes"
  22. node-key="key"
  23. tick-strategy="leaf"
  24. selected-color="black"
  25. :filter="search"
  26. no-nodes-label="Жанров нет"
  27. no-results-label="Ничего не найдено"
  28. >
  29. </q-tree>
  30. </div>
  31. <template #footer>
  32. <q-btn class="q-px-md q-ml-sm" color="primary" dense no-caps @click="okClick">
  33. OK
  34. </q-btn>
  35. </template>
  36. </Dialog>
  37. </template>
  38. <script>
  39. //-----------------------------------------------------------------------------
  40. import vueComponent from '../../vueComponent.js';
  41. import Dialog from '../../share/Dialog.vue';
  42. const componentOptions = {
  43. components: {
  44. Dialog
  45. },
  46. watch: {
  47. modelValue(newValue) {
  48. this.dialogVisible = newValue;
  49. if (newValue)
  50. this.init();//no await
  51. },
  52. dialogVisible(newValue) {
  53. this.$emit('update:modelValue', newValue);
  54. },
  55. genre() {
  56. this.updateTicked();
  57. },
  58. ticked() {
  59. this.checkAllTicked();
  60. this.updateGenre();
  61. },
  62. }
  63. };
  64. class GenreSelectDialog {
  65. _options = componentOptions;
  66. _props = {
  67. modelValue: Boolean,
  68. genre: {type: String, value: ''},
  69. genreTree: Array,
  70. };
  71. dialogVisible = false;
  72. search = '';
  73. ticked = [];
  74. expanded = [];
  75. tickAll = false;
  76. allKeys = [];
  77. created() {
  78. }
  79. mounted() {
  80. this.updateTicked();
  81. }
  82. async init() {
  83. await this.$refs.dialog.waitShown();
  84. //чтобы не скакало при поиске
  85. this.$refs.box.style.height = `${document.body.clientHeight - 160}px`;
  86. }
  87. get nodes() {
  88. const result = [];
  89. this.allKeys = [];
  90. for (const section of this.genreTree) {
  91. const rkey = `r-${section.name}`;
  92. const sec = {label: section.name, key: rkey, children: []};
  93. for (const g of section.value) {
  94. sec.children.push({label: g.name, key: g.value});
  95. this.allKeys.push(g.value);
  96. }
  97. result.push(sec);
  98. }
  99. return result;
  100. }
  101. makeTickAll() {
  102. if (this.tickAll) {
  103. const newTicked = [];
  104. for (const key of this.allKeys) {
  105. newTicked.push(key);
  106. }
  107. this.ticked = newTicked;
  108. } else {
  109. this.ticked = [];
  110. this.tickAll = false;
  111. }
  112. }
  113. checkAllTicked() {
  114. const ticked = new Set(this.ticked);
  115. let newTickAll = !!(this.nodes.length);
  116. for (const key of this.allKeys) {
  117. if (!ticked.has(key)) {
  118. newTickAll = false;
  119. break;
  120. }
  121. }
  122. if (this.ticked.length && !newTickAll) {
  123. this.tickAll = undefined;
  124. } else {
  125. this.tickAll = newTickAll;
  126. }
  127. }
  128. updateTicked() {
  129. this.ticked = this.genre.split(',').filter(s => s);
  130. }
  131. updateGenre() {
  132. this.$emit('update:genre', this.ticked.join(','));
  133. }
  134. okClick() {
  135. this.dialogVisible = false;
  136. }
  137. }
  138. export default vueComponent(GenreSelectDialog);
  139. //-----------------------------------------------------------------------------
  140. </script>
  141. <style scoped>
  142. .top-panel {
  143. border-radius: 10px;
  144. padding: 5px;
  145. }
  146. .checkbox-tick-all {
  147. border-bottom: 1px solid #bbbbbb;
  148. margin-bottom: 7px;
  149. padding: 5px 5px 2px 16px;
  150. }
  151. </style>