cleaned up post controller. much nice. wow smoother

sql-version-freeze
Ro 6 years ago
parent a7239a3027
commit c2b5d9a56c

@ -1,6 +1,7 @@
var express = require('express');
import DateUtils from '../../tools/utilities/DateUtils';
import StringUtils from '../../tools/utilities/StringUtils';
import * as DataEvent from '../../tools/events/DataEvent';
import RightsManager,
{
TASK_CREATE,
@ -157,7 +158,7 @@ router.post('/add-post-image', function(req, res, next)
var postImage = req.files[0].path;
return res.json(
{
message: "post image added",
message: DataEvent.POST_IMAGE_ADDED,
url: postImage.substr(7, postImage.length)
});
}
@ -200,7 +201,7 @@ router.post('/add-feature-image', function(req, res, next)
var postImage = req.files[0].path;
return res.json(
{
message: "post image added",
message: DataEvent.FEATURE_IMAGE_ADDED,
url: postImage.substr(7, postImage.length)
});
}

@ -108,7 +108,6 @@ router.get('/edit/:id', function(req, res)
}
}).then(item =>
{
console.log("**ITEM***", item);
let featured = 'null';
if(item.post.feature != null || item.post.feature != '')
featured = item.post.feature.substr(8, item.post.feature.length);

@ -2276,8 +2276,8 @@ select {
color: #b2cce5;
padding: 5px;
}
#post-edit-index #post-edit-index-wrapper #post-header #post-meta #featured-click,
#post-edit-index #post-edit-index-wrapper #post-header #post-meta #post-image {
#post-edit-index #post-edit-index-wrapper #post-header #post-meta #featured-image-upload,
#post-edit-index #post-edit-index-wrapper #post-header #post-meta #post-image-upload {
display: none;
}
#post-edit-index #post-edit-index-wrapper #post-feature {
@ -2306,11 +2306,11 @@ select {
position: absolute;
margin: 20px;
}
#post-edit-index #post-edit-index-wrapper #post-feature #featured-new-image-btn #new-upload-link {
#post-edit-index #post-edit-index-wrapper #post-feature #featured-new-image-btn #new-feature-upload {
padding-top: 4px;
background: #f2f1ef;
}
#post-edit-index #post-edit-index-wrapper #post-feature #featured-new-image-btn #new-upload-link svg {
#post-edit-index #post-edit-index-wrapper #post-feature #featured-new-image-btn #new-feature-upload svg {
fill: #fc6399;
}
#post-edit-index #post-edit-index-wrapper #edit-post {

File diff suppressed because one or more lines are too long

