Dataset/JS/AngularCosmoPage/pageCtrl.js (313 lines of code) (raw):

/************************************************** * Page Controller * * Make new pages and edit old pages. * **************************************************/ angular.module('cosmo').controller('pageCtrl', ['$scope', 'REST', '$location', 'Page', '$rootScope', '$routeParams', '$upload', 'Users', '$translate', function($scope, REST, $location, Page, $rootScope, $routeParams, $upload, Users, $translate){ // Initialize variables $scope.page = { id: Page.id, title: Page.title, description: Page.description, url: Page.url, publish: Page.publish, scheduleDate: Page.scheduleDate, tags: Page.tags, type: Page.type, themePages: [] }; // Set the date to today if no date was set if(!$scope.page.scheduleDate || $location.path() === '/new') $scope.page.scheduleDate = new Date(); // Math.round(+new Date().getTime()/1000); Depreciate? // Initialize schedule date - Depreciate? var date = new Date($scope.page.scheduleDate * 1000); var hours = date.getHours() > 12 ? date.getHours() - 12 : date.getHours(); var ampm = date.getHours() > 12 ? 'PM' : 'AM'; var formattedDate = date.getMonth() + 1 +'/'+ date.getDate() +'/'+ date.getFullYear() +' '+ hours +':'+ date.getMinutes() +' '+ ampm; // $scope.page.scheduleDate = formattedDate; // Get the pages available to this theme $scope.page.themePages = Page.themePages; // Initialize the page type if(Page.type) $scope.page.type = Page.type; else $scope.page.type = $scope.page.themePages[0]; // todo: Save Page.extras save locally too // Check if there's an unsaved version from a previous session var elements = ['title', 'description', 'publish', 'scheduleDate', 'header', 'subheader', 'body', 'url']; if($location.path() !== '/new'){ // Don't apply this to new pages angular.forEach(elements, function(value){ if(localStorage.getItem($routeParams.url + value) !== Page[value] && localStorage.getItem($routeParams.url + value) !== 'null') $scope.newerVersion = true; }); } // Revert to the previously saved version $scope.localVersion = function(){ var elements = ['title', 'description', 'publish', 'scheduleDate', 'header', 'subheader', 'body', 'url']; angular.forEach(elements, function(value){ // Restore item if(localStorage.getItem($routeParams.url + value) !== 'null') Page[value] = localStorage.getItem($routeParams.url + value); // Clear item from storage localStorage.setItem($routeParams.url + value, null); }); $scope.newerVersion = false; $rootScope.$broadcast('contentGet'); }; // Delete newer version $scope.deleteNewerVersion = function(){ var elements = ['title', 'description', 'publish', 'scheduleDate', 'header', 'subheader', 'body', 'url']; angular.forEach(elements, function(value){ localStorage.setItem($routeParams.url + value, null); }); $scope.newerVersion = false; }; // Delete the page $scope.deletePage = function(){ // Delete the page REST.content.delete({ contentID: $scope.page.id }, function(data){ // Success message $translate('deleted').then(function(translatedText){ $rootScope.$broadcast('notify', {message: translatedText}); }); }); // Delete all revisions of this page REST.contentRevisions.delete({ contentID: $scope.page.id }); // Delte all extra revisions REST.contentRevisionsExtras.delete({ contentID: $scope.page.id }); // Delete all extras from this page REST.contentExtras.delete({ contentID: $scope.page.id }); // Delete all tags for this page REST.contentTags.delete({ contentID: $scope.page.id }); // Redirect to the default new page $location.path('new'); }; // Watch for page change var updatePage = function() { $scope.page.title = Page.title; $scope.page.description = Page.description; $scope.page.url = Page.url; $scope.page.type = Page.type; $scope.page.tags = Page.tags; }; updatePage(); $scope.$on('contentGet', function(){ updatePage(); }); // Update the page type $scope.updatePageType = function(){ Page.type = $scope.page.type; $rootScope.$broadcast('settingsGet'); }; // Auto-generate the url from the title $scope.titleChange = function(){ // Log changes to the Page object Page.title = $scope.page.title; // Only auto-generate urls for new pages if($scope.page.url === '/new' || $scope.page.url === 'new' || !$scope.page.url) $scope.autoURL = true; if($scope.autoURL){ // Change spaces to hyphens, convert to lowercase, and remove punctuation $scope.page.url = $scope.page.title.toLowerCase().replace(/ /g, '-').replace(/[\.,\/#!$%\^&\*;:{}=_'~()\?]/g, ''); Page.url = $scope.page.url; } }; // Save changes to the description $scope.descriptionChange = function(){ Page.description = $scope.page.description; }; // Save changes to the url $scope.urlChange = function(){ Page.url = $scope.page.url; }; // Update page variables when they are changed $scope.saveLocal = function(){ Page.title = $scope.page.title; Page.description = $scope.page.description; Page.url = $scope.page.url; Page.type = $scope.page.type; // Save to local Storage localStorage.setItem($routeParams.url + 'title', Page.title); localStorage.setItem($routeParams.url + 'description', Page.description); localStorage.setItem($routeParams.url + 'url', Page.url); localStorage.setItem($routeParams.url + 'publish', Page.publish); localStorage.setItem($routeParams.url + 'scheduleDate', Page.scheduleDate); localStorage.setItem($routeParams.url + 'type', Page.type); }; // Autocomplete tags $scope.autocompleteTags = function(){ var tag = $scope.page.tags[$scope.page.tags.length - 1]; if(tag){ REST.contentTags.query({ tag: tag }, function(data){ $scope.page.suggestions = data; }, function(){ // no tag found $scope.page.suggestions = []; }); } else $scope.page.suggestions = []; }; // Select tag from autocomplete $scope.selectSuggestion = function(tag){ var tags = angular.copy($scope.page.tags); tags[tags.length - 1] = tag; tags[tags.length] = ''; $scope.page.tags = tags; $scope.page.suggestions = []; }; // Save the page $scope.savePage = function(duplicate){ // Check for duplicate URL if(duplicate && $scope.page.url === $location.path()){ $translate('page_different_url').then(function(translatedText){ $rootScope.$broadcast('notify', {message: translatedText, classes: 'alert-error'}); }); return; } // Make sure there is a page type if(!$scope.page.type){ $translate('page_no_type_selected').then(function(translatedText){ $rootScope.$broadcast('notify', {message: translatedText, classes: 'alert-error'}); }); return; } // If there's no custom title tag, use the header if($scope.page.title){ if($scope.page.title.length === 0) $scope.page.title = Page.header; } // If there's no custom url, throw an error if($scope.page.url.length === 0 || $scope.page.url === 'new'){ $translate('page_no_url').then(function(translatedText){ $rootScope.$broadcast('notify', { message: translatedText, classes: 'alert-error' }); }); return; } // Get the scheduled date to publish var scheduleDate; if($scope.page.publish === 'Y' && Page.publish === 'Y') // If this was already published, don't update the published date scheduleDate = Page.scheduleDate; else if($scope.page.publish === 'Y') // If publishing now, set the publish date to the current time scheduleDate = Math.round(+new Date().getTime()/1000); else if($scope.page.publish === 'schedule'){ scheduleDate = Date.parse($scope.page.scheduleDate).getTime()/1000; // Check if this is back dated if(Date.parse($scope.page.scheduleDate).getTime() < Math.round(+new Date().getTime())) $scope.page.publish = 'Y'; else $scope.page.publish = 'N'; } // Get the featured image URL if(Page.extras.featured) var featured = Page.extras.featured.src; else var featured = null; // Create a new page or a duplicate if($location.path() === '/new' || duplicate){ // Save content REST.content.save({ title: $scope.page.title, description: $scope.page.description, header: Page.header, subheader: Page.subheader, featured: featured, body: Page.body, url: $scope.page.url, type: $scope.page.type, published: $scope.page.publish, published_date: scheduleDate, author: Users.id }, newPagePromise, function(){ // Error $translate('page_error_saving').then(function(translatedText){ $rootScope.$broadcast('notify', {message: translatedText, classes: 'alert-error'}); }); }); } else { // Update existing page var revisionID; // Update the page REST.content.update({ contentID: Page.id, title: $scope.page.title, description: $scope.page.description, header: Page.header, subheader: Page.subheader, featured: featured, body: Page.body, url: $scope.page.url, type: $scope.page.type, published: $scope.page.publish, published_date: scheduleDate, author: Users.id }, updatePagePromise, function(data){ // Error $translate('page_error_updating').then(function(translatedText){ $rootScope.$broadcast('notify', {message: translatedText, classes: 'alert-error'}); }); }); } // Update the page after a new page was saved function newPagePromise(data){ var contentID = data.id; // Reset variables to edit page $scope.page.id = contentID; $scope.autoURL = false; // Save new tags if($scope.page.tags){ angular.forEach($scope.page.tags, function(value){ REST.contentTags.save({ contentID: contentID, tag: value }); }); } // Save page as a revision REST.contentRevisions.save({ contentID: contentID, title: $scope.page.title, description: $scope.page.description, header: Page.header, subheader: Page.subheader, featured: featured, body: Page.body, url: $scope.page.url, type: $scope.page.type, published: $scope.page.publish, published_date: scheduleDate, author: Users.id }, saveRevisionPromise); } // Update the page after saving a page revision function saveRevisionPromise(data){ revisionID = data.id; // Save additional data if there is any if(Object.keys(Page.extras).length === 0){ // Success message $translate('saved').then(function(translatedText){ $rootScope.$broadcast('notify', {message: translatedText}); }); // Redirect to new page $location.path($scope.page.url); } else { for(var key in Page.extras){ // Stringify arrays and objects if(typeof Page.extras[key] === 'object') Page.extras[key] = angular.toJson(Page.extras[key]); // Save extra REST.contentExtras.save({ contentID: $scope.page.id, name: key, extra: Page.extras[key] }, saveExtrasPromise, saveExtrasPromise); // Save extra to revisions REST.contentRevisionsExtras.save({ revisionID: revisionID, contentID: $scope.page.id, name: key, extra: Page.extras[key] }); } } $translate('page_created').then(function(translatedText){ $rootScope.$broadcast('notify', {message: translatedText}); }); } var extrasCounter = { i: 1 }; // Notify the user after saving the last extra function saveExtrasPromise(){ // Wait for the last extra to be saved, then redirect the user if(extrasCounter.i === Object.keys(Page.extras).length){ // Success message $translate('page_created').then(function(translatedText){ $rootScope.$broadcast('notify', {message: translatedText}); }); // Redirect to new page $location.path($scope.page.url); } else extrasCounter.i++; } // Update the page after it's been saved function updatePagePromise(data){ // Delete old tags REST.contentTags.delete({ contentID: $scope.page.id }, deleteTagsPromise); // Save page as a revision REST.contentRevisions.save({ contentID: $scope.page.id, title: $scope.page.title, description: $scope.page.description, header: Page.header, subheader: Page.subheader, featured: featured, body: Page.body, url: $scope.page.url, type: $scope.page.type, published: $scope.page.publish, published_date: $scope.page.scheduleDate, author: Users.id }, savePageRevisionPromise); } // Callback for saving a page revision function savePageRevisionPromise(data){ revisionID = data.id; // Delete old extras REST.contentExtras.delete({ contentID: $scope.page.id }, deleteExtrasPromise); } // Callback after tags are deleted function deleteTagsPromise(){ // Save new tags angular.forEach($scope.page.tags, function(value){ REST.contentTags.save({ contentID: $scope.page.id, tag: value }); }); } // Callback after deleting extras function deleteExtrasPromise(){ // Save additional data for (var key in Page.extras){ if (Page.extras.hasOwnProperty(key)){ // Stringify arrays and objects if(typeof Page.extras[key] === 'object') Page.extras[key] = angular.toJson(Page.extras[key]); // Save new extra REST.contentExtras.save({ contentID: $scope.page.id, name: key, extra: Page.extras[key] }, saveExtrasPromise, saveExtrasPromise); // Save new extra to revisions REST.contentRevisionsExtras.save({ revisionID: revisionID, contentID: $scope.page.id, name: key, extra: Page.extras[key] }); } } // If there were no extras, notify right away if(!Page.extras.length) { $translate('page_updated').then(function(translatedText){ $rootScope.$broadcast('notify', {message: translatedText}); }); } } }; }]);