From d0a17e3e9854fe01f46df9416b77eaad0b2623b7 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Wed, 7 Jan 2015 17:34:27 +0100 Subject: [PATCH] SONAR-5893 Add base JS component for tag cloud widget --- server/sonar-web/Gruntfile.coffee | 2 + .../src/main/coffee/widgets/base.coffee | 4 +- .../src/main/coffee/widgets/tag-cloud.coffee | 54 +++++++++++++++++++ .../sonar-web/src/main/js/widgets/widget.js | 16 +++--- 4 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 server/sonar-web/src/main/coffee/widgets/tag-cloud.coffee diff --git a/server/sonar-web/Gruntfile.coffee b/server/sonar-web/Gruntfile.coffee index 595d21ec6bc..53281779357 100644 --- a/server/sonar-web/Gruntfile.coffee +++ b/server/sonar-web/Gruntfile.coffee @@ -93,6 +93,7 @@ module.exports = (grunt) -> '<%= pkg.assets %>js/widgets/pie-chart.js' '<%= pkg.assets %>js/widgets/histogram.js' '<%= pkg.assets %>js/widgets/word-cloud.js' + '<%= pkg.assets %>js/widgets/tag-cloud.js' '<%= pkg.assets %>js/widgets/treemap.js' '<%= pkg.assets %>js/graphics/pie-chart.js' '<%= pkg.assets %>js/top-search.js' @@ -128,6 +129,7 @@ module.exports = (grunt) -> '<%= pkg.assets %>js/widgets/pie-chart.js' '<%= pkg.assets %>js/widgets/histogram.js' '<%= pkg.assets %>js/widgets/word-cloud.js' + '<%= pkg.assets %>js/widgets/tag-cloud.js' '<%= pkg.assets %>js/widgets/treemap.js' '<%= pkg.assets %>js/graphics/pie-chart.js' '<%= pkg.assets %>js/top-search.js' diff --git a/server/sonar-web/src/main/coffee/widgets/base.coffee b/server/sonar-web/src/main/coffee/widgets/base.coffee index 53b66bcba4d..698bf09130c 100644 --- a/server/sonar-web/src/main/coffee/widgets/base.coffee +++ b/server/sonar-web/src/main/coffee/widgets/base.coffee @@ -63,8 +63,8 @@ class BaseWidget tooltip: (d) -> title = d.longName - title += "\n#{@colorMetric.name}: #{@colorMetric.formattedValue d}" if @colorMetric.value(d)? - title += "\n#{@sizeMetric.name}: #{@sizeMetric.formattedValue d}" if @sizeMetric.value(d)? + title += "\n#{@colorMetric.name}: #{@colorMetric.formattedValue d}" if @colorMetric and @colorMetric.value(d)? + title += "\n#{@sizeMetric.name}: #{@sizeMetric.formattedValue d}" if @sizeMetric and @sizeMetric.value(d)? title diff --git a/server/sonar-web/src/main/coffee/widgets/tag-cloud.coffee b/server/sonar-web/src/main/coffee/widgets/tag-cloud.coffee new file mode 100644 index 00000000000..c776443dacb --- /dev/null +++ b/server/sonar-web/src/main/coffee/widgets/tag-cloud.coffee @@ -0,0 +1,54 @@ +class TagCloud extends window.SonarWidgets.BaseWidget + sizeLow: 10 + sizeHigh: 24 + + + constructor: -> + @addField 'width', [] + @addField 'height', [] + @addField 'tags', [] + @addField 'maxResultsReached', false + super + + + renderWords: -> + words = @wordContainer.selectAll('.cloud-word').data @tags() + + wordsEnter = words.enter().append('a').classed 'cloud-word', true + wordsEnter.text (d) -> d.key + wordsEnter.attr 'href', (d) => + url = @options().baseUrl + '|tags=' + d.key + url + wordsEnter.attr 'title', (d) => @tooltip d + + words.style 'font-size', (d) => + "#{@size d.value}px" + + words.sort (a, b) => + if a.key.toLowerCase() > b.key.toLowerCase() then 1 else -1 + + + render: (container) -> + box = d3.select(container).append('div') + box.classed 'sonar-d3', true + box.classed 'cloud-widget', true + @wordContainer = box.append 'div' + + sizeDomain = d3.extent @tags(), (d) => d.value + @size = d3.scale.linear().domain(sizeDomain).range [@sizeLow, @sizeHigh] + + # Show maxResultsReached message + if @maxResultsReached() + maxResultsReachedLabel = box.append('div').text @options().maxItemsReachedMessage + maxResultsReachedLabel.classed 'max-results-reached-message', true + + @renderWords() + + super + + + parseSource: (response) -> + @tags(response.tags) + + +window.SonarWidgets.TagCloud = TagCloud diff --git a/server/sonar-web/src/main/js/widgets/widget.js b/server/sonar-web/src/main/js/widgets/widget.js index 13e290a3af1..4a8b90e89a0 100644 --- a/server/sonar-web/src/main/js/widgets/widget.js +++ b/server/sonar-web/src/main/js/widgets/widget.js @@ -44,15 +44,17 @@ window.SonarWidgets = window.SonarWidgets == null ? {} : window.SonarWidgets; d3.json(this.source(), function(error, response) { if (response && !error) { that.hideSpinner(); - if (response.components.length > 0) { + if (typeof(response.components) === 'undefined' || response.components.length > 0) { that.widget = new SonarWidgets[that.type()](); - that.widget - .metrics(response.metrics) - .metricsPriority(that.metricsPriority()) - .components(response.components) - .options(that.options()); + that.widget.metricsPriority(that.metricsPriority()); + that.widget.options(that.options()) + that.widget.metrics(response.metrics); + that.widget.components(response.components); + if(typeof(that.widget.parseSource) === 'function') { + that.widget.parseSource(response); + } if (typeof that.widget.maxResultsReached === 'function') { - that.widget.maxResultsReached(response.paging.pages > 1); + that.widget.maxResultsReached(response.paging != null && response.paging.pages > 1); } if (that.height()) { that.widget.height(that.height()); -- 2.39.5