From 5ffeeda268af4ba12641a9801c8f43fdd94b6b33 Mon Sep 17 00:00:00 2001
From: Mateus Rambo Strey <mars11@inf.ufpr.br>
Date: Tue, 17 Nov 2015 08:09:21 -0200
Subject: [PATCH] partial support to collection manipulation

---
 .../application/collections.coffee            | 84 ++++++++++++++++++-
 .../stylesheets/application/collections.scss  |  6 ++
 .../application/learning_objects.scss         |  8 ++
 app/controllers/collections_controller.rb     | 56 ++++++++++++-
 app/views/collections/show.html.erb           | 26 +++---
 .../_learning_object_vertical.erb             |  2 +-
 config/routes.rb                              |  8 ++
 7 files changed, 169 insertions(+), 21 deletions(-)

diff --git a/app/assets/javascripts/application/collections.coffee b/app/assets/javascripts/application/collections.coffee
index 21be34e5..682a2f97 100644
--- a/app/assets/javascripts/application/collections.coffee
+++ b/app/assets/javascripts/application/collections.coffee
@@ -1,13 +1,13 @@
 $ ->
+  # create collection popover
   $('#create_collection_popover').popover
     html: true
     content: ->
       $('#create_collection_popover_content').html()
     title: ->
       $('#create_collection_popover_title').html()
-  return
 
-$ ->
+  # add learning object to collection
   $(document).on 'click', 'input[class=collection-element]', ->
     url = '/collections/' + encodeURIComponent($(this).data('cid')) + '/learning_object/' + encodeURIComponent($(this).data('loid'))
     if this.checked
@@ -16,12 +16,88 @@ $ ->
     else
       $.ajax {method: "DELETE", url: url }, (d) ->
         d
-  return
 
-$ ->
+  # change collection privacy
   $(document).on 'click', 'input[name=privacy]', ->
     url = '/collections/' + encodeURIComponent($(this).data('cid')) + '/change_privacy'
     value = $('input[name=privacy]:checked').val()
     $.post url, {'privacy':value}, (d) ->
       d
+
+  $(document).on 'open_collections_modal', (evt, params) ->
+    url = '/collections/' + encodeURIComponent(params) + '/list'
+    $.get url, (d) ->
+      $('#collections-modal').remove()
+      $('body').append d
+      $('#collections-modal').modal('show')
+      return
   return
+
+# manipulate collections in show page
+$ ->
+  $(document).on 'ready page:load', ->
+    if $('.collection-show-page').val() != undefined
+      # array with selected collections ids
+      selected_collections = []
+      collection = $('.collection-show-page').data('cid')
+
+      # add selectors
+      $('.learning-object-vertical').each (e) ->
+        loid = $(this).data('loid')
+        $('.learning-object-thumbnail', this).append '<input class="collection-selector" type="checkbox" value="' + loid + '"></input>'
+        return
+
+      # add/remove collection to array when click checkbox
+      $(document).on 'click', '.collection-selector', ->
+        if this.checked
+          if selected_collections.indexOf(this.value) < 0
+            selected_collections.push this.value
+            $(document).trigger('check_selected_collection');
+          return
+        else
+          index = selected_collections.indexOf(this.value)
+          if !!(~index)
+            selected_collections.splice(index, 1)
+            $(document).trigger('check_selected_collection');
+          return
+
+      # clear selected collections
+      $(document).on 'clear_collections', ->
+        selected_collections = []
+        $('.collection-selector').attr('checked', false);
+        $(document).trigger('check_selected_collection')
+
+      $(document).on 'click', '.collection-show-select-nav .navbar-brand', ->
+        $(document).trigger('clear_collections')
+
+      # manipulation buttons
+      $(document).on 'click', '.collection-button', ->
+        switch $(this).data('action')
+          when 'download' then
+          when 'copy' then $(document).trigger('open_collections_modal', [collection])
+          when 'move' then
+          when 'remove' then
+
+      # update interface when add/remove a collection
+      $(document).on 'check_selected_collection', (e) ->
+        length = selected_collections.length
+        if length == 0
+          return $('.collection-show-select-nav').slideUp('slow')
+        else if (length == 1)
+          html = "1 objeto selecionado"
+        else
+          html = length + " objetos selecionados"
+        $('.collection-show-select-nav .navbar-brand').html html
+        $('.collection-show-select-nav').slideDown('slow')
+
+      $(document).on 'submit', '#collections-modal-form', (evt) ->
+        evt.preventDefault()
+        url = '/collections/' + encodeURIComponent(collection) + '/copy'
+        collections = $(this).serializeArray()
+        $.ajax {method: "POST", url: url, data: {col: collections} }, (d) ->
+          console.log d
+          return
+        console.log collections
+        $('#collections-modal').modal('close')
+        return
+      return
diff --git a/app/assets/stylesheets/application/collections.scss b/app/assets/stylesheets/application/collections.scss
index 3a380078..50eb4a46 100644
--- a/app/assets/stylesheets/application/collections.scss
+++ b/app/assets/stylesheets/application/collections.scss
@@ -10,3 +10,9 @@ ul.collection-header {
   margin-top: 20px;
   margin-right: 30px;
 }
