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
var $q = $injector.get('$q');
/**
* 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>'
];
var patchService = $injector.get('patchService');
/**
* 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,
* applying all HTML patches from any installed Guacamole extensions
* to the HTML of the requested template.
* Applies each of the given HTML patches to the given template.
*
* @param {String} url
* The URL of the template being requested.
* @param {Element[]} root
* The JQuery-wrapped root element of the template being
* patched.
*
* @returns {Promise.<String>}
* A Promise which resolves with the patched HTML contents of the
* requested template if retrieval of the template is successful.
* @param {String[]} patches
* An array of all HTML patches to be applied to the given
* template.
*/
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) {
// Parse HTML into DOM tree
var root = $('<div></div>').html(data);
var applyPatches = function applyPatches(root, patches) {
// Apply all defined patches
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
deferred.resolve.call(this, root.html());
}, deferred.reject);
}, deferred.reject);
// Cache this promise for future results
promiseCache[url] = deferred.promise;
return deferred.promise;