|
| 1 | +import app from 'flarum/admin/app'; |
| 2 | +import Button from 'flarum/common/components/Button'; |
| 3 | +import Form from 'flarum/common/components/Form'; |
| 4 | +import FormModal, { IFormModalAttrs } from 'flarum/common/components/FormModal'; |
| 5 | +import Icon from 'flarum/common/components/Icon'; |
| 6 | +import extractText from 'flarum/common/utils/extractText'; |
| 7 | +import ItemList from 'flarum/common/utils/ItemList'; |
| 8 | +import Stream from 'flarum/common/utils/Stream'; |
| 9 | +import { Children, Vnode } from 'mithril'; |
| 10 | +import CustomTabItem from '../models/CustomTabItem'; |
| 11 | + |
| 12 | +interface EditCustomTabItemModalAttrs extends IFormModalAttrs { |
| 13 | + model?: CustomTabItem; |
| 14 | +} |
| 15 | + |
| 16 | +export default class EditCustomTabItemModal extends FormModal<EditCustomTabItemModalAttrs> { |
| 17 | + protected customTabItem!: CustomTabItem; |
| 18 | + protected label!: Stream<string>; |
| 19 | + protected icon!: Stream<string>; |
| 20 | + protected url!: Stream<string>; |
| 21 | + |
| 22 | + oninit(vnode: Vnode<EditCustomTabItemModalAttrs, this>) { |
| 23 | + super.oninit(vnode); |
| 24 | + |
| 25 | + this.customTabItem = this.attrs.model || app.store.createRecord('custom-tab-items'); |
| 26 | + |
| 27 | + this.label = Stream(this.customTabItem?.label() || ''); |
| 28 | + this.icon = Stream(this.customTabItem?.icon() || ''); |
| 29 | + this.url = Stream(this.customTabItem?.url() || ''); |
| 30 | + } |
| 31 | + |
| 32 | + className() { |
| 33 | + return 'EditCustomTabItemModal Modal--small'; |
| 34 | + } |
| 35 | + |
| 36 | + title() { |
| 37 | + const label = this.label(); |
| 38 | + if (!label.length) return app.translator.trans('acpl-mobile-tab.admin.edit_item.modal_title'); |
| 39 | + |
| 40 | + const iconClass = this.icon(); |
| 41 | + if (iconClass.length) |
| 42 | + return ( |
| 43 | + <> |
| 44 | + <Icon name={iconClass} /> {label} |
| 45 | + </> |
| 46 | + ); |
| 47 | + |
| 48 | + return label; |
| 49 | + } |
| 50 | + |
| 51 | + content() { |
| 52 | + return ( |
| 53 | + <div className="Modal-body"> |
| 54 | + <Form>{this.fields().toArray()}</Form> |
| 55 | + </div> |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + fields() { |
| 60 | + const items = new ItemList<Children>(); |
| 61 | + |
| 62 | + items.add( |
| 63 | + 'label', |
| 64 | + <div className="Form-group"> |
| 65 | + <label>{app.translator.trans('acpl-mobile-tab.admin.edit_item.label')}</label> |
| 66 | + <input |
| 67 | + className="FormControl" |
| 68 | + type="text" |
| 69 | + placeholder={app.translator.trans('acpl-mobile-tab.admin.edit_item.label_placeholder')} |
| 70 | + bidi={this.label} |
| 71 | + /> |
| 72 | + </div> |
| 73 | + ); |
| 74 | + |
| 75 | + items.add( |
| 76 | + 'url', |
| 77 | + <div className="Form-group"> |
| 78 | + <label>{app.translator.trans('acpl-mobile-tab.admin.edit_item.url')}</label> |
| 79 | + <input className="FormControl" type="url" placeholder="https://" bidi={this.url} /> |
| 80 | + </div> |
| 81 | + ); |
| 82 | + |
| 83 | + items.add( |
| 84 | + 'icon', |
| 85 | + <div className="Form-group"> |
| 86 | + <label>{app.translator.trans('acpl-mobile-tab.admin.edit_item.icon')}</label> |
| 87 | + <input className="FormControl" type="text" placeholder="fas fa-home" bidi={this.icon} /> |
| 88 | + </div> |
| 89 | + ); |
| 90 | + |
| 91 | + items.add( |
| 92 | + 'actions', |
| 93 | + <div className="Form-group Form-controls"> |
| 94 | + <Button type="submit" className="Button Button--primary"> |
| 95 | + {app.translator.trans('acpl-mobile-tab.admin.edit_item.submit_button')} |
| 96 | + </Button> |
| 97 | + {this.customTabItem.exists && ( |
| 98 | + <Button className="Button Button--danger EditCustomTabItemModal-delete" onclick={this.delete.bind(this)}> |
| 99 | + {app.translator.trans('acpl-mobile-tab.admin.edit_item.delete_button')} |
| 100 | + </Button> |
| 101 | + )} |
| 102 | + </div> |
| 103 | + ); |
| 104 | + |
| 105 | + return items; |
| 106 | + } |
| 107 | + |
| 108 | + submitData() { |
| 109 | + return { label: this.label(), url: this.url(), icon: this.icon() }; |
| 110 | + } |
| 111 | + |
| 112 | + onsubmit(e: SubmitEvent) { |
| 113 | + e.preventDefault(); |
| 114 | + this.loading = true; |
| 115 | + |
| 116 | + this.customTabItem.save(this.submitData()).then( |
| 117 | + () => this.hide(), |
| 118 | + () => (this.loading = false) |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + delete() { |
| 123 | + if (!confirm(extractText(app.translator.trans('acpl-mobile-tab.admin.edit_item.delete_item_confirmation')))) { |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + this.loading = true; |
| 128 | + this.customTabItem.delete().then( |
| 129 | + () => this.hide(), |
| 130 | + () => (this.loading = false) |
| 131 | + ); |
| 132 | + } |
| 133 | +} |
0 commit comments