]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3812 Enhanced selection of metric properties
authorsimonbrandhof <simon.brandhof@gmail.com>
Wed, 19 Sep 2012 21:10:31 +0000 (23:10 +0200)
committersimonbrandhof <simon.brandhof@gmail.com>
Wed, 19 Sep 2012 21:10:31 +0000 (23:10 +0200)
plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties
sonar-server/src/main/webapp/WEB-INF/app/controllers/api/resources_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb
sonar-server/src/main/webapp/WEB-INF/app/helpers/properties_helper.rb
sonar-server/src/main/webapp/WEB-INF/app/views/dashboard/_widget_properties.html.erb

index cef409fa0b6384b3d0ca0924e1cf44ecdfd8887a..5af08a41b808cf6647277f0fd8a1cde990ff7895 100644 (file)
@@ -601,6 +601,17 @@ dashboard.TimeMachine.name=Time Machine
 search.results=results
 search.duration=({0} seconds)
 
+
+#------------------------------------------------------------------------------
+#
+# SELECT2.js 
+#
+#------------------------------------------------------------------------------
+select2.noMatches=No matches
+select2.searching=Searching...
+select2.tooShort=Please enter {0} more characters
+
+
 #------------------------------------------------------------------------------
 #
 # WIDGETS
index 959b15dbdec4e0f39aa8605eb005fa58f0ebc429..279c9577cf66e9e88f921d6d74ee8225df242515 100644 (file)
 #
 class Api::ResourcesController < Api::ApiController
 
+  # since version 3.3
   def search
       search_text = params[:s]||''
       page=(params[:p] ? params[:p].to_i : 1)
       page_size=(params[:ps] ? params[:ps].to_i : 10)
       if params[:q]
         qualifiers=params[:q].split(',')
-      elsif params[:rtp]
-        qualifiers=Java::OrgSonarServerUi::JRubyFacade.getInstance().getQualifiersWithProperty(params[:rtp])
+      elsif params[:qp]
+        qualifiers=Java::OrgSonarServerUi::JRubyFacade.getInstance().getQualifiersWithProperty(params[:qp])
       else
         qualifiers=[]
       end
index d3ca2a2bb35d4e1a65ea402d51904806b935db88..f00302f9ed7f01689ad1d66199fe555b47b322a9 100644 (file)
@@ -633,7 +633,17 @@ module ApplicationHelper
     return 0
   end
 
-  def resource_dropdown(name, options={})
+
+  #
+  # Creates an enhanced dropdown selection box of resources. Values are loaded on the fly via Ajax requests.
+  # ==== Options
+  # * <tt>:width</tt> - The width suffixed with unit, for example '300px' or '100%'. Default is '250px'
+  # * <tt>:html_id</tt> - The id of the HTML element. Default is the name.
+  # * <tt>:qualifiers</tt> - Array of resource qualifiers to filter.
+  # * <tt>:resource_type_property</tt> -Filter on resource types on which the property is enabled, for example 'supportsGlobalDashboards'.
+  # * <tt>:selected_resource</tt> - the resource that is selected by default.
+  #
+  def resource_select_tag(name, options={})
     width=options[:width]
     html_id=options[:html_id]||name
 
@@ -641,7 +651,7 @@ module ApplicationHelper
     if options[:qualifiers]
       ws_url+="q=#{options[:qualifiers].join(',')}"
     elsif options[:resource_type_property]
-      ws_url+="rtp=#{options[:resource_type_property]}"
+      ws_url+="qp=#{options[:resource_type_property]}"
     end
 
     ajax_options={
@@ -660,11 +670,18 @@ FUNC
     }
     ajax_options.merge!(options[:select2_ajax_options]) if options[:select2_ajax_options]
 
-    js_options={'minimumInputLength' => options[:minimumInputLength]||3}
+    min_length = 3 # see limitation in /api/resources/search
+    js_options={
+        'minimumInputLength' => min_length,
+        'formatNoMatches' => "function(term){return '#{escape_javascript message('select2.noMatches')}'}",
+        'formatSearching' => "function(){return '#{escape_javascript message('select2.searching')}'}",
+        'formatInputTooShort' => "function(term, minLength){return '#{escape_javascript message('select2.tooShort', :params => [min_length])}'}"
+    }
+    js_options['width']= "'#{width}'" if width
     js_options['ajax']='{' + ajax_options.map{|k,v| "#{k}:#{v}"}.join(',') + '}'
     js_options.merge!(options[:select2_options]) if options[:select2_options]
 
