made post syncing action a bit more clean and concise.

This commit is contained in:
Ro 2018-11-27 17:19:57 -05:00
parent c2b5d9a56c
commit ec5a58c49d
5 changed files with 102 additions and 260 deletions

View file

@ -35024,9 +35024,8 @@ function () {
_createClass(PostActions, [{
key: "update",
value: function update(id, data, files, lastKey) {
var _this = this;
var self = this;
var newID = null;
return new Promise(function (resolve, reject) {
var txt = document.createElement("textarea");
txt.innerHTML = document.getElementById('edit-post-text').innerHTML;
@ -35063,13 +35062,13 @@ function () {
continue;
}
data.feature = "/content/blog-images/" + _this.dateUtils.getDate('year', new Date()) + "/" + _this.dateUtils.getDate('month', new Date()) + "/" + file.name;
data.feature = "/content/blog-images/" + self.dateUtils.getDate('year', new Date()) + "/" + self.dateUtils.getDate('month', new Date()) + "/" + file.name;
}
} else {//data.feature = "";
}
if (id == null) {
_this.db.postList.put({
self.db.postList.put({
id: lastKey + 1,
post: {
uuid: uuidv4(),
@ -35086,11 +35085,36 @@ function () {
author: "user"
}
}).then(function (fresh) {
newID = fresh;
});
} else {
self.db.postList.update(Number(id), {
post: data
}).then(function (updated) {});
}
self.db.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) {
self.sync(array, newID).then(function (response) {
resolve({
response: response
});
}).catch(function (err) {
reject({
err: err
});
});
});
});
}
}, {
key: "sync",
value: function sync(db, newPostId) {
var self = this;
return new Promise(function (resolve, reject) {
self.dataUtils.request('/api/post/sync', DataEvent.POSTS_SYNCED, _DataUtils.REQUEST_TYPE_POST, _DataUtils.CONTENT_TYPE_JSON, db).then(function (response) {
var bounce = {
message: response,
newPost: fresh
newPost: newPostId
};
resolve({
bounce: bounce
@ -35101,31 +35125,11 @@ function () {
});
});
});
});
} else {
_this.db.postList.update(Number(id), {
post: data
}).then(function (updated) {
self.db.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
});
});
}); //(updated) ? resolve("UPDATED") : reject("NOT UPDATED");
});
}
});
}
}, {
key: "deletePost",
value: function deletePost() {
var self = this;
var postID = document.getElementById('edit-update').getAttribute('data-id');
return new Promise(function (resolve, reject) {
self.dataUtils.request("/api/post/delete/" + postID, DataEvent.POST_DELETED, _DataUtils.REQUEST_TYPE_POST, _DataUtils.CONTENT_TYPE_FORM).then(function (response) {
resolve({
@ -35136,7 +35140,7 @@ function () {
err: err
});
});
}); //this.dataUtils.re
});
} //--------------------------
// event handlers
//--------------------------
@ -37156,9 +37160,7 @@ function () {
_classCallCheck(this, PostEditor);
//reframe('iframe');
var self = this;
this.uploadFiles;
this.anim = new _Animate.default();
this.dataUtils = new _DataUtils.default();
this.dateUtils = new _DateUtils.default();
@ -37291,7 +37293,7 @@ function () {
postList: 'id, post'
});
setTimeout(function (f) {
freshDB.postList.get(Number(response.bounce.newPost)).then(function (obj) {
freshDB.postList.get(Number(response.response.bounce.newPost)).then(function (obj) {
window.location = "/@/dashboard/posts/edit/" + obj.post.slug;
});
}, 200);
@ -37302,6 +37304,7 @@ function () {
case EditorEvent.EDITOR_UPDATE:
new _PostActions.default().update(this.postID, this.post, PostEditor.uploadFiles, this.postFinalKey).then(function (response) {
//console.log(response.response.bounce.newPost);
_this3.editor.notify(DataEvent.POST_UPDATED, _this3.postID);
}).catch(function (err) {
console.log("ERROR", err);
@ -37312,7 +37315,7 @@ function () {
if (confirm('Aye! You know you\'re deleting this post, right?')) {
new _PostActions.default().deletePost().then(function (response) {
var note = JSON.parse(response['response']['request'].response);
window.location = "/@/dashboard/posts/"; //console.log(note);
window.location = "/@/dashboard/posts/";
}).catch(function (err) {
console.log(err);
});
@ -37400,82 +37403,6 @@ function () {
console.log(err);
});
}
}, {
key: "handleFeatureImageAdd",
value: function handleFeatureImageAdd(e) {
e.stopPropagation();
e.preventDefault();
var 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('feature_image', file, file.name);
}
this.dataUtils.request("/api/post/add-feature-image", DataEvent.FEATURE_IMAGE_ADDED, _DataUtils.REQUEST_TYPE_POST, _DataUtils.CONTENT_TYPE_FORM, postData).then(function (response) {//self.editor.notify(EditorEvent.EDITOR_UPLOAD_POST_IMAGE, JSON.parse(response.request['response']).url);
}).catch(function (err) {
console.log(err);
});
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').insertBefore(span, null);
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);
}
}
}, {
key: "handlePostImageAdd",
value: function handlePostImageAdd(e) {
e.stopPropagation();
e.preventDefault();
var 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/post/add-post-image", DataEvent.POST_IMAGE_ADDED, _DataUtils.REQUEST_TYPE_POST, _DataUtils.CONTENT_TYPE_FORM, postData).then(function (response) {
self.editor.notify(EditorEvent.EDITOR_UPLOAD_POST_IMAGE, JSON.parse(response.request['response']).url);
}).catch(function (err) {
console.log(err);
});
}
}]);
return PostEditor;

