From e3ee523fbedfcfd1e5d0cba0370b616110cca557 Mon Sep 17 00:00:00 2001 From: Arnost Pleskot Date: Fri, 9 Oct 2015 16:45:55 +0200 Subject: [PATCH] In date range linked both calendars together. --- dist/angular-datepicker.js | 578 +++++++++++++++++++++---------------- 1 file changed, 322 insertions(+), 256 deletions(-) diff --git a/dist/angular-datepicker.js b/dist/angular-datepicker.js index e406ee5..c856cbc 100644 --- a/dist/angular-datepicker.js +++ b/dist/angular-datepicker.js @@ -39,13 +39,15 @@ after: '=?', before: '=?', minDate: '=?', - maxDate: '=?' + maxDate: '=?', + useMonth: '=?', + currentMonth: '=?' }, link: function(scope, element, attrs, ngModel) { var arrowClick = false; - scope.date = new Date(scope.model || new Date()); + scope.date = new Date(scope.useMonth || new Date()); scope.views = datePickerConfig.views.concat(); scope.view = attrs.view || datePickerConfig.view; scope.now = new Date(); @@ -53,6 +55,15 @@ scope.hidePrev = hidePrevMonth(scope.date); scope.hideNext = hideNextMonth(scope.date); + scope.$watch('useMonth', function(newVal) { + if(newVal) { + scope.date = new Date([newVal.year, newVal.month + 1]); + update(); + // Destroy watcher (http://stackoverflow.com/questions/14957614/angularjs-clear-watch) + } + + }); + var step = parseInt(attrs.step || datePickerConfig.step, 10); var partial = !!attrs.partial; @@ -84,8 +95,8 @@ /** @namespace attrs.minView, attrs.maxView */ scope.views = scope.views.slice( - scope.views.indexOf(attrs.maxView || 'year'), - scope.views.indexOf(attrs.minView || 'minutes') + 1 + scope.views.indexOf(attrs.maxView || 'year'), + scope.views.indexOf(attrs.minView || 'minutes') + 1 ); if (scope.views.length === 1 || scope.views.indexOf(scope.view) === -1) { @@ -147,7 +158,7 @@ function update () { var view = scope.view; - if (scope.model && !arrowClick) { + if (scope.model && !arrowClick && !scope.useMonth) { scope.date = new Date(scope.model); arrowClick = false; } @@ -174,6 +185,11 @@ scope.hidePrev = hidePrevMonth(scope.date); scope.hideNext = hideNextMonth(scope.date); + + scope.currentMonth = { + year: scope.date.getFullYear(), + month: scope.date.getMonth(), + }; } function watch () { @@ -447,6 +463,20 @@ scope.start = new Date(scope.start || new Date()); scope.end = new Date(scope.end || new Date()); + scope.useFromMonth = { + year: new Date().getFullYear(), + month: new Date().getMonth() - 1 + }; + + scope.useToMonth = { + year: new Date().getFullYear(), + month: new Date().getMonth() + }; + + scope.currentFromMonth = null; + + scope.currentToMonth = null + attrs.$observe('disabled', function(isDisabled) { scope.disableDatePickers = !!isDisabled; }); @@ -460,6 +490,42 @@ scope.start = new Date(value); } }); + + scope.$watch('currentFromMonth', function(newMonth, previousMonth) { + if(previousMonth && (newMonth.month !== previousMonth.month || newMonth.year !== previousMonth.year)) { + if((newMonth.month == scope.currentToMonth.month && newMonth.year == scope.currentToMonth.year) && (newMonth.month !== scope.to.getMonth() || newMonth.year !== scope.to.getFullYear())) { + if(newMonth.month !== 11) { + scope.useToMonth = { + year: newMonth.year, + month: newMonth.month + 1 + } + } else { + scope.useToMonth = { + year: newMonth.year + 1, + month: 0 + } + } + } + } + }); + + scope.$watch('currentToMonth', function(newMonth, previousMonth) { + if(previousMonth && (newMonth.month !== previousMonth.month || newMonth.year !== previousMonth.year)) { + if ((newMonth.month == scope.currentFromMonth.month && newMonth.year == scope.currentFromMonth.year) && (newMonth.month !== scope.from.getMonth() || newMonth.year !== scope.from.getFullYear())) { + if (newMonth.month !== 0) { + scope.useFromMonth = { + year: newMonth.year, + month: newMonth.month - 1 + }; + } else { + scope.useFromMonth = { + year: newMonth.year - 1, + month: 11 + }; + } + } + } + }) } }; }); @@ -467,23 +533,23 @@ 'use strict'; var PRISTINE_CLASS = 'ng-pristine', - DIRTY_CLASS = 'ng-dirty'; + DIRTY_CLASS = 'ng-dirty'; var Module = angular.module('datePicker'); Module.constant('dateTimeConfig', { template: function(attrs) { return '' + - '
'; + '
'; }, format: 'yyyy-MM-dd HH:mm', views: ['date', 'year', 'month', 'hours', 'minutes'], @@ -645,249 +711,249 @@ angular.module("datePicker").run(["$templateCache", function($templateCache) { $templateCache.put("templates/datepicker.html", - "
\r" + - "\n" + - "
\r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - "
{{date|date:\"MMMM, yyyy\"}}
{{ day|date:\"EEE\" }}
\r" + - "\n" + - " \r" + - "\n" + - "
\r" + - "\n" + - "
\r" + - "\n" + - "
\r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - "
{{years[0].getFullYear()}}-{{years[years.length-1].getFullYear()}}
\r" + - "\n" + - " \r" + - "\n" + - "
\r" + - "\n" + - "
\r" + - "\n" + - "
\r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - "
{{ date|date:\"yyyy\" }}
\r" + - "\n" + - " \r" + - "\n" + - "
\r" + - "\n" + - "
\r" + - "\n" + - "
\r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - "
{{ date|date:\"dd MMMM yyyy\" }}
\r" + - "\n" + - " \r" + - "\n" + - "
\r" + - "\n" + - "
\r" + - "\n" + - "
\r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - "
{{ date|date:\"dd MMMM yyyy\" }}\r" + - "\n" + - "
\r" + - "\n" + - " \r" + - "\n" + - "
\r" + - "\n" + - "
\r" + - "\n" + - "
\r" + - "\n" + "
\r" + + "\n" + + "
\r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + "
{{date|date:\"MMMM, yyyy\"}}
{{ day|date:\"EEE\" }}
\r" + + "\n" + + " \r" + + "\n" + + "
\r" + + "\n" + + "
\r" + + "\n" + + "
\r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + "
{{years[0].getFullYear()}}-{{years[years.length-1].getFullYear()}}
\r" + + "\n" + + " \r" + + "\n" + + "
\r" + + "\n" + + "
\r" + + "\n" + + "
\r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + "
{{ date|date:\"yyyy\" }}
\r" + + "\n" + + " \r" + + "\n" + + "
\r" + + "\n" + + "
\r" + + "\n" + + "
\r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + "
{{ date|date:\"dd MMMM yyyy\" }}
\r" + + "\n" + + " \r" + + "\n" + + "
\r" + + "\n" + + "
\r" + + "\n" + + "
\r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + "
{{ date|date:\"dd MMMM yyyy\" }}\r" + + "\n" + + "
\r" + + "\n" + + " \r" + + "\n" + + "
\r" + + "\n" + + "
\r" + + "\n" + + "
\r" + + "\n" ); $templateCache.put("templates/daterange.html", - "
\r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - " \r" + - "\n" + - "
\r" + - "\n" + - "
\r" + - "\n" + - "
\r" + - "\n" + - "
\r" + - "\n" + - "
\r" + - "\n" + - "
\r" + - "\n" + "
\r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + " \r" + + "\n" + + "
\r" + + "\n" + + "
\r" + + "\n" + + "
\r" + + "\n" + + "
\r" + + "\n" + + "
\r" + + "\n" + + "
\r" + + "\n" ); }]);