diff --git a/guacamole/src/main/webapp/app/form/controllers/dateFieldController.js b/guacamole/src/main/webapp/app/form/controllers/dateFieldController.js index af325be93..34acabe62 100644 --- a/guacamole/src/main/webapp/app/form/controllers/dateFieldController.js +++ b/guacamole/src/main/webapp/app/form/controllers/dateFieldController.js @@ -38,6 +38,13 @@ angular.module('form').controller('dateFieldController', ['$scope', '$injector', */ $scope.modelOptions = { + /** + * Space-delimited list of events on which the model will be updated. + * + * @type String + */ + updateOn : 'blur', + /** * The time zone to use when reading/writing the Date object of the * model. diff --git a/guacamole/src/main/webapp/app/form/directives/guacLenientDate.js b/guacamole/src/main/webapp/app/form/directives/guacLenientDate.js new file mode 100644 index 000000000..fe21c8efc --- /dev/null +++ b/guacamole/src/main/webapp/app/form/directives/guacLenientDate.js @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2015 Glyptodon LLC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * A directive which modifies the parsing and formatting of ngModel when used + * on an HTML5 date input field, relaxing the otherwise strict parsing and + * validation behavior. The behavior of this directive for other input elements + * is undefined. + */ +angular.module('form').directive('guacLenientDate', ['$injector', + function guacLenientDate($injector) { + + // Required services + var $filter = $injector.get('$filter'); + + /** + * Directive configuration object. + * + * @type Object. + */ + var config = { + restrict : 'A', + require : 'ngModel' + }; + + // Linking function + config.link = function linkGuacLenientDate($scope, $element, $attrs, ngModel) { + + // Parse date strings leniently + ngModel.$parsers = [function parse(viewValue) { + + // If blank, return null + if (!viewValue) + return null; + + // Match basic date pattern + var match = /([0-9]*)(?:-([0-9]*)(?:-([0-9]*))?)?/.exec(viewValue); + if (!match) + return null; + + // Determine year, month, and day based on pattern + var year = parseInt(match[1] || '0') || new Date().getFullYear(); + var month = parseInt(match[2] || '0') || 1; + var day = parseInt(match[3] || '0') || 1; + + // Convert to Date object + return new Date(Date.UTC(year, month - 1, day)); + + }]; + + // Format date strings as "yyyy-MM-dd" + ngModel.$formatters = [function format(modelValue) { + return modelValue ? $filter('date')(modelValue, 'yyyy-MM-dd', 'UTC') : ''; + }]; + + }; + + return config; + +}]); diff --git a/guacamole/src/main/webapp/app/form/templates/dateField.html b/guacamole/src/main/webapp/app/form/templates/dateField.html index 239f3f52a..a186e19a5 100644 --- a/guacamole/src/main/webapp/app/form/templates/dateField.html +++ b/guacamole/src/main/webapp/app/form/templates/dateField.html @@ -2,6 +2,7 @@