SearchPage.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <template>
  2. <Window ref="window" height="125px" max-width="600px" :top-shift="-50" @close="close">
  3. <template slot="header">
  4. {{ header }}
  5. </template>
  6. <div class="content">
  7. <span v-show="initStep">{{ initPercentage }}%</span>
  8. <div v-show="!initStep" class="input">
  9. <!--input ref="input"
  10. placeholder="что ищем"
  11. :value="needle" @input="needle = $event.target.value"/-->
  12. <q-input ref="input" class="col" outlined dense
  13. placeholder="что ищем"
  14. v-model="needle"
  15. />
  16. <div style="position: absolute; right: 10px; margin-top: 10px; font-size: 16px;">{{ foundText }}</div>
  17. </div>
  18. <q-btn-group v-show="!initStep" class="button-group row no-wrap">
  19. <q-btn class="button" dense stretch @click="showNext"><q-icon class="icon" name="o_expand_more" dense size="30px"/></q-btn>
  20. <q-btn class="button" dense stretch @click="showPrev"><q-icon class="icon" name="o_expand_less" dense size="30px"/></q-btn>
  21. </q-btn-group>
  22. </div>
  23. </Window>
  24. </template>
  25. <script>
  26. //-----------------------------------------------------------------------------
  27. import Vue from 'vue';
  28. import Component from 'vue-class-component';
  29. import Window from '../../share/Window.vue';
  30. import {sleep} from '../../../share/utils';
  31. export default @Component({
  32. components: {
  33. Window,
  34. },
  35. watch: {
  36. needle: function() {
  37. this.find();
  38. },
  39. foundText: function(newValue) {
  40. const el = this.$refs.input.$el.querySelector('label div div div input');
  41. if (el)
  42. el.style.paddingRight = newValue.length*12 + 'px';
  43. },
  44. },
  45. })
  46. class SearchPage extends Vue {
  47. header = null;
  48. initStep = null;
  49. initPercentage = 0;
  50. needle = null;
  51. foundList = [];
  52. foundCur = -1;
  53. created() {
  54. this.commit = this.$store.commit;
  55. this.reader = this.$store.state.reader;
  56. }
  57. async init(parsed) {
  58. this.$refs.window.init();
  59. if (this.parsed != parsed) {
  60. this.initStep = true;
  61. this.stopInit = false;
  62. this.header = 'Подготовка';
  63. await this.$nextTick();
  64. await sleep(10);
  65. let nextPerc = 0;
  66. let text = '';
  67. for (let i = 0; i < parsed.para.length; i++) {
  68. const p = parsed.para[i];
  69. const parts = parsed.splitToStyle(p.text);
  70. if (this.stopInit)
  71. return;
  72. for (const part of parts)
  73. text += part.text;
  74. const perc = Math.round(i/parsed.para.length*100);
  75. if (perc > nextPerc) {
  76. this.initPercentage = perc;
  77. await sleep(1);
  78. nextPerc = perc + 10;
  79. }
  80. }
  81. this.text = text.toLowerCase();
  82. this.initStep = false;
  83. this.needle = '';
  84. this.foundList = [];
  85. this.foundCur = -1;
  86. this.parsed = parsed;
  87. }
  88. this.header = 'Найти';
  89. await this.$nextTick();
  90. this.$refs.input.focus();
  91. this.$refs.input.select();
  92. }
  93. get foundText() {
  94. if (this.foundList.length && this.foundCur >= 0)
  95. return `${this.foundCur + 1}/${this.foundList.length}`;
  96. else
  97. return '';
  98. }
  99. find() {
  100. let foundList = [];
  101. if (this.needle) {
  102. const needle = this.needle.toLowerCase();
  103. let i = 0;
  104. while (i < this.text.length) {
  105. const found = this.text.indexOf(needle, i);
  106. if (found >= 0)
  107. foundList.push(found);
  108. i = (found >= 0 ? found + 1 : this.text.length);
  109. }
  110. }
  111. this.foundList = foundList;
  112. this.foundCur = -1;
  113. this.showNext();
  114. }
  115. showNext() {
  116. const next = this.foundCur + 1;
  117. if (next < this.foundList.length)
  118. this.foundCur = next;
  119. else
  120. this.foundCur = (this.foundList.length ? 0 : -1);
  121. if (this.foundCur >= 0) {
  122. this.$emit('start-text-search', {needle: this.needle.toLowerCase()});
  123. this.$emit('book-pos-changed', {bookPos: this.foundList[this.foundCur]});
  124. } else {
  125. this.$emit('stop-text-search');
  126. }
  127. this.$refs.input.focus();
  128. }
  129. showPrev() {
  130. const prev = this.foundCur - 1;
  131. if (prev >= 0)
  132. this.foundCur = prev;
  133. else
  134. this.foundCur = this.foundList.length - 1;
  135. if (this.foundCur >= 0) {
  136. this.$emit('start-text-search', {needle: this.needle.toLowerCase()});
  137. this.$emit('book-pos-changed', {bookPos: this.foundList[this.foundCur]});
  138. } else {
  139. this.$emit('stop-text-search');
  140. }
  141. this.$refs.input.focus();
  142. }
  143. close() {
  144. this.stopInit = true;
  145. this.$emit('search-toggle');
  146. }
  147. keyHook(event) {
  148. //недостатки сторонних ui
  149. if (document.activeElement === this.$refs.input && event.type == 'keydown' && event.key == 'Enter') {
  150. this.showNext();
  151. }
  152. if (event.type == 'keydown' && (event.code == 'Escape')) {
  153. this.close();
  154. }
  155. return true;
  156. }
  157. }
  158. //-----------------------------------------------------------------------------
  159. </script>
  160. <style scoped>
  161. .content {
  162. flex: 1;
  163. display: flex;
  164. justify-content: center;
  165. align-items: center;
  166. padding: 10px;
  167. min-width: 430px;
  168. }
  169. .input {
  170. display: flex;
  171. margin: 0;
  172. padding: 0;
  173. width: 100%;
  174. position: relative;
  175. }
  176. .button-group {
  177. width: 120px;
  178. margin: 0;
  179. padding: 0;
  180. height: 37px;
  181. }
  182. .button {
  183. padding: 9px 17px 9px 17px;
  184. width: 60px;
  185. }
  186. .icon {
  187. top: -8px;
  188. }
  189. </style>