]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3992 Replace prototype ajax by jquery ajax on Quality Profile alert page
authorJulien Lancelot <julien.lancelot@gmail.com>
Fri, 11 Jan 2013 11:33:35 +0000 (12:33 +0100)
committerJulien Lancelot <julien.lancelot@gmail.com>
Fri, 11 Jan 2013 11:33:35 +0000 (12:33 +0100)
sonar-server/src/main/webapp/WEB-INF/app/controllers/alerts_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/views/alerts/_edit.html.erb
sonar-server/src/main/webapp/WEB-INF/app/views/alerts/_new.html.erb
sonar-server/src/main/webapp/WEB-INF/app/views/alerts/index.html.erb

index 457fcd2611ed63b03f24125a633489ac373d554a..a2518af4c29590ca04bcd78a335dd3775179706c 100644 (file)
@@ -21,6 +21,7 @@ class AlertsController < ApplicationController
 
   verify :method => :post, :only => ['create', 'update', 'delete'], :redirect_to => { :action => 'index' }
   before_filter :admin_required, :except => [ 'index' ]
+  before_filter :load_new_metrics_id, :only => [:index, :create, :update]
 
   SECTION=Navigation::SECTION_CONFIGURATION
 
@@ -35,13 +36,6 @@ class AlertsController < ApplicationController
     @alerts = @profile.alerts.sort
     @alert=Alert.new
 
-    @newMetricsId = []
-    Metric.domains.collect do |domain|
-      Metric.by_domain(domain).select{ |m| m.alertable? }.each do |metric|
-        @newMetricsId << metric.id if metric.name.index("new_") == 0
-      end
-    end
-
     add_breadcrumbs ProfilesController::ROOT_BREADCRUMB, Api::Utils.language_name(@profile.language), {:name => @profile.name, :url => {:controller => 'rules_configuration', :action => 'index', :id => @profile.id}}
   end
 
@@ -74,24 +68,15 @@ class AlertsController < ApplicationController
     @profile = Profile.find(params[:profile_id])
     params[:alert][:period] = nil if params[:alert][:period] == '0'
     @alert = @profile.alerts.build(params[:alert])
-    
-    respond_to do |format|
-      if @alert.save
-        flash[:notice] = message('alerts.alert_created')
-        format.html { redirect_to :action => 'index', :id=>@profile.id }
-        format.js { render :update do |page|
-          page.redirect_to :action => 'index', :id=>@profile.id
-        end}
-      else
-
-        @alerts = @profile.alerts.reload
-        format.html { render :action => "index" }
-        format.xml  { render :xml => @alert.errors, :status => :unprocessable_entity }
-        format.js { render :update do |page|
-          page.replace_html( 'new_alert_form', :partial => 'new')
-          
-        end}
-      end
+
+    if @alert.save
+      flash[:notice] = message('alerts.alert_created')
+      render :text => 'ok', :status => 200
+    else
+      @alerts = @profile.alerts.reload
+      errors = []
+      @alert.errors.full_messages.each{|msg| errors<<msg + '<br/>'}
+      render :text => errors, :status => 404
     end
   end
 
@@ -106,20 +91,13 @@ class AlertsController < ApplicationController
     @alerts=@profile.alerts
     alert = @alerts.find(params[:id])
 
-    respond_to do |format|
-      if alert.update_attributes(params[:alert])
-        flash[:notice] = message('alerts.alert_updated')
-        format.html { redirect_to :action => 'index', :id=>@profile.id }
-        format.xml  { head :ok }
-        format.js { render :update do |page| page.redirect_to :action => 'index', :id=>@profile.id end}
-      else
-        @alert=Alert.new
-        format.html { render :action => "index" }
-        format.xml  { render :xml => @alert.errors, :status => :unprocessable_entity }
-        format.js { render :update do |page| 
-          page.replace_html( "row_alert_#{alert.id}", :partial => 'edit', :locals => {:alert => alert})
-        end}
-      end
+    if alert.update_attributes(params[:alert])
+      flash[:notice] = message('alerts.alert_updated')
+      render :text => 'ok', :status => 200
+    else
+      errors = []
+      alert.errors.full_messages.each{|msg| errors<<msg + '<br/>'}
+      render :text => errors, :status => 404
     end
   end
 
@@ -133,10 +111,17 @@ class AlertsController < ApplicationController
     @alert = @profile.alerts.find(params[:id])
     @alert.destroy
     flash[:notice] = message('alerts.alert_deleted')
+    redirect_to(:action => 'index', :id=>@profile.id)
+  end
 
