BookmarkSettings.vue 13 KB

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