File diff suppressed because one or more lines are too long

View file

@ -26,7 +26,7 @@ block main-content
if(post_feature == 'null')
#featured-image-drop
| DRAG AND DROP IMAGE OR
label(for="featured-click") CLICK TO CHOOSE
label(for="featured-image-upload") CLICK TO CHOOSE
else
#featured-new-image-btn

View file

@ -11,7 +11,7 @@ from '../../../../../brain//tools/utilities/DataUtils';
import * as DataEvent from '../../../../../brain//tools/events/DataEvent';
import StringUtils from '../../../../../brain//tools/utilities/StringUtils';
import Dexie from 'dexie';
import sanitize from 'sanitize-html'
import sanitize from 'sanitize-html' //NOTE: Santize is a really big add - explore ways to reduce this
import DateUtils from '../../../../../brain/tools/utilities/DateUtils';
var md = require('markdown-it')('commonmark');
var uuidv4 = require('uuid/v4');
@ -36,7 +36,8 @@ export default class PostActions
update(id, data, files, lastKey)
{
let self = this;
return new Promise((resolve, reject) =>
let newID = null;
return new Promise(function(resolve, reject)
{
let txt = document.createElement("textarea");
txt.innerHTML = document.getElementById('edit-post-text').innerHTML;
@ -76,7 +77,7 @@ export default class PostActions
{
continue;
}
data.feature = "/content/blog-images/" + this.dateUtils.getDate('year', new Date()) + "/" + this.dateUtils.getDate('month', new Date()) + "/" + file.name;
data.feature = "/content/blog-images/" + self.dateUtils.getDate('year', new Date()) + "/" + self.dateUtils.getDate('month', new Date()) + "/" + file.name;
}
}
else
@ -85,7 +86,7 @@ export default class PostActions
}
if (id == null)
{
this.db.postList.put(
self.db.postList.put(
{
id: lastKey + 1,
post:
@ -95,7 +96,7 @@ export default class PostActions
slug: data.slug,
plaintext: data.plaintext,
html: data.html,
feature:data.feature,
feature: data.feature,
created: data.created,
tags: data.tags,
page: data.page,
@ -105,13 +106,45 @@ export default class PostActions
}
}).then(fresh =>
{
newID = fresh;
});
}
else
{
self.db.postList.update(Number(id),
{
post: data
}).then(updated =>
{});
}
self.db.postList.toArray(array =>
{
self.dataUtils.request('/api/post/sync', DataEvent.POSTS_SYNCED, REQUEST_TYPE_POST, CONTENT_TYPE_JSON, array).then((response) =>
self.sync(array, newID).then(response =>
{
resolve(
{
response
})
}).catch(err =>
{
reject(
{
err
});
});
})
})
}
sync(db, newPostId)
{
let self = this;
return new Promise(function(resolve, reject)
{
self.dataUtils.request('/api/post/sync', DataEvent.POSTS_SYNCED, REQUEST_TYPE_POST, CONTENT_TYPE_JSON, db).then((response) =>
{
let bounce = {
message: response,
newPost: fresh
newPost: newPostId
}
resolve(
{
@ -125,42 +158,10 @@ export default class PostActions
});
})
})
});
}
else
{
this.db.postList.update(Number(id),
{
post: data
}).then(updated =>
{
self.db.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
});
})
})
//(updated) ? resolve("UPDATED") : reject("NOT UPDATED");
});
}
})
}
deletePost()
{
let self = this;
let postID = document.getElementById('edit-update').getAttribute('data-id');
return new Promise(function(resolve, reject)
{
self.dataUtils.request("/api/post/delete/" + postID, DataEvent.POST_DELETED, REQUEST_TYPE_POST, CONTENT_TYPE_FORM).then((response) =>
@ -177,7 +178,6 @@ export default class PostActions
});
})
})
//this.dataUtils.re
}
//--------------------------
// event handlers