-    respond_to do |format|
-      format.html { redirect_to(:action => 'index', :id=>@profile.id) }
-      format.xml  { head :ok }
+  private
+
+  def load_new_metrics_id
+    @newMetricsId = []
+    Metric.domains.collect do |domain|
+      Metric.by_domain(domain).select{ |m| m.alertable? }.each do |metric|
+        @newMetricsId << metric.id if metric.name.index("new_") == 0
+      end
     end
   end
 
index 481bc8e6ddae0ed1ed40724fcb4cdb6f238c8700..535b5acae10bcd6047c0333cd05e85f3286c6500 100644 (file)
@@ -1,4 +1,4 @@
-<%= form_remote_tag :url => {:action => 'update', :id => alert.id, :profile_id => @profile.id}, :html => {:id => "edit_alert_#{alert.id}"} %>
+<%= form_tag({:action => 'update', :id => alert.id, :profile_id => @profile.id}, :id => "edit_alert_#{alert.id}") %>
 <table>
 <tr>
   <td nowrap>
   </td>
 </tr>
 </table>
-<% unless alert.errors.empty? %>
-<div class="error" style="margin: 5px;">
-   <ul>
-     <% alert.errors.full_messages.each do |message| %>
-     <li><%= h message %></li>
-     <% end %>
-   </ul>
-</div>
-<% end %>
+<div id="alert_error_<%= alert.id -%>" class="error" style="margin: 5px;" />
 </form>
 
 <script type="text/javascript">
+  $j("#alert_error_<%= alert.id -%>").hide();
+  $j(document).ready(function () {
+    ajaxForm("edit_alert_<%= alert.id -%>", "alert_error_<%= alert.id -%>");
+  });
+
   $j('#alert_period_<%= alert.id %>').select2({
     allowClear: false,
     minimumResultsForSearch: 10,
index 0f520124b4c9c12bed6d9539c4adb5f717c7ca7d..a1b34dd5ba7d57165a8cf33008662db7f820bcd7 100644 (file)
@@ -1,5 +1,5 @@
 <h3><%= message('alerts.create_alert') -%></h3>
-<%= form_remote_tag :url => {:action => 'create', :profile_id => @profile.id}, :html => {:id => 'new_alert_form'} %>
+<%= form_tag({:action => 'create', :profile_id => @profile.id}, :id => 'new_alert_form') %>
 <table class="spaced">
 <tr>
   <td valign="top">
   </td>
 </tr>
 </table>
-<% unless @alert.errors.empty? %>
-<div class="error" style="margin: 5px;">
-  <ul>
-     <% @alert.errors.full_messages.each do |message| %>
-     <li><%= h message %></li>
-     <% end %>
-  </ul>
-</div>
-<% end %>
+<div id="alert_error" class="error" style="margin: 5px;"/>
 </form>
 
 <script type="text/javascript">
-  //<![CDATA[
+
+  $j("#alert_error").hide();
+  $j(document).ready(function () {
+    ajaxForm("new_alert_form", "alert_error");
+  });
+
   $j('#alert_metric_id').select2({
     allowClear: false,
     width: '250px',
@@ -68,7 +65,7 @@
 
   function htmlDecode(value) {
     if (value) {
-      return $j('<div />').html(value).text();
+      return $j('<div/>').html(value).text();
     } else {
       return '';
     }
     });
   });
 
-
-  //]]>
 </script>
 
 
index c672968789d8f44febe00e35eb2c8869cf506c39..4b5d583475c65195220304e7d13b988212629965 100644 (file)
@@ -1,7 +1,7 @@
 <%= render :partial => 'profiles/tabs', :locals => {:selected_tab=>'Alerts'} %>
 
 <% if is_admin? %>
-  <div class="tabs-panel marginbottom10 admin" id="new_alert_form">
+  <div class="tabs-panel marginbottom10 admin" id="new_alert_content">
     <%= render :partial => 'new' %>
   </div>
 <% else %>
 <tr class="even"><td><%= image_tag 'levels/error.png' %></td><td> <%= message('alerts.notes.error') -%></td></tr>
 </tbody>
 </table>
-</div>
\ No newline at end of file
+</div>
+
+<script type="text/javascript">
+  function ajaxForm(formId, errorId){
+    var obj = $j("#"+ formId);
+    obj.submit(function (event) {
+      $j.ajax({
+        type: 'POST',
+        url: obj.attr('action'),
+        data: obj.serialize(),
+        success: function (data) {
+          window.location.reload();
+        },
+        error: function (xhr, textStatus, errorThrown) {
+          $j("#"+ errorId).show();
+          $j("#"+ errorId).html(xhr.responseText);
+        }
+      });
+      return false;
+    });
+  }
+</script>
\ No newline at end of file