Skip to content

Commit

Permalink
Merge pull request #209 from friesoft/master
Browse files Browse the repository at this point in the history
Disable autorefresh for grafana iframe
  • Loading branch information
Carsten committed May 12, 2019
2 parents 3cf3461 + e837fbf commit ca20655
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions configuration.php
Expand Up @@ -24,3 +24,5 @@
'url' => 'graph'
));
}

$this->provideJsFile('behavior/iframe.js');
68 changes: 68 additions & 0 deletions public/js/behavior/iframe.js
@@ -0,0 +1,68 @@
/* Garrett Seward | @spectralsun | GPLv2 */

/**
* Icinga.Behavior.Iframe
*
* Merges content for containers when iframes are present to prevent reloading.
*/
(function(Icinga, $) {
'use strict';

function Iframe(icinga) {
Icinga.EventListener.call(this, icinga);
}

Iframe.prototype = new Icinga.EventListener();

/**
* Mutates the HTML before it is placed in the DOM after a reload
*
* @param content {string} The content to be rendered
* @param $container {jQuery} The target container
* @param action {string} The URL that caused the reload
* @param autorefresh {bool} Whether the rendering is due to an auto-refresh
*
* @return {string|null} The content to be rendered or null, when nothing should be changed
*/
Iframe.prototype.renderHook = function(content, $container, action, autorefresh) {
if (!autorefresh) {
return content;
} else {
var containerId = $container.attr('id');
if (containerId === 'menu' || containerId === 'application-state') {
return content;
}
}

if (!$container.find('iframe').length) {
return content;
}

var $children = $container.children();
var $contentChildren = $container.find('.content').children();
var $content = $('<div>').html(content);

$content.children().each(function(idx) {
var $child = $(this);
if (!$child.hasClass('content')) {
$($children[idx]).html($child.html());
} else {
$child.children().each(function(contentIdx) {
var $contentChild = $(this);
// All the iframes we use have this class, overwrite any others
if (!$contentChild.hasClass('module-grafana')) {
$($contentChildren[contentIdx]).html($contentChild.html());
}
});
}
});
$container.find('.controls .tabs').after('<div class="tabs-spacer">');
return null;
};

Icinga.Behaviors = Icinga.Behaviors || {};

Icinga.Behaviors.Iframe = Iframe;

}) (Icinga, jQuery);

0 comments on commit ca20655

Please sign in to comment.