diff --git a/app/assets/javascripts/application/collections.coffee b/app/assets/javascripts/application/collections.coffee index 682a2f9738788679eeba61af6a293331f381e937..979ceea3f1b9b6ba34751ba8cafc2866d64232fd 100644 --- a/app/assets/javascripts/application/collections.coffee +++ b/app/assets/javascripts/application/collections.coffee @@ -1,4 +1,7 @@ $ -> + # valid types of operations in collections + permitted_types = ['add', 'download', 'copy', 'move', 'remove'] + # create collection popover $('#create_collection_popover').popover html: true @@ -7,15 +10,9 @@ $ -> title: -> $('#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 + # add/remove learning object to collection + $(document).on 'click', '.add-to-collection', -> + $(document).trigger('open_collections_modal', [learning_object: [$(this).data('loid')], type: 'add']) # change collection privacy $(document).on 'click', 'input[name=privacy]', -> @@ -25,20 +22,62 @@ $ -> d $(document).on 'open_collections_modal', (evt, params) -> - url = '/collections/' + encodeURIComponent(params) + '/list' + url = '/collections/list?type=' + params.type + url += '&id=' + encodeURIComponent(params.collection) if params.collection != undefined + url += '&learning_objects_ids=' + encodeURIComponent(JSON.stringify(params.learning_object)) if params.learning_object != undefined $.get url, (d) -> $('#collections-modal').remove() $('body').append d $('#collections-modal').modal('show') return - return -# manipulate collections in show page -$ -> + mount_modal_params = (e) -> + array = $(e).serializeArray() + object = {} + collection = "" + type = "" + collections = [] + learning_objects = [] + + i = 0 + length = array.length + while i < length + switch array[i].name + when 'collection' then collections.push(array[i].value) + when 'learning_object' then learning_objects.push(array[i].value) + when 'collection_id' then collection = array[i].value + when 'type' then type = array[i].value + ++i + + object.collection_id = collection + object.collections_ids = collections if collections.length > 0 + object.learning_objects_ids = learning_objects if learning_objects.length > 0 + object.type = type + return object + + $(document).on 'submit', '#collections-modal-form', (evt, params) -> + evt.preventDefault() + + refreshable_type = ['move'] + + data = mount_modal_params(this) + + return false if (data.learning_objects_ids.length < 1) || (data.collections_ids.length < 1) || (permitted_types.indexOf(data.type) < 0) + + id = if (data.collection_id == undefined) then data.collections_ids else data.collection_id + + url = '/collections/' + encodeURIComponent(id) + '/' + data.type + $.ajax {method: "POST", url: url, data: data } + + location.reload(true) if !(refreshable_type.indexOf(data.type) < 0) + $('#collections-modal').modal('hide') + return + +# manipulate collections $(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') # add selectors @@ -47,23 +86,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 { name: 'learning_object', value: this.value } $(document).trigger('check_selected_collection'); return else - index = selected_collections.indexOf(this.value) + index = selected_objects.indexOf({ name: 'learning_object', value: 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 +111,20 @@ $ -> # 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')) + learning_objects = selected_objects.map (o) -> + return o.value + + if permitted_types[index] == 'remove' + url = '/collections/' + encodeURIComponent(collection) + '/remove' + $.ajax {method: 'DELETE', url: url, data: { learning_objects_ids: learning_objects } } if confirm('Você tem certeza?') + location.reload(true) + else + $(document).trigger('open_collections_modal', [collection: collection, type: permitted_types[index], learning_object: learning_objects ]) 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) @@ -89,15 +133,4 @@ $ -> 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/javascripts/application/learning_objects.coffee.erb b/app/assets/javascripts/application/learning_objects.coffee.erb index c3e5b23ed56cb7d97727085261fe8bd4aa91be2f..42128666805d8190834cb1d22d3cfc21fd4d364b 100644 --- a/app/assets/javascripts/application/learning_objects.coffee.erb +++ b/app/assets/javascripts/application/learning_objects.coffee.erb @@ -1,44 +1,44 @@ $ -> $('.default_btn').toggleClass 'active_btn' - $(document).on 'click', '*[data-poload]', -> - e = $(this) - e.popover({content: "Carregando..."}).popover('show') - all = undefined - objects = undefined - url = $(this).data('poload') - loid = $(this).data('loid') - $.get '/collections.json', (d) -> - all = d - $.get url, (d) -> - object = d - - # generate a lookup table for object's collections id - lookup = {} - i = 0 - len = object.length - while i < len - lookup[object[i].id] = object[i] - i++ - - # mark checked objects - i = 0 - len = all.length - html = '' - while i < len - name = 'collection_' + all[i].id.split(':').slice(-1)[0] - html += '<input type="checkbox" class="collection-element" data-cid="' + all[i].id + '" data-loid="' + loid + '" id="' + name + '" value="' + all[i].id + '"' - if lookup[all[i].id] != undefined - html += " checked" - html += '>' - html += '<label for="' + name + '">' + all[i].name + '</label><br/>' - ++i - - html = 'Você não possui coleções.<br>É possÃvel criá-las na sua página pessoal.' if html == "" - - $('.popover-content').html(html) - return - return - return + # $(document).on 'click', '*[data-poload]', -> + # e = $(this) + # e.popover({content: "Carregando..."}).popover('show') + # all = undefined + # objects = undefined + # url = $(this).data('poload') + # loid = $(this).data('loid') + # $.get '/collections.json', (d) -> + # all = d + # $.get url, (d) -> + # object = d + # + # # generate a lookup table for object's collections id + # lookup = {} + # i = 0 + # len = object.length + # while i < len + # lookup[object[i].id] = object[i] + # i++ + # + # # mark checked objects + # i = 0 + # len = all.length + # html = '' + # while i < len + # name = 'collection_' + all[i].id.split(':').slice(-1)[0] + # html += '<input type="checkbox" class="collection-element" data-cid="' + all[i].id + '" data-loid="' + loid + '" id="' + name + '" value="' + all[i].id + '"' + # if lookup[all[i].id] != undefined + # html += " checked" + # html += '>' + # html += '<label for="' + name + '">' + all[i].name + '</label><br/>' + # ++i + # + # html = 'Você não possui coleções.<br>É possÃvel criá-las na sua página pessoal.' if html == "" + # + # $('.popover-content').html(html) + # return + # return + # return $ -> $(document).on 'click', '.add_to_collection', (e) -> diff --git a/app/controllers/collections_controller.rb b/app/controllers/collections_controller.rb index d36cf9dcfb3a829302a4772f96d0008a1479c6f6..6e48608711376f05c0b30baed537332b4cc0b0fc 100644 --- a/app/controllers/collections_controller.rb +++ b/app/controllers/collections_controller.rb @@ -1,7 +1,6 @@ class CollectionsController < ApplicationController 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 @@ -75,36 +74,34 @@ class CollectionsController < ApplicationController 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)) + @collections.select!{|c| c.id != @collection.id} unless @collection.blank? - @collections = collection_repository.all Collections::UserContext.new(current_user) + @type = params[:type] unless params[:type].blank? render layout: false end - # POST /collections/1/learning_object/43 + # POST /collections/1/learning_object def add_learning_object - learning_object = learning_object_repository.find params[:learning_object_id] - @collection.add learning_object + response = learning_objects_to_collections(@learning_objects, @collection) - if request.xhr? && collection_repository.save_learning_objects(@collection) - render json: {status: true} + if request.xhr? + render json: {status: response} end end - # DELETE /collections/1/learning_object/43 + # DELETE /collections/1/learning_object def remove_learning_object - learning_object = learning_object_repository.find params[:learning_object_id] - @collection.remove learning_object - collection_repository.save_learning_objects @collection + response = remove_learning_objects_from_collection(@learning_objects, @collection) if request.xhr? - render json: {status: true} + render json: {status: response} end 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,15 +109,9 @@ class CollectionsController < ApplicationController 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 = learning_objects_to_collections(@learning_objects, @target_collections) - response = collection_repository.save_learning_objects(@target_collection) - end + remove_learning_objects_from_collection(@learning_objects, @collection) if response if request.xhr? render json: {status: response} @@ -141,14 +132,26 @@ 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 + 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 + + collection_repository.save_learning_objects(collection) end + end + end - return collection_repository.save_learning_objects(@target_collection) + def remove_learning_objects_from_collection(learning_objects, collection) + learning_objects = [learning_objects] if learning_objects.class == String + + learning_objects.each do |learning_object| + collection.remove learning_object end + + collection_repository.save_learning_objects(collection) end def check_collection_privacy!(collection) @@ -158,15 +161,12 @@ class CollectionsController < ApplicationController end def set_collection - @collection = collection_repository.find params[:id] - end + @collection = collection_repository.find params[:id] unless params[:id].blank? - 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].blank? - @learning_objects = params[:learning_objects].map{|id| learning_object_repository.find id || false} unless params[:learning_objects].empty? + params[:learning_objects_ids] = JSON.parse(params[:learning_objects_ids]) if params[:learning_objects_ids].class == String + @learning_objects = params[:learning_objects_ids].map{|id| learning_object_repository.find id || false} unless params[:learning_objects_ids].blank? 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..22efd618cacefbf4889ea06ed20bb3e01f028973 100644 --- a/app/views/collections/list.html.erb +++ b/app/views/collections/list.html.erb @@ -7,14 +7,19 @@ </div> <form id="collections-modal-form"> <div class="modal-body"> - <% 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 %> + <%= hidden_field_tag 'collection_id', @collection.id unless @collection.blank? %> + <%= hidden_field_tag 'type', @type unless @type.blank? %> + <% unless @learning_objects.blank? %> + <% @learning_objects.each do |object| %> + <%= hidden_field_tag 'learning_object', object.id %> <% end %> <% end %> + + <% @collections.each do |collection| %> + <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? %> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button> diff --git a/app/views/learning_objects/_collections_button.html.erb b/app/views/learning_objects/_collections_button.html.erb index 159378255504aaad28d4cbb487cb61de78ab0857..1f2b4f7802cf33b365f73a207f3e9620a0124aad 100644 --- a/app/views/learning_objects/_collections_button.html.erb +++ b/app/views/learning_objects/_collections_button.html.erb @@ -1,3 +1,3 @@ -<a tabindex="0" data-container="body" class="btn btn-default btn-xs add_to_collection" role="button" data-toggle="popover" data-placement="left" title="Adicionar as coleções" data-loid="<%= learning_object.id %>" data-poload="/learning_objects/<%= ERB::Util.url_encode(learning_object.id) %>/collections.json"> +<a tabindex="0" class="add-to-collection btn btn-default btn-xs" role="button" title="Adicionar as coleções" data-loid="<%= learning_object.id %>"> <span class="glyphicon glyphicon-list" aria-hidden="true"></span> </a> diff --git a/config/routes.rb b/config/routes.rb index 44d23cbf108917c170fbfb9c69607449ede56f05..2a696ece014ef52d51b750a002ed883dae7c16a9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -65,23 +65,21 @@ Rails.application.routes.draw do end end + # collection list + get '/collections/list' => "collections#list", as: 'collection_list' 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 + member do # 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 + # add/remove a learning object for some collection + post '/add', as: :add_learning_object, action: :add_learning_object + delete '/remove', as: :remove_learning_object, action: :remove_learning_object + + # copy / move learning objects from a collection to another + post '/copy', as: :copy_learning_objects, action: :copy_learning_objects + post '/move', as: :move_learning_objects, action: :move_learning_objects end end