BookmarkSettings.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <template>
  2. <Window ref="window" width="600px" height="95%" @close="close">
  3. <template slot="header">
  4. Настроить закладки
  5. </template>
  6. <div class="col 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 no-wrap bg-grey-3">
  19. <q-btn class="q-my-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 class="space"/>
  38. </div>
  39. <div class="col fit tree">
  40. <div v-show="nodes.length" class="checkbox-tick-all">
  41. <q-checkbox v-model="tickAll" @input="makeTickAll" size="36px" label="Выбрать все" />
  42. </div>
  43. <q-tree
  44. class="q-my-xs"
  45. :nodes="nodes"
  46. node-key="key"
  47. tick-strategy="leaf"
  48. :selected.sync="selected"
  49. :ticked.sync="ticked"
  50. :expanded.sync="expanded"
  51. selected-color="black"
  52. :filter="search"
  53. no-nodes-label="Закладок пока нет"
  54. no-results-label="Ничего не найдено"
  55. >
  56. <template v-slot:default-header="p">
  57. <div class="q-px-xs" :class="{selected: selected == p.key}">{{ p.node.label }}</div>
  58. </template>
  59. </q-tree>
  60. </div>
  61. </div>
  62. </div>
  63. </Window>
  64. </template>
  65. <script>
  66. //-----------------------------------------------------------------------------
  67. import Vue from 'vue';
  68. import Component from 'vue-class-component';
  69. import _ from 'lodash';
  70. import Window from '../../share/Window.vue';
  71. import * as lu from '../linkUtils';
  72. import rstore from '../../../store/modules/reader';
  73. const BookmarkSettingsProps = Vue.extend({
  74. props: {
  75. libs: Object,
  76. addBookmarkVisible: Boolean,
  77. }
  78. });
  79. export default @Component({
  80. components: {
  81. Window,
  82. },
  83. watch: {
  84. ticked: function() {
  85. this.checkAllTicked();
  86. },
  87. }
  88. })
  89. class BookmarkSettings extends BookmarkSettingsProps {
  90. search = '';
  91. selected = '';
  92. ticked = [];
  93. expanded = [];
  94. tickAll = false;
  95. created() {
  96. this.afterInit = true;
  97. }
  98. mounted() {
  99. }
  100. init() {
  101. this.$refs.window.init();
  102. }
  103. get nodes() {
  104. const result = [];
  105. const expanded = [];
  106. this.links = {};
  107. this.libs.groups.forEach(group => {
  108. const rkey = `r-${group.r}`;
  109. const g = {label: group.r, key: rkey, children: []};
  110. this.links[rkey] = {l: group.r, c: ''};
  111. group.list.forEach(link => {
  112. const key = link.l;
  113. g.children.push({
  114. label: (link.c ? link.c + ' ': '') + lu.removeOrigin(link.l),
  115. key
  116. });
  117. this.links[key] = link;
  118. if (link.l == this.libs.startLink && expanded.indexOf(rkey) < 0) {
  119. expanded.push(rkey);
  120. }
  121. });
  122. result.push(g);
  123. });
  124. if (this.afterInit) {
  125. this.$nextTick(() => {
  126. this.expanded = expanded;
  127. });
  128. this.afterInit = false;
  129. }
  130. return result;
  131. }
  132. makeTickAll() {
  133. if (this.tickAll) {
  134. const newTicked = [];
  135. for (const key of Object.keys(this.links)) {
  136. if (key.indexOf('r-') != 0)
  137. newTicked.push(key);
  138. }
  139. this.ticked = newTicked;
  140. } else {
  141. this.ticked = [];
  142. }
  143. }
  144. checkAllTicked() {
  145. const ticked = new Set(this.ticked);
  146. let newTickAll = !!(this.nodes.length);
  147. for (const key of Object.keys(this.links)) {
  148. if (key.indexOf('r-') != 0 && !ticked.has(key))
  149. newTickAll = false;
  150. }
  151. this.tickAll = newTickAll;
  152. }
  153. resetSearch() {
  154. this.search = '';
  155. this.$refs.search.focus();
  156. }
  157. openSelected() {
  158. if (!this.selected)
  159. return;
  160. if (this.selected.indexOf('r-') === 0) {//rootLink
  161. this.$emit('do-action', {action: 'setRootLink', data: this.links[this.selected].l});
  162. } else {//selectedLink
  163. this.$emit('do-action', {action: 'setSelectedLink', data: this.links[this.selected].l});
  164. }
  165. this.close();
  166. }
  167. editBookmark() {
  168. this.$emit('do-action', {action: 'editBookmark', data: {link: this.links[this.selected].l, desc: this.links[this.selected].c}});
  169. }
  170. addBookmark() {
  171. this.$emit('do-action', {action: 'addBookmark'});
  172. }
  173. async delBookmark() {
  174. const newLibs = _.cloneDeep(this.libs);
  175. if (await this.$root.stdDialog.confirm(`Подтвердите удаление ${this.ticked.length} закладок:`, ' ')) {
  176. const ticked = new Set(this.ticked);
  177. for (let i = newLibs.groups.length - 1; i >= 0; i--) {
  178. const g = newLibs.groups[i];
  179. for (let j = g.list.length - 1; j >= 0; j--) {
  180. if (ticked.has(g.list[j].l)) {
  181. delete g.list[j];
  182. }
  183. }
  184. g.list = g.list.filter(v => v);
  185. if (!g.list.length)
  186. delete newLibs.groups[i];
  187. else {
  188. const item = lu.getListItemByLink(g.list, g.s);
  189. if (!item)
  190. g.s = g.list[0].l;
  191. }
  192. }
  193. newLibs.groups = newLibs.groups.filter(v => v);
  194. this.ticked = [];
  195. this.selected = '';
  196. this.$emit('do-action', {action: 'setLibs', data: newLibs});
  197. }
  198. }
  199. moveBookmark(down = false) {
  200. const newLibs = _.cloneDeep(this.libs);
  201. const ticked = new Set(this.ticked);
  202. let moved = false;
  203. let prevFull = false;
  204. if (!down) {
  205. for (let i = 0; i < newLibs.groups.length; i++) {
  206. const g = newLibs.groups[i];
  207. let count = 0;
  208. for (let j = 0; j < g.list.length; j++) {
  209. if (ticked.has(g.list[j].l)) {
  210. if (j > 0 && !ticked.has(g.list[j - 1].l)) {
  211. [g.list[j], g.list[j - 1]] = [g.list[j - 1], g.list[j]];
  212. moved = true;
  213. }
  214. count++;
  215. }
  216. }
  217. if (count == g.list.length && !prevFull && i > 0) {
  218. const gs = newLibs.groups;
  219. [gs[i], gs[i - 1]] = [gs[i - 1], gs[i]];
  220. moved = true;
  221. } else
  222. prevFull = (count == g.list.length);
  223. }
  224. } else {
  225. for (let i = newLibs.groups.length - 1; i >= 0; i--) {
  226. const g = newLibs.groups[i];
  227. let count = 0;
  228. for (let j = g.list.length - 1; j >= 0; j--) {
  229. if (ticked.has(g.list[j].l)) {
  230. if (j < g.list.length - 1 && !ticked.has(g.list[j + 1].l)) {
  231. [g.list[j], g.list[j + 1]] = [g.list[j + 1], g.list[j]];
  232. moved = true;
  233. }
  234. count++;
  235. }
  236. }
  237. if (count == g.list.length && !prevFull && i < newLibs.groups.length - 1) {
  238. const gs = newLibs.groups;
  239. [gs[i], gs[i + 1]] = [gs[i + 1], gs[i]];
  240. moved = true;
  241. } else
  242. prevFull = (count == g.list.length);
  243. }
  244. }
  245. if (moved)
  246. this.$emit('do-action', {action: 'setLibs', data: newLibs});
  247. }
  248. async setDefaultBookmarks() {
  249. const result = await this.$root.stdDialog.prompt(`Введите 'да' для сброса всех закладок в предустановленные значения:`, ' ', {
  250. inputValidator: (str) => { if (str && str.toLowerCase() === 'да') return true; else return 'Удаление не подтверждено'; },
  251. });
  252. if (result && result.value && result.value.toLowerCase() == 'да') {
  253. this.$emit('do-action', {action: 'setLibs', data: _.cloneDeep(
  254. Object.assign({helpShowed: true}, rstore.libsDefaults)
  255. )});
  256. }
  257. }
  258. close() {
  259. this.afterInit = false;
  260. this.$emit('close');
  261. }
  262. keyHook(event) {
  263. if (this.addBookmarkVisible)
  264. return false;
  265. if (event.type == 'keydown' && event.key == 'Escape') {
  266. this.close();
  267. return true;
  268. }
  269. return false;
  270. }
  271. }
  272. //-----------------------------------------------------------------------------
  273. </script>
  274. <style scoped>
  275. .top-panel {
  276. height: 50px;
  277. border-bottom: 1px solid gray;
  278. padding: 0 10px 0 12px;
  279. }
  280. .left-panel {
  281. width: 60px;
  282. height: 100%;
  283. border-right: 1px solid gray;
  284. overflow-x: hidden;
  285. overflow-y: auto;
  286. }
  287. .tree {
  288. padding: 0px 10px 10px 10px;
  289. overflow-x: auto;
  290. overflow-y: auto;
  291. }
  292. .selected {
  293. text-shadow: 0 0 20px yellow, 0 0 15px yellow, 0 0 10px yellow, 0 0 10px yellow, 0 0 5px yellow;
  294. }
  295. .checkbox-tick-all {
  296. border-bottom: 1px solid #bbbbbb;
  297. margin-bottom: 7px;
  298. padding: 5px 5px 2px 16px;
  299. }
  300. .space {
  301. min-height: 1px;
  302. width: 1px;
  303. }
  304. </style>