sheercms
Version:
Sheer Cliff CMS is a simple and powerful content management system (CMS) for Node JS.
202 lines (169 loc) • 6.42 kB
JavaScript
var ItemSelectorController = function ($scope, $q, $modalInstance, ContentService, GroupService, HelperService, multiple, title, branch, statuses, selectedIds, groups, onLoadItems) {
$scope.dialog = {
title: "",
selectedItem: null,
message: "",
messageType: "",
disableOk: true
};
$scope.data = {
allowedStatuses: statuses,
multiple: multiple,
items: [],
groups: [],
group: null,
selectedItems: [],
selectionLabel: ""
};
$scope.paging = {
pageSize: 10,
pageNum: 1,
pageCount: 0,
paginate: function(num) {
loadItems($scope.data.group._id, num);
}
};
$scope.$watch(function(scope) {
return scope.data.group;
},
function() {
if ($scope.data.group)
loadItems($scope.data.group._id, 1);
}
);
$scope.ok = function () {
var items = [];
if ($scope.data.selectedItems) {
for(var i = 0; i < $scope.data.selectedItems.length; i++) {
var a = $scope.data.selectedItems[i];
items.push({
contentId: a.contentId,
name: a.name,
url: a.url
});
}
}
$modalInstance.close(items);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
$scope.selectItem = function(item) {
if (isEnabled(item)) {
//add this item to the selected list if it's not already there
if ($scope.data.selectedItems && $scope.data.multiple === true) {
for(var i = 0; i < $scope.data.selectedItems.length; i++) {
if ($scope.data.selectedItems[i].contentId == item.contentId)
return;
}
$scope.data.selectedItems.push(item);
}
else {
$scope.data.selectedItems = [];
$scope.data.selectedItems.push(item);
}
}
};
$scope.unSelectItem = function(item) {
//remove the item from the selected list
if ($scope.data.selectedItems) {
for(var i = 0; i < $scope.data.selectedItems.length; i++) {
if ($scope.data.selectedItems[i].contentId == item.contentId) {
$scope.data.selectedItems.splice(i, 1);
return;
}
}
}
};
function isEnabled(item) {
if ($scope.data.allowedStatuses === null || $scope.data.allowedStatuses.indexOf(item.status) < 0)
return false;
else
return true;
}
function loadGroups(branch) {
var d = $q.defer();
GroupService.list(branch, null).then(function(result) {
if (result && result.success === true) {
//load groups hash
var hash = HelperService.toBoolHash(groups);
var hasGroups = (groups && groups.length > 0);
//only add groups that the user has read access to
$scope.data.groups = [];
for(var i = 0; i < result.groups.length; i++) {
var g = result.groups[i];
if (g.rights && g.rights.read == true) {
if (hasGroups === true) {
//see if this group is in the groups hash
if (hash[g.name] === true)
$scope.data.groups.push(g);
}
else
$scope.data.groups.push(g);
}
}
} else if (result) {
setMessage(result.message, 'alert-danger', false);
} else {
setMessage('An unknown error occurred.', 'alert-danger', false);
}
d.resolve();
});
return d.promise;
}
function loadItems(groupId, pageNumber) {
$scope.paging.pageNum = pageNumber;
//load the items in this group
ContentService.getItems(groupId, pageNumber, $scope.paging.pageSize, true).then(function(data) {
setMessage(data.message, data.success == true ? "alert-success" : "alert-danger");
if (data.success && data.items) {
for(var i = 0; i < data.items.length; i++) {
var item = data.items[i];
item.statusCss = ContentService.getItemStatusCss(item.status);
item.ui = {};
item.ui.enabled = true;
if (!isEnabled(item)) {
item.ui.enabled = false;
item.ui.tooltip = "This item cannot be selected due to it's status";
}
}
if (onLoadItems && typeof onLoadItems === 'function')
onLoadItems(data.items);
$scope.data.items = data.items;
//set the paging data
$scope.paging.pageCount = Math.ceil(data.total / $scope.paging.pageSize);
$scope.paging.pageNum = pageNumber;
}
else
$scope.data.items = [];
});
};
function clearMessage() {
$scope.dialog.message = "";
$scope.dialog.messageType = "";
}
function setMessage(msg, type) {
$scope.dialog.message = msg;
$scope.dialog.messageType = type;
}
function init()
{
$scope.dialog.title = title || "Item Selector";
$scope.data.selectionLabel = multiple === true ? "Selected Items:" : "Selected Item:";
loadGroups(branch).then(function() {
if ($scope.data.groups && $scope.data.groups.length > 0) {
//load the items from the first group in the list
$scope.data.group = $scope.data.groups[0];
}
});
if (selectedIds && selectedIds.length > 0) {
//load the pre-selected items
ContentService.getItemList(selectedIds, true).then(function(result) {
if (result && result.success == true && result.items) {
$scope.data.selectedItems = result.items;
}
});
}
}
init();
};