View file

@ -24,9 +24,7 @@ export default class PostEditor
//--------------------------
constructor()
{
//reframe('iframe');
let self = this;
this.uploadFiles;
this.anim = new Animate();
this.dataUtils = new DataUtils();
this.dateUtils = new DateUtils();
@ -54,7 +52,8 @@ 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-upload').addEventListener('change', e => {
document.getElementById('post-image-upload').addEventListener('change', e =>
{
self.handleImageUpload(e.target.id, e.target.files);
}, false);
TinyDatePicker(document.getElementById('post-date'),
@ -88,7 +87,7 @@ export default class PostEditor
{
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);
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', e =>
@ -141,7 +140,7 @@ export default class PostEditor
});
setTimeout(f =>
{
freshDB.postList.get(Number(response.bounce.newPost)).then(obj =>
freshDB.postList.get(Number(response.response.bounce.newPost)).then(obj =>
{
window.location = "/@/dashboard/posts/edit/" + obj.post.slug;
});
@ -154,6 +153,7 @@ export default class PostEditor
case EditorEvent.EDITOR_UPDATE:
new PostActions().update(this.postID, this.post, PostEditor.uploadFiles, this.postFinalKey).then(response =>
{
//console.log(response.response.bounce.newPost);
this.editor.notify(DataEvent.POST_UPDATED, this.postID);
}).catch(err =>
{
@ -167,7 +167,6 @@ export default class PostEditor
{
let note = JSON.parse(response['response']['request'].response);
window.location = "/@/dashboard/posts/";
//console.log(note);
}).catch((err) =>
{
console.log(err)
@ -226,7 +225,7 @@ export default class PostEditor
// 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);
if (e.target.id == "featured-image-upload") this.handleImageUpload(e.target.id, PostEditor.uploadFiles);
break;
}
}
@ -235,8 +234,8 @@ export default class PostEditor
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
(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++)
{
@ -247,95 +246,11 @@ export default class PostEditor
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)
{
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('feature_image', file, file.name);
}
this.dataUtils.request("/api/post/add-feature-image", DataEvent.FEATURE_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)
})
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').insertBefore(span, null);
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);
}
}
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/post/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);
if (r.message == DataEvent.POST_IMAGE_ADDED) self.editor.notify(EditorEvent.EDITOR_UPLOAD_POST_IMAGE, r.url);
}).catch((err) =>
{
console.log(err)