UNPKG

sheercms

Version:

Sheer Cliff CMS is a simple and powerful content management system (CMS) for Node JS.

364 lines (312 loc) 11.3 kB
app.controller('ContentTypeController', function($scope, $rootScope, ContentTypeService, TemplateService) { $scope.types = []; $scope.type = null; //the content type being edited $scope.editFieldIndex = -1; $scope.lists = { templates: [], selectedTemplates: [], inheritTypes: [], inheritedTypes: [], mediaTypes: ['None', 'Image', 'File', 'Video', 'Audio', 'Other'] }; $scope.sortableOptions = { helper: function(e, tr) { var $originals = tr.children(); var $helper = tr.clone(); $helper.children().each(function(index) { // Set helper cell sizes to match the original sizes $(this).width($originals.eq(index).width()); }); return $helper; } }; $scope.data = { title: "Content Types", message: "", messageType: "", //alert-success, alert-warning, alert-info, alert-danger fieldCheckCount: 0, fieldCheckAll: false, editing: false, isNew: false, tab: 'props', isAuthorized: false }; $scope.buttons = { back: false, newType: true, refresh: true, save: false, delete: false }; $scope.changeTab = function(tab) { $scope.data.tab = tab; }; $scope.showNewType = function() { $scope.data.title = "New Content Type"; $scope.type = { fields: [] }; $scope.data.editing = true; $scope.data.isNew = true; resetEditData(); updateButtons(); }; $scope.editType = function(id) { clearMessage(); $scope.data.title = "Edit Content Type"; $scope.data.editing = true; $scope.data.isNew = false; resetEditData(); ContentTypeService.getContentType(id).then(function(data) { if (data) { if (data.success && data.contentType) { $scope.type = data.contentType; updateSelectedListItems(); updateButtons(); } else if (data.success) setMessage('Content type not found.', 'alert-danger'); else setMessage(data.message, 'alert-danger'); } else { setMessage('Error loading the content type data.', 'alert-danger'); } }); //update the inheritTypes so it's every type EXCEPT the current one $scope.lists.inheritTypes = []; for(var i = 0; i < $scope.types.length; i++) { if ($scope.types[i]._id != id) { $scope.lists.inheritTypes.push($scope.types[i]); } } }; function updateSelectedListItems() { var type = $scope.type; if (type) { $scope.lists.inheritedTypes = []; if (type.inheritTypes) { for (var i = 0; i < type.inheritTypes.length; i++) { var id = type.inheritTypes[i]; //get the corresponding object for(var j = 0; j < $scope.types.length; j++) { if ($scope.types[j]._id == id) { $scope.lists.inheritedTypes.push($scope.types[j]); break; } } } } $scope.lists.selectedTemplates = []; if (type.templates) { for (var i = 0; i < type.templates.length; i++) { var id = type.templates[i]; //get the corresponding object for(var j = 0; j < $scope.lists.templates.length; j++) { var t = $scope.lists.templates[j]; if (t._id == id) { $scope.lists.selectedTemplates.push(t); break; } } } } } } $scope.showNewField = function() { $scope.editFieldIndex = -1; //get the existing field names var names = []; if ($scope.type.fields) { for(var i = 0; i < $scope.type.fields.length; i++) names.push($scope.type.fields[i].name); } ContentTypeService.openFieldDialog(null, names, function(field) { //ok clicked if (!$scope.type.fields) $scope.type.fields = []; $scope.type.fields.push(field); }, function() { //cancelled }); }; $scope.editField = function(field, index) { $scope.editFieldIndex = index; //get the existing field names var names = []; if ($scope.type.fields) { for(var i = 0; i < $scope.type.fields.length; i++) { //don't include the name of this field if (i != index) names.push($scope.type.fields[i].name); } } ContentTypeService.openFieldDialog(field, names, function(field) { //ok clicked if ($scope.editFieldIndex > -1 && $scope.editFieldIndex < $scope.type.fields.length) { $scope.type.fields[$scope.editFieldIndex] = field; } else { setMessage('The field was not saved because the form fields have changed.', 'alert-danger'); } }, function() { //cancelled }); }; $scope.checkAllFields = function() { if ($scope.type.fields) { for (var i = 0; i < $scope.type.fields.length; i++) { $scope.type.fields[i].checked = $scope.data.fieldCheckAll; if ($scope.type.fields[i].checked === true) $scope.data.fieldCheckCount++; else $scope.data.fieldCheckCount--; } } }; $scope.checkField = function(field) { if (field.checked === false) { $scope.data.fieldCheckAll = false; $scope.data.fieldCheckCount--; } else { $scope.data.fieldCheckCount++; } }; $scope.removeFields = function() { if ($scope.data.fieldCheckCount > 0 && $scope.type.fields && confirm("Are you sure you want to remove the checked fields?")) { //just keep the fields that aren't checked var arr = []; for (var i = 0; i < $scope.type.fields.length; i++) { var f = $scope.type.fields[i]; if (f.checked !== true) arr.push(f); } $scope.type.fields = arr; $scope.data.fieldCheckCount = 0; } }; $scope.save = function() { setMessage("Saving content type...", "alert-info"); //first copy over the inherit types and templates list values $scope.type.inheritTypes = []; if ($scope.lists.inheritedTypes) { for(var i = 0; i < $scope.lists.inheritedTypes.length; i++) { var x = $scope.lists.inheritedTypes[i]; $scope.type.inheritTypes.push(x._id); } } $scope.type.templates = []; if ($scope.lists.selectedTemplates) { for(var i = 0; i < $scope.lists.selectedTemplates.length; i++) { var x = $scope.lists.selectedTemplates[i]; $scope.type.templates.push(x._id); } } ContentTypeService.save($scope.type).then(function(data) { if (data.success) { setMessage(data.message, "alert-success"); //update the template's properties $scope.type = data.contentType; $scope.data.isNew = false; updateButtons(); loadRecords(false, false); } else { setMessage(data.message, "alert-danger"); } }); }; $scope.delete = function() { if (confirm('Are you sure you want to delete this content type?')) { setMessage("Deleting content type...", "alert-info"); ContentTypeService.delete($scope.type._id).then(function (data) { if (data.success) { loadRecords(false, false); $scope.back(); setMessage(data.message, "alert-success"); } else { setMessage(data.message, "alert-danger"); } }); } }; $scope.back = function() { $scope.data.title = "Content Types"; $scope.data.editing = false; $scope.type = null; clearMessage(); updateButtons(); }; $scope.reload = function() { loadRecords(true, false); } function resetEditData() { $scope.data.tab = 'props'; $scope.data.fieldCheckAll = false; $scope.data.fieldCheckCount = 0; $scope.lists.inheritedTypes = []; $scope.lists.selectedTemplates = []; //update the inheritTypes so it's every type $scope.lists.inheritTypes = []; for(var i = 0; i < $scope.types.length; i++) $scope.lists.inheritTypes.push($scope.types[i]); } function init() { $scope.$parent.resize('.types-view'); if ($scope.global.isAdmin === true) { $scope.data.isAuthorized = true; loadRecords(true, true); TemplateService.list(true).then(function (data) { var arr = []; arr.push({ _id: '', name: '' }); if (data && data.templates) $scope.lists.templates = arr.concat(data.templates); }); } else { $scope.data.isAuthorized = false; } } function loadRecords(showMessage, useCache) { if (useCache !== true) ContentTypeService.clearCache(); ContentTypeService.list(false).then(function(data) { if (data.success) { if (showMessage) setMessage(data.message, "alert-success"); $scope.types = data.contentTypes; } else { setMessage(data.message, "alert-danger"); } }); } function updateButtons() { if ($scope.data.editing == true) { $scope.buttons.back = true; $scope.buttons.save = true; $scope.buttons.delete = ($scope.data.isNew === false); $scope.buttons.newType = false; $scope.buttons.refresh = false; } else { $scope.buttons.back = false; $scope.buttons.save = false; $scope.buttons.delete = false; $scope.buttons.newType = true; $scope.buttons.refresh = true; } } function clearMessage() { $scope.data.message = ""; $scope.data.messageType = ""; } function setMessage(msg, type) { $scope.data.message = msg; $scope.data.messageType = type; } init(); });