From 9c11363224b8c362cd3b7051d933d94fcf900de1 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Thu, 18 Feb 2016 13:34:07 -0800 Subject: [PATCH] GUAC-1378: Define HTML patches using a special root element. --- .../index/config/templateRequestDecorator.js | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/guacamole/src/main/webapp/app/index/config/templateRequestDecorator.js b/guacamole/src/main/webapp/app/index/config/templateRequestDecorator.js index b4c78c754..cb848c3c0 100644 --- a/guacamole/src/main/webapp/app/index/config/templateRequestDecorator.js +++ b/guacamole/src/main/webapp/app/index/config/templateRequestDecorator.js @@ -31,6 +31,18 @@ angular.module('index').config(['$provide', function($provide) { // Required services var $q = $injector.get('$q'); + /** + * Array of the root elements of all patches which should be applied to + * the HTML of retrieved templates. + * + * @type Element[] + */ + var patches = [ + $('

HELLO BEFORE

')[0], + $('

HELLO AFTER

')[0], + $('
:-)
')[0] + ]; + /** * Invokes $templateRequest() with all arguments exactly as provided, * applying all HTML patches from any installed Guacamole extensions @@ -50,8 +62,34 @@ angular.module('index').config(['$provide', function($provide) { // Parse HTML into DOM tree var root = $('
').html(data); - // STUB: Apply HTML patches - root.find('a').after('

HELLO

'); + // Apply all defined patches + angular.forEach(patches, function applyPatch(patch) { + + // Ignore any patches which are malformed + if (patch.tagName !== 'GUAC-PATCH') + return; + + // Insert after any elements which match the "after" + // selector (if defined) + var after = patch.getAttribute('after'); + if (after) + root.find(after).after(patch.innerHTML); + + // Insert before any elements which match the "before" + // selector (if defined) + var before = patch.getAttribute('before'); + if (before) + root.find(before).before(patch.innerHTML); + + // Replace any elements which match the "replace" selector + // (if defined) + var replace = patch.getAttribute('replace'); + if (replace) + root.find(replace).html(patch.innerHTML); + + // Ignore all other attributes + + }); // Transform back into HTML deferred.resolve.call(this, root.html());