MetronicApp.controller('AppController', ['$scope', 'appConfig', '$http', '$rootScope', function ($scope, appConfig, $http, $rootScope) {
$scope.appConfig = appConfig;
$scope.dateLocale = 'th';
$scope.datePickerFormat = 'DD/MM/YYYY';
$scope.dateOptions = {
changeYear: true,
changeMonth: true,
yearRange: '1980:2050',
};
$scope.regEx = {
decimal: '^[0-9](\.[0-9]+)?$'
};
$scope.cacheConfig = {
minInitTick: 1521403381466, // 2018-03-19 03:xxAM
timeToLive: 14400000 // 4 hours
};
$scope.searchCacheTTL = 14400000; // 4 hours
$scope.$on('$viewContentLoaded', function () {
Metronic.initComponents(); // init core components
//Layout.init(); // Init entire layout(header, footer, sidebar, etc) on page load if the partials included in server side instead of loading with ng-include directive
});
$scope.isNothing = function (value) {
if (angular.isUndefined(value)
|| value == null
|| value.toString().trim().length == 0) {
return true;
}
return false;
}
$scope.isNullOrWhitespace = function (input) {
if (angular.isUndefined(input)
|| input == null
|| input.toString().trim().length == 0) {
return true;
}
return false;
}
$scope.formatErrorMessage = function (title, error) {
var msg = title.toString();
if ($scope.isNothing(error.Message) == false) {
msg += '' + error.Message + '';
}
if ($scope.isNothing(error.MessageItems) == false && error.MessageItems.length > 0) {
msg += '
';
for (var i = 0; i < error.MessageItems.length; i++) {
msg += '- ' + error.MessageItems[i] + '
';
}
msg += '
';
}
return msg;
}
$scope.hasError = function (fld, submitted) {
if (angular.isDefined(fld) && fld.$invalid && (submitted == true || fld.$dirty)) { return true; }
return false;
}
$scope.getError = function (fld) {
if (angular.isDefined(fld) && angular.isDefined(fld.$error)) {
var error = fld.$error;
if (error.required) { return "กรุณาใส่ข้อมูลนี้"; }
else if (error.email) { return "รูปแบบของอีเมล์ไม่ถูกต้อง"; }
else if (error.date) { return "รูปแบบของวันที่ไม่ถูกต้อง"; }
else if (error.orgFieldRequired) { return "สำหรับลูกค้าองค์กร กรุณาใส่ข้อมูลนี้"; }
else if (error.indFieldRequired) { return "สำหรับลูกค้าบุคคล กรุณาใส่ข้อมูลนี้"; }
else if (error.phoneRequired) { return "กรุณาใส่อย่างน้อยหนึ่งเบอร์ติดต่อ"; }
else { return "ข้อมูลนี้ไม่ถูกต้อง"; }
}
}
$scope.filteredList = function (list, text, pname) {
if (text == null || text.length == 0) { return list; }
var results = [];
var search_text = $.trim(text.toLowerCase());
for (var i = 0; i < list.length; i++) {
var item = list[i];
var value = (angular.isUndefined(pname) || pname == null) ? item : item[pname];
if (value.toLowerCase().indexOf(search_text) > -1)
{ results.push(item); }
}
return results;
}
$scope.formatDate = function (dt) {
if (dt == null) { return null; }
moment.locale($scope.dateLocale);
return moment(dt).format('D MMM YYYY');
}
$scope.formatShortDate = function (dt) {
if (dt == null) { return null; }
moment.locale($scope.dateLocale);
return moment(dt).format('D MMM YY');
}
$scope.formatFullDate = function (dt) {
if (dt == null) { return null; }
moment.locale($scope.dateLocale);
return moment(dt).format('D MMMM YYYY');
}
$scope.getDateFullDayName = function (dt) {
moment.locale($scope.dateLocale);
return moment(dt).format('dddd');
}
$scope.getDateFullMonthName = function (dt) {
moment.locale($scope.dateLocale);
return moment(dt).format('MMMM');
}
$scope.formatDateTime = function (dt) {
if (dt == null) { return null; }
moment.locale($scope.dateLocale);
var fdt = moment(dt);
var aa = (fdt.hour() < 12) ? 'am' : 'pm';
return fdt.format('D MMM YYYY h:mm') + aa;
}
$scope.event_Notification_OnItemCountChanged = function () {
$http.get($scope.appConfig.apiBaseUrl + "/Api/Notification/CountActive/", { params: {} })
.success(function (result) {
var itemCount = result.ItemCount;
$rootScope.$broadcast('Notification_OnItemCountChanged', {
ItemCount: itemCount
});
});
}
}]);