From 5834a8a0a8d69c2080697506e229b26181a68af8 Mon Sep 17 00:00:00 2001
From: Sascha Peilicke <sasch.pe@gmx.de>
Date: Thu, 14 Oct 2010 08:53:27 +0200
Subject: [PATCH] [webui] paths order can now be changed in the project repository configuration.

Allow changing the order of repository paths (to fetch additional RPMs
from) in the webui directly.
---
 src/webui/app/controllers/project_controller.rb    |   22 ++++++--
 src/webui/app/models/project.rb                    |   58 +++++++++++++++++++-
 .../app/views/project/_edit_repository.html.erb    |   14 ++++-
 .../vendor/neutral/images/icons/arrow_down.png     |  Bin 0 -> 379 bytes
 .../vendor/neutral/images/icons/arrow_up.png       |  Bin 0 -> 372 bytes
 5 files changed, 86 insertions(+), 8 deletions(-)
 create mode 100644 src/webui/public/vendor/neutral/images/icons/arrow_down.png
 create mode 100644 src/webui/public/vendor/neutral/images/icons/arrow_up.png

diff --git a/src/webui/app/controllers/project_controller.rb b/src/webui/app/controllers/project_controller.rb
index 96188e9..7f5d109 100644
--- a/src/webui/app/controllers/project_controller.rb
+++ b/src/webui/app/controllers/project_controller.rb
@@ -573,10 +573,24 @@ class ProjectController < ApplicationController
   end
 
   def remove_path_from_target
-   @project.remove_path_from_target( params['repository'], params['path_project'], params['path_repository'] )
-   @project.save
-   redirect_to :action => :repositories, :project => @project
-   return
+    @project.remove_path_from_target( params['repository'], params['path_project'], params['path_repository'] )
+    @project.save
+    redirect_to :action => :repositories, :project => @project
+    return
+  end
+
+  def move_path_up
+    @project.repository[params['repository']].move_path(params['path_project'] + '/' + params['path_repository'], :up)
+    @project.save
+    redirect_to :action => :repositories, :project => @project
+    return
+  end
+
+  def move_path_down
+    @project.repository[params['repository']].move_path(params['path_project'] + '/' + params['path_repository'], :down)
+    @project.save
+    redirect_to :action => :repositories, :project => @project
+    return
   end
 
   def save_person
diff --git a/src/webui/app/models/project.rb b/src/webui/app/models/project.rb
index 1296f85..48a9271 100644
--- a/src/webui/app/models/project.rb
+++ b/src/webui/app/models/project.rb
@@ -45,6 +45,63 @@ class Project < ActiveXML::Base
       set_archs new_archs
     end
 
+    def paths
+      @paths ||= each_path.map { |p| p.project + '/' + p.repository }
+      return @paths
+    end
+
+    def add_path (path)
+      return nil if paths.include? path
+      project, repository = path.split("/")
+      @paths.push path
+      e = add_element('path')
+      e.data.attributes['repository'] = repository
+      e.data.attributes['project'] = project
+    end
+
+    def remove_path (path)
+      return nil unless paths.include? path
+      project, repository = path.split("/")
+      each_path do |p|
+        delete_element p if p.data.attributes['project'] == project and p.data.attributes['repository'] == repository
+      end
+      @paths.delete path
+    end
+
+    def set_paths (new_paths)
+      paths.clone.each{ |path| remove_path path }
+      new_paths.each{ |path| add_path path }
+    end
+    def paths= (new_paths)
+      set_paths new_paths
+    end
+
+    # directions are :up and :down
+    def move_path (path, direction=:up)
+      return nil unless (path and not paths.empty?)
+      new_paths = paths.clone
+      for i in 0..new_paths.length
+        if new_paths[i] == path           # found the path to move?
+          if direction == :up and i != 0  # move up and is not the first?
+            tmp = new_paths[i - 1]
+            new_paths[i - 1] = new_paths[i]
+            new_paths[i] = tmp
+            break
+          elsif direction == :down and i != new_paths.length - 1
+            tmp = new_paths[i + 1]
+            new_paths[i + 1] = new_paths[i]
+            new_paths[i] = tmp
+            break
+          end
+        end
+      end
+      set_paths new_paths
+      # Relax-NG is only happy when <arch> elems come after <path> elems
+      same_archs = archs.clone
+      set_archs []
+      set_archs same_archs
+    end
+
     #    def name= (name)
     #      data.attributes['name'] = name
     #    end
@@ -162,7 +219,6 @@ class Project < ActiveXML::Base
     delete_element "repository[@name='#{repository}']"
   end
 
-
   #get all architectures used in this project
   #TODO could/should be optimized... somehow...here are many possibilities
   #eg. object attribute, ...
diff --git a/src/webui/app/views/project/_edit_repository.html.erb b/src/webui/app/views/project/_edit_repository.html.erb
index dab449a..11f8561 100644
--- a/src/webui/app/views/project/_edit_repository.html.erb
+++ b/src/webui/app/views/project/_edit_repository.html.erb
@@ -23,9 +23,17 @@
             <% if path.project == 'deleted' %>
               <%= image_tag 'icons/exclamation.png' %> Target repository has been removed
             <% else %>
-              <span><%=  h path.project + "/" + path.repository %></span>
-              <%=  link_to(image_tag('icons/drive_delete.png'), {:action => :remove_path_from_target, :project => @project, :repository => repository.name, :path_project => path.project, :path_repository => path.repository},
-                {:confirm => "Really remove #{path.project} / #{path.repository} path from repository  '#{repository.name}'?", :class => 'x', :method => :post}) -%>
+              <span><%= h path.project + "/" + path.repository %></span>
+              <% if repository.each_path.length > 1 %>
+                <% if path.data != repository.each_path.first.data %>
+                    <%= link_to(image_tag('icons/arrow_up.png'), {:action => :move_path_up, :project => @project, :repository => repository.name, :path_project => path.project, :path_repository => path.repository}, {:class => 'x', :method => :post}) -%>
+                <% end %>
+                <% if path.data != repository.each_path.last.data %>
+                    <%= link_to(image_tag('icons/arrow_down.png'), {:action => :move_path_down, :project => @project, :repository => repository.name, :path_project => path.project, :path_repository => path.repository}, {:class => 'x', :method => :post}) -%>
+                <% end %>
+              <% end %>
+              <%= link_to(image_tag('icons/drive_delete.png'), {:action => :remove_path_from_target, :project => @project, :repository => repository.name, :path_project => path.project, :path_repository => path.repository},
+                           {:confirm => "Really remove #{path.project} / #{path.repository} path from repository  '#{repository.name}'?", :class => 'x', :method => :post}) -%>
               <br/>
             <% end %>
           <% end -%>
diff --git a/src/webui/public/vendor/neutral/images/icons/arrow_down.png b/src/webui/public/vendor/neutral/images/icons/arrow_down.png
new file mode 100644
index 0000000000000000000000000000000000000000..2c4e279377bf348f9cf53894e76bb673ccf067bd
GIT binary patch
diff --git a/src/webui/public/vendor/neutral/images/icons/arrow_up.png b/src/webui/public/vendor/neutral/images/icons/arrow_up.png
new file mode 100644
index 0000000000000000000000000000000000000000..1ebb193243780b8eb1919a51ef27c2a0d36ccec2
GIT binary patch
-- 
1.7.1

