Files
biomedjs/public/js/controllers/checkLists.js

115 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

2015-08-05 06:03:02 -07:00
angular.module('biomed')
2015-12-16 09:12:35 -08:00
.controller("CheckListIndexCtrl", checkListsIndexController)
.controller("CheckListAddCtrl", checkListsControllerFactory(false))
.controller("CheckListEditCtrl", checkListsControllerFactory(true))
2015-08-05 06:03:02 -07:00
function checkListsIndexController($scope, $filter, $routeParams, CheckLists, LocationBinder) {
$scope.loading = true;
2015-12-16 09:12:35 -08:00
var allData = CheckLists.index(function () {
2015-08-05 06:03:02 -07:00
$scope.loading = false;
$scope.filter();
});
var filteredData = [];
var index = 0;
var initialPageSize = 100;
var pageSize = 5;
$scope.canLoad = true;
2015-12-16 09:12:35 -08:00
$scope.$watch('query', function () {
2015-08-05 06:03:02 -07:00
$scope.filter();
});
LocationBinder($scope, ['query']);
2015-12-16 09:12:35 -08:00
$scope.filter = function () {
2015-08-05 06:03:02 -07:00
filteredData = $filter('orderBy')($filter('filter')(allData, $scope.query), $scope.sort.column, $scope.sort.descending);
index = initialPageSize;
$scope.canLoad = true;
$scope.checkLists = filteredData.slice(0, initialPageSize);
};
2015-12-16 09:12:35 -08:00
$scope.addItems = function () {
2015-08-05 06:03:02 -07:00
$scope.checkLists = $scope.checkLists.concat(filteredData.slice(index, index + pageSize));
index += pageSize;
$scope.canLoad = index < filteredData.length;
}
$scope.sort = {
column: 'name',
descending: false
};
2015-12-16 09:12:35 -08:00
$scope.selectedCls = function (column) {
2015-08-05 06:03:02 -07:00
return column == $scope.sort.column && 'sort-' + $scope.sort.descending;
}
2015-12-16 09:12:35 -08:00
$scope.changeSorting = function (column) {
2015-08-05 06:03:02 -07:00
var sort = $scope.sort;
if (sort.column == column) {
sort.descending = !sort.descending;
} else {
sort.column = column;
sort.descending = false;
}
$scope.filter();
};
}
function checkListsControllerFactory(isEdit) {
2015-12-16 09:12:35 -08:00
return function ($scope, CheckLists, $location, $filter, $routeParams) {
2015-08-05 06:03:02 -07:00
function addField() {
2015-12-16 09:12:35 -08:00
$scope.model.fields.push({type: 'boolean'})
2015-08-05 06:03:02 -07:00
}
function removeField(index) {
console.log('Index: ', index);
if (index != -1) {
$scope.model.fields.splice(index, 1);
}
}
2015-10-14 02:51:05 -07:00
function moveUpField(index) {
2015-12-16 09:12:35 -08:00
$scope.model.fields.splice(index - 1, 0, $scope.model.fields.splice(index, 1)[0]);
2015-10-14 02:51:05 -07:00
}
2015-12-16 09:12:35 -08:00
function moveDownField(index) {
$scope.model.fields.splice(index + 1, 0, $scope.model.fields.splice(index, 1)[0]);
}
2015-10-14 02:51:05 -07:00
2015-08-05 06:03:02 -07:00
function save() {
if (isEdit) {
2015-12-16 09:12:35 -08:00
CheckLists.update({id: $scope.model._id}, $scope.model, function () {
2015-08-10 01:42:48 -04:00
$location.path("/checkLists/");
});
2015-08-05 06:03:02 -07:00
} else {
2015-12-16 09:12:35 -08:00
CheckLists.create($scope.model, function (result) {
2015-08-05 06:03:02 -07:00
$location.path("/checkLists/" + result._id);
2015-12-16 09:12:35 -08:00
});
2015-08-05 06:03:02 -07:00
}
}
$scope.addField = addField;
$scope.removeField = removeField;
2015-10-14 02:51:05 -07:00
$scope.moveUpField = moveUpField;
$scope.moveDownField = moveDownField;
2015-08-05 06:03:02 -07:00
$scope.save = save;
$scope.isEdit = isEdit;
if (!isEdit) {
$scope.model = {
name: '',
fields: []
};
addField();
} else {
$scope.model = CheckLists.get($routeParams);
}
}
2015-08-10 01:42:48 -04:00
}