Moved page editing to angular controller and started work on update drafts

This commit is contained in:
Dan Brown
2016-03-09 22:32:07 +00:00
parent 1d6137f7e2
commit 59ce228c2e
14 changed files with 202 additions and 32 deletions

View File

@ -213,4 +213,49 @@ module.exports = function (ngApp, events) {
}]);
ngApp.controller('PageEditController', ['$scope', '$http', '$attrs', '$interval', function ($scope, $http, $attrs, $interval) {
$scope.editorOptions = require('./pages/page-form');
$scope.editorHtml = '';
$scope.draftText = '';
var pageId = Number($attrs.pageId);
var isEdit = pageId !== 0;
if (isEdit) {
startAutoSave();
}
$scope.editorChange = function() {
$scope.draftText = '';
}
function startAutoSave() {
var currentTitle = $('#name').val();
var currentHtml = $scope.editorHtml;
console.log('Starting auto save');
$interval(() => {
var newTitle = $('#name').val();
var newHtml = $scope.editorHtml;
if (newTitle !== currentTitle || newHtml !== currentHtml) {
currentHtml = newHtml;
currentTitle = newTitle;
saveDraftUpdate(newTitle, newHtml);
}
}, 1000*5);
}
function saveDraftUpdate(title, html) {
$http.put('/ajax/page/' + pageId + '/save-draft', {
name: title,
html: html
}).then((responseData) => {
$scope.draftText = 'Draft saved'
})
}
}]);
};