Skip to content

Commit 1e28dab

Browse files
committed
Merge pull request #26 from MartinNuc/jump-on-separator-press
Jump on next segment when pressing separator key
2 parents 799d0dc + 1b2b544 commit 1e28dab

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Check demo page for live example.
7575
<input type="text" datetime="yyyy-MM-dd" ng-model="myDate" min="Jan 1, 1990" max="Dec 31, 2050">
7676
<input type="text" datetime="yyyy-MM-dd" ng-model="myDate" datetime-model="yyyy-MM-ddTHH:mm:ss">
7777
<input type="text" datetime="yyyy-MM-dd" ng-model="myDate" default="Jan 1, 2000">
78+
<input type="text" datetime="dd.MM.yyyy" ng-model="myDate" datetime-separator=",">
7879
```
7980

8081
Parsing errors
@@ -103,6 +104,9 @@ Todos
103104

104105
Changelog
105106
---------
107+
* next
108+
- jump on the next segment on pressing next separator key
109+
- customizable separator key
106110
* 3.0.1 (Apr 9, 2016)
107111
- Fix validator and datetime-model bug. [#27](https://github.com/eight04/angular-datetime/issues/27)
108112
* 3.0.0 (Apr 1, 2016)

example/demo-angular-1.5.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,12 @@ <h3>Other tests</h3>
134134
<pre class="code"><code>{{data.myDateString2 | json}}</code></pre>
135135
</label>
136136
</div>
137+
<h3>Custom separator</h3>
138+
<p>Used to jump on the next segment when pressing separator key. In this example angular-datetime will jump on the next segment when pressing `.` or `,`.</p>
139+
<div class="form-group">
140+
<label>
141+
<input type="text" class="form-control" datetime="dd.MM.yyyy" ng-model="data.myDate" datetime-separator=",">
142+
</label>
143+
</div>
137144
</body>
138145
</html>

example/demo.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,12 @@ <h3>Other tests</h3>
135135
<pre class="code"><code>{{data.myDateString2 | json}}</code></pre>
136136
</label>
137137
</div>
138+
<h3>Custom separator</h3>
139+
<p>Used to jump on the next segment when pressing separator key. In this example angular-datetime will jump on the next segment when pressing <code>.</code> or <code>,</code>.</p>
140+
<div class="form-group">
141+
<label>
142+
<input type="text" class="form-control" datetime="dd.MM.yyyy" ng-model="data.myDate" datetime-separator=",">
143+
</label>
144+
</div>
138145
</body>
139146
</html>

src/directive.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,14 @@ angular.module("datetime").directive("datetime", function(datetime, $log, $docum
192192
node: null,
193193
start: 0,
194194
end: 0
195-
};
196-
195+
},
196+
lastError;
197+
var datetimeSeparator;
198+
199+
if (angular.isDefined(attrs.datetimeSeparator)) {
200+
datetimeSeparator = attrs.datetimeSeparator;
201+
}
202+
197203
if (angular.isDefined(attrs.datetimeUtc)) {
198204
parser.setTimezone("+0000");
199205
if (modelParser) {
@@ -262,9 +268,12 @@ angular.module("datetime").directive("datetime", function(datetime, $log, $docum
262268
return null;
263269
}
264270

271+
ngModel.$setValidity("tooShort", true);
272+
265273
try {
266274
parser.parse(viewValue);
267275
} catch (err) {
276+
lastError = err;
268277
$log.error(err);
269278

270279
ngModel.$setValidity("datetime", false);
@@ -312,6 +321,8 @@ angular.module("datetime").directive("datetime", function(datetime, $log, $docum
312321
return undefined;
313322
}
314323

324+
lastError = null;
325+
315326
ngModel.$setValidity("datetime", true);
316327

317328
if (ngModel.$validate || validMinMax(parser.getDate())) {
@@ -446,7 +457,29 @@ angular.module("datetime").directive("datetime", function(datetime, $log, $docum
446457
break;
447458

448459
case "keypress":
449-
if (isPrintableKey(e)) {
460+
var nextSeparatorKeyCode;
461+
// check for separator only when there is a next node which is static string
462+
if (range.node.next && range.node.next.token.name === "string" && range.node.next.token.type === "static") {
463+
nextSeparatorKeyCode = range.node.next.viewValue.charCodeAt(0);
464+
}
465+
466+
if (e.keyCode === nextSeparatorKeyCode || (datetimeSeparator && e.keyCode == datetimeSeparator.charCodeAt(0))) {
467+
e.preventDefault();
468+
if (!ngModel.$error.datetime) {
469+
selectRange(range, "next");
470+
}
471+
else if (lastError && lastError.code == "NUMBER_TOOSHORT") {
472+
parser.nodeParseValue(lastError.node, lastError.properValue);
473+
ngModel.$setViewValue(parser.getText());
474+
ngModel.$setValidity("tooShort", true);
475+
ngModel.$render();
476+
scope.$apply();
477+
selectRange(range, "next");
478+
} else {
479+
selectRange(errorRange);
480+
}
481+
}
482+
else if (isPrintableKey(e)) {
450483
setTimeout(function(){
451484
range = getRange(element, parser.nodes, range.node);
452485
if (isRangeAtEnd(range)) {

src/factory.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,8 @@ angular.module("datetime").factory("datetime", function($locale){
509509
text: text,
510510
node: p,
511511
pos: pos,
512-
match: value
512+
match: value,
513+
properValue: num2str(+value, p.token.minLength, p.token.maxLength)
513514
};
514515
}
515516

0 commit comments

Comments
 (0)