diff --git a/app/assets/javascripts/application/collections.coffee b/app/assets/javascripts/application/collections.coffee index 682a2f9738788679eeba61af6a293331f381e937..189048249fdb3189bc767b300c9748dde3bc9933 100644 --- a/app/assets/javascripts/application/collections.coffee +++ b/app/assets/javascripts/application/collections.coffee @@ -8,14 +8,14 @@ $ -> $('#create_collection_popover_title').html() # 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 - $.ajax {method: "POST", url: url }, (d) -> - d - else - $.ajax {method: "DELETE", url: url }, (d) -> - d + # $(document).on 'click', 'input[class=collection-element]', -> + # url = '/collections/' + encodeURIComponent($(this).data('cid')) + '/learning_object/' + encodeURIComponent($(this).data('loid')) + # if this.checked + # $.ajax {method: "POST", url: url }, (d) -> + # d + # else + # $.ajax {method: "DELETE", url: url }, (d) -> + # d # change collection privacy $(document).on 'click', 'input[name=privacy]', -> @@ -25,21 +25,34 @@ $ -> d $(document).on 'open_collections_modal', (evt, params) -> - url = '/collections/' + encodeURIComponent(params) + '/list' + url = '/collections/' + encodeURIComponent(params.collection) + '/list?type=' + params.type $.get url, (d) -> $('#collections-modal').remove() $('body').append d $('#collections-modal').modal('show') return - return + + mount_modal_params = (e) -> + array = $(e).serializeArray() + collections = [] + type = "" + i = 0 + length = array.length + while i < length + if array[i].name == "collection" + collections.push(array[i].value) + else if array[i].name == "type" + type = array[i].value + ++i + return { collections_ids: collections, type: type } # manipulate collections in show page -$ -> $(document).on 'ready page:load', -> if $('.collection-show-page').val() != undefined - # array with selected collections ids - selected_collections = [] + # array with selected objects ids + selected_objects = [] collection = $('.collection-show-page').data('cid') + permitted_types = ['download', 'copy', 'move', 'delete'] # add selectors $('.learning-object-vertical').each (e) -> @@ -47,23 +60,23 @@ $ -> $('.learning-object-thumbnail', this).append '<input class="collection-selector" type="checkbox" value="' + loid + '"></input>' return - # add/remove collection to array when click checkbox + # add/remove object 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 + if selected_objects.indexOf(this.value) < 0 + selected_objects.push this.value $(document).trigger('check_selected_collection'); return else - index = selected_collections.indexOf(this.value) + index = selected_objects.indexOf(this.value) if !!(~index) - selected_collections.splice(index, 1) + selected_objects.splice(index, 1) $(document).trigger('check_selected_collection'); return - # clear selected collections + # clear selected objects $(document).on 'clear_collections', -> - selected_collections = [] + selected_objects = [] $('.collection-selector').attr('checked', false); $(document).trigger('check_selected_collection') @@ -72,15 +85,12 @@ $ -> # 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 + index = permitted_types.indexOf($(this).data('action')) + $(document).trigger('open_collections_modal', [collection: collection, type: permitted_types[index]]) if !!(~index) - # update interface when add/remove a collection + # update interface when add/remove an object $(document).on 'check_selected_collection', (e) -> - length = selected_collections.length + length = selected_objects.length if length == 0 return $('.collection-show-select-nav').slideUp('slow') else if (length == 1) @@ -90,14 +100,18 @@ $ -> $('.collection-show-select-nav .navbar-brand').html html $('.collection-show-select-nav').slideDown('slow') - $(document).on 'submit', '#collections-modal-form', (evt) -> + $(document).on 'submit', '#collections-modal-form', (evt, params) -> 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') + + data = mount_modal_params(this) + + return false if (selected_objects.length < 1) || (data.collections_ids.length < 1) || (permitted_types.indexOf(data.type) < 0) + + url = '/collections/' + encodeURIComponent(collection) + '/' + data.type + data.id = collection + data.learning_objects_ids = selected_objects + + $.ajax {method: "POST", url: url, data: data } + $('#collections-modal').modal('hide') return return diff --git a/app/controllers/collections_controller.rb b/app/controllers/collections_controller.rb index d36cf9dcfb3a829302a4772f96d0008a1479c6f6..6c5c64cc76f83176f0d30f6fd956915ab046fc56 100644 --- a/app/controllers/collections_controller.rb +++ b/app/controllers/collections_controller.rb @@ -75,9 +75,11 @@ class CollectionsController < ApplicationController end def list + @collections = collection_repository.all(Collections::UserContext.new(current_user)).select{|c| c.id != @collection.id} + @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) + @type = params[:type] unless params[:type].blank? render layout: false end @@ -104,7 +106,7 @@ class CollectionsController < ApplicationController end def copy_learning_objects - response = learning_objects_to_collection(@learning_objects, @target_collection) + response = learning_objects_to_collections(@learning_objects, @target_collections) if request.xhr? render json: {status: response} @@ -112,14 +114,14 @@ class CollectionsController < ApplicationController end def move_learning_objects - response = learning_objects_to_collection(@learning_objects, @target_collection) + response = learning_objects_to_collections(@learning_objects, @target_collections) if response @learning_objects.each do |learning_object| @collection.remove learning_object end - response = collection_repository.save_learning_objects(@target_collection) + response = collection_repository.save_learning_objects(@collection) end if request.xhr? @@ -141,13 +143,15 @@ 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 + def learning_objects_to_collections(learning_objects, collections) + collections.each do |collection| + if collection.owner?(current_user) + learning_objects.each do |learning_object| + collection.add learning_object + end - return collection_repository.save_learning_objects(@target_collection) + collection_repository.save_learning_objects(collection) + end end end @@ -162,11 +166,9 @@ class CollectionsController < ApplicationController end def set_manipulation_params - p "AQUI" - p JSON.stringify(params) - @target_collection = collection_repository.find params[:collection_id] unless params[:collection_id].empty? + @target_collections = params[:collections_ids].map{|id| collection_repository.find id} unless params[:collections_ids].empty? - @learning_objects = params[:learning_objects].map{|id| learning_object_repository.find id || false} unless params[:learning_objects].empty? + @learning_objects = params[:learning_objects_ids].map{|id| learning_object_repository.find id || false} unless params[:learning_objects_ids].empty? end # Never trust parameters from the scary internet, only allow the white list through. diff --git a/app/views/collections/list.html.erb b/app/views/collections/list.html.erb index 15f03b62ff298d2bf2242d86fd582c4143f78710..be112f5c9c0acd13d01ff07f6081290cce113600 100644 --- a/app/views/collections/list.html.erb +++ b/app/views/collections/list.html.erb @@ -7,13 +7,13 @@ </div> <form id="collections-modal-form"> <div class="modal-body"> + <%= hidden_field_tag 'type', @type unless @type.blank? %> <% if @learning_object.blank? %> <% @collections.each do |collection| %> - <% unless collection.id == @collection.id %> - <input type="checkbox" class="collection-element" id="<%= collection.id %>" name="collections" value="<%= collection.id %>"> - <label for="<%= collection.id %>"><%= collection.name %></label><br/> - <% end %> + <input type="checkbox" class="collection-element" id="<%= collection.id %>" name="collection" value="<%= collection.id %>"> + <label for="<%= collection.id %>"><%= collection.name %></label><br/> <% end %> + <%= 'Nenhuma coleção disponÃvel. Crie novas no seu perfil de usuário.' if @collections.blank? %> <% end %> </div> <div class="modal-footer"> diff --git a/config/routes.rb b/config/routes.rb index 44d23cbf108917c170fbfb9c69607449ede56f05..c032d4ba1c199cd0cacdeff5d50902b48144cf2f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -80,8 +80,8 @@ Rails.application.routes.draw do 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 + post '/copy', as: :copy_learning_objects, action: :copy_learning_objects + post '/move', as: :move_learning_objects, action: :move_learning_objects end end