GUAC-1378: Pull and apply patches from REST endpoint.

This commit is contained in:
Michael Jumper
2016-02-19 00:23:39 -08:00
parent bda64a2108
commit a66513b724

View File

@@ -82,18 +82,7 @@ angular.module('index').config(['$provide', function($provide) {
// Required services // Required services
var $q = $injector.get('$q'); var $q = $injector.get('$q');
var patchService = $injector.get('patchService');
/**
* Array of the raw HTML of all patches which should be applied to the
* HTML of retrieved templates.
*
* @type String[]
*/
var patches = [
'<meta name="before" content="a"><p>HELLO BEFORE</p>',
'<meta name="after" content="a"><p>HELLO AFTER</p>',
'<meta name="replace" content="div.protocol"><div class="protocol">:-)</div>'
];
/** /**
* Represents a single HTML patching operation which will be applied * Represents a single HTML patching operation which will be applied
@@ -272,33 +261,17 @@ angular.module('index').config(['$provide', function($provide) {
}; };
/** /**
* Invokes $templateRequest() with all arguments exactly as provided, * Applies each of the given HTML patches to the given template.
* applying all HTML patches from any installed Guacamole extensions
* to the HTML of the requested template.
* *
* @param {String} url * @param {Element[]} root
* The URL of the template being requested. * The JQuery-wrapped root element of the template being
* patched.
* *
* @returns {Promise.<String>} * @param {String[]} patches
* A Promise which resolves with the patched HTML contents of the * An array of all HTML patches to be applied to the given
* requested template if retrieval of the template is successful. * template.
*/ */
var decoratedTemplateRequest = function decoratedTemplateRequest(url) { var applyPatches = function applyPatches(root, patches) {
var deferred = $q.defer();
// Chain to cached promise if it already exists
var cachedPromise = promiseCache[url];
if (cachedPromise) {
cachedPromise.then(deferred.resolve, deferred.reject);
return deferred.promise;
}
// Resolve promise with patched template HTML
$delegate.apply(this, arguments).then(function patchTemplate(data) {
// Parse HTML into DOM tree
var root = $('<div></div>').html(data);
// Apply all defined patches // Apply all defined patches
angular.forEach(patches, function applyPatch(patch) { angular.forEach(patches, function applyPatch(patch) {
@@ -338,11 +311,50 @@ angular.module('index').config(['$provide', function($provide) {
}); });
};
/**
* Invokes $templateRequest() with all arguments exactly as provided,
* applying all HTML patches from any installed Guacamole extensions
* to the HTML of the requested template.
*
* @param {String} url
* The URL of the template being requested.
*
* @returns {Promise.<String>}
* A Promise which resolves with the patched HTML contents of the
* requested template if retrieval of the template is successful.
*/
var decoratedTemplateRequest = function decoratedTemplateRequest(url) {
var deferred = $q.defer();
// Chain to cached promise if it already exists
var cachedPromise = promiseCache[url];
if (cachedPromise) {
cachedPromise.then(deferred.resolve, deferred.reject);
return deferred.promise;
}
// Resolve promise with patched template HTML
$delegate.apply(this, arguments).then(function patchTemplate(data) {
// Retrieve and apply all patches
patchService.getPatches().success(function applyRetrievedPatches(patches) {
// Parse HTML into DOM tree
var root = $('<div></div>').html(data);
// Apply all HTML patches to the parsed DOM
applyPatches(root, patches);
// Transform back into HTML // Transform back into HTML
deferred.resolve.call(this, root.html()); deferred.resolve.call(this, root.html());
}, deferred.reject); }, deferred.reject);
}, deferred.reject);
// Cache this promise for future results // Cache this promise for future results
promiseCache[url] = deferred.promise; promiseCache[url] = deferred.promise;
return deferred.promise; return deferred.promise;