SearchPage.vue 6.1 KB

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