szurubooru/client/js/controls/post_edit_sidebar_control.js

87 lines
2.9 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');
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;
views.replaceContent(this._hostNode, template({
post: this._post,
2016-07-03 13:46:49 +02:00
canEditPostContent: api.hasPrivilege('posts:edit:content'),
canEditPostFlags: api.hasPrivilege('posts:edit:flags'),
canEditPostNotes: api.hasPrivilege('posts:edit:notes'),
canEditPostRelations: api.hasPrivilege('posts:edit:relations'),
canEditPostSafety: api.hasPrivilege('posts:edit:safety'),
canEditPostSource: api.hasPrivilege('posts:edit:source'),
canEditPostTags: api.hasPrivilege('posts:edit:tags'),
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-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,
tags: this._tagInputNode ?
misc.splitByWhitespace(this._tagInputNode.value) :
undefined,
relations: this._relationsInputNode ?
misc.splitByWhitespace(this._relationsInputNode.value) :
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 _relationsInputNode() {
return this._formNode.querySelector('.relations input');
}
};
module.exports = PostEditSidebarControl;