mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-08 06:01:22 +00:00
GUAC-1176: formService must use a provider to be configurable within a config block.
This commit is contained in:
@@ -24,68 +24,21 @@
|
|||||||
* A service for maintaining form-related metadata and linking that data to
|
* A service for maintaining form-related metadata and linking that data to
|
||||||
* corresponding controllers and templates.
|
* corresponding controllers and templates.
|
||||||
*/
|
*/
|
||||||
angular.module('form').factory('formService', ['$injector',
|
angular.module('form').provider('formService', function formServiceProvider() {
|
||||||
function formService($injector) {
|
|
||||||
|
|
||||||
// Required services
|
|
||||||
var $compile = $injector.get('$compile');
|
|
||||||
var $http = $injector.get('$http');
|
|
||||||
var $q = $injector.get('$q');
|
|
||||||
var $templateCache = $injector.get('$templateCache');
|
|
||||||
|
|
||||||
var service = {};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a Promise which resolves with the HTML contents of the
|
* Reference to the provider itself.
|
||||||
* template at the given URL. The template contents will be retrieved from
|
|
||||||
* the $templateCache if possible.
|
|
||||||
*
|
*
|
||||||
* @param {String} url
|
* @type formServiceProvider
|
||||||
* The URL of the template to retrieve.
|
|
||||||
*
|
|
||||||
* @returns {Promise.<String>}
|
|
||||||
* A Promise which resolves with the HTML contents of the template at
|
|
||||||
* the given URL.
|
|
||||||
*/
|
*/
|
||||||
var templateRequest = function templateRequest(url) {
|
var provider = this;
|
||||||
|
|
||||||
// Pull template from cache if present
|
|
||||||
var template = $templateCache.get(url);
|
|
||||||
if (template)
|
|
||||||
return $q.when(template);
|
|
||||||
|
|
||||||
// Defer retrieval of template
|
|
||||||
var templateContent = $q.defer();
|
|
||||||
|
|
||||||
// Retrieve template manually
|
|
||||||
$http({
|
|
||||||
method : 'GET',
|
|
||||||
url : url,
|
|
||||||
cache : true
|
|
||||||
})
|
|
||||||
|
|
||||||
// Upon success, resolve promise and update template cache
|
|
||||||
.success(function templateRetrieved(html) {
|
|
||||||
$templateCache.put(url, html);
|
|
||||||
templateContent.resolve(html);
|
|
||||||
})
|
|
||||||
|
|
||||||
// Fail if template cannot be retrieved
|
|
||||||
.error(function templateError() {
|
|
||||||
templateContent.reject();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Return promise which will resolve with the retrieved template
|
|
||||||
return templateContent.promise;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Map of all registered field type definitions by name.
|
* Map of all registered field type definitions by name.
|
||||||
*
|
*
|
||||||
* @type Object.<String, FieldType>
|
* @type Object.<String, FieldType>
|
||||||
*/
|
*/
|
||||||
service.fieldTypes = {
|
this.fieldTypes = {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Text field type.
|
* Text field type.
|
||||||
@@ -179,75 +132,135 @@ angular.module('form').factory('formService', ['$injector',
|
|||||||
* @param {FieldType} fieldType
|
* @param {FieldType} fieldType
|
||||||
* The field type definition to associate with the given name.
|
* The field type definition to associate with the given name.
|
||||||
*/
|
*/
|
||||||
service.registerFieldType = function registerFieldType(fieldTypeName, fieldType) {
|
this.registerFieldType = function registerFieldType(fieldTypeName, fieldType) {
|
||||||
|
|
||||||
// Store field type
|
// Store field type
|
||||||
service.fieldTypes[fieldTypeName] = fieldType;
|
provider.fieldTypes[fieldTypeName] = fieldType;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
// Factory method required by provider
|
||||||
* Compiles and links the field associated with the given name to the given
|
this.$get = ['$injector', function formServiceFactory($injector) {
|
||||||
* scope, producing a distinct and independent DOM Element which functions
|
|
||||||
* as an instance of that field. The scope object provided must include at
|
|
||||||
* least the following properties:
|
|
||||||
*
|
|
||||||
* namespace:
|
|
||||||
* A String which defines the unique namespace associated the
|
|
||||||
* translation strings used by the form using a field of this type.
|
|
||||||
*
|
|
||||||
* field:
|
|
||||||
* The Field object that is being rendered, representing a field of
|
|
||||||
* this type.
|
|
||||||
*
|
|
||||||
* model:
|
|
||||||
* The current String value of the field, if any.
|
|
||||||
*
|
|
||||||
* @param {String} fieldTypeName
|
|
||||||
* The name of the field type defining the nature of the element to be
|
|
||||||
* created.
|
|
||||||
*
|
|
||||||
* @param {Object} scope
|
|
||||||
* The scope to which the new element will be linked.
|
|
||||||
*
|
|
||||||
* @return {Promise.<Element>}
|
|
||||||
* A Promise which resolves to the compiled Element. If an error occurs
|
|
||||||
* while retrieving the field type, this Promise will be rejected.
|
|
||||||
*/
|
|
||||||
service.createFieldElement = function createFieldElement(fieldTypeName, scope) {
|
|
||||||
|
|
||||||
// Ensure field type is defined
|
// Required services
|
||||||
var fieldType = service.fieldTypes[fieldTypeName];
|
var $compile = $injector.get('$compile');
|
||||||
if (!fieldType)
|
var $http = $injector.get('$http');
|
||||||
return $q.reject();
|
var $q = $injector.get('$q');
|
||||||
|
var $templateCache = $injector.get('$templateCache');
|
||||||
|
|
||||||
// Populate scope using defined controller
|
var service = {};
|
||||||
if (fieldType.module && fieldType.controller) {
|
|
||||||
var $controller = angular.injector(['ng', fieldType.module]).get('$controller');
|
|
||||||
$controller(fieldType.controller, {'$scope' : scope});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Defer compilation of template pending successful retrieval
|
service.fieldTypes = provider.fieldTypes;
|
||||||
var compiledTemplate = $q.defer();
|
|
||||||
|
|
||||||
// Attempt to retrieve template HTML
|
/**
|
||||||
templateRequest(fieldType.templateUrl)
|
* Returns a Promise which resolves with the HTML contents of the
|
||||||
|
* template at the given URL. The template contents will be retrieved from
|
||||||
|
* the $templateCache if possible.
|
||||||
|
*
|
||||||
|
* @param {String} url
|
||||||
|
* The URL of the template to retrieve.
|
||||||
|
*
|
||||||
|
* @returns {Promise.<String>}
|
||||||
|
* A Promise which resolves with the HTML contents of the template at
|
||||||
|
* the given URL.
|
||||||
|
*/
|
||||||
|
var templateRequest = function templateRequest(url) {
|
||||||
|
|
||||||
// Resolve with compiled HTML upon success
|
// Pull template from cache if present
|
||||||
.then(function templateRetrieved(html) {
|
var template = $templateCache.get(url);
|
||||||
compiledTemplate.resolve($compile(html)(scope));
|
if (template)
|
||||||
})
|
return $q.when(template);
|
||||||
|
|
||||||
// Reject on failure
|
// Defer retrieval of template
|
||||||
['catch'](function templateError() {
|
var templateContent = $q.defer();
|
||||||
compiledTemplate.reject();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Return promise which resolves to the compiled template
|
// Retrieve template manually
|
||||||
return compiledTemplate.promise;
|
$http({
|
||||||
|
method : 'GET',
|
||||||
|
url : url,
|
||||||
|
cache : true
|
||||||
|
})
|
||||||
|
|
||||||
};
|
// Upon success, resolve promise and update template cache
|
||||||
|
.success(function templateRetrieved(html) {
|
||||||
|
$templateCache.put(url, html);
|
||||||
|
templateContent.resolve(html);
|
||||||
|
})
|
||||||
|
|
||||||
return service;
|
// Fail if template cannot be retrieved
|
||||||
|
.error(function templateError() {
|
||||||
|
templateContent.reject();
|
||||||
|
});
|
||||||
|
|
||||||
}]);
|
// Return promise which will resolve with the retrieved template
|
||||||
|
return templateContent.promise;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compiles and links the field associated with the given name to the given
|
||||||
|
* scope, producing a distinct and independent DOM Element which functions
|
||||||
|
* as an instance of that field. The scope object provided must include at
|
||||||
|
* least the following properties:
|
||||||
|
*
|
||||||
|
* namespace:
|
||||||
|
* A String which defines the unique namespace associated the
|
||||||
|
* translation strings used by the form using a field of this type.
|
||||||
|
*
|
||||||
|
* field:
|
||||||
|
* The Field object that is being rendered, representing a field of
|
||||||
|
* this type.
|
||||||
|
*
|
||||||
|
* model:
|
||||||
|
* The current String value of the field, if any.
|
||||||
|
*
|
||||||
|
* @param {String} fieldTypeName
|
||||||
|
* The name of the field type defining the nature of the element to be
|
||||||
|
* created.
|
||||||
|
*
|
||||||
|
* @param {Object} scope
|
||||||
|
* The scope to which the new element will be linked.
|
||||||
|
*
|
||||||
|
* @return {Promise.<Element>}
|
||||||
|
* A Promise which resolves to the compiled Element. If an error occurs
|
||||||
|
* while retrieving the field type, this Promise will be rejected.
|
||||||
|
*/
|
||||||
|
service.createFieldElement = function createFieldElement(fieldTypeName, scope) {
|
||||||
|
|
||||||
|
// Ensure field type is defined
|
||||||
|
var fieldType = provider.fieldTypes[fieldTypeName];
|
||||||
|
if (!fieldType)
|
||||||
|
return $q.reject();
|
||||||
|
|
||||||
|
// Populate scope using defined controller
|
||||||
|
if (fieldType.module && fieldType.controller) {
|
||||||
|
var $controller = angular.injector(['ng', fieldType.module]).get('$controller');
|
||||||
|
$controller(fieldType.controller, {'$scope' : scope});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Defer compilation of template pending successful retrieval
|
||||||
|
var compiledTemplate = $q.defer();
|
||||||
|
|
||||||
|
// Attempt to retrieve template HTML
|
||||||
|
templateRequest(fieldType.templateUrl)
|
||||||
|
|
||||||
|
// Resolve with compiled HTML upon success
|
||||||
|
.then(function templateRetrieved(html) {
|
||||||
|
compiledTemplate.resolve($compile(html)(scope));
|
||||||
|
})
|
||||||
|
|
||||||
|
// Reject on failure
|
||||||
|
['catch'](function templateError() {
|
||||||
|
compiledTemplate.reject();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Return promise which resolves to the compiled template
|
||||||
|
return compiledTemplate.promise;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
return service;
|
||||||
|
|
||||||
|
}];
|
||||||
|
|
||||||
|
});
|
||||||
|
Reference in New Issue
Block a user