]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5893 Add base JS component for tag cloud widget
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 7 Jan 2015 16:34:27 +0000 (17:34 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 7 Jan 2015 17:18:06 +0000 (18:18 +0100)
server/sonar-web/Gruntfile.coffee
server/sonar-web/src/main/coffee/widgets/base.coffee
server/sonar-web/src/main/coffee/widgets/tag-cloud.coffee [new file with mode: 0644]
server/sonar-web/src/main/js/widgets/widget.js

index 595d21ec6bca3e6ee1c3e055ca7e6d2e89dcc04f..532817793572fae2413da37c0252d3d924c1bc68 100644 (file)
@@ -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'
index 53b66bcba4dc904f91690622f9d4ed8de4714514..698bf09130cea7e520ef47ac38d46c9a1c4dd33a 100644 (file)
@@ -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 (file)
index 0000000..c776443
--- /dev/null
@@ -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
index 13e290a3af189ac84381ec5358b955090cbbfe58..4a8b90e89a0fecc4699cc624ff439d0d9607e275 100644 (file)
@@ -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());