demo20230512/Browser_Local/js/Directive/Simapro.js
2023-05-12 10:20:28 +08:00

140 lines
6.7 KiB
JavaScript

/*
Logic Solution
WeeeCarbonFootprint
Template Directive
displayOption example:
{
show: true/false,
onOk(selected,selectedType): callback function , selected : the parameter that user selected
, selectedType : the category that parameter belongs
}
*/
angular.module('View.Directive')
.directive('simapro', function (CONSTANTS) {
// set baseUrl
if (typeof (baseUrl) === 'undefined' || baseUrl == null) {
var arr = window.location.href.split("/");
if (arr[3].indexOf('app') == 0)
baseUrl = '';
else
baseUrl = '/' + arr[3];
}
return {
restrict: 'E',
scope: {
displayOption: '=displayOption'
},
transclude: true,
controller: function ($scope, $element, $timeout, $http) {
$scope.simaproShow = function (para1) {
//console.log('simaproShow',para1);
}
$scope.simaproDone = function () {
if ($scope.displayOption.onOk && $scope.simaproParameterSelected._category) {
$scope.displayOption.onOk($scope.simaproParameterSelected, $scope.simaproParameterSelected._category);
} else if ($scope.displayOption.onOk && $scope.simaproCategorySelected) {
$scope.displayOption.onOk($scope.simaproParameterSelected, $scope.simaproCategorySelected);
}
else
throw 'exception';
$scope.displayOption.show = false;
}
$scope.simaproVersionChange = function () {
if ($scope.simaproVersionSelected != null)
$scope.simaproCategories = $scope.simaproVersionSelected.Categories;
else
$scope.simaproCategories = [];
$scope.simaproCategorySelected = {};
$scope.simaproTypeSelected = {};
$scope.simaproTypes = [];
}
$scope.simaproCategoryChange = function () {
//console.log('simaproCategoryChange 1', $scope.displayOption);
var filter = $scope.displayOption.filter;
if (typeof (filter) === 'undefined')
filter = '';
$http.get(baseUrl+'/api/parameter/GetTypeWithoutParameter/' + $scope.simaproCategorySelected.ID
+ '/' + filter).success(function (data) {
$scope.simaproTypes = data
})
$scope.simaproTypeSelected = {};
}
$scope.simaproTypeChange = function () {
//console.log('simaproTypeChange 2', $scope.displayOption);
var filter = $scope.displayOption.filter;
if (typeof (filter) === 'undefined')
filter = '';
if ($scope.simaproTypeSelected != null) {
$http.get(baseUrl+'/api/parameter/GetParameter/' + $scope.simaproTypeSelected.ID
+ '/' + filter).success(function (data) {
$scope.simaproParameters = data
})
}
else
$scope.simaproParameters = [];
}
$scope.searchByEncoding = function (encoding) {
var filter = $scope.displayOption.filter;
if (typeof (filter) === 'undefined')
filter = '';
if (encoding && (encoding.length >= 1)) {
$scope.simaproParameters = [];
$http.get(baseUrl+'/api/Parameter/GetParametersByEncoding/' + $scope.simaproVersionSelected?.ID + '/' + $scope.simaproCategorySelected?.ID + '/' + $scope.simaproTypeSelected?.ID + '/' + filter + '/' + encoding).success(function (data) {
angular.forEach(data, function (entry) {
entry.Parameter._category = entry.Category
$scope.simaproParameters.push(entry.Parameter)
})
return;
})
}
else
$scope.simaproTypeChange();
}
$scope.selectParameter = function (parameter) {
$scope.simaproParameterSelected = parameter == $scope.simaproParameterSelected ? null : parameter;
}
$scope.closeSimapro = function () {
$scope.displayOption.show = false;
}
$scope.filterSimapro = function (sf) {
}
$scope.$watch('displayOption', function (newValue, oldValue) {
if ($scope.displayOption && ($scope.displayOption.show == true)) {
//console.log('simaproCategoryChange 1', $scope.displayOption);
var filter = $scope.displayOption.filter;
if (typeof (filter) === 'undefined')
filter = '';
$http.get(baseUrl+'/api/parameter/GetVersionCategory/' + filter).success(function (data) {
$scope.simaproVersions = data;
//console.log('GetVersionCategory',data);
});
}
})
},
link: function (scope, element, attrs) {
// using $watch to find modal opening event
scope.$watch('displayOption', function (newValue, oldValue) {
if (scope.displayOption && (scope.displayOption.show == true)) {
// add class 'modal-open' to <body> if a modal is opening
angular.element('body').addClass('modal-open');
//alert('show');
}
else if (scope.displayOption && (scope.displayOption.show == false)) {
// remove class 'modal-open' from <body> if a modal is colsing
angular.element('body').removeClass('modal-open');
//alert('hide');
}
// Reset form status by $setPristine
if (scope.passedForm) {
scope.passedForm.$setPristine();
}
}, true)
},
templateUrl: CONSTANTS.TB + 'simapro'
}
})