//TOOLS import DataUtils, { REQUEST_TYPE_GET, REQUEST_TYPE_PUT, REQUEST_TYPE_POST, REQUEST_TYPE_DELETE, CONTENT_TYPE_JSON, CONTENT_TYPE_FORM } from '../tools/utilities/DataUtils'; import * as DataEvent from '../tools/events/DataEvent'; import Animate from '../tools/effects/Animate'; import * as Ease from '../tools/effects/Animate'; import EntryTasks from '../tasks/EntryTasks'; import * as EditorEvent from '../tools/events/EditorEvent'; import TextEditor from '../tools/utilities/TextEditor'; import TinyDatePicker from 'tiny-date-picker'; import DateUtils from '../tools/utilities/DateUtils'; class Entry { //-------------------------- // constructor //-------------------------- constructor() { reframe('iframe'); let self = this; this.uploadFiles; this.dataUtils = new DataUtils(); this.dateUtils = new DateUtils(); if (document.getElementById('edit-text-code')) { this.editor = new TextEditor(document.getElementById('edit-text-code'), document.getElementById('header').offsetHeight + document.getElementById('entry-header').offsetHeight + document.getElementById('entry-feature').offsetHeight ); this.editor.addListener(EditorEvent.EDITOR_DELETE, f => this.handleEditorOptions(EditorEvent.EDITOR_DELETE), false) this.editor.addListener(EditorEvent.EDITOR_UPLOAD_POST_IMAGE, f => this.handleEditorOptions(EditorEvent.EDITOR_UPLOAD_POST_IMAGE), false) this.editor.addListener(EditorEvent.EDITOR_UPDATE, f => this.handleEditorOptions(EditorEvent.EDITOR_UPDATE), false) this.editor.addListener(EditorEvent.EDITOR_SAVE, f => this.handleEditorOptions(EditorEvent.EDITOR_SAVE), false) document.getElementById('post-image').addEventListener('change', e => this.handlePostImageAdd(e), false); TinyDatePicker(document.getElementById('entry-date'), { mode: 'dp-below', format(date) { //return date; return self.dateUtils.getDate('origin', date); } }); } this.start(); } //-------------------------- // methods //-------------------------- start() { let self = this; new Animate().object({ targets: document.getElementById('loader'), duration: 300, opacity: 0, easing: 'easeInOutQuad', complete: function () { document.getElementById('loader').style.display = 'none'; document.getElementById('loader').style.visibility = 'hidden'; new Animate().object({ targets: document.getElementById('header'), duration: 10, opacity: 1, easing: 'easeInOutQuad', complete: function () { if (document.getElementById('the-intro')) document.getElementById('the-intro').style.opacity = 1; if (document.getElementById('blog-entry')) document.getElementById('blog-entry').style.opacity = 1; } }); } }); if (document.getElementById('featured-image-drop')) { document.getElementById('featured-image-drop').addEventListener('dragover', this.handleDragOver, false); document.getElementById('featured-image-drop').addEventListener('drop', this.handleDrop, false); document.getElementById('featured-click').addEventListener('change', this.handleClicked, false); if (document.getElementById('new-upload-link')) { document.getElementById('new-upload-link').addEventListener('click', e => { document.getElementById('featured-click').click(); }) } } } //-------------------------- // event handlers //-------------------------- handleEditorOptions(e) { switch (e) { case EditorEvent.EDITOR_SAVE: case EditorEvent.EDITOR_UPDATE: let edit = false; if (e == EditorEvent.EDITOR_UPDATE) edit = true; new EntryTasks().submitPost(edit, Entry.uploadFiles).then((response) => { let note = JSON.parse(response['response']['request'].response); this.editor.notify(note.message, note.postID); if (note.message == DataEvent.POST_ADDED) window.location = "/@/dashboard/entries/edit/" + note.postID; }).catch((err) => { console.log(err) }); break; case EditorEvent.EDITOR_DELETE: if (confirm('Aye! You know you\'re deleting this post, right?')) { new EntryTasks().deletePost().then((response) => { let note = JSON.parse(response['response']['request'].response); window.location = "/@/dashboard/entries/"; //console.log(note); }).catch((err) => { console.log(err) }); } else { // Do nothing! } break; case EditorEvent.EDITOR_UPLOAD_POST_IMAGE: document.getElementById('post-image').click(); break; } } handleDragOver(e) { e.stopPropagation(); e.preventDefault(); e.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy. } handleClicked(e) { e.stopPropagation(); e.preventDefault(); //console.log("IMAGES " + e.target.files); Entry.uploadFiles = e.target.files; for (var i = 0, f; f = Entry.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 span = document.createElement('span'); span.innerHTML = [ '<img src="', f.target.result, '" title="', escape(theFile.name), '"/>' ].join(''); //document.getElementById('featured-image-drop').insertBefore(span, null); document.getElementById('featured-image-drop').innerHTML = ''; document.getElementById('featured-image-drop').appendChild(span); }; })(f); // Read in the image file as a data URL. reader.readAsDataURL(f); } } handleDrop(e) { e.stopPropagation(); e.preventDefault(); Entry.uploadFiles = e.dataTransfer.files; //console.log(MemberArea.uploadFiles.length); for (var i = 0, f; f = Entry.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 span = document.createElement('span'); span.innerHTML = [ '<img src="', f.target.result, '" title="', escape(theFile.name), '"/>' ].join(''); //document.getElementById('featured-image-drop').insertBefore(span, null); document.getElementById('featured-image-drop').innerHTML = ''; document.getElementById('featured-image-drop').appendChild(span); }; })(f); // Read in the image file as a data URL. reader.readAsDataURL(f); } } handlePostImageAdd(e) { e.stopPropagation(); e.preventDefault(); let self = this; var postData = new FormData(); var files = e.target.files; for (var i = 0; i < files.length; i++) { var file = files[i]; // Check the file type. if (!file.type.match('image.*')) { continue; } postData.append('post_image', file, file.name); } this.dataUtils.request("/api/blog/add-post-image", DataEvent.POST_IMAGE_ADDED, REQUEST_TYPE_POST, CONTENT_TYPE_FORM, postData) .then((response) => { self.editor.notify(EditorEvent.EDITOR_UPLOAD_POST_IMAGE, JSON.parse(response.request['response']).url); }).catch((err) => { console.log(err) }) } } Entry.uploadFiles = []; export { Entry as default }