167 lines
6.7 KiB
JavaScript
167 lines
6.7 KiB
JavaScript
/*
|
|
** Views/Partial/LCA/BOM.cshtml is using this controller
|
|
*/
|
|
|
|
angular.module('CarbonFootprint')
|
|
.controller('AssmtFactorController', ['$scope', '$http', '$routeParams', 'Notification', 'JoinSurveyItemAndParameter', 'MultiLanguageService', '$q', '$filter',
|
|
function ($scope, $http, $routeParams, Notification, JoinService, MultiLanguageService, $q, $filter) {
|
|
// Multi-language resource object
|
|
var resource,
|
|
inited = false;
|
|
|
|
// This property controlls modal form, will be passed to table-edit-modal directive
|
|
$scope.modalFormOption = {};
|
|
$scope.model = [];
|
|
$scope.gradingScore = 100;
|
|
$scope.scoreRange = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
|
|
// set $scope.baseUrl
|
|
var baseUrl = $scope.baseUrl;
|
|
if (typeof (baseUrl) === 'undefined' || baseUrl == null) {
|
|
var arr = window.location.href.split("/");
|
|
if (arr[3].indexOf('app') == 0)
|
|
baseUrl = '';
|
|
else
|
|
baseUrl = '/' + arr[3];
|
|
$scope.baseUrl = baseUrl;
|
|
}
|
|
|
|
$scope.createProcess = function () {
|
|
// Create an object & select it
|
|
var toBeCreated = {};
|
|
toBeCreated.ID = 0;
|
|
toBeCreated.LCAID = $routeParams.LCAID;
|
|
toBeCreated.FactorScores = [{
|
|
ID: 0,
|
|
FactorId: 0,
|
|
OptionDescription: "",
|
|
OptionScore: 0
|
|
}];
|
|
|
|
// For modal form
|
|
$scope.selectRow.toBeEdit = toBeCreated;
|
|
$scope.modalFormOption.show = true;
|
|
|
|
$scope.addItem = function (index) {
|
|
$scope.selectRow.toBeEdit.FactorScores.splice(index + 1, 0, {
|
|
ID: 0,
|
|
FactorId: 0,
|
|
OptionDescription: "",
|
|
OptionScore: 1
|
|
});
|
|
};
|
|
|
|
$scope.deleteItem = function (index) {
|
|
$scope.selectRow.toBeEdit.FactorScores.splice(index, 1);
|
|
};
|
|
|
|
$scope.save = function () {
|
|
|
|
$http.post($scope.baseUrl+'/api/AssmtFactor/Save/' + $routeParams.LCAID + "/" + toBeCreated.ID, toBeCreated)
|
|
.success(function (data) {
|
|
$scope.model.unshift(data);
|
|
$scope.modalFormOption.show = false;
|
|
})
|
|
.error(function (error) {
|
|
console.log(error);
|
|
Notification.error({ message: error.ExceptionMessage, positionX: 'center', delay: 3000 });
|
|
});
|
|
};
|
|
};
|
|
|
|
$scope.editProcess = function (selected) {
|
|
var toBeEdit = $scope.selectRow.toBeEdit = angular.copy(selected);
|
|
|
|
$scope.modalFormOption.show = true;
|
|
|
|
$scope.addItem = function (index) {
|
|
$scope.selectRow.toBeEdit.FactorScores.splice(index + 1, 0, {
|
|
ID: 0,
|
|
FactorId: 0,
|
|
OptionDescription: "",
|
|
OptionScore: 1
|
|
});
|
|
};
|
|
|
|
$scope.deleteItem = function (index) {
|
|
$scope.selectRow.toBeEdit.FactorScores.splice(index, 1);
|
|
};
|
|
|
|
$scope.save = function () {
|
|
|
|
$http.post($scope.baseUrl+'/api/AssmtFactor/Save/' + toBeEdit.LCAID + "/" + toBeEdit.ID, toBeEdit)
|
|
.success(function (data) {
|
|
getAllDataAsync($routeParams.LCAID)
|
|
.then(function (response) {
|
|
$scope.model = response.Factors;
|
|
}, function (error) {
|
|
console.log(error);
|
|
});
|
|
$scope.modalFormOption.show = false;
|
|
})
|
|
.error(function (error) {
|
|
console.log(error);
|
|
Notification.error({ message: error.ExceptionMessage, positionX: 'center', delay: 3000 });
|
|
});
|
|
};
|
|
};
|
|
|
|
$scope.deleteProcess = function (selected) {
|
|
var isConfirm = confirm(resource.DeleteItemMsg);
|
|
|
|
if (isConfirm) {
|
|
$http.delete($scope.baseUrl+"/api/AssmtFactor/Delete/" + selected.ID)
|
|
.success(function () {
|
|
var index = $scope.model.indexOf(selected);
|
|
$scope.model.splice(index, 1);
|
|
|
|
// This operate is base on selectRow directive in TableEdit.js file
|
|
$scope.selectRow.unSelect();
|
|
});
|
|
}
|
|
};
|
|
|
|
$scope.$on('changeSelectedTab', function (event, args) {
|
|
if (!inited && args.selectedTab == 'AssmtFactor') {
|
|
inited = true;
|
|
|
|
// Get data and prepare $scope.model
|
|
getAllDataAsync($routeParams.LCAID)
|
|
.then(function (response) {
|
|
$scope.model = response.Factors;
|
|
$scope.gradingScore = response.GradingScore;
|
|
}, function (error) {
|
|
console.log(error);
|
|
});
|
|
|
|
// Get multilanguage resource
|
|
MultiLanguageService.getResourceAsync()
|
|
.then(function (response) {
|
|
resource = response;
|
|
}, function (error) {
|
|
console.log(error);
|
|
});
|
|
}
|
|
});
|
|
|
|
$scope.$on('updateGradingScore', function (event, args) {
|
|
$scope.gradingScore = args.gradingScore;
|
|
});
|
|
|
|
/**
|
|
* Get all list data from server
|
|
* @param {[Int]} : LCAID
|
|
* @return {[promise]}
|
|
*/
|
|
function getAllDataAsync(LCAID) {
|
|
var deferred = $q.defer();
|
|
$http.get($scope.baseUrl+'/api/AssmtFactor/GetByLcaId/' + LCAID)
|
|
.success(function (data) {
|
|
deferred.resolve(data);
|
|
})
|
|
.error(function () {
|
|
deferred.resolve(null);
|
|
})
|
|
return deferred.promise;
|
|
}
|
|
}]); |