| 1 | import { getSortableDelay } from '../../../utils.js'; |
| 2 | import { QuickReplySetLink } from './QuickReplySetLink.js'; |
| 3 | import { QuickReplySet } from './QuickReplySet.js'; |
| 4 | |
| 5 | export class QuickReplyConfig { |
| 6 | /**@type {QuickReplySetLink[]}*/ setList = []; |
| 7 | /**@type {'global'|'chat'|'character'}*/ scope; |
| 8 | |
| 9 | /**@type {Function}*/ onUpdate; |
| 10 | /**@type {Function}*/ onRequestEditSet; |
| 11 | |
| 12 | /**@type {HTMLElement}*/ dom; |
| 13 | /**@type {HTMLElement}*/ setListDom; |
| 14 | |
| 15 | |
| 16 | static from(props) { |
| 17 | props.setList = props.setList?.map(it => QuickReplySetLink.from(it))?.filter(it => it.set) ?? []; |
| 18 | const instance = Object.assign(new this(), props); |
| 19 | instance.init(); |
| 20 | return instance; |
| 21 | } |
| 22 | |
| 23 | |
| 24 | init() { |
| 25 | this.setList.forEach(it => this.hookQuickReplyLink(it)); |
| 26 | } |
| 27 | |
| 28 | |
| 29 | hasSet(qrs) { |
| 30 | return this.setList.find(it => it.set == qrs) != null; |
| 31 | } |
| 32 | addSet(qrs, isVisible = true) { |
| 33 | if (!this.hasSet(qrs)) { |
| 34 | const qrl = new QuickReplySetLink(); |
| 35 | qrl.set = qrs; |
| 36 | qrl.isVisible = isVisible; |
| 37 | this.hookQuickReplyLink(qrl); |
| 38 | this.setList.push(qrl); |
| 39 | this.setListDom.append(qrl.renderSettings(this.setList.length - 1)); |
| 40 | this.update(); |
| 41 | } |
| 42 | } |
| 43 | removeSet(qrs) { |
| 44 | const idx = this.setList.findIndex(it => it.set == qrs); |
| 45 | if (idx > -1) { |
| 46 | this.setList.splice(idx, 1); |
| 47 | this.update(); |
| 48 | this.updateSetListDom(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | |
| 53 | renderSettingsInto(/**@type {HTMLElement}*/root) { |
| 54 | /**@type {HTMLElement}*/ |
| 55 | this.setListDom = root.querySelector('.qr--setList'); |
| 56 | root.querySelector('.qr--setListAdd').addEventListener('click', () => { |
| 57 | const newSet = QuickReplySet.list.find(qr => !this.setList.find(qrl => qrl.set == qr)); |
| 58 | if (newSet) { |
| 59 | this.addSet(newSet); |
| 60 | } else { |
| 61 | toastr.warning('All existing QR Sets have already been added.'); |
| 62 | } |
| 63 | }); |
| 64 | this.updateSetListDom(); |
| 65 | } |
| 66 | updateSetListDom() { |
| 67 | this.setListDom.innerHTML = ''; |
| 68 | // @ts-ignore |
| 69 | $(this.setListDom).sortable({ |
| 70 | delay: getSortableDelay(), |
| 71 | stop: () => this.onSetListSort(), |
| 72 | }); |
| 73 | this.setList.filter(it => !it.set.isDeleted).forEach((qrl, idx) => this.setListDom.append(qrl.renderSettings(idx))); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | onSetListSort() { |
| 78 | this.setList = Array.from(this.setListDom.children).map((it, idx) => { |
| 79 | const qrl = this.setList[Number(it.getAttribute('data-order'))]; |
| 80 | qrl.index = idx; |
| 81 | it.setAttribute('data-order', String(idx)); |
| 82 | return qrl; |
| 83 | }); |
| 84 | this.update(); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /** |
| 89 | * @param {QuickReplySetLink} qrl |
| 90 | */ |
| 91 | hookQuickReplyLink(qrl) { |
| 92 | qrl.onDelete = () => this.deleteQuickReplyLink(qrl); |
| 93 | qrl.onUpdate = () => this.update(); |
| 94 | qrl.onRequestEditSet = () => this.requestEditSet(qrl.set); |
| 95 | } |
| 96 | |
| 97 | deleteQuickReplyLink(qrl) { |
| 98 | this.setList.splice(this.setList.indexOf(qrl), 1); |
| 99 | this.update(); |
| 100 | } |
| 101 | |
| 102 | update() { |
| 103 | if (this.onUpdate) { |
| 104 | this.onUpdate(this); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | requestEditSet(qrs) { |
| 109 | if (this.onRequestEditSet) { |
| 110 | this.onRequestEditSet(qrs); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | toJSON() { |
| 115 | return { |
| 116 | setList: this.setList, |
| 117 | }; |
| 118 | } |
| 119 | } |