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

@@ -81,19 +81,8 @@ angular.module('index').config(['$provide', function($provide) {
function decorateTemplateRequest($delegate, $injector) { function decorateTemplateRequest($delegate, $injector) {
// 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
@@ -271,6 +260,59 @@ angular.module('index').config(['$provide', function($provide) {
}; };
/**
* Applies each of the given HTML patches to the given template.
*
* @param {Element[]} root
* The JQuery-wrapped root element of the template being
* patched.
*
* @param {String[]} patches
* An array of all HTML patches to be applied to the given
* template.
*/
var applyPatches = function applyPatches(root, patches) {
// Apply all defined patches
angular.forEach(patches, function applyPatch(patch) {
var elements = $(patch);
// Filter out and parse all applicable meta tags
var operations = [];
elements = elements.filter(function filterMetaTags(index, element) {
// Leave non-meta tags untouched
if (element.tagName !== 'META')
return true;
// Only meta tags having a valid "name" attribute need
// to be filtered
var name = element.getAttribute('name');
if (!name || !(name in PatchOperation.Operations))
return true;
// The "content" attribute must be present for any
// valid "name" meta tag
var content = element.getAttribute('content');
if (!content)
return true;
// Filter out and parse meta tag
operations.push(new PatchOperation(name, content));
return false;
});
// Apply each operation implied by the meta tags
angular.forEach(operations, function applyOperation(operation) {
operation.apply(root, elements);
});
});
};
/** /**
* Invokes $templateRequest() with all arguments exactly as provided, * Invokes $templateRequest() with all arguments exactly as provided,
* applying all HTML patches from any installed Guacamole extensions * applying all HTML patches from any installed Guacamole extensions
@@ -297,49 +339,19 @@ angular.module('index').config(['$provide', function($provide) {
// Resolve promise with patched template HTML // Resolve promise with patched template HTML
$delegate.apply(this, arguments).then(function patchTemplate(data) { $delegate.apply(this, arguments).then(function patchTemplate(data) {
// Parse HTML into DOM tree // Retrieve and apply all patches
var root = $('<div></div>').html(data); patchService.getPatches().success(function applyRetrievedPatches(patches) {
// Apply all defined patches // Parse HTML into DOM tree
angular.forEach(patches, function applyPatch(patch) { var root = $('<div></div>').html(data);
var elements = $(patch); // Apply all HTML patches to the parsed DOM
applyPatches(root, patches);
// Filter out and parse all applicable meta tags // Transform back into HTML
var operations = []; deferred.resolve.call(this, root.html());
elements = elements.filter(function filterMetaTags(index, element) {
// Leave non-meta tags untouched }, deferred.reject);
if (element.tagName !== 'META')
return true;
// Only meta tags having a valid "name" attribute need
// to be filtered
var name = element.getAttribute('name');
if (!name || !(name in PatchOperation.Operations))
return true;
// The "content" attribute must be present for any
// valid "name" meta tag
var content = element.getAttribute('content');
if (!content)
return true;
// Filter out and parse meta tag
operations.push(new PatchOperation(name, content));
return false;
});
// Apply each operation implied by the meta tags
angular.forEach(operations, function applyOperation(operation) {
operation.apply(root, elements);
});
});
// Transform back into HTML
deferred.resolve.call(this, root.html());
}, deferred.reject); }, deferred.reject);