2019-11-22 07:36:18 +01:00
|
|
|
//TOOLS
|
2019-12-02 22:07:16 +01:00
|
|
|
import ApiUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_FORM } from '../utils/APIUtils';
|
2019-11-25 01:11:12 +01:00
|
|
|
import * as DataEvent from '../events/DataEvent';
|
|
|
|
import PageActions from '../actions/PageActions';
|
|
|
|
import * as EditorEvent from '../events/EditorEvent';
|
2019-11-22 07:36:18 +01:00
|
|
|
import TinyDatePicker from 'tiny-date-picker';
|
2019-11-25 01:11:12 +01:00
|
|
|
import TextEditor from '../ui/TextEditor';
|
2019-11-25 03:39:43 +01:00
|
|
|
import Notfications from '../ui/Notifications';
|
2019-12-02 22:07:16 +01:00
|
|
|
const api = new ApiUtils();
|
2019-11-25 03:39:43 +01:00
|
|
|
const notify = new Notfications();
|
2019-11-22 07:36:18 +01:00
|
|
|
export default class PostEditor {
|
|
|
|
//--------------------------
|
|
|
|
// constructor
|
|
|
|
//--------------------------
|
|
|
|
constructor() {
|
|
|
|
let self = this;
|
|
|
|
this.urlPieces = document.URL.split('/');
|
|
|
|
this.post = [];
|
|
|
|
this.postID = null;
|
2019-12-02 22:07:16 +01:00
|
|
|
api.authStatus();
|
2019-11-22 07:36:18 +01:00
|
|
|
if (document.getElementById('post-edit-index').getAttribute('data-index')) {
|
|
|
|
this.postID = document.getElementById('post-edit-index').getAttribute('data-index');
|
|
|
|
}
|
|
|
|
if (document.getElementById('edit-post-text')) {
|
|
|
|
this.editor = new TextEditor(
|
|
|
|
document.getElementById('edit-post-text'),
|
|
|
|
document.getElementById('header').offsetHeight +
|
|
|
|
document.getElementById('post-header').offsetHeight +
|
|
|
|
document.getElementById('post-feature').offsetHeight
|
|
|
|
);
|
|
|
|
this.editor.addListener(
|
|
|
|
EditorEvent.EDITOR_DELETE,
|
|
|
|
() => this.handleEditorOptions(EditorEvent.EDITOR_DELETE),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
this.editor.addListener(
|
|
|
|
EditorEvent.EDITOR_UPLOAD_POST_IMAGE,
|
|
|
|
() => this.handleEditorOptions(EditorEvent.EDITOR_UPLOAD_POST_IMAGE),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
this.editor.addListener(
|
|
|
|
EditorEvent.EDITOR_UPDATE,
|
|
|
|
() => this.handleEditorOptions(EditorEvent.EDITOR_UPDATE),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
this.editor.addListener(
|
|
|
|
EditorEvent.EDITOR_SAVE,
|
|
|
|
() => this.handleEditorOptions(EditorEvent.EDITOR_SAVE),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
document.getElementById('post-image-upload').addEventListener(
|
|
|
|
'change',
|
|
|
|
e => {
|
|
|
|
self.handleImageUpload(e.target.id, e.target.files);
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
TinyDatePicker(document.getElementById('post-date'), {
|
|
|
|
mode: 'dp-below',
|
2019-11-26 01:51:35 +01:00
|
|
|
format() {
|
|
|
|
//return self.dateUtils.getDate('origin', date);
|
2019-11-22 07:36:18 +01:00
|
|
|
}
|
|
|
|
});
|
2019-11-24 01:56:28 +01:00
|
|
|
|
|
|
|
this.start();
|
2019-11-22 07:36:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//--------------------------
|
|
|
|
// methods
|
|
|
|
//--------------------------
|
|
|
|
start() {
|
|
|
|
if (document.getElementById('featured-image-drop')) {
|
|
|
|
document
|
|
|
|
.getElementById('featured-image-drop')
|
|
|
|
.addEventListener('dragover', this.handleImageActions, false);
|
|
|
|
document
|
|
|
|
.getElementById('featured-image-drop')
|
|
|
|
.addEventListener('drop', this.handleImageActions, false);
|
|
|
|
document
|
|
|
|
.getElementById('featured-image-upload')
|
|
|
|
.addEventListener('change', e => this.handleImageActions(e), false);
|
|
|
|
if (document.getElementById('new-feature-upload')) {
|
|
|
|
document.getElementById('new-feature-upload').addEventListener('click', () => {
|
|
|
|
document.getElementById('featured-image-upload').click();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
var optionButtons = document.querySelectorAll('.post-option-btn');
|
|
|
|
for (var i = 0, length = optionButtons.length; i < length; i++) {
|
|
|
|
optionButtons[i].addEventListener('click', e => this.handlePostOptions(e), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//--------------------------
|
|
|
|
// event handlers
|
|
|
|
//--------------------------
|
|
|
|
handlePostOptions(e) {
|
|
|
|
let currentOption;
|
|
|
|
switch (e.target.id) {
|
|
|
|
case 'option-page-icon':
|
2019-11-25 22:58:34 +01:00
|
|
|
case 'option-menu-pin':
|
|
|
|
currentOption = document.getElementById('option-menu-pin');
|
2019-11-22 07:36:18 +01:00
|
|
|
break;
|
|
|
|
case 'option-feature-icon':
|
|
|
|
case 'option-feature':
|
|
|
|
currentOption = document.getElementById('option-feature');
|
|
|
|
break;
|
|
|
|
case 'option-published-icon':
|
|
|
|
case 'option-published':
|
|
|
|
currentOption = document.getElementById('option-published');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
let active = currentOption.getAttribute('data-active');
|
|
|
|
active == 'false'
|
|
|
|
? currentOption.setAttribute('data-active', 'true')
|
|
|
|
: currentOption.setAttribute('data-active', 'false');
|
|
|
|
}
|
|
|
|
handleEditorOptions(e) {
|
|
|
|
switch (e) {
|
|
|
|
case EditorEvent.EDITOR_SAVE:
|
|
|
|
case EditorEvent.EDITOR_UPDATE:
|
2019-11-25 22:58:34 +01:00
|
|
|
var apiUrl = '';
|
|
|
|
e === EditorEvent.EDITOR_SAVE
|
|
|
|
? (apiUrl = '/api/v1/page/write/new')
|
|
|
|
: (apiUrl = '/api/v1/page/write');
|
2019-11-25 01:11:12 +01:00
|
|
|
new PageActions()
|
|
|
|
.collectInfo(document.getElementById('featured-image-upload').files[0])
|
2019-11-25 22:58:34 +01:00
|
|
|
|
2019-11-25 01:11:12 +01:00
|
|
|
.then(page => {
|
2019-12-02 22:07:16 +01:00
|
|
|
api.request(
|
2019-11-25 22:58:34 +01:00
|
|
|
apiUrl,
|
2019-11-25 01:11:12 +01:00
|
|
|
DataEvent.API_PAGE_WRITE,
|
|
|
|
REQUEST_TYPE_POST,
|
|
|
|
CONTENT_TYPE_FORM,
|
|
|
|
page
|
|
|
|
)
|
|
|
|
.then(response => {
|
|
|
|
let r = JSON.parse(response.request['response']);
|
2019-11-25 03:39:43 +01:00
|
|
|
if (r.type === DataEvent.PAGE_ERROR) {
|
|
|
|
notify.alert(r.message, false);
|
|
|
|
} else {
|
2019-11-25 22:58:34 +01:00
|
|
|
if (r.type === DataEvent.PAGE_UPDATED) {
|
|
|
|
notify.alert(r.message, true);
|
|
|
|
} else {
|
|
|
|
notify.alert(r.message, true);
|
|
|
|
window.location = '/@/dashboard/page/edit/' + r.id;
|
|
|
|
}
|
2019-11-25 03:39:43 +01:00
|
|
|
}
|
2019-11-25 01:11:12 +01:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-11-25 03:39:43 +01:00
|
|
|
notify.alert(err, false);
|
2019-11-25 01:11:12 +01:00
|
|
|
});
|
2019-11-22 07:36:18 +01:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
case EditorEvent.EDITOR_DELETE:
|
2019-11-25 03:39:43 +01:00
|
|
|
if (confirm("AYE! You know you're deleting this post, right?")) {
|
2019-11-25 01:11:12 +01:00
|
|
|
new PageActions()
|
2019-11-22 07:36:18 +01:00
|
|
|
.deletePost(this.postID, this.post)
|
|
|
|
.then(() => {
|
|
|
|
setTimeout(() => {
|
|
|
|
window.location = '/@/dashboard/posts/';
|
|
|
|
}, 100);
|
|
|
|
})
|
|
|
|
.catch(() => {});
|
|
|
|
} else {
|
|
|
|
// Do nothing!
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EditorEvent.EDITOR_UPLOAD_POST_IMAGE:
|
|
|
|
document.getElementById('post-image-upload').click();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handleImageActions(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
switch (e.type) {
|
|
|
|
case 'dragover':
|
|
|
|
e.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
|
|
|
|
break;
|
|
|
|
case 'change':
|
|
|
|
case 'drop':
|
|
|
|
e.type == 'drop'
|
|
|
|
? (PostEditor.uploadFiles = e.dataTransfer.files)
|
|
|
|
: (PostEditor.uploadFiles = e.target.files);
|
|
|
|
for (var i = 0, f; (f = PostEditor.uploadFiles[i]); i++) {
|
|
|
|
// Only process image files.
|
|
|
|
if (!f.type.match('image.*')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var reader = new FileReader();
|
|
|
|
// Closure to capture the file information.
|
|
|
|
reader.onload = (function(theFile) {
|
|
|
|
return function(f) {
|
|
|
|
// Render thumbnail.
|
|
|
|
var image = document.createElement('img');
|
|
|
|
image.src = f.target.result;
|
|
|
|
image.title = escape(theFile.name);
|
|
|
|
var span = document.createElement('div');
|
|
|
|
span.innerHTML = [
|
|
|
|
'<img src="',
|
|
|
|
f.target.result,
|
|
|
|
'" title="',
|
|
|
|
escape(theFile.name),
|
|
|
|
'"/>'
|
|
|
|
].join('');
|
|
|
|
document.getElementById('featured-image-drop').innerHTML = '';
|
|
|
|
document.getElementById('featured-image-drop').appendChild(image);
|
|
|
|
};
|
|
|
|
})(f);
|
|
|
|
// Read in the image file as a data URL.
|
|
|
|
reader.readAsDataURL(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handleImageUpload(type, files) {
|
2019-11-26 01:51:35 +01:00
|
|
|
let url = '/api/v1/page/add-post-image';
|
2019-12-02 22:07:16 +01:00
|
|
|
let eventType = DataEvent.API_IMAGES_UPLOAD;
|
2019-11-22 07:36:18 +01:00
|
|
|
let self = this;
|
|
|
|
var imageData = new FormData();
|
|
|
|
for (var i = 0; i < files.length; i++) {
|
|
|
|
var file = files[i];
|
|
|
|
// Check the file type.
|
|
|
|
if (!file.type.match('image.*')) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-26 01:51:35 +01:00
|
|
|
imageData.append('post_image', file, file.name);
|
2019-11-22 07:36:18 +01:00
|
|
|
}
|
2019-12-02 22:07:16 +01:00
|
|
|
api.request(url, eventType, REQUEST_TYPE_POST, CONTENT_TYPE_FORM, imageData)
|
2019-11-22 07:36:18 +01:00
|
|
|
.then(response => {
|
|
|
|
let r = JSON.parse(response.request['response']);
|
2019-11-26 01:51:35 +01:00
|
|
|
if (r.type == DataEvent.POST_IMAGE_ADDED)
|
2019-11-22 07:36:18 +01:00
|
|
|
self.editor.notify(EditorEvent.EDITOR_UPLOAD_POST_IMAGE, r.url);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
//console.log(err)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PostEditor.uploadFiles = [];
|