(function(){ angular .module("app.utils") .service("appUtils.datePickerService", ["$filter", function($filter){ /** * Major method to initialize date picker into special html-inputs and set appropriate * behaviour into them. Input have to be like this: * * @param $scope {Object} */ this.init = function($scope){ $scope.today = function () { $scope.dt = new Date(); }; $scope.today(); $scope.clear = function () { $scope.dt = null; }; // Disable weekend selection $scope.disabled = function (date, mode) { return (mode === 'day' && (date.getDay() === 0 || date.getDay() === 6)); }; $scope.toggleMin = function () { $scope.minDate = $scope.minDate ? null : new Date(); }; $scope.toggleMin(); $scope.open = function ($event) { $event.preventDefault(); $event.stopPropagation(); $scope.opened = !$scope.opened; }; $scope.endOpen = function ($event) { $event.preventDefault(); $event.stopPropagation(); $scope.startOpened = false; $scope.endOpened = !$scope.endOpened; }; $scope.startOpen = function ($event) { $event.preventDefault(); $event.stopPropagation(); $scope.endOpened = false; $scope.startOpened = !$scope.startOpened; }; $scope.dateOptions = { formatYear: 'yy', startingDay: 1 }; $scope.formats = ['MM/dd/yyyy', 'dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate']; $scope.format = $scope.formats[0]; $scope.hstep = 1; $scope.mstep = 15; // Time Picker $scope.options = { hstep: [1, 2, 3], mstep: [1, 5, 10, 15, 25, 30] }; $scope.ismeridian = true; $scope.toggleMode = function () { $scope.ismeridian = !$scope.ismeridian; }; $scope.update = function () { var d = new Date(); d.setHours(14); d.setMinutes(0); $scope.dt = d; }; $scope.changed = function () { $log.log('Time changed to: ' + $scope.dt); }; $scope.clear = function () { $scope.dt = null; }; }; this.convertStringToDate = convertStringToDate; this.generateDateFirstDay = generateDateFirstDay; this.generateDateLastDay = generateDateLastDay; this.generateMinimalDay = generateMinimalDay; this.generateTodaysDay = generateTodaysDay; this.generateWeekAgo = generateWeekAgo; this.getInitialData = getInitialData; this.resetToDefault = resetToDefault; this.renderJQueryDatePicker = renderJQueryDatePicker; function convertStringToDate(string, mask){ return $filter('date')(new Date(Date.parse(string)), mask) } function generateDateFirstDay(){ var today = new Date(), firstDayUTC = new Date(today.getFullYear(), today.getMonth(), 1); return $filter('date')(Date.parse(firstDayUTC), 'yyyy-MM-dd'); } function generateDateLastDay(){ var today = new Date(), lastDayUTC = new Date(today.getFullYear(), today.getMonth() + 1, 0); return $filter('date')(Date.parse(lastDayUTC), 'yyyy-MM-dd'); } function generateTodaysDay(){ var today = new Date(); return $filter('date')(Date.parse(today), 'yyyy-MM-dd'); } function generateMinimalDay(string){ return string; // using format -> '2014-01-01' } function generateWeekAgo(){ return $filter("date")(new Date(new Date().setDate(new Date().getDate()-6)), "yyyy-MM-dd"); } function generateThreeDaysFuture(number){ var someDate = new Date(); someDate.setDate(someDate.getDate() + number); return $filter("date")(someDate, 'yyyy-MM-dd'); } function getInitialData(){ return { From: generateWeekAgo(), Minimal: generateMinimalDay(), To: generateTodaysDay(), Today: generateTodaysDay(), WeekAgo: generateWeekAgo(), DaysInFuture: generateThreeDaysFuture(365) } } function resetToDefault(dateValues, param){ dateValues.forEach(function(d){ d = param; }); } function renderJQueryDatePicker(htmlId, val, $scope, dateType, dateTypeId){ var selected = $scope[val] || val; $( htmlId ).datepicker({ defaultDate: new Date(), changeMonth: true, numberOfMonths: 1, onClose: function( selectedDate ) { //$( "#to" ).datepicker( "option", "minDate", selectedDate ); if (dateType && dateTypeId) { $(dateTypeId).datepicker("option", dateType, selectedDate); } selected = $( htmlId ).datepicker( "getDate" ); } }) } }]); })();