+
+.collection-show-select-nav {
+  .navbar-brand {
+    color: #FFF !important;
+  }
+}
diff --git a/app/assets/stylesheets/application/learning_objects.scss b/app/assets/stylesheets/application/learning_objects.scss
index 3d79699d..2089a8e3 100644
--- a/app/assets/stylesheets/application/learning_objects.scss
+++ b/app/assets/stylesheets/application/learning_objects.scss
@@ -85,6 +85,14 @@ $checked_icon: 'icons/checked.png';
     .learning-object-actions {
       bottom: 20px !important;
     }
+
+    .collection-selector {
+      position: absolute;
+      left: 10px;
+      top: 6px;
+      width: 28px;
+      height: 28px;
+    }
   }
 
 }
diff --git a/app/controllers/collections_controller.rb b/app/controllers/collections_controller.rb
index 1c4a9bd6..d36cf9dc 100644
--- a/app/controllers/collections_controller.rb
+++ b/app/controllers/collections_controller.rb
@@ -1,12 +1,12 @@
 class CollectionsController < ApplicationController
-  before_action :set_collection, only: [:show, :update, :destroy, :like, :add_learning_object, :remove_learning_object, :change_privacy]
-  before_action :authenticate_user!, only: [:update, :destroy, :like, :add_learning_object, :remove_learning_object, :change_privacy]
+  before_action :set_collection, only: [:show, :update, :destroy, :like, :list, :add_learning_object, :remove_learning_object, :move_learning_objects, :copy_learning_objects, :change_privacy]
+  before_action :authenticate_user!, only: [:update, :destroy, :like, :list, :add_learning_object, :remove_learning_object, :move_learning_objects, :copy_learning_objects, :change_privacy]
+  before_action :set_manipulation_params, only: [:move_learning_objects, :copy_learning_objects]
 
   # GET /collections
   # GET /collections.json
   def index
     respond_to do |format|
-
       # for HTML page, returns institutional collections
       format.html do
         context = Collections::InstitutionsContext.new(institution_repository.all)
@@ -74,6 +74,14 @@ class CollectionsController < ApplicationController
     end
   end
 
+  def list
+    @learning_object = learning_object_repository.find params[:learning_object_id] unless params[:learning_object_id].blank?
+
+    @collections = collection_repository.all Collections::UserContext.new(current_user)
+
+    render layout: false
+  end
+
   # POST /collections/1/learning_object/43
   def add_learning_object
     learning_object = learning_object_repository.find params[:learning_object_id]
@@ -95,6 +103,30 @@ class CollectionsController < ApplicationController
     end
   end
 
+  def copy_learning_objects
+    response = learning_objects_to_collection(@learning_objects, @target_collection)
+
+    if request.xhr?
+      render json: {status: response}
+    end
+  end
+
+  def move_learning_objects
+    response = learning_objects_to_collection(@learning_objects, @target_collection)
+
+    if response
+      @learning_objects.each do |learning_object|
+        @collection.remove learning_object
+      end
+
+      response = collection_repository.save_learning_objects(@target_collection)
+    end
+
+    if request.xhr?
+      render json: {status: response}
+    end
+  end
+
   # change collection privacy
   def change_privacy
     collection_repository.change_privacy(@collection, params[:privacy])
@@ -109,6 +141,16 @@ class CollectionsController < ApplicationController
   def authorize_create_collection!
   end
 
+  def learning_objects_to_collection(learning_objects, collection)
+    if collection.owner?
+      learning_objects.each do |learning_object|
+        collection.add learning_object
+      end
+
+      return collection_repository.save_learning_objects(@target_collection)
+    end
+  end
+
   def check_collection_privacy!(collection)
     if collection.privacy == "private"
       redirect_to :root, notice: 'Está é uma coleção privada.' unless collection.owner?(current_user)
@@ -119,6 +161,14 @@ class CollectionsController < ApplicationController
     @collection = collection_repository.find params[:id]
   end
 
+  def set_manipulation_params
+    p "AQUI"
+    p JSON.stringify(params)
+    @target_collection = collection_repository.find params[:collection_id] unless params[:collection_id].empty?
+
+    @learning_objects = params[:learning_objects].map{|id| learning_object_repository.find id || false} unless params[:learning_objects].empty?
+  end
+
   # Never trust parameters from the scary internet, only allow the white list through.
   def collection_params
     params.require(:collection).permit(:name, :description, :owner,learning_objects: [])
