szurubooru/client/js/controls/post_edit_sidebar_control.js

177 lines
5.4 KiB
JavaScript
Raw Normal View History

'use strict';
2016-07-03 13:46:49 +02:00
const api = require('../api.js');
const events = require('../events.js');
2016-07-03 13:46:49 +02:00
const misc = require('../util/misc.js');
const views = require('../util/views.js');
2016-07-03 13:46:49 +02:00
const TagInputControl = require('./tag_input_control.js');
2016-07-27 22:27:33 +02:00
const FileDropperControl = require('../controls/file_dropper_control.js');
const template = views.getTemplate('post-edit-sidebar');
class PostEditSidebarControl extends events.EventTarget {
constructor(hostNode, post, postContentControl) {
super();
this._hostNode = hostNode;
this._post = post;
this._postContentControl = postContentControl;
2016-07-27 22:27:33 +02:00
this._newPostContent = null;
views.replaceContent(this._hostNode, template({
post: this._post,
2016-07-03 13:46:49 +02:00
canEditPostSafety: api.hasPrivilege('posts:edit:safety'),
canEditPostSource: api.hasPrivilege('posts:edit:source'),
canEditPostTags: api.hasPrivilege('posts:edit:tags'),
canEditPostRelations: api.hasPrivilege('posts:edit:relations'),
canEditPostNotes: api.hasPrivilege('posts:edit:notes'),
canEditPostFlags: api.hasPrivilege('posts:edit:flags'),
canEditPostContent: api.hasPrivilege('posts:edit:content'),
2016-07-03 13:46:49 +02:00
canEditPostThumbnail: api.hasPrivilege('posts:edit:thumbnail'),
canCreateAnonymousPosts: api.hasPrivilege('posts:create:anonymous'),
canDeletePosts: api.hasPrivilege('posts:delete'),
canFeaturePosts: api.hasPrivilege('posts:feature'),
}));
2016-07-03 13:46:49 +02:00
if (this._formNode) {
this._formNode.addEventListener('submit', e => this._evtSubmit(e));
}
if (this._tagInputNode) {
this._tagControl = new TagInputControl(this._tagInputNode);
}
2016-07-27 22:27:33 +02:00
if (this._contentInputNode) {
this._contentFileDropper = new FileDropperControl(
this._contentInputNode,
{
lock: true,
resolve: files => {
this._newPostContent = files[0];
},
});
}
if (this._thumbnailInputNode) {
this._thumbnailFileDropper = new FileDropperControl(
this._thumbnailInputNode,
{
lock: true,
resolve: files => {
this._newPostThumbnail = files[0];
},
});
}
2016-07-27 22:27:33 +02:00
this._post.addEventListener(
'changeContent', e => this._evtPostContentChange(e));
this._post.addEventListener(
'changeThumbnail', e => this._evtPostThumbnailChange(e));
if (this._formNode) {
const inputNodes = this._formNode.querySelectorAll(
'input, textarea');
for (let node of inputNodes) {
node.addEventListener(
'change', e => {
this.dispatchEvent(new CustomEvent('change'));
});
}
}
2016-07-27 22:27:33 +02:00
}
_evtPostContentChange(e) {
this._contentFileDropper.reset();
2016-07-03 13:46:49 +02:00
}
_evtPostThumbnailChange(e) {
this._thumbnailFileDropper.reset();
}
2016-07-03 13:46:49 +02:00
_evtSubmit(e) {
e.preventDefault();
this.dispatchEvent(new CustomEvent('submit', {
detail: {
post: this._post,
safety: this._safetyButtonNodes.legnth ?
2016-07-03 13:46:49 +02:00
Array.from(this._safetyButtonNodes)
.filter(node => node.checked)[0]
.value.toLowerCase() :
undefined,
flags: this._loopVideoInputNode ?
(this._loopVideoInputNode.checked ? ['loop'] : []) :
undefined,
tags: this._tagInputNode ?
misc.splitByWhitespace(this._tagInputNode.value) :
undefined,
relations: this._relationsInputNode ?
misc.splitByWhitespace(this._relationsInputNode.value) :
undefined,
2016-07-27 22:27:33 +02:00
content: this._newPostContent ?
this._newPostContent :
undefined,
thumbnail: this._newPostThumbnail ?
this._newPostThumbnail :
undefined,
2016-07-03 13:46:49 +02:00
},
}));
}
get _formNode() {
return this._hostNode.querySelector('form');
}
get _submitButtonNode() {
return this._hostNode.querySelector('.submit');
}
get _safetyButtonNodes() {
return this._formNode.querySelectorAll('.safety input');
}
get _tagInputNode() {
return this._formNode.querySelector('.tags input');
}
get _loopVideoInputNode() {
return this._formNode.querySelector('.flags input[name=loop]');
}
2016-07-03 13:46:49 +02:00
get _relationsInputNode() {
return this._formNode.querySelector('.relations input');
}
2016-07-27 22:27:33 +02:00
get _contentInputNode() {
return this._formNode.querySelector('.post-content .dropper-container');
}
get _thumbnailInputNode() {
return this._formNode.querySelector(
'.post-thumbnail .dropper-container');
}
enableForm() {
views.enableForm(this._formNode);
}
disableForm() {
views.disableForm(this._formNode);
}
showSuccess(message) {
views.showSuccess(this._hostNode, message);
}
showError(message) {
views.showError(this._hostNode, message);
}
};
module.exports = PostEditSidebarControl;