123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586 |
- import axios from 'axios';
- import dayjs from 'dayjs';
- import _ from 'lodash';
- import authorBooksStorage from './authorBooksStorage';
- import BookView from './BookView/BookView.vue';
- import LoadingMessage from './LoadingMessage/LoadingMessage.vue';
- import * as utils from '../../share/utils';
- const showMoreCount = 100;//значение для "Показать еще"
- const maxItemCount = 500;//выше этого значения показываем "Загрузка"
- const componentOptions = {
- components: {
- BookView,
- LoadingMessage,
- },
- watch: {
- settings() {
- this.loadSettings();
- },
- search: {
- handler() {
- if (!this.isExtendedSearch)
- this.refresh();
- },
- deep: true,
- },
- extSearch: {
- handler() {
- if (this.isExtendedSearch)
- this.refresh();
- },
- deep: true,
- },
- showDeleted() {
- this.refresh();
- },
- },
- };
- export default class BaseList {
- _options = componentOptions;
- _props = {
- list: Object,
- search: Object,
- extSearch: Object,
- genreMap: Object,
- };
-
- error = '';
- loadingMessage = '';
- loadingMessage2 = '';
- //settings
- expandedAuthor = [];
- expandedSeries = [];
- downloadAsZip = false;
- showCounts = true;
- showRates = true;
- showGenres = true;
- showDeleted = false;
- abCacheEnabled = true;
- //stuff
- refreshing = false;
- showMoreCount = showMoreCount;
- maxItemCount = maxItemCount;
- searchResult = {};
- tableData = [];
- created() {
- this.isExtendedSearch = false;
- this.commit = this.$store.commit;
- this.api = this.$root.api;
- this.loadSettings();
- }
- mounted() {
- this.refresh();//no await
- }
- loadSettings() {
- const settings = this.settings;
- this.expandedAuthor = _.cloneDeep(settings.expandedAuthor);
- this.expandedSeries = _.cloneDeep(settings.expandedSeries);
- this.downloadAsZip = settings.downloadAsZip;
- this.showCounts = settings.showCounts;
- this.showRates = settings.showRates;
- this.showGenres = settings.showGenres;
- this.showDeleted = settings.showDeleted;
- this.abCacheEnabled = settings.abCacheEnabled;
- }
- get config() {
- return this.$store.state.config;
- }
- get settings() {
- return this.$store.state.settings;
- }
- get showReadLink() {
- return this.config.bookReadLink != '' || this.list.liberamaReady;
- }
- scrollToTop() {
- this.$emit('listEvent', {action: 'scrollToTop'});
- }
- selectAuthor(author) {
- const search = (this.isExtendedSearch ? this.extSearch : this.search);
- search.author = `=${author}`;
- this.scrollToTop();
- }
- selectSeries(series) {
- const search = (this.isExtendedSearch ? this.extSearch : this.search);
- search.series = `=${series}`;
- }
- selectTitle(title) {
- const search = (this.isExtendedSearch ? this.extSearch : this.search);
- search.title = `=${title}`;
- }
- async download(book, action) {
- if (this.downloadFlag)
- return;
- this.downloadFlag = true;
- (async() => {
- await utils.sleep(200);
- if (this.downloadFlag)
- this.loadingMessage2 = 'Подготовка файла...';
- })();
- try {
- //подготовка
- const response = await this.api.getBookLink(book._uid);
-
- const link = response.link;
- let href = `${window.location.origin}${link}`;
- //downloadAsZip
- if (this.downloadAsZip && (action == 'download' || action == 'copyLink')) {
- href += '/zip';
- //подожлем формирования zip-файла
- await axios.head(href);
- }
- //action
- if (action == 'download') {
- //скачивание
- const d = this.$refs.download;
- d.href = href;
- d.click();
- } else if (action == 'copyLink') {
- //копирование ссылки
- if (await utils.copyTextToClipboard(href))
- this.$root.notify.success('Ссылка успешно скопирована');
- else
- this.$root.stdDialog.alert(
- `Копирование ссылки не удалось. Пожалуйста, попробуйте еще раз.
- <br><br>
- <b>Пояснение</b>: вероятно, браузер запретил копирование, т.к. прошло<br>
- слишком много времени с момента нажатия на кнопку (инициация<br>
- пользовательского события). Сейчас ссылка уже закеширована,<br>
- поэтому повторная попытка должна быть успешной.`, 'Ошибка');
- } else if (action == 'readBook') {
- //читать
- if (this.list.liberamaReady) {
- this.$emit('listEvent', {action: 'submitUrl', data: href});
- } else {
- const bookReadLink = this.config.bookReadLink;
- let url = bookReadLink;
- if (bookReadLink.indexOf('${DOWNLOAD_LINK}') >= 0) {
- url = bookReadLink.replace('${DOWNLOAD_LINK}', href);
- } else if (bookReadLink.indexOf('${DOWNLOAD_URI}') >= 0) {
- const hrefUrl = new URL(href);
- const urlWithoutHost = hrefUrl.pathname + hrefUrl.search + hrefUrl.hash;
- url = bookReadLink.replace('${DOWNLOAD_URI}', urlWithoutHost);
- }
- window.open(url, '_blank');
- }
- } else if (action == 'bookInfo') {
- //информация о книге
- const response = await this.api.getBookInfo(book._uid);
- this.$emit('listEvent', {action: 'bookInfo', data: response.bookInfo});
- }
- } catch(e) {
- this.$root.stdDialog.alert(e.message, 'Ошибка');
- } finally {
- this.downloadFlag = false;
- this.loadingMessage2 = '';
- }
- }
- bookEvent(event) {
- switch (event.action) {
- case 'authorClick':
- this.selectAuthor(event.book.author);
- break;
- case 'seriesClick':
- this.selectSeries(event.book.series);
- break;
- case 'titleClick':
- this.selectTitle(event.book.title);
- break;
- case 'download':
- case 'copyLink':
- case 'readBook':
- case 'bookInfo':
- this.download(event.book, event.action);//no await
- break;
- }
- }
- isExpandedAuthor(item) {
- return this.expandedAuthor.indexOf(item.author) >= 0;
- }
- isExpandedSeries(seriesItem) {
- return this.expandedSeries.indexOf(seriesItem.key) >= 0;
- }
- setSetting(name, newValue) {
- this.commit('setSettings', {[name]: _.cloneDeep(newValue)});
- }
- highlightPageScroller(query) {
- this.$emit('listEvent', {action: 'highlightPageScroller', query});
- }
- async expandSeries(seriesItem) {
- this.$emit('listEvent', {action: 'ignoreScroll'});
- const expandedSeries = _.cloneDeep(this.expandedSeries);
- const key = seriesItem.key;
- if (!this.isExpandedSeries(seriesItem)) {
- expandedSeries.push(key);
- if (expandedSeries.length > 100) {
- expandedSeries.shift();
- }
- this.getSeriesBooks(seriesItem); //no await
- this.setSetting('expandedSeries', expandedSeries);
- } else {
- const i = expandedSeries.indexOf(key);
- if (i >= 0) {
- expandedSeries.splice(i, 1);
- this.setSetting('expandedSeries', expandedSeries);
- }
- }
- }
- async loadAuthorBooks(authorId) {
- try {
- let result;
- if (this.abCacheEnabled) {
- const key = `author-${authorId}-${this.list.inpxHash}`;
- const data = await authorBooksStorage.getData(key);
- if (data) {
- result = JSON.parse(data);
- } else {
- result = await this.api.getAuthorBookList(authorId);
- await authorBooksStorage.setData(key, JSON.stringify(result));
- }
- } else {
- result = await this.api.getAuthorBookList(authorId);
- }
- return result.books;
- } catch (e) {
- this.$root.stdDialog.alert(e.message, 'Ошибка');
- }
- }
- async loadAuthorSeries(authorId) {
- try {
- let result;
- if (this.abCacheEnabled) {
- const key = `author-${authorId}-series-${this.list.inpxHash}`;
- const data = await authorBooksStorage.getData(key);
- if (data) {
- result = JSON.parse(data);
- } else {
- result = await this.api.getAuthorSeriesList(authorId);
- await authorBooksStorage.setData(key, JSON.stringify(result));
- }
- } else {
- result = await this.api.getAuthorSeriesList(authorId);
- }
- return result.series;
- } catch (e) {
- this.$root.stdDialog.alert(e.message, 'Ошибка');
- }
- }
- async loadSeriesBooks(series) {
- try {
- let result;
- if (this.abCacheEnabled) {
- const key = `series-${series}-${this.list.inpxHash}`;
- const data = await authorBooksStorage.getData(key);
- if (data) {
- result = JSON.parse(data);
- } else {
- result = await this.api.getSeriesBookList(series);
- await authorBooksStorage.setData(key, JSON.stringify(result));
- }
- } else {
- result = await this.api.getSeriesBookList(series);
- }
- return result.books;
- } catch (e) {
- this.$root.stdDialog.alert(e.message, 'Ошибка');
- }
- }
- async getSeriesBooks(seriesItem) {
- //блокируем повторный вызов
- if (seriesItem.seriesBookLoading)
- return;
- seriesItem.seriesBookLoading = true;
- try {
- seriesItem.allBooksLoaded = await this.loadSeriesBooks(seriesItem.series);
- if (seriesItem.allBooksLoaded) {
- seriesItem.allBooksLoaded = seriesItem.allBooksLoaded.filter(book => (this.showDeleted || !book.del));
- this.sortSeriesBooks(seriesItem.allBooksLoaded);
- this.showMoreAll(seriesItem);
- }
- } finally {
- seriesItem.seriesBookLoading = false;
- }
- }
- filterBooks(books) {
- const s = this.search;
- const emptyFieldValue = '?';
- const maxUtf8Char = String.fromCodePoint(0xFFFFF);
- const ruAlphabet = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя';
- const enAlphabet = 'abcdefghijklmnopqrstuvwxyz';
- const enru = new Set((ruAlphabet + enAlphabet).split(''));
- const splitAuthor = (author) => {
- if (!author) {
- author = emptyFieldValue;
- }
- const result = author.split(',');
- if (result.length > 1)
- result.push(author);
- return result;
- };
- const filterBySearch = (bookValue, searchValue) => {
- if (!searchValue)
- return true;
- if (!bookValue)
- bookValue = emptyFieldValue;
- bookValue = bookValue.toLowerCase();
- if (searchValue[0] !== '~')
- searchValue = searchValue.toLowerCase();
- //особая обработка префиксов
- if (searchValue[0] === '=') {
- searchValue = searchValue.substring(1);
- return bookValue.localeCompare(searchValue) == 0;
- } else if (searchValue[0] === '%') {
- searchValue = searchValue.substring(1);
- const words = searchValue.split(' ').filter(a => a);
- if (!words.length)
- words.push('');
- for (const w of words)
- if (bookValue !== emptyFieldValue && bookValue.indexOf(w) >= 0)
- return true;
- return false;
- } else if (searchValue[0] === '*') {
- searchValue = searchValue.substring(1);
- return bookValue !== emptyFieldValue && bookValue.indexOf(searchValue) >= 0;
- } else if (searchValue[0] === '#') {
- searchValue = searchValue.substring(1);
- if (!bookValue)
- return false;
- return bookValue !== emptyFieldValue && !enru.has(bookValue[0]) && bookValue.indexOf(searchValue) >= 0;
- } else if (searchValue[0] === '~') {//RegExp
- searchValue = searchValue.substring(1);
- const re = new RegExp(searchValue, 'i');
- return re.test(bookValue);
- } else {
- //where = `@dirtyIndexLR('value', ${db.esc(a)}, ${db.esc(a + maxUtf8Char)})`;
- return bookValue.localeCompare(searchValue) >= 0 && bookValue.localeCompare(searchValue + maxUtf8Char) <= 0;
- }
- };
- return books.filter((book) => {
- //author
- let authorFound = false;
- const authors = splitAuthor(book.author);
- for (const a of authors) {
- if (filterBySearch(a, s.author)) {
- authorFound = true;
- break;
- }
- }
- //genre
- let genreFound = !s.genre;
- if (!genreFound) {
- const searchGenres = new Set(s.genre.split(','));
- const bookGenres = book.genre.split(',');
- for (let g of bookGenres) {
- if (!g)
- g = emptyFieldValue;
- if (searchGenres.has(g)) {
- genreFound = true;
- break;
- }
- }
- }
- //lang
- let langFound = !s.lang;
- if (!langFound) {
- const searchLang = new Set(s.lang.split(','));
- langFound = searchLang.has(book.lang || emptyFieldValue);
- }
- //date
- let dateFound = !s.date;
- if (!dateFound) {
- const date = this.queryDate(s.date).split(',');
- let [from = '0000-00-00', to = '9999-99-99'] = date;
- dateFound = (book.date >= from && book.date <= to);
- }
- //librate
- let librateFound = !s.librate;
- if (!librateFound) {
- const searchLibrate = new Set(s.librate.split(',').map(n => parseInt(n, 10)).filter(n => !isNaN(n)));
- librateFound = searchLibrate.has(book.librate);
- }
- //ext
- let extFound = !s.ext;
- if (!extFound) {
- const searchExt = new Set(s.ext.split('|'));
- extFound = searchExt.has(book.ext.toLowerCase() || emptyFieldValue);
- }
- return (this.showDeleted || !book.del)
- && authorFound
- && filterBySearch(book.series, s.series)
- && filterBySearch(book.title, s.title)
- && genreFound
- && langFound
- && dateFound
- && librateFound
- && extFound
- ;
- });
- }
- showMore(item, all = false) {
- if (item.booksLoaded) {
- const currentLen = (item.books ? item.books.length : 0);
- let books;
- if (all || currentLen + this.showMoreCount*1.5 > item.booksLoaded.length) {
- books = item.booksLoaded;
- } else {
- books = item.booksLoaded.slice(0, currentLen + this.showMoreCount);
- }
- item.showMore = (books.length < item.booksLoaded.length);
- item.books = books;
- }
- }
- showMoreAll(seriesItem, all = false) {
- if (seriesItem.allBooksLoaded) {
- const currentLen = (seriesItem.allBooks ? seriesItem.allBooks.length : 0);
- let books;
- if (all || currentLen + this.showMoreCount*1.5 > seriesItem.allBooksLoaded.length) {
- books = seriesItem.allBooksLoaded;
- } else {
- books = seriesItem.allBooksLoaded.slice(0, currentLen + this.showMoreCount);
- }
- seriesItem.showMoreAll = (books.length < seriesItem.allBooksLoaded.length);
- seriesItem.allBooks = books;
- }
- }
- sortSeriesBooks(seriesBooks) {
- seriesBooks.sort((a, b) => {
- const dserno = (a.serno || Number.MAX_VALUE) - (b.serno || Number.MAX_VALUE);
- const dtitle = a.title.localeCompare(b.title);
- const dext = a.ext.localeCompare(b.ext);
- return (dserno ? dserno : (dtitle ? dtitle : dext));
- });
- }
- queryDate(date) {
- if (!utils.isManualDate(date)) {//!manual
- /*
- {label: 'сегодня', value: 'today'},
- {label: 'за 3 дня', value: '3days'},
- {label: 'за неделю', value: 'week'},
- {label: 'за 2 недели', value: '2weeks'},
- {label: 'за месяц', value: 'month'},
- {label: 'за 2 месяца', value: '2months'},
- {label: 'за 3 месяца', value: '3months'},
- {label: 'указать даты', value: 'manual'},
- */
- const sqlFormat = 'YYYY-MM-DD';
- switch (date) {
- case 'today': date = utils.dateFormat(dayjs(), sqlFormat); break;
- case '3days': date = utils.dateFormat(dayjs().subtract(3, 'days'), sqlFormat); break;
- case 'week': date = utils.dateFormat(dayjs().subtract(1, 'weeks'), sqlFormat); break;
- case '2weeks': date = utils.dateFormat(dayjs().subtract(2, 'weeks'), sqlFormat); break;
- case 'month': date = utils.dateFormat(dayjs().subtract(1, 'months'), sqlFormat); break;
- case '2months': date = utils.dateFormat(dayjs().subtract(2, 'months'), sqlFormat); break;
- case '3months': date = utils.dateFormat(dayjs().subtract(3, 'months'), sqlFormat); break;
- default:
- date = '';
- }
- }
- return date;
- }
- getQuery() {
- const search = (this.isExtendedSearch ? this.extSearch : this.search);
- const newQuery = {};
- search.setDefaults(newQuery, search);
- //дата
- if (newQuery.date) {
- newQuery.date = this.queryDate(newQuery.date);
- }
- //offset
- newQuery.offset = (newQuery.page - 1)*newQuery.limit;
- //del
- if (!newQuery.del && !this.showDeleted)
- newQuery.del = '0';
- return newQuery;
- }
- }
|