]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3263 add JS API for the forms opened in modal windows
authorSimon Brandhof <simon.brandhof@gmail.com>
Sun, 23 Sep 2012 12:36:37 +0000 (14:36 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Sun, 23 Sep 2012 12:36:37 +0000 (14:36 +0200)
sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb
sonar-server/src/main/webapp/WEB-INF/app/views/confirm/_confirm.html.erb
sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_copy_form.html.erb [new file with mode: 0644]
sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_rename_form.html.erb
sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb
sonar-server/src/main/webapp/javascripts/application.js

index 8cde294ce385fdea02b324e1864c540c33fa8f09..2e41cc0eab81ec610863985cbe664e514bbcbe34 100644 (file)
@@ -113,6 +113,10 @@ class ProfilesController < ApplicationController
   end
 
 
+  def copy_form
+    @profile = Profile.find(params[:id])
+    render :partial => 'profiles/copy_form'
+  end
 
   #
   #
@@ -120,18 +124,20 @@ class ProfilesController < ApplicationController
   #
   #
   def copy
-    profile = Profile.find(params[:id])
-    name = params['copy_' + profile.id.to_s]
+    render :text => 'Not an ajax request', :status => '400' unless request.xhr?
 
-    validation_errors = profile.validate_copy(name)
+    @profile = Profile.find(params[:id])
+    name = params['name']
+
+    validation_errors = @profile.validate_copy(name)
     if validation_errors.empty?
-      java_facade.copyProfile(profile.id, name)
+      java_facade.copyProfile(@profile.id, name)
       flash[:notice]= message('quality_profiles.profile_x_not_activated', :params => name)
+      render :text => 'ok', :status => 200
     else
-      flash[:error] = validation_errors.full_messages.first
+      @error = validation_errors.full_messages.first
+      render :partial => 'profiles/copy_form', :status => 400
     end
-
-    redirect_to :action => 'index'
   end
 
 
index dce4ccbea678ead98c53916e1294d89f2c8fad43..ad2aa501ad8f6fe3db534586f766217e561642a2 100644 (file)
@@ -739,7 +739,7 @@ module ApplicationHelper
     url = "#{ApplicationController.root_context}/confirm?url=#{u post_url}"
     if message_key
       url += "&k=#{message_key}&"
-      url += message_params.map{|p| "p=#{p}"}.join('&') if message_params
+      url += message_params.map{|p| "p=#{p}"}.join('&') if message_params
     end
 
     "<a href='#{url}' modal-width='#{width}' class='open-modal button #{clazz}' #{id}>#{label}</a>"
index 241ce2d0d4ff3272d3ea3f28ce7fdb21fbbd61d2..dceb6db2f4a7785e1a56bf9960f4c268e0a75f5d 100644 (file)
@@ -6,7 +6,7 @@
   <fieldset>
     <div class="form-head">
       <img src="<%= ApplicationController.root_context -%>/images/warning.png" style="vertical-align: text-bottom"/>
-      <%= message(message_key, :params => message_params) -%>
+      <%= message(message_key, :params => message_params) -%>
     </div>
     <div class="form-foot">
       <input type="submit" value="Confirm"/>
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_copy_form.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_copy_form.html.erb
new file mode 100644 (file)
index 0000000..13c26d3
--- /dev/null
@@ -0,0 +1,27 @@
+<form id="form-copy-profile" method="post" action="profiles/copy">
+  <input type="hidden" name="id" value="<%= @profile.id -%>"/>
+  <fieldset>
+    <div class="form-head">
+      <h2>Copy Profile: <%= h @profile.name -%></h2>
+    </div>
+    <div class="form-body">
+      <% if @error %>
+        <div class="form-field">
+          <p class="error"><%= h @error -%></p>
+        </div>
+      <% end %>
+      <div class="form-field">
+        <label for="name">New name</label>
+        <input id="new-name" name="name" type="text" size="50" maxlength="100"/>
+      </div>
+    </div>
+    <div class="form-foot">
+      <input type="submit" value="Copy"/>
+      <a href="#" onclick="return closeModalWindow()"><%= message('cancel') -%></a>
+    </div>
+  </fieldset>
+</form>
+<script>
+  $j("#form-copy-profile").modalForm();
+  $j('#new-name').focus();
+</script>
\ No newline at end of file
index 4364287eff62d30f9fea5f78a1b2e903990e4e28..6026b876a279809520df9c32a177699d0bd1525b 100644 (file)
   </fieldset>
 </form>
 <script>
+  $j("#form-rename-profile").modalForm();
   $j('#new-name').focus();
-  $j("#form-rename-profile").submit(function (event) {
-    $j('input[type=submit]', this).attr('disabled', 'disabled');
-    $j.ajax({type:"POST", data:$j(this).serialize(), url:$j(this).attr('action'),
-      success:function (data) {
-        location.reload();
-      },
-      error:function (xhr, textStatus, errorThrown) {
-        $j("#modal").html(xhr.responseText);
-      }
-    });
-    return false;
-  });
 </script>
\ No newline at end of file
index f4bd608413af2b54ea21505305025963157b7b5a..fd31c1266d7c148207d69b486e5eec39af22e117 100644 (file)
 
           <td align="right">
             <% if !profile.provided? %>
-              <a id="rename-<%= profile.key.parameterize -%>" href="profiles/rename_form?id=<%= profile.id -%>" class="button open-modal"><%= message('rename') -%></a>
+              <a id="rename-<%= profile.key.parameterize -%>" href="profiles/rename_form/<%= profile.id -%>" class="button open-modal"><%= message('rename') -%></a>
             <% end %>
           </td>
 
           <td align="right">
-            <% form_tag(:action => 'copy', :id => profile.id) do -%>
-              <%= hidden_field_tag 'copy_' + profile.id.to_s %>
-              <input type="button" name="button_copy" id="copy_<%= u profile.key %>" value="<%= message('copy') -%>" onClick='var name=prompt("<%= message('quality_profiles.name_for_new_profile') -%>"); if (name!=null) {$("copy_<%= profile.id %>").value=name; submit();} else {return false;}'>
-            <% end %>
+            <a id="copy-<%= profile.key.parameterize -%>" href="profiles/copy_form/<%= profile.id -%>" class="button open-modal"><%= message('copy') -%></a>
           </td>
 
           <td>
index 50ae86c1269c85378d1e4771dd413b9f713a7517..a4a1ac077faf787e2430ba83e2c0a4cef8234c09 100644 (file)
@@ -215,11 +215,11 @@ Treemap.prototype.load = function () {
   }
 
   new Ajax.Request(
-    baseUrl + '/treemap/index?id=' + this.id + '&width=' + width + '&height=' + height + '&size_metric=' + this.sizeMetric + '&color_metric=' + this.colorMetric + '&' + context.type + '=' + context.id,
-    {
-      asynchronous:true,
-      evalScripts:true
-    });
+      baseUrl + '/treemap/index?id=' + this.id + '&width=' + width + '&height=' + height + '&size_metric=' + this.sizeMetric + '&color_metric=' + this.colorMetric + '&' + context.type + '=' + context.id,
+      {
+        asynchronous:true,
+        evalScripts:true
+      });
 };
 Treemap.prototype.htmlNode = function (nodeId) {
   return $('tm-node-' + this.id + '-' + nodeId);
@@ -272,17 +272,17 @@ Treemap.prototype.onLoaded = function (componentsSize) {
           $j.get($link.attr('href'), {}, function (html) {
             $dialog.html(html);
             $dialog
-              .dialog({
-                width:($link.attr('modal-width')||540),
-                draggable:false,
-                autoOpen:false,
-                modal:true,
-                minHeight: 50,
-                resizable:false,
-                close: function() {
-                  $j('#modal').remove();
-                }
-              });
+                .dialog({
+                  width:($link.attr('modal-width') || 540),
+                  draggable:false,
+                  autoOpen:false,
+                  modal:true,
+                  minHeight:50,
+                  resizable:false,
+                  close:function () {
+                    $j('#modal').remove();
+                  }
+                });
             $dialog.dialog("open");
           });
 
@@ -291,6 +291,26 @@ Treemap.prototype.onLoaded = function (componentsSize) {
             return false;
           });
 
+          return false;
+        });
+      });
+    },
+    modalForm:function () {
+      return this.each(function () {
+        var obj = $j(this);
+        obj.submit(function (event) {
+          $j('input[type=submit]', this).attr('disabled', 'disabled');
+          $j.ajax({
+            type:'POST',
+            url:obj.attr('action'),
+            data:obj.serialize(),
+            success:function (data) {
+              location.reload();
+            },
+            error:function (xhr, textStatus, errorThrown) {
+              $j("#modal").html(xhr.responseText);
+            }
+          });
           return false;
         });
       });