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

55 lines
1.6 KiB
JavaScript

/*
ui.datepicker
*/
angular.module('ui.datepicker', []);
angular.module('ui.datepicker')
.constant('DATE_CONSTANTS', {
week_title: ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'],
months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
days: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
})
.directive('datepicker', function () {
return {
restrict: 'A',
scope: {
dpType: '@',
ngModel: '=ngModel'
},
require: 'ngModel',
compile: function () {
return {
pre: function (scope, element, attrs, ngModelCtrl) {
var format, dateObj;
format = (!attrs.dpFormat) ? 'd/m/yyyy' : attrs.dpFormat;
if (!attrs.initDate && !attrs.dpFormat) {
dateObj = new Date();
scope[attrs.ngModel] = dateObj.getDate() + '/' + (dateObj.getMonth() + 1) + '/' + dateObj.getFullYear();
} else if (attrs.initDate) {
scope[attrs.ngModel] = attrs.initDate;
}
// Initialize the date-picker
$(element).datepicker({
format: format
}).on('changeDate', function (ev) {
scope.$apply(function () {
ngModelCtrl.$setViewValue(ev);
});
});
scope.$watch('ngModel', function (newValue) {
if (newValue && angular.isString(newValue)) {
var newDate = moment(newValue, format.toUpperCase(), true);
if (newDate.isValid()) {
$(element).datepicker('setDate', newDate.toDate());
}
}
});
}
}
}
}
})