BookmarkSettings.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <template>
  2. <Window ref="window" width="600px" height="95%" @close="close">
  3. <template slot="header">
  4. Настроить закладки
  5. </template>
  6. <div class="column fit">
  7. <div class="row items-center top-panel bg-grey-3">
  8. <q-btn class="q-mr-md" round dense color="blue" icon="la la-check" @click.stop="openSelected" size="16px" :disabled="!selected">
  9. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">Открыть выбранную закладку</q-tooltip>
  10. </q-btn>
  11. <q-input class="col" ref="search" rounded outlined dense bg-color="white" placeholder="Найти" v-model="search">
  12. <template v-slot:append>
  13. <q-icon v-if="search !== ''" name="la la-times" class="cursor-pointer" @click="resetSearch"/>
  14. </template>
  15. </q-input>
  16. </div>
  17. <div class="col row">
  18. <div class="left-panel column items-center bg-grey-3">
  19. <q-btn class="q-mb-sm" round dense color="blue" icon="la la-plus" @click.stop="addBookmark" size="14px">
  20. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">Добавить закладку</q-tooltip>
  21. </q-btn>
  22. <q-btn class="q-mb-sm" round dense color="blue" icon="la la-minus" @click.stop="delBookmark" size="14px" :disabled="!ticked.length">
  23. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">Удалить отмеченные закладки</q-tooltip>
  24. </q-btn>
  25. <q-btn class="q-mb-sm" round dense color="blue" icon="la la-edit" @click.stop="editBookmark" size="14px" :disabled="!selected || selected.indexOf('r-') == 0">
  26. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">Редактировать закладку</q-tooltip>
  27. </q-btn>
  28. <q-btn class="q-mb-sm" round dense color="blue" icon="la la-arrow-up" @click.stop="moveBookmark(false)" size="14px" :disabled="!ticked.length">
  29. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">Переместить отмеченные вверх</q-tooltip>
  30. </q-btn>
  31. <q-btn class="q-mb-sm" round dense color="blue" icon="la la-arrow-down" @click.stop="moveBookmark(true)" size="14px" :disabled="!ticked.length">
  32. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">Переместить отмеченные вниз</q-tooltip>
  33. </q-btn>
  34. <q-btn class="q-mb-sm" round dense color="blue" icon="la la-broom" @click.stop="setDefaultBookmarks" size="14px">
  35. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">Установить по умолчанию</q-tooltip>
  36. </q-btn>
  37. </div>
  38. <div class="col tree">
  39. <q-tree
  40. :nodes="nodes"
  41. node-key="key"
  42. tick-strategy="leaf"
  43. :selected.sync="selected"
  44. :ticked.sync="ticked"
  45. :expanded.sync="expanded"
  46. selected-color="black"
  47. :filter="search"
  48. no-nodes-label="Закладок пока нет"
  49. no-results-label="Ничего не найдено"
  50. >
  51. <template v-slot:default-header="p">
  52. <div class="q-px-xs" :class="{selected: selected == p.key}">{{ p.node.label }}</div>
  53. </template>
  54. </q-tree>
  55. </div>
  56. </div>
  57. </div>
  58. </Window>
  59. </template>
  60. <script>
  61. //-----------------------------------------------------------------------------
  62. import Vue from 'vue';
  63. import Component from 'vue-class-component';
  64. import _ from 'lodash';
  65. import Window from '../../share/Window.vue';
  66. import * as lu from '../linkUtils';
  67. import rstore from '../../../store/modules/reader';
  68. const BookmarkSettingsProps = Vue.extend({
  69. props: {
  70. libs: Object,
  71. addBookmarkVisible: Boolean,
  72. }
  73. });
  74. export default @Component({
  75. components: {
  76. Window,
  77. },
  78. watch: {
  79. libs: function() {
  80. },
  81. }
  82. })
  83. class BookmarkSettings extends BookmarkSettingsProps {
  84. search = '';
  85. selected = '';
  86. ticked = [];
  87. expanded = [];
  88. created() {
  89. this.afterInit = true;
  90. }
  91. mounted() {
  92. }
  93. init() {
  94. this.$refs.window.init();
  95. }
  96. get nodes() {
  97. const result = [];
  98. const expanded = [];
  99. this.links = {};
  100. this.libs.groups.forEach(group => {
  101. const rkey = `r-${group.r}`;
  102. const g = {label: group.r, key: rkey, children: []};
  103. this.links[rkey] = {l: group.r, c: ''};
  104. group.list.forEach(link => {
  105. const key = link.l;
  106. g.children.push({
  107. label: (link.c ? link.c + ' ': '') + lu.removeOrigin(link.l),
  108. key
  109. });
  110. this.links[key] = link;
  111. if (link.l == this.libs.startLink && expanded.indexOf(rkey) < 0) {
  112. expanded.push(rkey);
  113. }
  114. });
  115. result.push(g);
  116. });
  117. if (this.afterInit) {
  118. this.$nextTick(() => {
  119. this.expanded = expanded;
  120. });
  121. this.afterInit = false;
  122. }
  123. return result;
  124. }
  125. resetSearch() {
  126. this.search = '';
  127. this.$refs.search.focus();
  128. }
  129. openSelected() {
  130. if (!this.selected)
  131. return;
  132. if (this.selected.indexOf('r-') === 0) {//rootLink
  133. this.$emit('do-action', {action: 'setRootLink', data: this.links[this.selected].l});
  134. } else {//selectedLink
  135. this.$emit('do-action', {action: 'setSelectedLink', data: this.links[this.selected].l});
  136. }
  137. this.close();
  138. }
  139. editBookmark() {
  140. this.$emit('do-action', {action: 'editBookmark', data: {link: this.links[this.selected].l, desc: this.links[this.selected].c}});
  141. }
  142. addBookmark() {
  143. this.$emit('do-action', {action: 'addBookmark'});
  144. }
  145. async delBookmark() {
  146. const newLibs = _.cloneDeep(this.libs);
  147. if (await this.$root.stdDialog.confirm(`Подтвердите удаление ${this.ticked.length} закладок:`, ' ')) {
  148. const ticked = new Set(this.ticked);
  149. for (let i = newLibs.groups.length - 1; i >= 0; i--) {
  150. const g = newLibs.groups[i];
  151. for (let j = g.list.length - 1; j >= 0; j--) {
  152. if (ticked.has(g.list[j].l)) {
  153. delete g.list[j];
  154. }
  155. }
  156. g.list = g.list.filter(v => v);
  157. if (!g.list.length)
  158. delete newLibs.groups[i];
  159. else {
  160. const item = lu.getListItemByLink(g.list, g.s);
  161. if (!item)
  162. g.s = g.list[0].l;
  163. }
  164. }
  165. newLibs.groups = newLibs.groups.filter(v => v);
  166. this.ticked = [];
  167. this.selected = '';
  168. this.$emit('do-action', {action: 'setLibs', data: newLibs});
  169. }
  170. }
  171. moveBookmark(down = false) {
  172. const newLibs = _.cloneDeep(this.libs);
  173. const ticked = new Set(this.ticked);
  174. let moved = false;
  175. let prevFull = false;
  176. if (!down) {
  177. for (let i = 0; i < newLibs.groups.length; i++) {
  178. const g = newLibs.groups[i];
  179. let count = 0;
  180. for (let j = 0; j < g.list.length; j++) {
  181. if (ticked.has(g.list[j].l)) {
  182. if (j > 0 && !ticked.has(g.list[j - 1].l)) {
  183. [g.list[j], g.list[j - 1]] = [g.list[j - 1], g.list[j]];
  184. moved = true;
  185. }
  186. count++;
  187. }
  188. }
  189. if (count == g.list.length && !prevFull && i > 0) {
  190. const gs = newLibs.groups;
  191. [gs[i], gs[i - 1]] = [gs[i - 1], gs[i]];
  192. moved = true;
  193. } else
  194. prevFull = (count == g.list.length);
  195. }
  196. } else {
  197. for (let i = newLibs.groups.length - 1; i >= 0; i--) {
  198. const g = newLibs.groups[i];
  199. let count = 0;
  200. for (let j = g.list.length - 1; j >= 0; j--) {
  201. if (ticked.has(g.list[j].l)) {
  202. if (j < g.list.length - 1 && !ticked.has(g.list[j + 1].l)) {
  203. [g.list[j], g.list[j + 1]] = [g.list[j + 1], g.list[j]];
  204. moved = true;
  205. }
  206. count++;
  207. }
  208. }
  209. if (count == g.list.length && !prevFull && i < newLibs.groups.length - 1) {
  210. const gs = newLibs.groups;
  211. [gs[i], gs[i + 1]] = [gs[i + 1], gs[i]];
  212. moved = true;
  213. } else
  214. prevFull = (count == g.list.length);
  215. }
  216. }
  217. if (moved)
  218. this.$emit('do-action', {action: 'setLibs', data: newLibs});
  219. }
  220. async setDefaultBookmarks() {
  221. const result = await this.$root.stdDialog.prompt(`Введите 'да' для сброса всех закладок в предустановленные значения:`, ' ', {
  222. inputValidator: (str) => { if (str && str.toLowerCase() === 'да') return true; else return 'Удаление не подтверждено'; },
  223. });
  224. if (result && result.value && result.value.toLowerCase() == 'да') {
  225. this.$emit('do-action', {action: 'setLibs', data: _.cloneDeep(rstore.libsDefaults)});
  226. }
  227. }
  228. close() {
  229. this.afterInit = false;
  230. this.$emit('close');
  231. }
  232. keyHook(event) {
  233. if (this.addBookmarkVisible)
  234. return false;
  235. if (event.type == 'keydown' && event.key == 'Escape') {
  236. this.close();
  237. return true;
  238. }
  239. return false;
  240. }
  241. }
  242. //-----------------------------------------------------------------------------
  243. </script>
  244. <style scoped>
  245. .top-panel {
  246. height: 50px;
  247. border-bottom: 1px solid gray;
  248. padding: 0 10px 0 12px;
  249. }
  250. .left-panel {
  251. width: 60px;
  252. border-right: 1px solid gray;
  253. padding: 10px 0 10px 0;
  254. }
  255. .tree {
  256. padding: 10px;
  257. }
  258. .selected {
  259. text-shadow: 0 0 20px yellow, 0 0 15px yellow, 0 0 10px yellow, 0 0 10px yellow, 0 0 5px yellow;
  260. }
  261. </style>