From 585662a97c78f921755222ecb3393c3a66a4a058 Mon Sep 17 00:00:00 2001 From: Maximilian Grundke Date: Wed, 25 Apr 2018 14:33:10 +0200 Subject: [PATCH] Generalize js to work for both user and rfc history --- .../javascripts/rfc_activity_history.js | 76 ---------------- .../statistics_activity_history.js | 88 +++++++++++++++++++ 2 files changed, 88 insertions(+), 76 deletions(-) delete mode 100644 app/assets/javascripts/rfc_activity_history.js create mode 100644 app/assets/javascripts/statistics_activity_history.js diff --git a/app/assets/javascripts/rfc_activity_history.js b/app/assets/javascripts/rfc_activity_history.js deleted file mode 100644 index d672c567..00000000 --- a/app/assets/javascripts/rfc_activity_history.js +++ /dev/null @@ -1,76 +0,0 @@ -$(document).ready(function () { - var containerId = 'rfc-activity-history'; - - if ($.isController('statistics') && $('.graph#' + containerId).isPresent()) { - - var chartData; - var dataset; - var graph; - var groups; - - var buildChartGroups = function() { - return _.map(chartData, function(element) { - return { - content: element.name, - id: element.key, - visible: true, - options: { - interpolation: false - } - }; - }); - }; - - var initializeChart = function() { - dataset = new vis.DataSet(); - groups = new vis.DataSet(buildChartGroups()); - graph = new vis.Graph2d(document.getElementById(containerId), dataset, groups, { - dataAxis: { - customRange: { - left: { - min: 0 - } - }, - showMinorLabels: true - }, - drawPoints: { - style: 'circle' - }, - legend: true, - start: $('#from-date')[0].value, - end: $('#to-date')[0].value - }); - }; - - var refreshData = function(callback) { - var params = new URLSearchParams(window.location.search.slice(1)); - var jqxhr = $.ajax('rfc-activity-history.json', { - dataType: 'json', - data: {from: params.get('from'), to: params.get('to'), interval: params.get('interval')}, - method: 'GET' - }); - jqxhr.done(function(response) { - (callback || _.noop)(response); - updateChartData(response); - }); - }; - - var updateChartData = function(response) { - _.each(response, function(group) { - _.each(group.data, function(data) { - dataset.add({ - group: group.key, - x: data.key, - y: data.value - }); - }); - }); - }; - - refreshData(function (data) { - chartData = data; - $('#' + containerId).parent().find('.spinner').hide(); - initializeChart(); - }); - } -}); diff --git a/app/assets/javascripts/statistics_activity_history.js b/app/assets/javascripts/statistics_activity_history.js new file mode 100644 index 00000000..27fb9a2f --- /dev/null +++ b/app/assets/javascripts/statistics_activity_history.js @@ -0,0 +1,88 @@ +$(document).ready(function () { + + function manageActivityHistory(prefix) { + var containerId = prefix + '-activity-history'; + + if ($('.graph#' + containerId).isPresent()) { + + var chartData; + var dataset; + var graph; + var groups; + + var buildChartGroups = function () { + return _.map(chartData, function (element) { + return { + content: element.name, + id: element.key, + visible: true, + options: { + interpolation: false, + yAxisOrientation: element.axis ? element.axis : 'left' + } + }; + }); + }; + + var initializeChart = function () { + dataset = new vis.DataSet(); + groups = new vis.DataSet(buildChartGroups()); + graph = new vis.Graph2d(document.getElementById(containerId), dataset, groups, { + dataAxis: { + customRange: { + left: { + min: 0 + }, + right: { + min: 0 + } + }, + showMinorLabels: true + }, + drawPoints: { + style: 'circle' + }, + legend: true, + start: $('#from-date')[0].value, + end: $('#to-date')[0].value + }); + }; + + var refreshData = function (callback) { + var params = new URLSearchParams(window.location.search.slice(1)); + var jqxhr = $.ajax(prefix + '-activity-history.json', { + dataType: 'json', + data: {from: params.get('from'), to: params.get('to'), interval: params.get('interval')}, + method: 'GET' + }); + jqxhr.done(function (response) { + (callback || _.noop)(response); + updateChartData(response); + }); + }; + + var updateChartData = function (response) { + _.each(response, function (group) { + _.each(group.data, function (data) { + dataset.add({ + group: group.key, + x: data.key, + y: data.value + }); + }); + }); + }; + + refreshData(function (data) { + chartData = data; + $('#' + containerId).parent().find('.spinner').hide(); + initializeChart(); + }); + } + } + + if ($.isController('statistics')) { + manageActivityHistory('rfc'); + manageActivityHistory('user'); + } +});