Skip to content

Jump on next segment when pressing separator key #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Check demo page for live example.
<input type="text" datetime="yyyy-MM-dd" ng-model="myDate" min="Jan 1, 1990" max="Dec 31, 2050">
<input type="text" datetime="yyyy-MM-dd" ng-model="myDate" datetime-model="yyyy-MM-ddTHH:mm:ss">
<input type="text" datetime="yyyy-MM-dd" ng-model="myDate" default="Jan 1, 2000">
<input type="text" datetime="dd.MM.yyyy" ng-model="myDate" datetime-separator=",">
```

Parsing errors
Expand Down Expand Up @@ -103,6 +104,9 @@ Todos

Changelog
---------
* next
- jump on the next segment on pressing next separator key
- customizable separator key
* 3.0.1 (Apr 9, 2016)
- Fix validator and datetime-model bug. [#27](https://github.com/eight04/angular-datetime/issues/27)
* 3.0.0 (Apr 1, 2016)
Expand Down
7 changes: 7 additions & 0 deletions example/demo-angular-1.5.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,12 @@ <h3>Other tests</h3>
<pre class="code"><code>{{data.myDateString2 | json}}</code></pre>
</label>
</div>
<h3>Custom separator</h3>
<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>
<div class="form-group">
<label>
<input type="text" class="form-control" datetime="dd.MM.yyyy" ng-model="data.myDate" datetime-separator=",">
</label>
</div>
</body>
</html>
7 changes: 7 additions & 0 deletions example/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,12 @@ <h3>Other tests</h3>
<pre class="code"><code>{{data.myDateString2 | json}}</code></pre>
</label>
</div>
<h3>Custom separator</h3>
<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>
<div class="form-group">
<label>
<input type="text" class="form-control" datetime="dd.MM.yyyy" ng-model="data.myDate" datetime-separator=",">
</label>
</div>
</body>
</html>
39 changes: 36 additions & 3 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,14 @@ angular.module("datetime").directive("datetime", function(datetime, $log, $docum
node: null,
start: 0,
end: 0
};

},
lastError;
var datetimeSeparator;

if (angular.isDefined(attrs.datetimeSeparator)) {
datetimeSeparator = attrs.datetimeSeparator;
}

if (angular.isDefined(attrs.datetimeUtc)) {
parser.setTimezone("+0000");
if (modelParser) {
Expand Down Expand Up @@ -262,9 +268,12 @@ angular.module("datetime").directive("datetime", function(datetime, $log, $docum
return null;
}

ngModel.$setValidity("tooShort", true);

try {
parser.parse(viewValue);
} catch (err) {
lastError = err;
$log.error(err);

ngModel.$setValidity("datetime", false);
Expand Down Expand Up @@ -312,6 +321,8 @@ angular.module("datetime").directive("datetime", function(datetime, $log, $docum
return undefined;
}

lastError = null;

ngModel.$setValidity("datetime", true);

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

case "keypress":
if (isPrintableKey(e)) {
var nextSeparatorKeyCode;
// check for separator only when there is a next node which is static string
if (range.node.next && range.node.next.token.name === "string" && range.node.next.token.type === "static") {
nextSeparatorKeyCode = range.node.next.viewValue.charCodeAt(0);
}

if (e.keyCode === nextSeparatorKeyCode || (datetimeSeparator && e.keyCode == datetimeSeparator.charCodeAt(0))) {
e.preventDefault();
if (!ngModel.$error.datetime) {
selectRange(range, "next");
}
else if (lastError && lastError.code == "NUMBER_TOOSHORT") {
parser.nodeParseValue(lastError.node, lastError.properValue);
ngModel.$setViewValue(parser.getText());
ngModel.$setValidity("tooShort", true);
ngModel.$render();
scope.$apply();
selectRange(range, "next");
} else {
selectRange(errorRange);
}
}
else if (isPrintableKey(e)) {
setTimeout(function(){
range = getRange(element, parser.nodes, range.node);
if (isRangeAtEnd(range)) {
Expand Down
3 changes: 2 additions & 1 deletion src/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,8 @@ angular.module("datetime").factory("datetime", function($locale){
text: text,
node: p,
pos: pos,
match: value
match: value,
properValue: num2str(+value, p.token.minLength, p.token.maxLength)
};
}

Expand Down