913 lines
35 KiB
JavaScript
913 lines
35 KiB
JavaScript
|
/*
|
|||
|
Logic Solution
|
|||
|
WeeeCarbonFootprint
|
|||
|
Factory for CarbonFootprint
|
|||
|
*/
|
|||
|
|
|||
|
angular.module('Services', [])
|
|||
|
.service('Survey_Data_Service', ['$resource', function ($resource) {
|
|||
|
|
|||
|
var arr = window.location.href.split("/");
|
|||
|
if (arr[3].indexOf('app') == 0)
|
|||
|
baseUrl = '';
|
|||
|
else
|
|||
|
baseUrl = '/' + arr[3];
|
|||
|
|
|||
|
return {
|
|||
|
API: $resource(baseUrl+'/api/:Controller/:Action/:pa1/:pa2/:pa3/:pa4',
|
|||
|
{ Controller: '@Controller', Action: '@Action', pa1: '@pa1', pa2: '@pa2', pa3: '@pa3', pa4: '@pa4' }, {
|
|||
|
//post: { method: 'POST', isArray: true }
|
|||
|
}),
|
|||
|
}
|
|||
|
}])
|
|||
|
|
|||
|
|
|||
|
.factory('Redirect', function ($location) {
|
|||
|
function Redirect() {
|
|||
|
}
|
|||
|
|
|||
|
Redirect.prototype = {
|
|||
|
backToMainPage: function () {
|
|||
|
$location.path('/LCA/LCA');
|
|||
|
},
|
|||
|
|
|||
|
goToEditUser: function (ID) {
|
|||
|
$location.path('/Management/EditUser/' + ID);
|
|||
|
},
|
|||
|
|
|||
|
goToEditSupplier: function (ID) {
|
|||
|
$location.path('/Management/EditSupplier/' + ID);
|
|||
|
},
|
|||
|
|
|||
|
backToSurveyPage: function () {
|
|||
|
$location.path('/LCA/Info');
|
|||
|
},
|
|||
|
|
|||
|
goToProductLCADetail: function (ID) {
|
|||
|
$location.path('/LCA/Detail/' + ID);
|
|||
|
},
|
|||
|
|
|||
|
backToProductSurveyPage: function () {
|
|||
|
$location.path('/LCA/High_Level_Analysis');
|
|||
|
},
|
|||
|
|
|||
|
backToOrganizationSurveyPage: function () {
|
|||
|
$location.path('/LCA/PowerUsage');
|
|||
|
},
|
|||
|
|
|||
|
goToProductInfoSurvey: function () {
|
|||
|
$location.path('/LCA/ProductInfoSurvey');
|
|||
|
},
|
|||
|
|
|||
|
goToOrganizationInfoSurvey: function () {
|
|||
|
$location.path('/LCA/OrganizationInfoSurvey');
|
|||
|
},
|
|||
|
|
|||
|
goToCreateProduct: function () {
|
|||
|
$location.path('/Product/Create');
|
|||
|
},
|
|||
|
|
|||
|
backToProduct: function () {
|
|||
|
$location.path('/Product/Index');
|
|||
|
},
|
|||
|
|
|||
|
goToCreateFab: function () {
|
|||
|
$location.path('/Fab/Create');
|
|||
|
},
|
|||
|
|
|||
|
backToFab: function () {
|
|||
|
$location.path('/Fab/Index');
|
|||
|
},
|
|||
|
|
|||
|
goToCreateUser: function () {
|
|||
|
$location.path('/Management/CreateUser');
|
|||
|
},
|
|||
|
|
|||
|
goToCreateSupplier: function () {
|
|||
|
$location.path('/Management/CreateSupplier');
|
|||
|
},
|
|||
|
|
|||
|
backToManagement: function () {
|
|||
|
$location.path('/Management/Index');
|
|||
|
},
|
|||
|
backToSupplier: function () {
|
|||
|
$location.path('/Management/Index/supplier');
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return (Redirect);
|
|||
|
})
|
|||
|
.service('LCADetailCacheService', function ($q, $http, $cacheFactory, ProductDetailCacheService, JoinSurveyItemAndParameter) {
|
|||
|
var service = this;
|
|||
|
|
|||
|
var baseUrl;
|
|||
|
var arr = window.location.href.split("/");
|
|||
|
if (arr[3].indexOf('app') == 0)
|
|||
|
baseUrl = '';
|
|||
|
else
|
|||
|
baseUrl = '/' + arr[3];
|
|||
|
//console.log('LCADetailCacheService', baseUrl);
|
|||
|
|
|||
|
this.clearLCADetailFromCache = function (ID) {
|
|||
|
var cache = getAndCreateCachIfNotExist('LCADetail');
|
|||
|
cache.remove(ID);
|
|||
|
};
|
|||
|
this.getLCADetailAsync = function (ID) {
|
|||
|
var cache = getAndCreateCachIfNotExist('LCADetail')
|
|||
|
var result = cache.get(ID);
|
|||
|
var deferred = $q.defer();
|
|||
|
//console.log('getLCADetailAsync', baseUrl);
|
|||
|
|
|||
|
if (!result || true) { //取消cache 避免特定狀況之錯誤
|
|||
|
$http.get(baseUrl+'/api/LCA/GetLCADetail/' + ID)
|
|||
|
.success(function (data) {
|
|||
|
//console.log('/api/LCA/GetLCADetail/', data);
|
|||
|
if (data != null && data.LCAobj != null) {
|
|||
|
data.LCAobj.isCompanyAdmin = data.isCompanyAdmin;
|
|||
|
data.LCAobj.productLCA = data.productLCA;
|
|||
|
data.LCAobj.product = data.product;
|
|||
|
}
|
|||
|
//if (cache.get(ID)) {
|
|||
|
// deferred.resolve(cache.get(ID))
|
|||
|
//}//this is ugly , add this because getLCADetail will be call mulitple times in each view change
|
|||
|
//else
|
|||
|
if (data != null && data.LCAobj != null && data.LCAobj.ProductID) {
|
|||
|
ProductDetailCacheService.getProductDetailAsync(data.LCAobj.ProductID).then(function (product) {
|
|||
|
data.LCAobj.productName = product.Name;
|
|||
|
cache.put(ID, data.LCAobj);
|
|||
|
result = cache.get(ID)
|
|||
|
deferred.resolve(result);
|
|||
|
})
|
|||
|
} else if (data != null && data.LCAobj != null) {
|
|||
|
cache.put(ID, data.LCAobj);
|
|||
|
result = cache.get(ID)
|
|||
|
deferred.resolve(result);
|
|||
|
}
|
|||
|
});
|
|||
|
} else {
|
|||
|
deferred.resolve(result);
|
|||
|
}
|
|||
|
//console.log('deferred.promise', deferred.promise);
|
|||
|
return deferred.promise;
|
|||
|
};
|
|||
|
this.setLCAStatusFromNewToProcessAsync = function (LCAID) {
|
|||
|
var deferred = $q.defer();
|
|||
|
$q.all([service.getLCADetailAsync(LCAID)]).then(function (result) {
|
|||
|
var lca = result[0];
|
|||
|
$http.post(baseUrl+'/api/LCA/NewProcessing/' + LCAID).success(function () {
|
|||
|
lca.Status = 1;
|
|||
|
deferred.resolve(1);
|
|||
|
})
|
|||
|
.error(function (data, status) {
|
|||
|
alert(data.ExceptionMessage);
|
|||
|
//console.log(data);
|
|||
|
});
|
|||
|
})
|
|||
|
return deferred.promise;
|
|||
|
};
|
|||
|
this.setLCAStatusFromProcessToWaitAsync = function (LCAID, changeStatus) {
|
|||
|
var deferred = $q.defer();
|
|||
|
$q.all([service.getLCADetailAsync(LCAID)]).then(function (result) {
|
|||
|
var lca = result[0];
|
|||
|
if (isProductLCA(lca.LCAType)) {
|
|||
|
$http.post(baseUrl+'/api/LCA/ProcessWaiting/' + LCAID + '/' + changeStatus).success(
|
|||
|
function (data) {
|
|||
|
if (changeStatus == 1)
|
|||
|
lca.Status = 2;
|
|||
|
deferred.resolve(data);
|
|||
|
})
|
|||
|
.error(function (data, status) {
|
|||
|
alert(data.ExceptionMessage);
|
|||
|
//console.log(data);
|
|||
|
});
|
|||
|
}
|
|||
|
else {
|
|||
|
$http.post(baseUrl+'/api/LCA/OrganizationProcessWaiting/' + LCAID).success(function () {
|
|||
|
lca.Status = 2;
|
|||
|
deferred.resolve(2);
|
|||
|
})
|
|||
|
}
|
|||
|
})
|
|||
|
return deferred.promise;
|
|||
|
};
|
|||
|
this.setLCAStatusFromRejectToProcessAsync = function (LCAID) {
|
|||
|
var deferred = $q.defer();
|
|||
|
$q.all([service.getLCADetailAsync(LCAID)]).then(function (result) {
|
|||
|
var lca = result[0];
|
|||
|
if (isProductLCA(lca.LCAType)) {
|
|||
|
$http.post(baseUrl+'/api/LCA/RejectProcessing/' + LCAID).success(function () {
|
|||
|
lca.Status = 1;
|
|||
|
deferred.resolve(1);
|
|||
|
});
|
|||
|
} else {
|
|||
|
$http.post(baseUrl+'/api/LCA/OrganizationRejectProcessing/' + LCAID).success(function () {
|
|||
|
lca.Status = 1;
|
|||
|
deferred.resolve(1);
|
|||
|
});
|
|||
|
}
|
|||
|
})
|
|||
|
return deferred.promise;
|
|||
|
};
|
|||
|
this.setLCAStatusFromConfirmToCompleteAsync = function (LCAID) {
|
|||
|
var deferred = $q.defer();
|
|||
|
$q.all([service.getLCADetailAsync(LCAID)]).then(function (result) {
|
|||
|
var lca = result[0];
|
|||
|
if (isProductLCA(lca.LCAType)) {//產品型 CFT-38
|
|||
|
$http.post(baseUrl+'/api/LCA/ConfirmCompleting/' + LCAID).success(function () {
|
|||
|
lca.Status = 5;
|
|||
|
deferred.resolve(5);
|
|||
|
})
|
|||
|
}
|
|||
|
else {//CFT-38 組織型
|
|||
|
$http.post(baseUrl+'/api/LCA/OrganizationConfirmCompleting/' + LCAID).success(function () {
|
|||
|
lca.Status = 5;
|
|||
|
deferred.resolve(5);
|
|||
|
})
|
|||
|
}
|
|||
|
})
|
|||
|
return deferred.promise;
|
|||
|
};
|
|||
|
//CFT-89
|
|||
|
this.GenerateReport = function (LCAID) {
|
|||
|
var deferred = $q.defer();
|
|||
|
$q.all([service.getLCADetailAsync(LCAID)]).then(function (result) {
|
|||
|
var lca = result[0];
|
|||
|
if (isProductLCA(lca.LCAType)) {//產品型
|
|||
|
$http.post(baseUrl+'/api/LCA/ProductGenerateReport/' + LCAID).success(function () {
|
|||
|
deferred.resolve(1);
|
|||
|
})
|
|||
|
}
|
|||
|
else { //組織型
|
|||
|
$http.post(baseUrl+'/api/LCA/OrganizationGenerateReport/' + LCAID).success(function (data) {
|
|||
|
deferred.resolve(data);
|
|||
|
})
|
|||
|
}
|
|||
|
})
|
|||
|
return deferred.promise;
|
|||
|
};
|
|||
|
|
|||
|
//CFT-318 將盤查表拆出
|
|||
|
this.GenerateLCAdataReport = function (LCAID) {
|
|||
|
var deferred = $q.defer();
|
|||
|
$q.all([service.getLCADetailAsync(LCAID)]).then(function (result) {
|
|||
|
var lca = result[0];
|
|||
|
if (isProductLCA(lca.LCAType)) {//產品型
|
|||
|
$http.post(baseUrl+'/api/LCA/ProductGenerateLCAdataReport/' + LCAID).success(function (data) {
|
|||
|
//console.log('data', data);
|
|||
|
deferred.resolve(data);
|
|||
|
})
|
|||
|
.error(function (data, status) {
|
|||
|
console.log('error');
|
|||
|
});
|
|||
|
}
|
|||
|
else { //組織型
|
|||
|
$http.post(baseUrl+'/api/LCA/OrganizationGenerateLCAdataReport/' + LCAID).success(function (data) {
|
|||
|
deferred.resolve(data);
|
|||
|
})
|
|||
|
}
|
|||
|
})
|
|||
|
return deferred.promise;
|
|||
|
};
|
|||
|
// 將清冊拆出
|
|||
|
this.GenerateLCAlistReport = function (LCAID) {
|
|||
|
var deferred = $q.defer();
|
|||
|
$q.all([service.getLCADetailAsync(LCAID)]).then(function (result) {
|
|||
|
var lca = result[0];
|
|||
|
if (isProductLCA(lca.LCAType)) {//產品型
|
|||
|
$http.post(baseUrl+'/api/LCA/ProductGenerateLCAlistReport/' + LCAID).success(function (data) {
|
|||
|
//console.log('data', data);
|
|||
|
deferred.resolve(data);
|
|||
|
})
|
|||
|
.error(function (data, status) {
|
|||
|
if (typeof (data.ExceptionMessage) != 'undefined') {
|
|||
|
var err = data.ExceptionMessage;
|
|||
|
if (err != null && err.length > 0)
|
|||
|
alert(err);
|
|||
|
}
|
|||
|
//console.log('error',data);
|
|||
|
});
|
|||
|
}
|
|||
|
else { //組織型
|
|||
|
$http.post(baseUrl+'/api/LCA/OrganizationGenerateLCAlistReport/' + LCAID).success(function (data) {
|
|||
|
deferred.resolve(data);
|
|||
|
})
|
|||
|
.error(function (data, status) {
|
|||
|
console.log('error');
|
|||
|
});
|
|||
|
}
|
|||
|
})
|
|||
|
return deferred.promise;
|
|||
|
};
|
|||
|
// 將報告書拆出
|
|||
|
this.GenerateLCAreport = function (LCAID) {
|
|||
|
var deferred = $q.defer();
|
|||
|
$q.all([service.getLCADetailAsync(LCAID)]).then(function (result) {
|
|||
|
var lca = result[0];
|
|||
|
if (isProductLCA(lca.LCAType)) {//產品型
|
|||
|
$http.post(baseUrl+'/api/LCA/ProductGenerateLCAReport/' + LCAID).success(function (data) {
|
|||
|
deferred.resolve(data);
|
|||
|
})
|
|||
|
}
|
|||
|
else { //組織型
|
|||
|
$http.post(baseUrl+'/api/LCA/OrganizationGenerateLCAreport/' + LCAID).success(function (data) {
|
|||
|
deferred.resolve(data);
|
|||
|
})
|
|||
|
}
|
|||
|
})
|
|||
|
return deferred.promise;
|
|||
|
};
|
|||
|
//風險評估報表
|
|||
|
this.GenerateRiskAssessmentReport = function (LCAID) {
|
|||
|
var deferred = $q.defer();
|
|||
|
$q.all([service.getLCADetailAsync(LCAID)]).then(function (result) {
|
|||
|
var lca = result[0];
|
|||
|
if (!isProductLCA(lca.LCAType)) { //組織型
|
|||
|
$http.post(baseUrl+'/api/LCA/GenerateRiskAssessmentReport/' + LCAID).success(function (data) {
|
|||
|
deferred.resolve(data);
|
|||
|
})
|
|||
|
.error(function (data, status) {
|
|||
|
alert('error');
|
|||
|
});
|
|||
|
}
|
|||
|
})
|
|||
|
return deferred.promise;
|
|||
|
};
|
|||
|
//敏感性分析表
|
|||
|
this.GenerateSensitivityAnalysis = function (LCAID) {
|
|||
|
var deferred = $q.defer();
|
|||
|
$q.all([service.getLCADetailAsync(LCAID)]).then(function (result) {
|
|||
|
var lca = result[0];
|
|||
|
if (isProductLCA(lca.LCAType)) { //產品型
|
|||
|
$http.post(baseUrl+'/api/LCA/GenerateSensitivityAnalysis/' + LCAID).success(function (data) {
|
|||
|
deferred.resolve(data);
|
|||
|
})
|
|||
|
.error(function (data, status) {
|
|||
|
alert('error');
|
|||
|
});
|
|||
|
}
|
|||
|
})
|
|||
|
return deferred.promise;
|
|||
|
};
|
|||
|
// 合併清冊
|
|||
|
this.GenerateMergedLCAlistReport = function (LCAID) {
|
|||
|
var deferred = $q.defer();
|
|||
|
$q.all([service.getLCADetailAsync(LCAID)]).then(function (result) {
|
|||
|
var lca = result[0];
|
|||
|
if (isProductLCA(lca.LCAType)) {//產品型
|
|||
|
$http.post(baseUrl+'/api/LCA/ProductGenerateReport/' + LCAID).success(function () {
|
|||
|
deferred.resolve(1);
|
|||
|
})
|
|||
|
}
|
|||
|
else { //組織型
|
|||
|
$http.post(baseUrl+'/api/LCA/OrganizationGenerateMergedLCAlistReport/' + LCAID).success(function (data) {
|
|||
|
deferred.resolve(data);
|
|||
|
})
|
|||
|
.error(function (data, status) {
|
|||
|
console.log('error');
|
|||
|
});
|
|||
|
}
|
|||
|
})
|
|||
|
return deferred.promise;
|
|||
|
};
|
|||
|
// 合併報告書
|
|||
|
this.GenerateMergedLCAreport = function (LCAID) {
|
|||
|
var deferred = $q.defer();
|
|||
|
$q.all([service.getLCADetailAsync(LCAID)]).then(function (result) {
|
|||
|
var lca = result[0];
|
|||
|
if (isProductLCA(lca.LCAType)) {//產品型
|
|||
|
$http.post(baseUrl+'/api/LCA/ProductGenerateReport/' + LCAID).success(function () {
|
|||
|
deferred.resolve(1);
|
|||
|
})
|
|||
|
}
|
|||
|
else { //組織型
|
|||
|
$http.post(baseUrl+'/api/LCA/OrganizationGenerateMergedLCAreport/' + LCAID).success(function (data) {
|
|||
|
deferred.resolve(data);
|
|||
|
})
|
|||
|
}
|
|||
|
})
|
|||
|
return deferred.promise;
|
|||
|
};
|
|||
|
this.downloadZipReplyReport = function (LCAID) {
|
|||
|
var deferred = $q.defer();
|
|||
|
$q.all([service.getLCADetailAsync(LCAID)]).then(function (result) {
|
|||
|
var lca = result[0];
|
|||
|
if (isProductLCA(lca.LCAType)) {//產品型
|
|||
|
$http.post(baseUrl+'/api/DownloadZipReplyReport/' + LCAID).success(function (data) {
|
|||
|
deferred.resolve(data);
|
|||
|
})
|
|||
|
}
|
|||
|
else { //組織型
|
|||
|
$http.post(baseUrl+'/api/DownloadZipReplyReport/' + LCAID).success(function (data) {
|
|||
|
deferred.resolve(data);
|
|||
|
})
|
|||
|
}
|
|||
|
})
|
|||
|
return deferred.promise;
|
|||
|
};
|
|||
|
this.downloadZipReport = function (LCAID) {
|
|||
|
var deferred = $q.defer();
|
|||
|
$q.all([service.getLCADetailAsync(LCAID)]).then(function (result) {
|
|||
|
var lca = result[0];
|
|||
|
if (isProductLCA(lca.LCAType)) {//產品型
|
|||
|
$http.post(baseUrl + '/api/DownloadZipReport/' + LCAID).success(function (data) {
|
|||
|
//console.log('downloadZipReport 1', data);
|
|||
|
deferred.resolve(data);
|
|||
|
})
|
|||
|
}
|
|||
|
else { //組織型
|
|||
|
$http.post(baseUrl + '/api/DownloadZipReport/' + LCAID).success(function (data) {
|
|||
|
//console.log('downloadZipReport 2', data);
|
|||
|
deferred.resolve(data);
|
|||
|
})
|
|||
|
}
|
|||
|
})
|
|||
|
return deferred.promise;
|
|||
|
};
|
|||
|
|
|||
|
this.getPCRAsync = function (LCAID) {
|
|||
|
var pcrCache = getAndCreateCachIfNotExist('PcrDetail');
|
|||
|
var deferred = $q.defer();
|
|||
|
service.getLCADetailAsync(LCAID).then(function (lca) {
|
|||
|
if (lca.PCRID == null) deferred.resolve(null);
|
|||
|
else {
|
|||
|
var result = pcrCache.get(lca.PCRID);
|
|||
|
if (result) {
|
|||
|
deferred.resolve(result);
|
|||
|
}
|
|||
|
else {
|
|||
|
$http.get(baseUrl+'/api/PCR/Get/' + lca.PCRID)
|
|||
|
.success(function (data) {
|
|||
|
pcrCache.put(data.ID, data)
|
|||
|
deferred.resolve(data);
|
|||
|
}).error(function () {
|
|||
|
deferred.resolve(null);
|
|||
|
});
|
|||
|
}
|
|||
|
}
|
|||
|
})
|
|||
|
return deferred.promise;
|
|||
|
|
|||
|
};
|
|||
|
this.getCommentsAsync = function (LCAID) {
|
|||
|
var CommentsCache = getAndCreateCachIfNotExist('Comments');
|
|||
|
var deferred = $q.defer();
|
|||
|
var result = CommentsCache.get(LCAID);
|
|||
|
if (result) {
|
|||
|
deferred.resolve(result);
|
|||
|
}
|
|||
|
else {
|
|||
|
$http.get(baseUrl+'/api/LCA/GetComments/' + LCAID)
|
|||
|
.success(function (data) {
|
|||
|
CommentsCache.put(LCAID, data)
|
|||
|
deferred.resolve(data);
|
|||
|
}).error(function () {
|
|||
|
deferred.resolve(null);
|
|||
|
});
|
|||
|
}
|
|||
|
return deferred.promise;
|
|||
|
};
|
|||
|
this.clearAllMaterialFromCache = function (ID) {
|
|||
|
var cache = getAndCreateCachIfNotExist('MaterialsCache');
|
|||
|
cache.remove(ID);
|
|||
|
};
|
|||
|
this.getAllMaterialAsync = function (LCAID) {
|
|||
|
var materialsCache = getAndCreateCachIfNotExist('MaterialsCache');
|
|||
|
var deferred = $q.defer();
|
|||
|
var result = materialsCache.get(LCAID);
|
|||
|
if (result) deferred.resolve(result)
|
|||
|
else {
|
|||
|
$http.get(baseUrl+'/api/Material/GetAllMaterials/' + LCAID).success(function (data) {
|
|||
|
var flattened = [];
|
|||
|
angular.forEach(data.materials, function (entry) {
|
|||
|
flattened.push(entry);
|
|||
|
angular.forEach(entry.ChildMaterials, function (child) {
|
|||
|
flattened.push(child);
|
|||
|
})
|
|||
|
})
|
|||
|
JoinSurveyItemAndParameter.joinAsync(flattened).then(function () {
|
|||
|
angular.forEach(flattened, function (entry) {
|
|||
|
if (entry.parameterResult) {
|
|||
|
var splited = entry.parameterResult.split(",");
|
|||
|
entry.localDisplayEncoding = splited[0];
|
|||
|
entry.localDisplayParameterValue = splited[splited.length - 1];
|
|||
|
}
|
|||
|
});
|
|||
|
materialsCache.put(LCAID, data.materials)
|
|||
|
deferred.resolve(data.materials);
|
|||
|
});
|
|||
|
}).error(function () {
|
|||
|
deferred.resolve(null);
|
|||
|
})
|
|||
|
}
|
|||
|
return deferred.promise;
|
|||
|
};
|
|||
|
this.updateMaterialsAsync = function (materials) {
|
|||
|
/*
|
|||
|
* Pay attention:
|
|||
|
* A landmine here! Api will return 500 status if some material's child material is another material that we save in the same time.
|
|||
|
* So we need reset all material's child material to avoid this exception.
|
|||
|
*
|
|||
|
*/
|
|||
|
|
|||
|
// start of set child material (buried mines)
|
|||
|
var cloneChildMaterials = [];
|
|||
|
angular.forEach(materials, function (material) {
|
|||
|
cloneChildMaterials.push(material.ChildMaterials);
|
|||
|
material.ChildMaterials = null;
|
|||
|
});
|
|||
|
// end
|
|||
|
|
|||
|
var deferred = $q.defer();
|
|||
|
$http.post(baseUrl+'/api/Material/SaveMaterials/', materials)
|
|||
|
.then(function (a) {
|
|||
|
JoinSurveyItemAndParameter.joinAsync(a.data.result).then(function () {
|
|||
|
angular.forEach(a.data.result, function (entry) {
|
|||
|
if (entry.parameterResult) {
|
|||
|
var splited = entry.parameterResult.split(",");
|
|||
|
entry.localDisplayEncoding = splited[0];
|
|||
|
entry.localDisplayParameterValue = splited[splited.length - 1];
|
|||
|
}
|
|||
|
});
|
|||
|
deferred.resolve(a.data.result);
|
|||
|
});
|
|||
|
});
|
|||
|
|
|||
|
return deferred.promise;
|
|||
|
};
|
|||
|
this.getCache = function (name) {
|
|||
|
return $cacheFactory.get(name)
|
|||
|
}
|
|||
|
function getAndCreateCachIfNotExist(name) {
|
|||
|
var cache = $cacheFactory.get(name);
|
|||
|
if (!cache) {
|
|||
|
cache = $cacheFactory(name);
|
|||
|
}
|
|||
|
return cache;
|
|||
|
}
|
|||
|
})
|
|||
|
.service('ProductDetailCacheService', function ($q, $http, $cacheFactory) {//this code is repeating like lca
|
|||
|
var cache = $cacheFactory.get('ProductDetail');
|
|||
|
|
|||
|
var arr = window.location.href.split("/");
|
|||
|
if (arr[3].indexOf('app') == 0)
|
|||
|
baseUrl = '';
|
|||
|
else
|
|||
|
baseUrl = '/' + arr[3];
|
|||
|
|
|||
|
if (!cache) {
|
|||
|
cache = $cacheFactory('ProductDetail');
|
|||
|
}
|
|||
|
|
|||
|
this.cacheProducts = function (obj) {
|
|||
|
obj.forEach(function (entry) {
|
|||
|
cache.put(entry.ID, entry);
|
|||
|
});
|
|||
|
};
|
|||
|
|
|||
|
this.getProductDetailAsync = function (ID) {
|
|||
|
var result = cache.get(ID);
|
|||
|
var deferred = $q.defer();
|
|||
|
|
|||
|
if (!result) {
|
|||
|
$http.get(baseUrl+'/api/Product/Get/' + ID)
|
|||
|
.success(function (data) {
|
|||
|
if (data != null) {
|
|||
|
cache.put(data.ID, data);
|
|||
|
}
|
|||
|
deferred.resolve(data);
|
|||
|
});
|
|||
|
} else {
|
|||
|
deferred.resolve(result);
|
|||
|
}
|
|||
|
|
|||
|
return deferred.promise;
|
|||
|
};
|
|||
|
})
|
|||
|
|
|||
|
.service('ParameterCacheService', function ($q, $http, $cacheFactory) {
|
|||
|
var cache = $cacheFactory.get('Parameters');
|
|||
|
|
|||
|
var arr = window.location.href.split("/");
|
|||
|
if (arr[3].indexOf('app') == 0)
|
|||
|
baseUrl = '';
|
|||
|
else
|
|||
|
baseUrl = '/' + arr[3];
|
|||
|
|
|||
|
if (!cache) {
|
|||
|
cache = $cacheFactory('Parameters');
|
|||
|
}
|
|||
|
|
|||
|
this.cacheParameters = function (obj) {
|
|||
|
obj.forEach(function (entry) {
|
|||
|
cache.put(entry.ID, entry);
|
|||
|
});
|
|||
|
};
|
|||
|
|
|||
|
this.getParametersAsync = function (IDs) {
|
|||
|
var result = [],
|
|||
|
toBeQuery = [],
|
|||
|
deferred = $q.defer();
|
|||
|
|
|||
|
angular.forEach(IDs, function (ID) {
|
|||
|
var cached = cache.get(ID);
|
|||
|
if (cached) {
|
|||
|
result[ID] = cached;
|
|||
|
} else {
|
|||
|
toBeQuery.push(ID);
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
if (toBeQuery.length != 0) {
|
|||
|
$http.post(baseUrl+'/api/Parameter/getParameterValues', toBeQuery)
|
|||
|
.success(function (data) {
|
|||
|
angular.forEach(data, function (value, key) {
|
|||
|
cache.put(key, value);
|
|||
|
result[key] = value;
|
|||
|
});
|
|||
|
deferred.resolve(result);
|
|||
|
});
|
|||
|
} else {
|
|||
|
deferred.resolve(result);
|
|||
|
}
|
|||
|
|
|||
|
return deferred.promise;
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* Remove identical element from the array
|
|||
|
* @parem Array
|
|||
|
* @returns Array
|
|||
|
*/
|
|||
|
this.filterDuplicatedData = function (a) {
|
|||
|
var ret = a.sort().filter(function (item, pos, ary) {
|
|||
|
return !pos || item != ary[pos - 1];
|
|||
|
});
|
|||
|
return ret;
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
this.getPosition = function (string, subString, index) {
|
|||
|
return string.split(subString, index).join(subString).length;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Get substring "area" from a string
|
|||
|
* @parem String
|
|||
|
* @returns String
|
|||
|
*/
|
|||
|
this.getAreaFromParameter = function (value) {
|
|||
|
return value.slice(0, this.getPosition(value, ",", 1));
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* Get substring "fuelType" from a string
|
|||
|
* @parem String
|
|||
|
* @returns String
|
|||
|
*/
|
|||
|
this.getFuelTypeFromParameter = function (value) {
|
|||
|
return value.slice(this.getPosition(value, ",", 1) + 1, this.getPosition(value, ",", 2));
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* Get substring "year" from a string
|
|||
|
* @parem String
|
|||
|
* @returns String
|
|||
|
*/
|
|||
|
this.getYearFromParameter = function (value) {
|
|||
|
if (this.getPosition(value, ",", 3) <= 0) {
|
|||
|
return value.slice(value.lastIndexOf(',') + 1, value.length);
|
|||
|
}
|
|||
|
else {
|
|||
|
return value.slice(this.getPosition(value, ",", 2) + 1, this.getPosition(value, ",", 3));
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* Get substring "heatValue" from a string
|
|||
|
* @parem String
|
|||
|
* @returns String
|
|||
|
*/
|
|||
|
this.getHeatValueFromParameter = function (value) {
|
|||
|
if (this.getPosition(value, ",", 3) <= 0) {
|
|||
|
return "";
|
|||
|
}
|
|||
|
else {
|
|||
|
return value.slice(this.getPosition(value, ",", 3) + 1, value.length);
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
*
|
|||
|
* @param {any} parameters Dictionary of parameter ID to parameter values
|
|||
|
*/
|
|||
|
this.getAreasFromParameters = function (parameters) {
|
|||
|
var areas = [];
|
|||
|
// Extract area from array "options"
|
|||
|
angular.forEach(parameters, function (parameterValue) {
|
|||
|
var area = this.getAreaFromParameter(parameterValue);
|
|||
|
//console.log(area);
|
|||
|
areas.push(area);
|
|||
|
}, this);
|
|||
|
return this.filterDuplicatedData(areas);
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* Handle area dropdown changed event
|
|||
|
* @param {any} parameters Dictionary of parameter ID to parameter values
|
|||
|
* @param {any} dropdownOptions Object that contains the options for area/fueltype/year dropdowns, usually the $scope of the controller
|
|||
|
* @param {any} toBeEdit Object that contains the selected area/fueltype/year and ParameterID property
|
|||
|
*/
|
|||
|
this.onAreaChanged = function (parameters, dropdownOptions, toBeEdit) {
|
|||
|
var selectedArea = toBeEdit.Area;
|
|||
|
var selectedFuelType = toBeEdit.FuelType;
|
|||
|
|
|||
|
var fuelTypes = [];
|
|||
|
angular.forEach(parameters, function (parameterValue) {
|
|||
|
if (selectedArea && parameterValue.search(selectedArea) !== -1) {
|
|||
|
var fuelType = this.getFuelTypeFromParameter(parameterValue);
|
|||
|
fuelTypes.push(fuelType);
|
|||
|
}
|
|||
|
}, this);
|
|||
|
dropdownOptions.fuelTypes = this.filterDuplicatedData(fuelTypes);
|
|||
|
|
|||
|
if (dropdownOptions.fuelTypes.indexOf(selectedFuelType) !== -1) {
|
|||
|
//console.log('onAreaChanged: selectedFuelType=' + selectedFuelType + ' already exists in fueltypes dropdown, trigger onFuelTypeChanged');
|
|||
|
this.onFuelTypeChanged(parameters, dropdownOptions, toBeEdit);
|
|||
|
}
|
|||
|
else {
|
|||
|
//console.log('onAreaChanged: selectedFuelType=' + selectedFuelType + ' does not exist in fueltypes dropdown, reset years dropdown and selected fueltype/year/parameterID');
|
|||
|
dropdownOptions.years = [];
|
|||
|
toBeEdit.FuelType = '';
|
|||
|
toBeEdit.Year = '';
|
|||
|
toBeEdit.ParameterID = null;
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* See onAreaChanged()
|
|||
|
* @param {any} parameters
|
|||
|
* @param {any} dropdownOptions
|
|||
|
* @param {any} toBeEdit
|
|||
|
*/
|
|||
|
this.onFuelTypeChanged = function (parameters, dropdownOptions, toBeEdit) {
|
|||
|
var selectedArea = toBeEdit.Area;
|
|||
|
var selectedFuelType = toBeEdit.FuelType;
|
|||
|
var selectedYear = toBeEdit.Year;
|
|||
|
|
|||
|
var years = [];
|
|||
|
angular.forEach(parameters, function (parameterValue) {
|
|||
|
var parameterArr = parameterValue.split(',');
|
|||
|
if (selectedArea && selectedFuelType &&
|
|||
|
parameterArr.length >= 2 &&
|
|||
|
parameterArr[0] == selectedArea &&
|
|||
|
parameterArr[1] == selectedFuelType) {
|
|||
|
var year = this.getYearFromParameter(parameterValue);
|
|||
|
years.push(year);
|
|||
|
toBeEdit.OriHeatValue = this.getHeatValueFromParameter(parameterValue);
|
|||
|
if (toBeEdit.heatValue == null || toBeEdit.heatValue == "") {
|
|||
|
toBeEdit.heatValue = toBeEdit.OriHeatValue;
|
|||
|
}
|
|||
|
}
|
|||
|
}, this);
|
|||
|
dropdownOptions.years = this.filterDuplicatedData(years);
|
|||
|
|
|||
|
if (dropdownOptions.years.indexOf(selectedYear) !== -1) {
|
|||
|
//console.log('onFuelTypeChanged: selectedYear=' + selectedYear + ' already exists in years dropdown, trigger onYearChanged');
|
|||
|
this.onYearChanged(parameters, dropdownOptions, toBeEdit);
|
|||
|
}
|
|||
|
else {
|
|||
|
//console.log('onFuelTypeChanged: selectedYear=' + selectedYear + ' does not exist in years dropdown, reset selected year/parameterID');
|
|||
|
toBeEdit.Year = '';
|
|||
|
toBeEdit.ParameterID = null;
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* See onAreaChanged()
|
|||
|
* @param {any} parameters
|
|||
|
* @param {any} dropdownOptions
|
|||
|
* @param {any} toBeEdit
|
|||
|
*/
|
|||
|
this.onYearChanged = function (parameters, dropdownOptions, toBeEdit) {
|
|||
|
var selectedArea = toBeEdit.Area;
|
|||
|
var selectedFuelType = toBeEdit.FuelType;
|
|||
|
var selectedYear = toBeEdit.Year;
|
|||
|
//console.log('onYearChanged: selectedArea=' + selectedArea + ', selectedFuelType=' + selectedFuelType + ', selectedYear=' + selectedYear);
|
|||
|
angular.forEach(parameters, function (parameterValue, key) {
|
|||
|
var parameterValueArr = parameterValue.split(',');
|
|||
|
if (selectedArea && selectedFuelType && selectedYear &&
|
|||
|
parameterValueArr[0] == selectedArea &&
|
|||
|
parameterValueArr[1] == selectedFuelType &&
|
|||
|
parameterValueArr[2] == selectedYear) {
|
|||
|
toBeEdit.ParameterID = key;
|
|||
|
//console.log('onYearChanged: toBeEdit.parameterID=' + key);
|
|||
|
}
|
|||
|
}, this);
|
|||
|
};
|
|||
|
})
|
|||
|
|
|||
|
.service('JoinSurveyItemAndParameter', function ($q, ParameterCacheService) {
|
|||
|
|
|||
|
var arr = window.location.href.split("/");
|
|||
|
if (arr[3].indexOf('app') == 0)
|
|||
|
baseUrl = '';
|
|||
|
else
|
|||
|
baseUrl = '/' + arr[3];
|
|||
|
|
|||
|
this.joinAsync = function (items) {
|
|||
|
var requireParameterValue = [];
|
|||
|
var deferred = $q.defer();
|
|||
|
|
|||
|
angular.forEach(items, function (entry) {
|
|||
|
if (entry.ParameterID != null &&
|
|||
|
requireParameterValue.indexOf(entry.ParameterID) == -1) {
|
|||
|
requireParameterValue.push(entry.ParameterID);
|
|||
|
}
|
|||
|
|
|||
|
if (entry.ParameterID2 != null &&
|
|||
|
requireParameterValue.indexOf(entry.ParameterID2) == -1) {
|
|||
|
requireParameterValue.push(entry.ParameterID2);
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
ParameterCacheService.getParametersAsync(requireParameterValue).then(function (result) {
|
|||
|
angular.forEach(items, function (entry) {
|
|||
|
if (entry.ParameterID != null) {
|
|||
|
entry.parameterResult = result[entry.ParameterID];
|
|||
|
}
|
|||
|
|
|||
|
if (entry.ParameterID2 != null) {
|
|||
|
entry.parameterResultTwo = result[entry.ParameterID2];
|
|||
|
}
|
|||
|
});
|
|||
|
deferred.resolve();
|
|||
|
});
|
|||
|
|
|||
|
return deferred.promise;
|
|||
|
}
|
|||
|
|
|||
|
})
|
|||
|
|
|||
|
.service('UXLoadingService', function () {
|
|||
|
var loadingTimes = 0;
|
|||
|
|
|||
|
var arr = window.location.href.split("/");
|
|||
|
if (arr[3].indexOf('app') == 0)
|
|||
|
baseUrl = '';
|
|||
|
else
|
|||
|
baseUrl = '/' + arr[3];
|
|||
|
|
|||
|
this.loadingStart = function () {
|
|||
|
if (loadingTimes == 0)
|
|||
|
loadingStart();
|
|||
|
loadingTimes += 1;
|
|||
|
}
|
|||
|
|
|||
|
this.loadingEnd = function () {
|
|||
|
loadingTimes -= 1;
|
|||
|
if (loadingTimes == 0)
|
|||
|
loadingDisable();
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
function loadingStart() {
|
|||
|
angular.element('#loindCirclePNG').show();
|
|||
|
}
|
|||
|
function loadingDisable() {
|
|||
|
angular.element('#loindCirclePNG').hide();
|
|||
|
}
|
|||
|
})
|
|||
|
|
|||
|
.factory('HttpInterceptor', ['$q', 'UXLoadingService', function ($q, UXLoadingService) {
|
|||
|
return {
|
|||
|
'request': function (config) {
|
|||
|
// show loader
|
|||
|
UXLoadingService.loadingStart();
|
|||
|
return config;
|
|||
|
},
|
|||
|
|
|||
|
'requestError': function (rejection) {
|
|||
|
// show loader
|
|||
|
UXLoadingService.loadingStart();
|
|||
|
return $q.reject(rejection);
|
|||
|
},
|
|||
|
|
|||
|
'response': function (response) {
|
|||
|
// hide loader
|
|||
|
UXLoadingService.loadingEnd();
|
|||
|
return response;
|
|||
|
},
|
|||
|
|
|||
|
'responseError': function (rejection) {
|
|||
|
// hide loader
|
|||
|
UXLoadingService.loadingEnd();
|
|||
|
return $q.reject(rejection);
|
|||
|
}
|
|||
|
};
|
|||
|
}])
|
|||
|
.config(['$httpProvider', function ($httpProvider) {
|
|||
|
$httpProvider.interceptors.push('HttpInterceptor');
|
|||
|
}])
|
|||
|
;
|