made post syncing action a bit more clean and concise.

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

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

File diff suppressed because one or more lines are too long

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

@ -11,7 +11,7 @@ from '../../../../../brain//tools/utilities/DataUtils';
import * as DataEvent from '../../../../../brain//tools/events/DataEvent'; import * as DataEvent from '../../../../../brain//tools/events/DataEvent';
import StringUtils from '../../../../../brain//tools/utilities/StringUtils'; import StringUtils from '../../../../../brain//tools/utilities/StringUtils';
import Dexie from 'dexie'; 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'; import DateUtils from '../../../../../brain/tools/utilities/DateUtils';
var md = require('markdown-it')('commonmark'); var md = require('markdown-it')('commonmark');
var uuidv4 = require('uuid/v4'); var uuidv4 = require('uuid/v4');
@ -36,7 +36,8 @@ export default class PostActions
update(id, data, files, lastKey) update(id, data, files, lastKey)
{ {
let self = this; let self = this;
return new Promise((resolve, reject) => let newID = null;
return new Promise(function(resolve, reject)
{ {
let txt = document.createElement("textarea"); let txt = document.createElement("textarea");
txt.innerHTML = document.getElementById('edit-post-text').innerHTML; txt.innerHTML = document.getElementById('edit-post-text').innerHTML;
@ -76,7 +77,7 @@ export default class PostActions
{ {
continue; 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 else
@ -85,7 +86,7 @@ export default class PostActions
} }
if (id == null) if (id == null)
{ {
this.db.postList.put( self.db.postList.put(
{ {
id: lastKey + 1, id: lastKey + 1,
post: post:
@ -95,7 +96,7 @@ export default class PostActions
slug: data.slug, slug: data.slug,
plaintext: data.plaintext, plaintext: data.plaintext,
html: data.html, html: data.html,
feature:data.feature, feature: data.feature,
created: data.created, created: data.created,
tags: data.tags, tags: data.tags,
page: data.page, page: data.page,
@ -105,43 +106,49 @@ export default class PostActions
} }
}).then(fresh => }).then(fresh =>
{ {
self.db.postList.toArray(array => newID = fresh;
});
}
else
{ {
self.dataUtils.request('/api/post/sync', DataEvent.POSTS_SYNCED, REQUEST_TYPE_POST, CONTENT_TYPE_JSON, array).then((response) => self.db.postList.update(Number(id),
{ {
let bounce = { post: data
message: response, }).then(updated =>
newPost: fresh {});
} }
self.db.postList.toArray(array =>
{
self.sync(array, newID).then(response =>
{
resolve( resolve(
{ {
bounce response
}) })
}).catch((err) => }).catch(err =>
{ {
reject( reject(
{ {
err err
}); });
});
}) })
}) })
});
} }
else sync(db, newPostId)
{
this.db.postList.update(Number(id),
{
post: data
}).then(updated =>
{ {
self.db.postList.toArray(array => let self = this;
return new Promise(function(resolve, reject)
{ {
self.dataUtils.request('/api/post/sync', DataEvent.POSTS_SYNCED, REQUEST_TYPE_POST, CONTENT_TYPE_JSON, array).then((response) => self.dataUtils.request('/api/post/sync', DataEvent.POSTS_SYNCED, REQUEST_TYPE_POST, CONTENT_TYPE_JSON, db).then((response) =>
{ {
let bounce = {
message: response,
newPost: newPostId
}
resolve( resolve(
{ {
response bounce
}) })
}).catch((err) => }).catch((err) =>
{ {
@ -151,16 +158,10 @@ export default class PostActions
}); });
}) })
}) })
//(updated) ? resolve("UPDATED") : reject("NOT UPDATED");
});
}
})
} }
deletePost() deletePost()
{ {
let self = this; let self = this;
let postID = document.getElementById('edit-update').getAttribute('data-id');
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
self.dataUtils.request("/api/post/delete/" + postID, DataEvent.POST_DELETED, REQUEST_TYPE_POST, CONTENT_TYPE_FORM).then((response) => 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 // event handlers

@ -24,9 +24,7 @@ export default class PostEditor
//-------------------------- //--------------------------
constructor() constructor()
{ {
//reframe('iframe');
let self = this; let self = this;
this.uploadFiles;
this.anim = new Animate(); this.anim = new Animate();
this.dataUtils = new DataUtils(); this.dataUtils = new DataUtils();
this.dateUtils = new DateUtils(); 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_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_UPDATE, f => this.handleEditorOptions(EditorEvent.EDITOR_UPDATE), false)
this.editor.addListener(EditorEvent.EDITOR_SAVE, f => this.handleEditorOptions(EditorEvent.EDITOR_SAVE), 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); self.handleImageUpload(e.target.id, e.target.files);
}, false); }, false);
TinyDatePicker(document.getElementById('post-date'), 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('dragover', this.handleImageActions, false);
document.getElementById('featured-image-drop').addEventListener('drop', 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')) if (document.getElementById('new-feature-upload'))
{ {
document.getElementById('new-feature-upload').addEventListener('click', e => document.getElementById('new-feature-upload').addEventListener('click', e =>
@ -141,7 +140,7 @@ export default class PostEditor
}); });
setTimeout(f => 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; window.location = "/@/dashboard/posts/edit/" + obj.post.slug;
}); });
@ -154,6 +153,7 @@ export default class PostEditor
case EditorEvent.EDITOR_UPDATE: case EditorEvent.EDITOR_UPDATE:
new PostActions().update(this.postID, this.post, PostEditor.uploadFiles, this.postFinalKey).then(response => 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); this.editor.notify(DataEvent.POST_UPDATED, this.postID);
}).catch(err => }).catch(err =>
{ {
@ -167,7 +167,6 @@ export default class PostEditor
{ {
let note = JSON.parse(response['response']['request'].response); let note = JSON.parse(response['response']['request'].response);
window.location = "/@/dashboard/posts/"; window.location = "/@/dashboard/posts/";
//console.log(note);
}).catch((err) => }).catch((err) =>
{ {
console.log(err) console.log(err)
@ -226,7 +225,7 @@ export default class PostEditor
// Read in the image file as a data URL. // Read in the image file as a data URL.
reader.readAsDataURL(f); 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; break;
} }
} }
@ -235,8 +234,8 @@ export default class PostEditor
let url = "" let url = ""
let eventType = ""; let eventType = "";
let self = this; let self = this;
(type == "featured-image-upload") ? url = "/api/post/add-feature-image" : url = "/api/post/add-post-image"; (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") ? eventType = DataEvent.FEATURE_IMAGE_ADDED: eventType = DataEvent.POST_IMAGE_ADDED
var imageData = new FormData(); var imageData = new FormData();
for (var i = 0; i < files.length; i++) for (var i = 0; i < files.length; i++)
{ {
@ -247,95 +246,11 @@ export default class PostEditor
continue; continue;
} }
(type == "featured-image-upload") ? imageData.append('feature_image', file, file.name): imageData.append('post_image', file, file.name); (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) => this.dataUtils.request(url, eventType, REQUEST_TYPE_POST, CONTENT_TYPE_FORM, imageData).then((response) =>
{ {
let r = JSON.parse(response.request['response']); let r = JSON.parse(response.request['response']);
if(r.message == DataEvent.POST_IMAGE_ADDED) if (r.message == DataEvent.POST_IMAGE_ADDED) self.editor.notify(EditorEvent.EDITOR_UPLOAD_POST_IMAGE, r.url);
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);
}).catch((err) => }).catch((err) =>
{ {
console.log(err) console.log(err)

Loading…
Cancel
Save