diff --git a/app/views/collections/show.html.erb b/app/views/collections/show.html.erb
index 4acaf646..17b645d8 100644
--- a/app/views/collections/show.html.erb
+++ b/app/views/collections/show.html.erb
@@ -1,5 +1,5 @@
-<nav class="navigation navbar-default "><br/>
-
+<nav class="navigation navbar-default collection-show-page" data-cid="<%= @collection.id %>">
+  <br/>
   <div class="container-fluid">
     <div class="navbar-right" style="text-align:right">
       <% if @own && @collection.class != Bookmarks %>
@@ -32,31 +32,31 @@
   </div>
   <br/>
 </nav>
-<!--<nav class="navigation navbar-inverse ">
+<nav class="navigation navbar-inverse collection-show-select-nav" style="display:none;">
   <div class="container-fluid">
-    <a class="navbar-brand" href="#"><span style="color: white">x arquivo(s) selecionado(s)</span></a>
+    <a class="navbar-brand" href="javascript:void(0);" data-toggle="tooltip" data-placement="bottom" title="Limpar seleção"></a>
     <ul class="nav navbar-nav navbar-right">
-      <li class="set-align">
-        <a href="#"><span class="left-edge1"><%= image_tag image_path("icons/Download_01.png"), class: "logo-image", size: "35x35" %></span><span style="color: white">
+      <li class="set-align collection-button" data-action="download">
+        <a href="javascript:void(0);"><span class="left-edge1"><%= image_tag image_path("icons/Download_01.png"), class: "logo-image", size: "35x35" %></span><span style="color: white">
           Salvar no <br>computador</span></a>
       </li>
-      <li class="set-align">
-        <a href="#"><span class="left-edge1"><%= image_tag image_path("icons/Copiar_Seleção.png"), class: "logo-image", size: "35x35" %></span><span style="color: white">
+      <li class="set-align collection-button" data-action="copy">
+        <a href="javascript:void(0);"><span class="left-edge1"><%= image_tag image_path("icons/Copiar_Seleção.png"), class: "logo-image", size: "35x35" %></span><span style="color: white">
           Copiar <br>para</span></a>
       </li>
       <% if @own %>
-        <li class="set-align">
-          <a href="#"><span class="left-edge1"><%= image_tag image_path("icons/Mover_Seleção.png"), class: "logo-image", size: "35x35" %></span><span style="color: white">
+        <li class="set-align collection-button" data-action="move">
+          <a href="javascript:void(0);"><span class="left-edge1"><%= image_tag image_path("icons/Mover_Seleção.png"), class: "logo-image", size: "35x35" %></span><span style="color: white">
             Mover <br>para</span></a>
         </li>
-        <li class="set-align">
-          <a href="#"><span class="left-edge1"><%= image_tag image_path("icons/Remover_da_Coleção_Seleção.png"), class: "logo-image", size: "35x35" %></span><span style="color: white">
+        <li class="set-align collection-button" data-action="remove">
+          <a href="javascript:void(0);"><span class="left-edge1"><%= image_tag image_path("icons/Remover_da_Coleção_Seleção.png"), class: "logo-image", size: "35x35" %></span><span style="color: white">
             Remover da <br>coleção</span></a>
         </li>
       <% end %>
     </ul>
   </div>
-</nav>-->
+</nav>
 
 <div class="row learning-object-columns">
   <br/>
diff --git a/app/views/learning_objects/_learning_object_vertical.erb b/app/views/learning_objects/_learning_object_vertical.erb
index 3fbb3521..50156e91 100644
--- a/app/views/learning_objects/_learning_object_vertical.erb
+++ b/app/views/learning_objects/_learning_object_vertical.erb
@@ -1,4 +1,4 @@
-<div class="learning-object-vertical">
+<div class="learning-object-vertical" data-loid="<%= learning_object.id %>">
   <div class="panel">
     <div class="learning-object-thumbnail">
       <%= link_to learning_object_path(id: learning_object.id) do
diff --git a/config/routes.rb b/config/routes.rb
index 8f9fe1a3..44d23cbf 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -67,13 +67,21 @@ Rails.application.routes.draw do
 
   resources :collections do
     member do
+      # collection list
+      get :list
+
       # add a learning object for some collection
       post '/learning_object/:learning_object_id', as: :add_learning_object, action: :add_learning_object
 
       # remove a learning object for some collection
       delete '/learning_object/:learning_object_id', as: :destroy_learning_object, action: :remove_learning_object
 
+      # change privacy
       post :change_privacy
+
+      # copy / move learning_objects
+      get '/copy', as: :copy_learning_objects, action: :copy_learning_objects
+      get '/move', as: :move_learning_objects, action: :move_learning_objects
     end
   end
 
-- 
GitLab