BookmarkSettings.vue 13 KB

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