-    html = "<input type='hidden' id='#{html_id}' name='#{name}' style='width:#{width}'/>"
+    html = "<input type='hidden' id='#{html_id}' name='#{name}'/>"
     js = "$j('##{html_id}').select2({#{js_options.map{|k,v| "#{k}:#{v}"}.join(',')}});"
 
     resource = options[:selected_resource]
@@ -675,4 +692,39 @@ FUNC
     "#{html}<script>#{js}</script>"
   end
 
+
+  #
+  # Creates an enhanced dropdown selection box of metrics.
+  # ==== Options
+  # * <tt>:selected_key</tt> - the key of the metric that is selected by default.
+  # * <tt>:multiple</tt> - If set to true the selection will allow multiple choices.
+  # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
+  # * <tt>:allow_empty</tt> - If set to true, selecting a value is not mandatory
+  # * <tt>:width</tt> - The width suffixed with unit, for example '300px' or '100%'. Default is '250px'
+  # * <tt>:html_id</tt> - The id of the HTML element. Default is the name.
+  #
+  def metric_select_tag(name, metrics, options={})
+    width=options[:width]||'250px'
+    html_id=options[:html_id]||name
+    js_options={
+      'formatNoMatches' => "function(term){return '#{escape_javascript message('select2.noMatches')}'}",
+      'width' => "'#{width}'"
+    }
+
+    select_tag_prompt=nil
+    if options[:allow_empty]
+      # add a cross icon to the select2 box
+      js_options['placeholder']="''"
+      js_options['allowClear']=true
+
+      # add a select <option> with empty value
+      select_tag_prompt=''
+    end
+
+    metrics_by_domain=metrics.sort_by(&:short_name).inject({}) { |h, metric| h[metric.domain]||=[]; h[metric.domain]<<[metric.short_name, metric.key]; h }
+
+    html = select_tag(name, grouped_options_for_select(metrics_by_domain, options[:selected_key], select_tag_prompt), :multiple => options[:multiple], :disabled => options[:disabled])
+    js = "$j('##{html_id}').select2({#{js_options.map{|k,v| "#{k}:#{v}"}.join(',')}});"
+    "#{html}<script>#{js}</script>"
+  end
 end
index 9e4eb3e29aeb15f82944b95d6ce92a4e1fab7ad2..a032076f2cc41dcfe2eae971716edf7f4a595260 100644 (file)
@@ -39,7 +39,7 @@ module PropertiesHelper
       text_field_tag key, value, {:size => 10}.update(options)
 
     elsif type==PropertyType::TYPE_METRIC
-      select_tag key, options_grouped_by_domain(Metric.all.select{|m| m.display?}.sort_by(&:short_name), value, :include_empty => true), options
+      metric_select_tag key, Metric.all.select{|m| m.display?}, :selected_key => value, :allow_empty => true
 
     elsif type==PropertyType::TYPE_REGULAR_EXPRESSION
       text_field_tag key, value, {:size => 25}.update(options)
index 60b6fe9c99f3d0857a7618cbcda89edc2c13963f..cdc03e0d8925bc797a633ebb03079d1079bb22bc 100644 (file)
       <tr>
         <td class="form-key-cell"><%= message('widget.resource_id') %> *</td>
         <td class="form-val-cell" id="row_resource">
-          <%= resource_dropdown('resource_id', {:resource_type_property => 'supportsGlobalDashboards', :selected_resource => widget.resource, :width => '250px', :html_id => "sel-prj-#{widget.id}"}) -%>
+          <%= resource_select_tag 'resource_id', {
+              :resource_type_property => 'supportsGlobalDashboards',
+              :selected_resource => widget.resource,
+              :width => '250px',
+              :html_id => "sel-prj-#{widget.id}"} -%>
         </td>
       </tr>
     <% end %>