You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Fipamo/src/com/actions/PageActions.js

111 lines
3.1 KiB
JavaScript

import ApiUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_JSON } from '../utils/APIUtils';
import StringUtils from '../utils/StringUtils';
import * as DataEvent from '../events/DataEvent';
const api = new ApiUtils();
export default class PostActions {
//--------------------------
// constructor
//--------------------------
constructor() {}
//--------------------------
// methods
//--------------------------
collectInfo(image) {
return new Promise((resolve, reject) => {
let pageInfo = new FormData();
let txt = document.createElement('textarea');
txt.innerHTML = document.getElementById('edit-post-text').innerHTML;
let html = txt.value;
//html = html.replace(/<\/?span[^>]*>/g, ''); //removes highightjs styling
html = html.replace(/<\/?br[^>]*>/g, '\n'); //convert back to encoded line break for storage
pageInfo.append(
'id',
document.getElementById('post-edit-index').getAttribute('data-index')
);
pageInfo.append(
'uuid',
document.getElementById('post-edit-index').getAttribute('data-uuid')
);
pageInfo.append('content', html);
pageInfo.append('title', document.getElementById('post_title').value);
pageInfo.append(
'created',
document.getElementById('post-date').getAttribute('data-raw')
);
pageInfo.append(
'slug',
new StringUtils().cleanString(document.getElementById('post_title').value)
);
pageInfo.append('tags', document.getElementById('post_tags').value);
pageInfo.append(
'menu',
document.getElementById('option-menu-pin').getAttribute('data-active')
);
pageInfo.append(
'featured',
document.getElementById('option-feature').getAttribute('data-active')
);
pageInfo.append(
'published',
document.getElementById('option-published').getAttribute('data-active')
);
if (image != null || image != undefined) {
if (image.type.match('image.*')) {
pageInfo.append('feature_image', image, image.name);
} else {
reject('Not an image file');
}
} else {
//check to see if image exists
var imageURL = document.getElementById('featured-image').src;
imageURL != null || imageURL != undefined
? pageInfo.append('feature_image', imageURL)
: pageInfo.append('feature_image', null);
}
resolve(pageInfo);
});
}
updateNav(add, id, post) {
api.request('/api/settings/nav', DataEvent.SETTINGS_LOADED)
.then(response => {
let menu = JSON.parse(response.request['response']);
let item = {
id: id,
uuid: post.uuid,
title: post.title,
slug: post.slug
};
if (add) {
menu.push(item);
} else {
for (let index = 0; index < menu.length; index++) {
if (menu[index].id == id) {
menu.splice(index, 1);
}
}
}
api.request(
'/api/settings/nav-sync',
DataEvent.API_SETTINGS_WRITE,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
menu
)
.then(() => {
//console.log(response);
})
.catch(() => {
//console.log(err);
});
})
.catch(() => {
//console.log(err);
});
}
//--------------------------
// event handlers
//--------------------------
}