SearchPage.vue 5.6 KB

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