sheercms
Version:
Sheer Cliff CMS is a simple and powerful content management system (CMS) for Node JS.
323 lines (279 loc) • 9.38 kB
JavaScript
app.service('ContentTypeService', function($q, $http, $modal, DSCacheFactory) {
var self = this;
var contentTypeCache = DSCacheFactory('contentTypeCache', {
maxAge: 1800000, // Items added to this cache expire after 30 minutes.
cacheFlushInterval: 3600000, // This cache will clear itself every hour.
deleteOnExpire: 'aggressive', // Items will be deleted from this cache right when they expire.
storageMode: 'localStorage' // This cache will sync itself with `localStorage`.
});
self.clearCache = function() {
contentTypeCache.removeAll();
}
self.list = function(selectableOnly) {
var d = $q.defer();
var key = "/cms/types/list";
var data = contentTypeCache.get(key);
if (data) {
if (selectableOnly === true) {
var obj = {
success: data.success,
message: data.message,
contentTypes: getSelectableTypes(data.contentTypes)
};
d.resolve(obj);
}
else
d.resolve(data);
}
else {
$http.get("/cms/types/list").success(function (results) {
if (results.success && results.contentTypes)
contentTypeCache.put(key, results);
d.resolve(results);
}).error(function (data, status) {
d.reject(data);
});
}
return d.promise;
};
function getSelectableTypes(types) {
var arr = [];
for(var i = 0; i < types.length; i++) {
var t = types[i];
if (t.abstract !== true)
arr.push(t);
}
return arr;
}
self.save = function(contentType) {
var d = $q.defer();
var json = JSON.stringify(contentType);
var url = (contentType._id && contentType._id.length > 0) ? "/cms/types/update" : "/cms/types/create";
$http.post(url, {data:json}).success(function(data) {
self.clearCache();
d.resolve(data);
}).error(function(data, status) {
d.reject(data);
});
return d.promise;
};
self.getContentType = function(id) {
var d = $q.defer();
$http({ url:"/cms/types/get", method: 'get', params: {typeId: id }}).success(function(data) {
d.resolve(data);
}).error(function(data, status) {
d.reject(data);
});
return d.promise;
};
self.delete = function(id) {
var d = $q.defer();
$http.post("/cms/types/delete", {typeId: id }).success(function(data) {
self.clearCache();
d.resolve(data);
}).error(function(data, status) {
d.reject(data);
});
return d.promise;
};
/*
onSuccess: (value)
onCancel: ()
*/
self.openContentTypeSelector = function(title, text, multiple, onSuccess, onCancel) {
var modalInstance = $modal.open({
templateUrl: 'ContentTypeSelectorDialog',
controller: ContentTypeSelectorController,
windowClass: 'large-dialog',
resolve: {
title: function() {
return title;
},
text: function() {
return text;
},
multiple: function() {
return multiple;
}
}
});
modalInstance.result.then(function (value) {
if (onSuccess) {
onSuccess(value);
}
}, function () {
if (onCancel) {
onCancel();
}
});
};
/*
onSuccess: (value)
onCancel: ()
*/
self.openFieldDialog = function(field, names, onSuccess, onCancel) {
var modalInstance = $modal.open({
templateUrl: 'FieldPropsDialog',
controller: FieldDialogController,
windowClass: 'large-dialog',
resolve: {
field: function() {
return field;
},
names: function() {
return names;
}
}
});
modalInstance.result.then(function (value) {
if (onSuccess) {
onSuccess(value);
}
}, function () {
if (onCancel) {
onCancel();
}
});
};
});
var ContentTypeSelectorController = function ($scope, $modalInstance, ContentTypeService, title, text, multiple) {
$scope.data = {
title: title,
text: text,
multiple: multiple,
types: [],
message: "",
messageType: ""
};
$scope.ok = function () {
var arr = [];
for(var i = 0; i < $scope.data.types.length; i++) {
var t = $scope.data.types[i];
if (t.selected === true)
arr.push(t);
}
$modalInstance.close(arr);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
$scope.select = function(type) {
if (type.selected === true) {
type.selected = false;
}
else {
if (multiple !== true) {
//deselect all others in the list
for(var i = 0; i < $scope.data.types.length; i++)
$scope.data.types[i].selected = false;
}
type.selected = true;
}
};
function loadContentTypes() {
//load the content types from the server
ContentTypeService.list(false, true).then(function(data) {
$scope.data.message = data.message;
$scope.data.messageType = data.success == true ? "alert-success" : "alert-danger";
if (data.success) {
if (data.contentTypes && data.contentTypes.length > 0)
$scope.data.types = data.contentTypes;
else {
$scope.data.message = "There are no content types in the system.";
$scope.data.messageType = "alert-warning";
$scope.data.types = [];
}
}
else
$scope.data.types = [];
});
};
function init()
{
loadContentTypes();
}
init();
};
var FieldDialogController = function ($scope, $modalInstance, ContentTypeService, HelperService, DialogService, field, names) {
$scope.data = {
field: null,
fieldTypes: ['TextBox', 'TextArea', 'RichText', 'Date', 'DateTime', 'CheckBox', 'DropDown', 'Hyperlink',
'ImageSelector', 'FileSelector', 'ItemSelector', 'HtmlCodeEditor', 'CssCodeEditor', 'JavascriptCodeEditor'],
message: "",
messageType: "",
names: names,
title: "Field Properties"
};
$scope.ok = function () {
if (isValidFieldName($scope.data.field.name) === false) {
$scope.data.message = "Invalid field name.";
$scope.data.messageType = "alert-danger";
return;
}
if (isNameFree($scope.data.field.name) === false) {
$scope.data.message = "There is already a field with this name.";
$scope.data.messageType = "alert-danger";
return;
}
$modalInstance.close($scope.data.field);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
$scope.addGroupSource = function() {
DialogService.openGroupSelector(null, "Select a group", function(group) {
if (group) {
var source = $scope.data.field.source || "";
source = source.trim();
if (source.length > 0 && HelperService.endsWith(source, ',') === false)
source += ',';
source += group.name;
$scope.data.field.source = source;
}
},
function() {
//cancelled
});
};
function init()
{
//create a copy of the field object
$scope.data.field = new Field(field);
}
function isValidFieldName(name) {
var pattern = /^[a-zA-Z]{1}[a-zA-Z0-9]{3,20}$/; //4-20 characters; starts with letter; numbers and letters only.
return pattern.test(name);
}
function isNameFree(name) {
var found = false;
if ($scope.data.names) {
for (var i = 0; i < $scope.data.names.length; i++) {
var n = $scope.data.names[i];
if (n === name) {
return false;
}
}
}
return true;
}
function Field(original) {
var self = this;
if (original) {
self.name = original.name;
self.type = original.type;
self.label = original.label;
self.source = original.source;
self.help = original.help;
self.isNew = false;
}
else {
self.name = "";
self.type = "";
self.label = "";
self.source = "";
self.help = "";
self.isNew = true;
}
}
init();
};