SearchPage.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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" @keydown="inputKeyDown"
  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 style="top: -6px" name="la la-angle-down" dense size="22px"/></q-btn>
  20. <q-btn class="button" dense stretch @click="showPrev"><q-icon style="top: -4px" class="icon" name="la la-angle-up" dense size="22px"/></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. //недостатки сторонних ui
  41. const el = this.$refs.input.$el.querySelector('label div div div input');
  42. if (el)
  43. el.style.paddingRight = newValue.length*12 + 'px';
  44. },
  45. },
  46. })
  47. class SearchPage extends Vue {
  48. header = null;
  49. initStep = null;
  50. initPercentage = 0;
  51. needle = null;
  52. foundList = [];
  53. foundCur = -1;
  54. created() {
  55. this.commit = this.$store.commit;
  56. this.reader = this.$store.state.reader;
  57. }
  58. async init(parsed) {
  59. this.$refs.window.init();
  60. if (this.parsed != parsed) {
  61. this.initStep = true;
  62. this.stopInit = false;
  63. this.header = 'Подготовка';
  64. await this.$nextTick();
  65. await sleep(10);
  66. let nextPerc = 0;
  67. let text = '';
  68. for (let i = 0; i < parsed.para.length; i++) {
  69. const p = parsed.para[i];
  70. const parts = parsed.splitToStyle(p.text);
  71. if (this.stopInit)
  72. return;
  73. for (const part of parts)
  74. text += part.text;
  75. const perc = Math.round(i/parsed.para.length*100);
  76. if (perc > nextPerc) {
  77. this.initPercentage = perc;
  78. await sleep(1);
  79. nextPerc = perc + 10;
  80. }
  81. }
  82. this.text = text.toLowerCase();
  83. this.initStep = false;
  84. this.needle = '';
  85. this.foundList = [];
  86. this.foundCur = -1;
  87. this.parsed = parsed;
  88. }
  89. this.header = 'Найти';
  90. await this.$nextTick();
  91. this.$refs.input.focus();
  92. this.$refs.input.select();
  93. }
  94. get foundText() {
  95. if (this.foundList.length && this.foundCur >= 0)
  96. return `${this.foundCur + 1}/${this.foundList.length}`;
  97. else
  98. return '';
  99. }
  100. find() {
  101. let foundList = [];
  102. if (this.needle) {
  103. const needle = this.needle.toLowerCase();
  104. let i = 0;
  105. while (i < this.text.length) {
  106. const found = this.text.indexOf(needle, i);
  107. if (found >= 0)
  108. foundList.push(found);
  109. i = (found >= 0 ? found + 1 : this.text.length);
  110. }
  111. }
  112. this.foundList = foundList;
  113. this.foundCur = -1;
  114. this.showNext();
  115. }
  116. showNext() {
  117. const next = this.foundCur + 1;
  118. if (next < this.foundList.length)
  119. this.foundCur = next;
  120. else
  121. this.foundCur = (this.foundList.length ? 0 : -1);
  122. if (this.foundCur >= 0) {
  123. this.$emit('start-text-search', {needle: this.needle.toLowerCase()});
  124. this.$emit('book-pos-changed', {bookPos: this.foundList[this.foundCur]});
  125. } else {
  126. this.$emit('stop-text-search');
  127. }
  128. this.$refs.input.focus();
  129. }
  130. showPrev() {
  131. const prev = this.foundCur - 1;
  132. if (prev >= 0)
  133. this.foundCur = prev;
  134. else
  135. this.foundCur = this.foundList.length - 1;
  136. if (this.foundCur >= 0) {
  137. this.$emit('start-text-search', {needle: this.needle.toLowerCase()});
  138. this.$emit('book-pos-changed', {bookPos: this.foundList[this.foundCur]});
  139. } else {
  140. this.$emit('stop-text-search');
  141. }
  142. this.$refs.input.focus();
  143. }
  144. close() {
  145. this.stopInit = true;
  146. this.$emit('search-toggle');
  147. }
  148. inputKeyDown(event) {
  149. if (event.key == 'Enter') {
  150. this.showNext();
  151. }
  152. }
  153. keyHook(event) {
  154. if (event.type == 'keydown' && (event.code == 'Escape')) {
  155. this.close();
  156. }
  157. return true;
  158. }
  159. }
  160. //-----------------------------------------------------------------------------
  161. </script>
  162. <style scoped>
  163. .content {
  164. flex: 1;
  165. display: flex;
  166. justify-content: center;
  167. align-items: center;
  168. padding: 10px;
  169. min-width: 430px;
  170. }
  171. .input {
  172. display: flex;
  173. margin: 0;
  174. padding: 0;
  175. width: 100%;
  176. position: relative;
  177. }
  178. .button-group {
  179. width: 100px;
  180. margin: 0;
  181. padding: 0;
  182. height: 37px;
  183. }
  184. .button {
  185. padding: 9px 17px 9px 17px;
  186. width: 50px;
  187. }
  188. </style>