GUAC-1160: Generalize parameters into fields. Depend on title in field.

This commit is contained in:
Michael Jumper
2015-04-15 13:51:07 -07:00
parent c413b8a1e1
commit 6d0d7b127f
7 changed files with 100 additions and 172 deletions

View File

@@ -281,26 +281,6 @@ angular.module('manage').controller('manageConnectionController', ['$scope', '$i
return 'PROTOCOL_' + translationStringService.canonicalize(protocolName) + '.NAME';
};
/**
* Given the internal name of a protocol and the internal name of a
* parameter for that protocol, produces the translation string
* for the localized, human-readable name of that protocol parameter.
*
* @param {String} protocolName
* The name of the protocol.
*
* @param {String} parameterName
* The name of the protocol parameter.
*
* @returns {String}
* The translation string which produces the translated name of the
* protocol parameter specified.
*/
$scope.getProtocolParameterName = function getProtocolParameterName(protocolName, parameterName) {
return 'PROTOCOL_' + translationStringService.canonicalize(protocolName)
+ '.FIELD_HEADER_' + translationStringService.canonicalize(parameterName);
};
/**
* Cancels all pending edits, returning to the management page.
*/

View File

@@ -33,36 +33,24 @@ angular.module('manage').directive('guacConnectionParameter', [function connecti
scope: {
/**
* The protocol this parameter is associated with.
* The field to display.
*
* @type Protocol
* @type Field
*/
protocol : '=',
field : '=',
/**
* The unique name of this parameter within the protocol
* definition.
*
* The property which contains this fields current value. When this
* field changes, the property will be updated accordingly.
*
* @type String
*/
name : '=',
/**
* The current map of parameter names to their corresponding string
* values.
*
* @type Object.<String, String>
*/
parameters : '='
model : '='
},
templateUrl: 'app/manage/templates/connectionParameter.html',
controller: ['$scope', '$injector', function connectionParameterController($scope, $injector) {
// Required services
var $q = $injector.get('$q');
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.
@@ -109,122 +97,82 @@ angular.module('manage').directive('guacConnectionParameter', [function connecti
};
/**
* Deferred load of the parameter definition, pending availability
* of the protocol definition as a whole.
* 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>.
*
* @type Deferred
* @param {String} modelValue
* The current string value of the field.
*/
var parameterDefinitionAvailable = $q.defer();
var setTypedValue = function setTypedValue(modelValue) {
/**
* Populates the parameter definition on the scope as
* <code>$scope.parameter</code> if both the parameter name and
* protocol definition are available. If either are unavailable,
* this function has no effect.
*/
var retrieveParameterDefinition = function retrieveParameterDefinition() {
// Both name and protocol are needed to retrieve the parameter definition
if (!$scope.name || !$scope.protocol)
// Don't bother if the modelValue is not yet defined
if (!$scope.field)
return;
// Once protocol definition is available, locate parameter definition by name
$scope.protocol.parameters.forEach(function findParameter(parameter) {
if (parameter.name === $scope.name) {
$scope.parameter = parameter;
parameterDefinitionAvailable.resolve(parameter);
}
});
// Coerce numeric strings to numbers
if ($scope.field.type === 'NUMERIC')
$scope.typedValue = (modelValue ? Number($scope.field.value) : null);
// Coerce boolean strings to boolean values
else if ($scope.field.type === 'BOOLEAN')
$scope.typedValue = (modelValue === $scope.field.value);
// All other parameter types are represented internally as strings
else
$scope.typedValue = modelValue || '';
};
// Load parameter definition once protocol definition is available.
$scope.$watch('name', retrieveParameterDefinition);
$scope.$watch('protocol', retrieveParameterDefinition);
/**
* 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();
}
// Convert boolean values back into strings based on protocol description
else if ($scope.field.type === 'BOOLEAN')
$scope.model = (typedValue ? $scope.field.value : '');
// All other parameter 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 parameter set is changed
$scope.$watch('parameters', function setParameters(parameters) {
// Don't bother if no parameters were provided
if (!parameters)
return;
// Wait for parameter definition
parameterDefinitionAvailable.promise.then(function setTypedValue() {
// Pull parameter value
var value = parameters[$scope.name];
// Coerce numeric strings to numbers
if ($scope.parameter.type === 'NUMERIC')
$scope.typedValue = (value ? Number(value) : null);
// Coerce boolean strings to boolean values
else if ($scope.parameter.type === 'BOOLEAN')
$scope.typedValue = (value === $scope.parameter.value);
// All other parameter types are represented internally as strings
else
$scope.typedValue = value || '';
});
$scope.$watch('model', function setModel(model) {
setTypedValue(model);
});
// Update string value in parameter set when typed value is changed
$scope.$watch('typedValue', function typedValueChanged(typedValue) {
// Don't bother if there's nothing to set
if (!$scope.parameters)
return;
// Wait for parameter definition
parameterDefinitionAvailable.promise.then(function setValue() {
// Convert numeric values back into strings
if ($scope.parameter.type === 'NUMERIC') {
if (!typedValue)
$scope.parameters[$scope.name] = '';
else
$scope.parameters[$scope.name] = typedValue.toString();
}
// Convert boolean values back into strings based on protocol description
else if ($scope.parameter.type === 'BOOLEAN')
$scope.parameters[$scope.name] = (typedValue ? $scope.parameter.value : '');
// All other parameter types are already strings
else
$scope.parameters[$scope.name] = typedValue || '';
});
}); // end watch typedValue
/**
* Given the internal name of a protocol, the internal name of a
* parameter for that protocol, and the internal name for a valid
* value of that parameter, produces the translation string for the
* localized, human-readable name of that parameter value.
*
* @param {String} protocolName
* The name of the protocol.
*
* @param {String} parameterName
* The name of the protocol parameter.
*
* @param {String} parameterValue
* The name of the parameter value.
*
* @returns {String}
* The translation string which produces the translated name of the
* parameter value specified.
*/
$scope.getProtocolParameterOption = function getProtocolParameterOption(protocolName, parameterName, parameterValue) {
return 'PROTOCOL_' + translationStringService.canonicalize(protocolName)
+ '.FIELD_OPTION_' + translationStringService.canonicalize(parameterName)
+ '_' + translationStringService.canonicalize(parameterValue || 'EMPTY');
};
setModelValue(typedValue);
});
}] // end controller
};

View File

@@ -22,21 +22,21 @@
-->
<!-- Generic input types -->
<input ng-show="parameter.type === 'TEXT'" type="text" ng-model="typedValue" autocorrect="off" autocapitalize="off"/>
<input ng-show="parameter.type === 'NUMERIC'" type="number" ng-model="typedValue" autocorrect="off" autocapitalize="off"/>
<input ng-show="parameter.type === 'USERNAME'" type="text" ng-model="typedValue" autocorrect="off" autocapitalize="off"/>
<input ng-show="parameter.type === 'BOOLEAN'" type="checkbox" ng-model="typedValue" autocorrect="off" autocapitalize="off"/>
<input ng-show="field.type === 'TEXT'" type="text" ng-model="typedValue" autocorrect="off" autocapitalize="off"/>
<input ng-show="field.type === 'NUMERIC'" type="number" ng-model="typedValue" autocorrect="off" autocapitalize="off"/>
<input ng-show="field.type === 'USERNAME'" type="text" ng-model="typedValue" autocorrect="off" autocapitalize="off"/>
<input ng-show="field.type === 'BOOLEAN'" type="checkbox" ng-model="typedValue" autocorrect="off" autocapitalize="off"/>
<!-- Password parameter -->
<div ng-show="parameter.type === 'PASSWORD'" class="password-field">
<!-- Password field -->
<div ng-show="field.type === 'PASSWORD'" class="password-field">
<input type="{{passwordInputType}}" ng-model="typedValue" autocorrect="off" autocapitalize="off"/>
<div class="icon toggle-password" ng-click="togglePassword()" title="{{getTogglePasswordHelpText() | translate}}"></div>
</div>
<!-- Multiline parameter -->
<textarea ng-show="parameter.type === 'MULTILINE'" ng-model="typedValue" autocorrect="off" autocapitalize="off"></textarea>
<!-- Multiline field -->
<textarea ng-show="field.type === 'MULTILINE'" ng-model="typedValue" autocorrect="off" autocapitalize="off"></textarea>
<!-- Enumerated parameter -->
<select ng-show="parameter.type === 'ENUM'" ng-model="typedValue" ng-options="option.value as getProtocolParameterOption(protocol.name, parameter.name, option.value) | translate for option in parameter.options | orderBy: value"></select>
<!-- Enumerated field -->
<select ng-show="field.type === 'ENUM'" ng-model="typedValue" ng-options="option.value as option.title | translate for option in field.options | orderBy: value"></select>
</div>

View File

@@ -64,9 +64,9 @@ THE SOFTWARE.
<!-- All the different possible editable field types -->
<tr ng-repeat="parameter in protocols[connection.protocol].parameters">
<th>{{getProtocolParameterName(connection.protocol, parameter.name) | translate}}</th>
<th>{{parameter.title | translate}}</th>
<td>
<guac-connection-parameter protocol="protocols[connection.protocol]" name="parameter.name" parameters="parameters"></guac-connection-parameter>
<guac-connection-parameter field="parameter" model="parameters[parameter.name]"></guac-connection-parameter>
</td>
</tr>
</table>

View File

@@ -21,20 +21,20 @@
*/
/**
* Service which defines the ProtocolParameter class.
* Service which defines the Field class.
*/
angular.module('rest').factory('ProtocolParameter', [function defineProtocolParameter() {
angular.module('rest').factory('Field', [function defineField() {
/**
* The object returned by REST API calls when representing the data
* associated with a configuration parameter of a remote desktop protocol.
*
* @constructor
* @param {ProtocolParameter|Object} [template={}]
* @param {Field|Object} [template={}]
* The object whose properties should be copied within the new
* ProtocolParameter.
* Field.
*/
var ProtocolParameter = function ProtocolParameter(template) {
var Field = function Field(template) {
// Use empty object by default
template = template || {};
@@ -56,12 +56,12 @@ angular.module('rest').factory('ProtocolParameter', [function defineProtocolPara
/**
* The type string defining which values this parameter may contain,
* as well as what properties are applicable. Valid types are listed
* within ProtocolParameter.Type.
* within Field.Type.
*
* @type String
* @default ProtocolParameter.Type.TEXT
* @default Field.Type.TEXT
*/
this.type = template.type || ProtocolParameter.Type.TEXT;
this.type = template.type || Field.Type.TEXT;
/**
* The value to set the parameter to, in the case of a BOOLEAN
@@ -75,7 +75,7 @@ angular.module('rest').factory('ProtocolParameter', [function defineProtocolPara
* All possible legal values for this parameter. This property is only
* applicable to ENUM type parameters.
*
* @type ProtocolParameterOption[]
* @type FieldOption[]
*/
this.options = template.options;
@@ -84,7 +84,7 @@ angular.module('rest').factory('ProtocolParameter', [function defineProtocolPara
/**
* All valid protocol parameter types.
*/
ProtocolParameter.Type = {
Field.Type = {
/**
* The type string associated with parameters that may contain a single
@@ -147,6 +147,6 @@ angular.module('rest').factory('ProtocolParameter', [function defineProtocolPara
};
return ProtocolParameter;
return Field;
}]);

View File

@@ -21,20 +21,20 @@
*/
/**
* Service which defines the ProtocolParameterOption class.
* Service which defines the FieldOption class.
*/
angular.module('rest').factory('ProtocolParameterOption', [function defineProtocolParameterOption() {
angular.module('rest').factory('FieldOption', [function defineFieldOption() {
/**
* The object returned by REST API calls when representing a single possible
* legal value of a configuration parameter of a remote desktop protocol.
*
* @constructor
* @param {ProtocolParameterOption|Object} [template={}]
* @param {FieldOption|Object} [template={}]
* The object whose properties should be copied within the new
* ProtocolParameterOption.
* FieldOption.
*/
var ProtocolParameterOption = function ProtocolParameterOption(template) {
var FieldOption = function FieldOption(template) {
// Use empty object by default
template = template || {};
@@ -56,6 +56,6 @@ angular.module('rest').factory('ProtocolParameterOption', [function defineProtoc
};
return ProtocolParameterOption;
return FieldOption;
}]);

View File

@@ -57,7 +57,7 @@ angular.module('rest').factory('Protocol', [function defineProtocol() {
* An array of all known parameters for this protocol, their types,
* and other information.
*
* @type ProtocolParameter[]
* @type Field[]
* @default []
*/
this.parameters = template.parameters || [];