@ -35100,7 +35100,7 @@ function () {
err: err
});
});
}); //(updated) ? resolve("UPDATED") : reject("NOT UPDATED");
});
});
} else {
_this.db.postList.update(Number(id), {
@ -35121,100 +35121,6 @@ function () {
}
});
}
}, {
key: "sync",
value: function sync(files) {
var self = this;
return new Promise(function (resolve, reject) {
var syncData = new FormData();
var postList = '';
for (var i = 0; i < files.length; i++) {
var file = files[i]; // Check the file type.
if (!file.type.match('image.*')) {
continue;
} // Add the file to the request.
syncData.append('feature_image', file, file.name);
}
var fipamoPosts = new _dexie.default("fipamo_posts");
fipamoPosts.version(1).stores({
postList: 'id,post'
});
fipamoPosts.postList.toArray(function (array) {
self.dataUtils.request('/api/post/sync', DataEvent.POSTS_SYNCED, _DataUtils.REQUEST_TYPE_POST, _DataUtils.CONTENT_TYPE_JSON, array).then(function (response) {
resolve({
response: response
});
}).catch(function (err) {
reject({
err: err
});
}); //console.log("LIST", postList);
});
});
}
}, {
key: "submitPost",
value: function submitPost(edit, uploadFiles) {
var self = this;
return new Promise(function (resolve, reject) {
//collect form data
//if(!this.validateForm())
var postData = new FormData(); //let projectImages = document.getElementById('projectImages');
//var fileSelect = projectImages;
var files = uploadFiles;
for (var i = 0; i < files.length; i++) {
var file = files[i]; // Check the file type.
if (!file.type.match('image.*')) {
continue;
} // Add the file to the request.
postData.append('feature_image', file, file.name);
} //var category = document.getElementById("content_category");
//let project_form = document.forms.namedItem("folio-project");
var txt = document.createElement("textarea");
txt.innerHTML = document.getElementById('edit-post-text').innerHTML;
postData.append("title", document.getElementById('post_title').value);
postData.append('slug', new _StringUtils.default().cleanString(document.getElementById('post_title').value));
postData.append("post_plaintext", txt.value);
postData.append("origin_date", document.getElementById('post-date').value);
postData.append("tags", document.getElementById('post_tags').value);
postData.append("status_page", document.getElementById('option-page').getAttribute('data-active'));
postData.append("status_feature", document.getElementById('option-feature').getAttribute('data-active'));
postData.append("status_published", document.getElementById('option-published').getAttribute('data-active'));
var postURL;
var postEventType;
if (edit) {
var postID = document.getElementById('edit-update').getAttribute('data-id');
postURL = "/api/post/update/" + postID;
postEventType = DataEvent.POST_UPDATED;
} else {
postURL = "/api/post/add";
postEventType = DataEvent.POST_ADDED;
}
self.dataUtils.request(postURL, postEventType, _DataUtils.REQUEST_TYPE_POST, _DataUtils.CONTENT_TYPE_FORM, postData).then(function (response) {
resolve({
response: response
});
}).catch(function (err) {
reject({
err: err
});
});
});
}
}, {
key: "deletePost",
value: function deletePost() {
@ -37287,8 +37193,8 @@ function () {
this.editor.addListener(EditorEvent.EDITOR_SAVE, function (f) {
return _this.handleEditorOptions(EditorEvent.EDITOR_SAVE);
}, false);
document.getElementById('post-image').addEventListener('change', function (e) {
return _this.handlePostImageAdd(e);
document.getElementById('post-image-upload').addEventListener('change', function (e) {
self.handleImageUpload(e.target.id, e.target.files);
}, false);
(0, _tinyDatePicker.default)(document.getElementById('post-date'), {
mode: 'dp-below',
@ -37323,15 +37229,15 @@ function () {
var self = this;
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', function (e) {
return _this2.handleFeatureImageAdd(e);
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', function (e) {
return _this2.handleImageActions(e);
}, false);
if (document.getElementById('new-upload-link')) {
document.getElementById('new-upload-link').addEventListener('click', function (e) {
document.getElementById('featured-click').click();
if (document.getElementById('new-feature-upload')) {
document.getElementById('new-feature-upload').addEventListener('click', function (e) {
document.getElementById('featured-image-upload').click();
});
}
@ -37380,7 +37286,6 @@ function () {
switch (e) {
case EditorEvent.EDITOR_SAVE:
new _PostActions.default().update(this.postID, this.post, PostEditor.uploadFiles, this.postFinalKey).then(function (response) {
//console.log(response.bounce.newPost);
var freshDB = new _dexie.default("fipamo_posts");
freshDB.version(1).stores({
postList: 'id, post'
@ -37389,7 +37294,7 @@ function () {
freshDB.postList.get(Number(response.bounce.newPost)).then(function (obj) {
window.location = "/@/dashboard/posts/edit/" + obj.post.slug;
});
}, 200); //this.editor.notify(DataEvent.POST_UPDATED, this.postID);
}, 200);
}).catch(function (err) {
console.log("ERROR", err);
});
@ -37417,23 +37322,26 @@ function () {
break;
case EditorEvent.EDITOR_UPLOAD_POST_IMAGE:
document.getElementById('post-image').click();
document.getElementById('post-image-upload').click();
break;
}
}
}, {
key: "handleDragOver",
value: function handleDragOver(e) {
key: "handleImageActions",
value: function handleImageActions(e) {
e.stopPropagation();
e.preventDefault();
var self = this;
switch (e.type) {
case "dragover":
e.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}
}, {
key: "handleDrop",
value: function handleDrop(e) {
e.stopPropagation();
e.preventDefault();
PostEditor.uploadFiles = e.dataTransfer.files; //console.log(MemberArea.uploadFiles.length);
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.
@ -37446,17 +37354,51 @@ function () {
reader.onload = function (theFile) {
return function (f) {
// Render thumbnail.
var span = document.createElement('span');
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').insertBefore(span, null);
document.getElementById('featured-image-drop').innerHTML = '';
document.getElementById('featured-image-drop').appendChild(span);
document.getElementById('featured-image-drop').appendChild(image);
};
}(f); // Read in the image file as a data URL.
reader.readAsDataURL(f);
}
if (e.target.id == "featured-image-upload") this.handleImageUpload(e.target.id, PostEditor.uploadFiles);
break;
}
}
}, {
key: "handleImageUpload",
value: function handleImageUpload(type, files) {
var url = "";
var eventType = "";
var self = this;
type == "featured-image-upload" ? url = "/api/post/add-feature-image" : url = "/api/post/add-post-image";
type == "featured-image-upload" ? eventType = DataEvent.FEATURE_IMAGE_ADDED : eventType = DataEvent.POST_IMAGE_ADDED;
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;
}
type == "featured-image-upload" ? imageData.append('feature_image', file, file.name) : imageData.append('post_image', file, file.name);
}
this.dataUtils.request(url, eventType, _DataUtils.REQUEST_TYPE_POST, _DataUtils.CONTENT_TYPE_FORM, imageData).then(function (response) {
var r = JSON.parse(response.request['response']);
if (r.message == DataEvent.POST_IMAGE_ADDED) self.editor.notify(EditorEvent.EDITOR_UPLOAD_POST_IMAGE, r.url);
}).catch(function (err) {
console.log(err);
});
}
}, {
key: "handleFeatureImageAdd",

File diff suppressed because one or more lines are too long

@ -30,8 +30,8 @@ block main-content
else
#featured-new-image-btn
button#new-upload-link
svg#new-upload-link(viewBox="0 0 20 20" class="icons")
button#new-feature-upload
svg#new-feature-upload(viewBox="0 0 20 20" class="icons")
use(xlink:href='/dash/assets/images/sprite.svg#entypo-image-inverted')
#featured-image-drop
img(src=post_feature)
@ -58,8 +58,8 @@ block main-content
textarea(id='post_tags' type='text', name='post_tags' class='form-control', placeholder='tags [comma seperated]', autofocus)
=post_tags
include partials/editor
input(id="featured-click" type="file" name="featured-click")
input(id="post-image" type="file" name="post-image")
input(id="featured-image-upload" type="file" name="featured-image-upload")
input(id="post-image-upload" type="file" name="post-image-upload")
#edit-post
#edit-post-wrapper
pre

@ -15,7 +15,7 @@ import sanitize from 'sanitize-html'
import DateUtils from '../../../../../brain/tools/utilities/DateUtils';
var md = require('markdown-it')('commonmark');
var uuidv4 = require('uuid/v4');
class PostActions
export default class PostActions
{
//--------------------------
// constructor
@ -125,7 +125,6 @@ class PostActions
});
})
})
//(updated) ? resolve("UPDATED") : reject("NOT UPDATED");
});
}
else
@ -158,110 +157,6 @@ class PostActions
}
})
}
sync(files)
{
let self = this;
return new Promise((resolve, reject) =>
{
var syncData = new FormData();
var postList = '';
for (var i = 0; i < files.length; i++)
{
var file = files[i];
// Check the file type.
if (!file.type.match('image.*'))
{
continue;
}
// Add the file to the request.
syncData.append('feature_image', file, file.name);
}
var fipamoPosts = new Dexie("fipamo_posts");
fipamoPosts.version(1).stores(
{
postList: 'id,post'
});
fipamoPosts.postList.toArray(array =>
{
self.dataUtils.request('/api/post/sync', DataEvent.POSTS_SYNCED, REQUEST_TYPE_POST, CONTENT_TYPE_JSON, array).then((response) =>
{
resolve(
{
response
})
}).catch((err) =>
{
reject(
{
err
});
})
//console.log("LIST", postList);
})
})
}
submitPost(edit, uploadFiles)
{
let self = this;
return new Promise(function(resolve, reject)
{
//collect form data
//if(!this.validateForm())
var postData = new FormData();
//let projectImages = document.getElementById('projectImages');
//var fileSelect = projectImages;
var files = uploadFiles;
for (var i = 0; i < files.length; i++)
{
var file = files[i];
// Check the file type.
if (!file.type.match('image.*'))
{
continue;
}
// Add the file to the request.
postData.append('feature_image', file, file.name);
}
//var category = document.getElementById("content_category");
//let project_form = document.forms.namedItem("folio-project");
var txt = document.createElement("textarea");
txt.innerHTML = document.getElementById('edit-post-text').innerHTML;
postData.append("title", document.getElementById('post_title').value);
postData.append('slug', new StringUtils().cleanString(document.getElementById('post_title').value));
postData.append("post_plaintext", txt.value);
postData.append("origin_date", document.getElementById('post-date').value);
postData.append("tags", document.getElementById('post_tags').value);
postData.append("status_page", document.getElementById('option-page').getAttribute('data-active'));
postData.append("status_feature", document.getElementById('option-feature').getAttribute('data-active'));
postData.append("status_published", document.getElementById('option-published').getAttribute('data-active'));
let postURL;
let postEventType;
if (edit)
{
let postID = document.getElementById('edit-update').getAttribute('data-id');
postURL = "/api/post/update/" + postID;
postEventType = DataEvent.POST_UPDATED;
}
else
{
postURL = "/api/post/add";
postEventType = DataEvent.POST_ADDED;
}
self.dataUtils.request(postURL, postEventType, REQUEST_TYPE_POST, CONTENT_TYPE_FORM, postData).then((response) =>
{
resolve(
{
response
})
}).catch((err) =>
{
reject(
{
err
});
})
});
}
deletePost()
{
let self = this;
@ -288,8 +183,3 @@ class PostActions
// event handlers
//--------------------------
}
export
{
PostActions as
default
}

