SearchPage.vue 6.1 KB

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