From 354327accc581f1515e41efb42cb9c8c6ed9ea88 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Fri, 28 Nov 2014 14:08:33 -0800 Subject: [PATCH] [Forms] Support patterns Support patterns for text fields, using the ng-pattern directive. WTD-530. --- .../res/templates/controls/textfield.html | 1 + platform/forms/res/templates/form.html | 23 ++++++------ platform/forms/src/MCTForm.js | 35 +++++++++++++++++-- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/platform/forms/res/templates/controls/textfield.html b/platform/forms/res/templates/controls/textfield.html index f4f8096ba5..57e61d3613 100644 --- a/platform/forms/res/templates/controls/textfield.html +++ b/platform/forms/res/templates/controls/textfield.html @@ -3,6 +3,7 @@ diff --git a/platform/forms/res/templates/form.html b/platform/forms/res/templates/form.html index 0195e0b22b..5e5bbaef73 100644 --- a/platform/forms/res/templates/form.html +++ b/platform/forms/res/templates/form.html @@ -23,23 +23,26 @@
- - - + +
- + + structure="row" + field="item.key"> + {{item.name}}
diff --git a/platform/forms/src/MCTForm.js b/platform/forms/src/MCTForm.js index 00f7f7065f..6cdcb4deda 100644 --- a/platform/forms/src/MCTForm.js +++ b/platform/forms/src/MCTForm.js @@ -8,6 +8,8 @@ define( function () { "use strict"; + var MATCH_ALL = /^.*$/; + /** * * @constructor @@ -20,18 +22,47 @@ define( ].join("/"); function controller($scope) { + var regexps = [], + matchAll = /.*/; + + // ng-pattern seems to want a RegExp, and not a + // string (despite what documentation says) but + // we want form structure to be JSON-expressible, + // so we make RegExp's from strings as-needed + function getRegExp(pattern) { + // If undefined, don't apply a pattern + if (!pattern) { + return MATCH_ALL; + } + + // Just echo if it's already a regexp + if (pattern instanceof RegExp) { + return pattern; + } + + // Otherwise, assume a string + // Cache for easy lookup later (so we don't + // creat a new RegExp every digest cycle) + if (!regexps[pattern]) { + regexps[pattern] = new RegExp(pattern); + } + + return regexps[pattern]; + } + $scope.$watch("mctForm", function (mctForm) { - console.log(JSON.stringify(mctForm)); if ($scope.name) { $scope.$parent[$scope.name] = mctForm; } }); + + $scope.getRegExp = getRegExp; } return { restrict: "E", templateUrl: templatePath, - link: controller, + controller: controller, scope: { structure: "=", ngModel: "=",