@ -54,7 +54,9 @@ export default class PostEditor
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);
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',
@ -84,14 +86,14 @@ export default class PostEditor
let self = this;
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', e => this.handleFeatureImageAdd(e), false);
if (document.getElementById('new-upload-link'))
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-upload-link').addEventListener('click', e =>
document.getElementById('new-feature-upload').addEventListener('click', e =>
{
document.getElementById('featured-click').click();
document.getElementById('featured-image-upload').click();
})
}
var optionButtons = document.querySelectorAll('.post-option-btn');
@ -132,7 +134,6 @@ export default class PostEditor
case EditorEvent.EDITOR_SAVE:
new PostActions().update(this.postID, this.post, PostEditor.uploadFiles, this.postFinalKey).then(response =>
{
//console.log(response.bounce.newPost);
let freshDB = new Dexie("fipamo_posts");
freshDB.version(1).stores(
{
@ -145,7 +146,6 @@ export default class PostEditor
window.location = "/@/dashboard/posts/edit/" + obj.post.slug;
});
}, 200);
//this.editor.notify(DataEvent.POST_UPDATED, this.postID);
}).catch(err =>
{
console.log("ERROR", err)
@ -179,22 +179,23 @@ export default class PostEditor
}
break;
case EditorEvent.EDITOR_UPLOAD_POST_IMAGE:
document.getElementById('post-image').click();
document.getElementById('post-image-upload').click();
break;
}
}
handleDragOver(e)
handleImageActions(e)
{
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}
handleDrop(e)
var self = this;
switch (e.type)
{
e.stopPropagation();
e.preventDefault();
PostEditor.uploadFiles = e.dataTransfer.files;
//console.log(MemberArea.uploadFiles.length);
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.
@ -209,19 +210,55 @@ export default class PostEditor
return function(f)
{
// Render thumbnail.
var span = document.createElement('span');
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').insertBefore(span, null);
document.getElementById('featured-image-drop').innerHTML = '';
document.getElementById('featured-image-drop').appendChild(span);
document.getElementById('featured-image-drop').appendChild(image);
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
if(e.target.id == "featured-image-upload") this.handleImageUpload(e.target.id, PostEditor.uploadFiles);
break;
}
}
handleImageUpload(type, files)
{
let url = ""
let eventType = "";
let self = this;
(type == "featured-image-upload") ? url = "/api/post/add-feature-image" : url = "/api/post/add-post-image";
(type == "featured-image-upload") ? eventType = DataEvent.FEATURE_IMAGE_ADDED : eventType = DataEvent.POST_IMAGE_ADDED
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;
}
(type == "featured-image-upload") ? imageData.append('feature_image', file, file.name): imageData.append('post_image', file, file.name);
}
this.dataUtils.request(url, eventType, REQUEST_TYPE_POST, CONTENT_TYPE_FORM, imageData).then((response) =>
{
let r = JSON.parse(response.request['response']);
if(r.message == DataEvent.POST_IMAGE_ADDED)
self.editor.notify(EditorEvent.EDITOR_UPLOAD_POST_IMAGE, r.url);
}).catch((err) =>
{
console.log(err)
})
}
handleFeatureImageAdd(e)
{

@ -140,7 +140,7 @@
color $secondary
padding 5px
#featured-click, #post-image
#featured-image-upload, #post-image-upload
display none
#post-feature
@ -169,7 +169,7 @@
position absolute
margin 20px
#new-upload-link
#new-feature-upload
padding-top 4px
background $white

Loading…
Cancel
Save