SearchPage.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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="findNext"><i class="el-icon-arrow-down"></i></el-button>
  18. <el-button @click="findPrev"><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 = 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. 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;
  78. this.initStep = false;
  79. this.header = 'Найти';
  80. await this.$nextTick();
  81. this.$refs.input.focus();
  82. this.$refs.input.select();
  83. }
  84. get foundText() {
  85. if (this.foundList.length && this.foundCur >= 0)
  86. return `${this.foundCur}/${this.foundList.length}`;
  87. else
  88. return '';
  89. }
  90. find() {
  91. let foundList = [];
  92. let i = 0;
  93. while (i < this.text.length) {
  94. i++;
  95. }
  96. this.foundList = foundList;
  97. }
  98. findNext() {
  99. console.log('1');
  100. }
  101. findPrev() {
  102. console.log('2');
  103. }
  104. close() {
  105. this.stopInit = true;
  106. this.$emit('search-toggle');
  107. }
  108. keyHook(event) {
  109. //недостатки сторонних ui
  110. if (document.activeElement === this.$refs.input && event.type == 'keydown' && event.key == 'Enter') {
  111. this.find();
  112. }
  113. if (event.type == 'keydown' && (event.code == 'Escape')) {
  114. this.close();
  115. }
  116. return true;
  117. }
  118. }
  119. //-----------------------------------------------------------------------------
  120. </script>
  121. <style scoped>
  122. .main {
  123. position: absolute;
  124. width: 100%;
  125. height: 100%;
  126. z-index: 40;
  127. display: flex;
  128. flex-direction: column;
  129. justify-content: center;
  130. align-items: center;
  131. }
  132. .mainWindow {
  133. width: 100%;
  134. max-width: 500px;
  135. height: 125px;
  136. display: flex;
  137. position: relative;
  138. top: -50px;
  139. }
  140. .content {
  141. flex: 1;
  142. display: flex;
  143. justify-content: center;
  144. align-items: center;
  145. padding: 10px;
  146. }
  147. .input {
  148. display: flex;
  149. margin: 0;
  150. padding: 0;
  151. width: 100%;
  152. position: relative;
  153. }
  154. .button-group {
  155. width: 150px;
  156. margin: 0;
  157. padding: 0;
  158. }
  159. .el-button {
  160. padding: 9px 17px 9px 17px;
  161. }
  162. i {
  163. font-size: 20px;
  164. }
  165. </style>