SearchPage.vue 6.1 KB

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