diff options
author | Thomas Steur <tsteur@users.noreply.github.com> | 2014-11-27 22:32:05 +0300 |
---|---|---|
committer | Thomas Steur <tsteur@users.noreply.github.com> | 2014-11-27 22:32:05 +0300 |
commit | 68a072b5513d13e27c6d96c4085e5a184a025ab2 (patch) | |
tree | cf6b937a124549d348a4100cd49e3f9aae05f075 /plugins/Live | |
parent | 5f5cfd27b92d3b5feacf6dab009a106d9e586075 (diff) | |
parent | 21ec2facac27f77e398679c456f3479cb9a7de12 (diff) |
Merge pull request #6625 from piwik/6545
Do not request live information in case tab is not active
Diffstat (limited to 'plugins/Live')
-rw-r--r-- | plugins/Live/Live.php | 1 | ||||
-rw-r--r-- | plugins/Live/javascripts/live.js | 56 |
2 files changed, 55 insertions, 2 deletions
diff --git a/plugins/Live/Live.php b/plugins/Live/Live.php index ff5bb72985..aaddca1f22 100644 --- a/plugins/Live/Live.php +++ b/plugins/Live/Live.php @@ -38,6 +38,7 @@ class Live extends \Piwik\Plugin public function getJsFiles(&$jsFiles) { + $jsFiles[] = "libs/bower_components/visibilityjs/lib/visibility.core.js"; $jsFiles[] = "plugins/Live/javascripts/live.js"; $jsFiles[] = "plugins/Live/javascripts/visitorProfile.js"; $jsFiles[] = "plugins/Live/javascripts/visitorLog.js"; diff --git a/plugins/Live/javascripts/live.js b/plugins/Live/javascripts/live.js index 59913b8b02..17e08cded3 100644 --- a/plugins/Live/javascripts/live.js +++ b/plugins/Live/javascripts/live.js @@ -189,6 +189,14 @@ }, /** + * Return true in case widget is started. + * @returns {boolean} + */ + started: function() { + return this.isStarted; + }, + + /** * Set the interval for refresh * * @param {int} interval new interval for refresh @@ -207,6 +215,16 @@ $(function() { return; } + function scheduleAnotherRequest() + { + setTimeout(function () { refreshWidget(element, refreshAfterXSecs); }, refreshAfterXSecs * 1000); + } + + if (Visibility.hidden()) { + scheduleAnotherRequest(); + return; + } + var lastMinutes = $(element).attr('data-last-minutes') || 3, translations = JSON.parse($(element).attr('data-translations')); @@ -248,8 +266,7 @@ $(function() { ? translations['one_minute'] : translations['minutes'].replace('%s', lastMinutes); $(metrics[2]).text(lastMinutesText); - // schedule another request - setTimeout(function () { refreshWidget(element, refreshAfterXSecs); }, refreshAfterXSecs * 1000); + scheduleAnotherRequest(); }); ajaxRequest.send(true); }; @@ -280,3 +297,38 @@ function onClickPlay() { $('#pauseImage').show(); return $('#visitsLive').liveWidget('start'); } + +(function () { + if (!Visibility.isSupported()) { + return; + } + + var isStoppedByBlur = false; + + function isStarted() + { + return $('#visitsLive').liveWidget('started'); + } + + function onTabBlur() { + if (isStarted()) { + isStoppedByBlur = true; + onClickPause(); + } + } + + function onTabFocus() { + if (isStoppedByBlur && !isStarted()) { + isStoppedByBlur = false; + onClickPlay(); + } + } + + Visibility.change(function (event, state) { + if (Visibility.hidden()) { + onTabBlur(); + } else { + onTabFocus(); + } + }); +})();
\ No newline at end of file |