mirror of
https://github.com/nasa/openmct.git
synced 2025-06-17 14:48:13 +00:00
[Forms] Separate out FormController
Separate out the controller for mct-form from its directive definition, to allow reuse by the mct-toolbar directive, WTD-684.
This commit is contained in:
57
platform/forms/src/controllers/FormController.js
Normal file
57
platform/forms/src/controllers/FormController.js
Normal file
@ -0,0 +1,57 @@
|
||||
/*global define*/
|
||||
|
||||
define(
|
||||
[],
|
||||
function () {
|
||||
"use strict";
|
||||
|
||||
// Default ng-pattern; any non whitespace
|
||||
var NON_WHITESPACE = /\S/;
|
||||
|
||||
/**
|
||||
* Controller for mct-form and mct-toolbar directives.
|
||||
* @constructor
|
||||
*/
|
||||
function FormController($scope) {
|
||||
var regexps = [];
|
||||
|
||||
// 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 NON_WHITESPACE;
|
||||
}
|
||||
|
||||
// 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];
|
||||
}
|
||||
|
||||
// Publish the form state under the requested
|
||||
// name in the parent scope
|
||||
$scope.$watch("mctForm", function (mctForm) {
|
||||
if ($scope.name) {
|
||||
$scope.$parent[$scope.name] = mctForm;
|
||||
}
|
||||
});
|
||||
|
||||
// Expose the regexp lookup
|
||||
$scope.getRegExp = getRegExp;
|
||||
}
|
||||
|
||||
return FormController;
|
||||
}
|
||||
);
|
Reference in New Issue
Block a user