2015-12-31 02:38:18 +08:00
|
|
|
"use strict";
|
|
|
|
|
2017-04-15 01:47:33 +08:00
|
|
|
const moment = require('moment');
|
|
|
|
require('moment/locale/en-gb');
|
|
|
|
const editorOptions = require("./pages/page-form");
|
2016-12-20 03:16:31 +08:00
|
|
|
|
2016-10-31 01:44:00 +08:00
|
|
|
moment.locale('en-gb');
|
2016-04-09 20:36:32 +08:00
|
|
|
|
2017-04-15 01:47:33 +08:00
|
|
|
module.exports = function (ngApp, events) {
|
2015-12-31 03:57:17 +08:00
|
|
|
|
2015-12-31 02:38:18 +08:00
|
|
|
|
2016-03-25 22:41:15 +08:00
|
|
|
ngApp.controller('PageEditController', ['$scope', '$http', '$attrs', '$interval', '$timeout', '$sce',
|
|
|
|
function ($scope, $http, $attrs, $interval, $timeout, $sce) {
|
2016-03-10 06:32:07 +08:00
|
|
|
|
2016-12-20 03:16:31 +08:00
|
|
|
$scope.editorOptions = editorOptions();
|
2016-03-25 22:41:15 +08:00
|
|
|
$scope.editContent = '';
|
2016-03-10 06:32:07 +08:00
|
|
|
$scope.draftText = '';
|
2016-12-20 03:16:31 +08:00
|
|
|
let pageId = Number($attrs.pageId);
|
|
|
|
let isEdit = pageId !== 0;
|
|
|
|
let autosaveFrequency = 30; // AutoSave interval in seconds.
|
|
|
|
let isMarkdown = $attrs.editorType === 'markdown';
|
2016-09-29 22:56:57 +08:00
|
|
|
$scope.draftsEnabled = $attrs.draftsEnabled === 'true';
|
2016-03-13 20:04:08 +08:00
|
|
|
$scope.isUpdateDraft = Number($attrs.pageUpdateDraft) === 1;
|
|
|
|
$scope.isNewPageDraft = Number($attrs.pageNewDraft) === 1;
|
2016-03-25 22:41:15 +08:00
|
|
|
|
2016-11-12 20:41:34 +08:00
|
|
|
// Set initial header draft text
|
2016-03-13 20:04:08 +08:00
|
|
|
if ($scope.isUpdateDraft || $scope.isNewPageDraft) {
|
2016-12-31 22:27:40 +08:00
|
|
|
$scope.draftText = trans('entities.pages_editing_draft');
|
2016-03-13 20:04:08 +08:00
|
|
|
} else {
|
2016-12-31 22:27:40 +08:00
|
|
|
$scope.draftText = trans('entities.pages_editing_page');
|
2016-11-12 20:41:34 +08:00
|
|
|
}
|
2016-03-12 23:52:19 +08:00
|
|
|
|
2016-12-20 03:16:31 +08:00
|
|
|
let autoSave = false;
|
2016-03-12 23:52:19 +08:00
|
|
|
|
2016-12-20 03:16:31 +08:00
|
|
|
let currentContent = {
|
2016-03-12 23:52:19 +08:00
|
|
|
title: false,
|
|
|
|
html: false
|
|
|
|
};
|
2016-03-10 06:32:07 +08:00
|
|
|
|
2016-09-29 22:56:57 +08:00
|
|
|
if (isEdit && $scope.draftsEnabled) {
|
2016-03-12 23:52:19 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
startAutoSave();
|
|
|
|
}, 1000);
|
2016-03-10 06:32:07 +08:00
|
|
|
}
|
|
|
|
|
2016-03-25 22:41:15 +08:00
|
|
|
// Actions specifically for the markdown editor
|
|
|
|
if (isMarkdown) {
|
|
|
|
$scope.displayContent = '';
|
|
|
|
// Editor change event
|
|
|
|
$scope.editorChange = function (content) {
|
|
|
|
$scope.displayContent = $sce.trustAsHtml(content);
|
|
|
|
}
|
|
|
|
}
|
2016-03-10 06:32:07 +08:00
|
|
|
|
2016-03-30 02:26:13 +08:00
|
|
|
if (!isMarkdown) {
|
|
|
|
$scope.editorChange = function() {};
|
|
|
|
}
|
|
|
|
|
2016-09-18 22:10:27 +08:00
|
|
|
let lastSave = 0;
|
|
|
|
|
2016-03-12 23:52:19 +08:00
|
|
|
/**
|
|
|
|
* Start the AutoSave loop, Checks for content change
|
|
|
|
* before performing the costly AJAX request.
|
|
|
|
*/
|
2016-03-10 06:32:07 +08:00
|
|
|
function startAutoSave() {
|
2016-03-12 23:52:19 +08:00
|
|
|
currentContent.title = $('#name').val();
|
2016-03-25 22:41:15 +08:00
|
|
|
currentContent.html = $scope.editContent;
|
2016-03-10 06:32:07 +08:00
|
|
|
|
2016-03-12 23:52:19 +08:00
|
|
|
autoSave = $interval(() => {
|
2016-09-18 22:10:27 +08:00
|
|
|
// Return if manually saved recently to prevent bombarding the server
|
|
|
|
if (Date.now() - lastSave < (1000*autosaveFrequency)/2) return;
|
2016-12-20 03:16:31 +08:00
|
|
|
let newTitle = $('#name').val();
|
|
|
|
let newHtml = $scope.editContent;
|
2016-03-10 06:32:07 +08:00
|
|
|
|
2016-03-12 23:52:19 +08:00
|
|
|
if (newTitle !== currentContent.title || newHtml !== currentContent.html) {
|
|
|
|
currentContent.html = newHtml;
|
|
|
|
currentContent.title = newTitle;
|
2016-03-25 22:41:15 +08:00
|
|
|
saveDraft();
|
2016-03-10 06:32:07 +08:00
|
|
|
}
|
2016-03-25 22:41:15 +08:00
|
|
|
|
2016-03-12 23:52:19 +08:00
|
|
|
}, 1000 * autosaveFrequency);
|
2016-03-10 06:32:07 +08:00
|
|
|
}
|
|
|
|
|
2016-09-18 22:10:27 +08:00
|
|
|
let draftErroring = false;
|
2016-03-12 23:52:19 +08:00
|
|
|
/**
|
|
|
|
* Save a draft update into the system via an AJAX request.
|
|
|
|
*/
|
2016-03-25 22:41:15 +08:00
|
|
|
function saveDraft() {
|
2016-09-29 22:56:57 +08:00
|
|
|
if (!$scope.draftsEnabled) return;
|
2016-12-20 03:16:31 +08:00
|
|
|
let data = {
|
2016-03-25 22:41:15 +08:00
|
|
|
name: $('#name').val(),
|
|
|
|
html: isMarkdown ? $sce.getTrustedHtml($scope.displayContent) : $scope.editContent
|
|
|
|
};
|
|
|
|
|
|
|
|
if (isMarkdown) data.markdown = $scope.editContent;
|
|
|
|
|
2016-08-14 19:29:35 +08:00
|
|
|
let url = window.baseUrl('/ajax/page/' + pageId + '/save-draft');
|
2016-09-18 22:10:27 +08:00
|
|
|
$http.put(url, data).then(responseData => {
|
|
|
|
draftErroring = false;
|
2016-12-20 03:16:31 +08:00
|
|
|
let updateTime = moment.utc(moment.unix(responseData.data.timestamp)).toDate();
|
2016-04-09 21:26:42 +08:00
|
|
|
$scope.draftText = responseData.data.message + moment(updateTime).format('HH:mm');
|
2016-03-13 20:04:08 +08:00
|
|
|
if (!$scope.isNewPageDraft) $scope.isUpdateDraft = true;
|
2016-09-03 02:26:12 +08:00
|
|
|
showDraftSaveNotification();
|
2016-09-18 22:10:27 +08:00
|
|
|
lastSave = Date.now();
|
|
|
|
}, errorRes => {
|
|
|
|
if (draftErroring) return;
|
2016-12-31 22:27:40 +08:00
|
|
|
events.emit('error', trans('errors.page_draft_autosave_fail'));
|
2016-09-18 22:10:27 +08:00
|
|
|
draftErroring = true;
|
2016-03-12 23:52:19 +08:00
|
|
|
});
|
2016-03-10 06:32:07 +08:00
|
|
|
}
|
|
|
|
|
2016-09-03 02:26:12 +08:00
|
|
|
function showDraftSaveNotification() {
|
|
|
|
$scope.draftUpdated = true;
|
|
|
|
$timeout(() => {
|
|
|
|
$scope.draftUpdated = false;
|
|
|
|
}, 2000)
|
|
|
|
}
|
|
|
|
|
2016-03-13 20:04:08 +08:00
|
|
|
$scope.forceDraftSave = function() {
|
2016-03-25 22:41:15 +08:00
|
|
|
saveDraft();
|
2016-03-13 20:04:08 +08:00
|
|
|
};
|
|
|
|
|
2017-07-03 02:35:13 +08:00
|
|
|
// Listen to save draft events from editor
|
|
|
|
$scope.$on('save-draft', saveDraft);
|
2016-05-28 20:51:07 +08:00
|
|
|
|
2016-03-12 23:52:19 +08:00
|
|
|
/**
|
|
|
|
* Discard the current draft and grab the current page
|
|
|
|
* content from the system via an AJAX request.
|
|
|
|
*/
|
|
|
|
$scope.discardDraft = function () {
|
2016-08-14 19:29:35 +08:00
|
|
|
let url = window.baseUrl('/ajax/page/' + pageId);
|
2017-07-22 21:02:47 +08:00
|
|
|
$http.get(url).then(responseData => {
|
2016-03-12 23:52:19 +08:00
|
|
|
if (autoSave) $interval.cancel(autoSave);
|
2016-12-31 22:27:40 +08:00
|
|
|
$scope.draftText = trans('entities.pages_editing_page');
|
2016-03-13 20:04:08 +08:00
|
|
|
$scope.isUpdateDraft = false;
|
2016-03-12 23:52:19 +08:00
|
|
|
$scope.$broadcast('html-update', responseData.data.html);
|
2016-03-25 22:41:15 +08:00
|
|
|
$scope.$broadcast('markdown-update', responseData.data.markdown || responseData.data.html);
|
2016-03-13 20:04:08 +08:00
|
|
|
$('#name').val(responseData.data.name);
|
2016-03-12 23:52:19 +08:00
|
|
|
$timeout(() => {
|
|
|
|
startAutoSave();
|
|
|
|
}, 1000);
|
2016-12-31 22:27:40 +08:00
|
|
|
events.emit('success', trans('entities.pages_draft_discarded'));
|
2016-03-12 23:52:19 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-03-10 06:32:07 +08:00
|
|
|
}]);
|
2016-05-13 06:12:05 +08:00
|
|
|
};
|