Built motion from commit 76eb00b9e.|1.0.24
[motion.git] / public / bower_components / ng-bs-daterangepicker / ng-bs-daterangepicker.js
1 /**
2  * @license ng-bs-daterangepicker v0.0.5
3  * (c) 2013 Luis Farzati http://github.com/luisfarzati/ng-bs-daterangepicker
4  * License: MIT
5  */
6 (function(angular) {
7
8         'use strict';
9
10         angular
11                 .module('ngBootstrap', [])
12                 .directive('input', ['$compile', '$parse', '$filter', function($compile, $parse, $filter) {
13                         return {
14                                 restrict: 'E',
15                                 require: '?ngModel',
16                                 link: function($scope, $element, $attributes, ngModel) {
17
18                                         if ($attributes.type !== 'daterange' || ngModel === null) {
19                                                 return;
20                                         }
21
22                                         var options = {};
23                                         options.format = $attributes.format || 'YYYY-MM-DD';
24                                         options.separator = $attributes.separator || ' - ';
25                                         options.minDate = $attributes.minDate && moment($attributes.minDate);
26                                         options.maxDate = $attributes.maxDate && moment($attributes.maxDate);
27                                         options.dateLimit = $attributes.limit && moment.duration.apply(this, $attributes.limit.split(' ').map(function(elem, index) {
28                                                 return index === 0 && parseInt(elem, 10) || elem;
29                                         }));
30                                         options.ranges = $attributes.ranges && $parse($attributes.ranges)($scope);
31                                         options.locale = $attributes.locale && $parse($attributes.locale)($scope);
32                                         options.opens = $attributes.opens || $parse($attributes.opens)($scope);
33
34                                         if ($attributes.enabletimepicker) {
35                                                 options.timePicker = true;
36                                                 angular.extend(options, $parse($attributes.enabletimepicker)($scope));
37                                         }
38
39                                         function datify(date) {
40                                                 return moment.isMoment(date) ? date.toDate() : date;
41                                         }
42
43                                         function momentify(date) {
44                                                 return (!moment.isMoment(date)) ? moment(date) : date;
45                                         }
46
47                                         function format(date) {
48                                                 return $filter('date')(datify(date), options.format.replace(/Y/g, 'y').replace(/D/g, 'd')); //date.format(options.format);
49                                         }
50
51                                         function formatted(dates) {
52                                                 return [format(dates.startDate), format(dates.endDate)].join(options.separator);
53                                         }
54
55                                         ngModel.$render = function() {
56                                                 if (!ngModel.$viewValue || !ngModel.$viewValue.startDate) {
57                                                         return;
58                                                 }
59                                                 $element.val(formatted(ngModel.$viewValue));
60                                         };
61
62                                         $scope.$watch(function() {
63                                                 return $attributes.ngModel;
64                                         }, function(modelValue, oldModelValue) {
65
66                                                 if (!ngModel.$modelValue || (!ngModel.$modelValue.startDate)) {
67                                                         ngModel.$setViewValue({
68                                                                 startDate: moment().startOf('day'),
69                                                                 endDate: moment().startOf('day')
70                                                         });
71                                                         return;
72                                                 }
73
74                                                 if (oldModelValue !== modelValue) {
75                                                         return;
76                                                 }
77
78                                                 $element.data('daterangepicker').startDate = momentify(ngModel.$modelValue.startDate);
79                                                 $element.data('daterangepicker').endDate = momentify(ngModel.$modelValue.endDate);
80                                                 $element.data('daterangepicker').updateView();
81                                                 $element.data('daterangepicker').updateCalendars();
82                                                 $element.data('daterangepicker').updateInputText();
83
84                                         });
85
86                                         $element.daterangepicker(options, function(start, end, label) {
87
88                                                 var modelValue = ngModel.$viewValue;
89
90                                                 if (angular.equals(start, modelValue.startDate) && angular.equals(end, modelValue.endDate)) {
91                                                         return;
92                                                 }
93
94                                                 $scope.$apply(function() {
95                                                         ngModel.$setViewValue({
96                                                                 startDate: (moment.isMoment(modelValue.startDate)) ? start : start.toDate(),
97                                                                 endDate: (moment.isMoment(modelValue.endDate)) ? end : end.toDate()
98                                                         });
99                                                         ngModel.$render();
100                                                 });
101
102                                         });
103
104                                 }
105
106                         };
107
108                 }]);
109
110 })(angular);