GUAC-1176: Migrate all field types to the new field registration service. Use field registration service within guacFormField directive.

This commit is contained in:
Michael Jumper
2015-06-05 00:09:52 -07:00
parent d292eb94ea
commit 48e10ab69a
13 changed files with 330 additions and 164 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014 Glyptodon LLC
* 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
@@ -60,55 +60,19 @@ angular.module('form').directive('guacFormField', [function formField() {
},
templateUrl: 'app/form/templates/formField.html',
controller: ['$scope', '$injector', function formFieldController($scope, $injector) {
controller: ['$scope', '$injector', '$element', function formFieldController($scope, $injector, $element) {
// Required services
var formService = $injector.get('formService');
var translationStringService = $injector.get('translationStringService');
/**
* The type to use for password input fields. By default, password
* input fields have type 'password', and are thus masked.
*
* @type String
* @default 'password'
*/
$scope.passwordInputType = 'password';
/**
* Returns a string which describes the action the next call to
* togglePassword() will have.
* The element which should contain any compiled field content. The
* actual content of a field is dynamically determined by its type.
*
* @return {String}
* A string which describes the action the next call to
* togglePassword() will have.
* @type Element[]
*/
$scope.getTogglePasswordHelpText = function getTogglePasswordHelpText() {
// If password is hidden, togglePassword() will show the password
if ($scope.passwordInputType === 'password')
return 'FORM.HELP_SHOW_PASSWORD';
// If password is shown, togglePassword() will hide the password
return 'FORM.HELP_HIDE_PASSWORD';
};
/**
* Toggles visibility of the field contents, if this field is a
* password field. Initially, password contents are masked
* (invisible).
*/
$scope.togglePassword = function togglePassword() {
// If password is hidden, show the password
if ($scope.passwordInputType === 'password')
$scope.passwordInputType = 'text';
// If password is shown, hide the password
else
$scope.passwordInputType = 'password';
};
var fieldContent = $element.find('.form-field');
/**
* Produces the translation string for the header of the current
@@ -135,113 +99,20 @@ angular.module('form').directive('guacFormField', [function formField() {
};
/**
* Produces the translation string for the given field option
* value. The translation string will be of the form:
*
* <code>NAMESPACE.FIELD_OPTION_NAME_VALUE<code>
*
* where <code>NAMESPACE</code> is the namespace provided to the
* directive, <code>NAME</code> is the field name transformed
* via translationStringService.canonicalize(), and
* <code>VALUE</code> is the option value transformed via
* translationStringService.canonicalize()
*
* @param {String} value
* The name of the option value.
*
* @returns {String}
* The translation string which produces the translated name of the
* value specified.
*/
$scope.getFieldOption = function getFieldOption(value) {
// Update field contents when field definition is changed
$scope.$watch('field', function setField(field) {
// If no field, or no value, then no corresponding translation string
if (!$scope.field || !$scope.field.name || !value)
return '';
// Reset contents
fieldContent.innerHTML = '';
return translationStringService.canonicalize($scope.namespace || 'MISSING_NAMESPACE')
+ '.FIELD_OPTION_' + translationStringService.canonicalize($scope.field.name)
+ '_' + translationStringService.canonicalize(value || 'EMPTY');
};
/**
* Translates the given string field value into an appropriately-
* typed value as dictated by the attributes of the field,
* exposing that typed value within the scope as
* <code>$scope.typedValue<code>.
*
* @param {String} modelValue
* The current string value of the field.
*/
var setTypedValue = function setTypedValue(modelValue) {
// Don't bother if the model is not yet defined
if (!$scope.field)
return;
// Coerce numeric strings to numbers
if ($scope.field.type === 'NUMERIC')
$scope.typedValue = (modelValue ? Number(modelValue) : null);
// Coerce boolean strings to boolean values
else if ($scope.field.type === 'BOOLEAN')
$scope.typedValue = (modelValue === $scope.field.value);
// All other field types are represented internally as strings
else
$scope.typedValue = modelValue || '';
};
/**
* Translates the given typed field value into a string as dictated
* by the attributes of the field, assigning that string value to
* the model.
*
* @param {String|Number|Boolean} typedValue
* The current value of the field, as an appropriate JavaScript
* type.
*/
var setModelValue = function setModelValue(typedValue) {
// Don't bother if the model is not yet defined
if (!$scope.field)
return;
// Convert numeric values back into strings
if ($scope.field.type === 'NUMERIC') {
if (!typedValue)
$scope.model = '';
else
$scope.model = typedValue.toString();
// Append field content
if (field) {
formService.createFieldElement(field.type, $scope)
.then(function fieldElementCreated(element) {
fieldContent.append(element);
});
}
// Convert boolean values back into strings based on field description
else if ($scope.field.type === 'BOOLEAN')
$scope.model = (typedValue ? $scope.field.value : '');
// All other field types are already strings
else
$scope.model = typedValue || '';
};
// Update string value and re-assign to model when field is changed
$scope.$watch('field', function setField(field) {
setTypedValue($scope.model);
setModelValue($scope.typedValue);
});
// Update typed value when model is changed
$scope.$watch('model', function setModel(model) {
setTypedValue(model);
});
// Update string value in model when typed value is changed
$scope.$watch('typedValue', function typedValueChanged(typedValue) {
setModelValue(typedValue);
});
}] // end controller