-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.html
137 lines (121 loc) · 4.48 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-date-input-directive-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.1/angular.min.js"></script>
</head>
<body ng-app="dateInputExample">
<script>
// Plunker: http://plnkr.co/edit/BBr668?p=preview
//
function createDateParser(regexp, mapping) {
var ISO_DATE_REGEXP = /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/;
return function(iso, date) {
var parts, map;
if (angular.isDate(iso)) {
return iso;
}
if (angular.isString(iso)) {
// When a date is JSON'ified to wraps itself inside of an extra
// set of double quotes. This makes the date parsing code unable
// to match the date string and parse it as a date.
if (iso.charAt(0) == '"' && iso.charAt(iso.length - 1) == '"') {
iso = iso.substring(1, iso.length - 1);
}
if (ISO_DATE_REGEXP.test(iso)) {
return new Date(iso);
}
regexp.lastIndex = 0;
parts = regexp.exec(iso);
if (parts) {
parts.shift();
if (date) {
map = {
yyyy: date.getFullYear(),
MM: date.getMonth() + 1,
dd: date.getDate(),
HH: date.getHours(),
mm: date.getMinutes(),
ss: date.getSeconds(),
sss: date.getMilliseconds() / 1000
};
} else {
map = { yyyy: 1970, MM: 1, dd: 1, HH: 0, mm: 0, ss: 0, sss: 0 };
}
angular.forEach(parts, function(part, index) {
if (index < mapping.length) {
map[mapping[index]] = +part;
}
});
return new Date(map.yyyy, map.MM - 1, map.dd, map.HH, map.mm, map.ss || 0, map.sss * 1000 || 0);
}
}
return NaN;
};
};
angular.module('dateInputExample', [])
.controller('DateController', ['$scope', function($scope) {
$scope.add=function(){
$scope.example = {
value: new Date(2016, 0, 15)};
}
$scope.add();
console.log('app')
}])
.directive('dateMinMaxValidate', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, elem, attr, ctrl) {
var DATE_REGEXP = /^(\d{4})-(\d{2})-(\d{2})$/;
var parseDate = createDateParser(DATE_REGEXP, ['yyyy', 'MM', 'dd']);
var minVal = null;
var maxVal = null;
if (angular.isDefined(attr.minDate)) {
minVal = parseObservedDateValue(attr.minDate);
}
if (angular.isDefined(attr.maxDate)) {
maxVal = parseObservedDateValue(attr.maxDate);
}
scope.$watch(attr.ngModel, function(newValue) {
console.log("Changed to " + newValue);
if (minVal) {
valid = !isValidDate(newValue) || angular.isUndefined(minVal) || parseDate(newValue) >= minVal;
if (!valid) {
//scope.example.value = minVal;
ctrl.$setViewValue(attr.minDate);
ctrl.$render();
}
}
if (maxVal) {
valid = !isValidDate(newValue) || angular.isUndefined(maxVal) || parseDate(newValue) <= maxVal;
if (!valid) {
//scope.example.value = maxVal;
ctrl.$setViewValue(attr.maxDate);
ctrl.$render();
}
}
});
function isValidDate(value) {
// Invalid Date: getTime() returns NaN
return value && !(value.getTime && value.getTime() !== value.getTime());
}
function parseObservedDateValue(val) {
return angular.isDefined(val) && !angular.isDate(val) ? parseDate(val) || undefined : val;
}
}
};
});
</script>
<form name="myForm" ng-controller="DateController as dateCtrl">
<label for="exampleInput">pick a date out of range (2016-01-15 : 2016-02-10) or that does not exist :</label>
<input ng-show="example.value" type="date" id="exampleInput" name="input" ng-model="example.value"
placeholder="yyyy-MM-dd" min-date="2016-01-15" max-date="2016-02-10" required date-min-max-validate />
<div role="alert">
<button ng-show="!example.value" ng-click="add()">add the date</button>
</div>
<tt>value = {{example.value ? example.value : "example.value has been destroyed"}}</tt><br/>
</form>
</body>
</html>