Skip to content
Snippets Groups Projects
Commit bebbf08d authored by Mauricio Giacomini Girardello's avatar Mauricio Giacomini Girardello
Browse files

Merge branch 'master' of gitlab.c3sl.ufpr.br:portalmec/portalmec

parents 009235dc 600a3b9c
No related branches found
No related tags found
No related merge requests found
...@@ -11,13 +11,13 @@ ...@@ -11,13 +11,13 @@
// about supported directives. // about supported directives.
// //
//= require jquery //= require jquery
//= require jquery.turbolinks
//= require jquery_ujs //= require jquery_ujs
//= require jquery-ui/autocomplete //= require jquery-ui/autocomplete
//= require bootstrap-sprockets //= require bootstrap-sprockets
//= require select2 //= require select2
//= require select2_locale_pt-BR //= require select2_locale_pt-BR
//= require bootstrap-tagsinput //= require bootstrap-tagsinput
//= require jquery.turbolinks
//= require turbolinks //= require turbolinks
//= require_tree ./application //= require_tree ./application
......
$ -> $ ->
# valid types of operations in collections
permitted_types = ['add', 'download', 'copy', 'move', 'remove']
# create collection popover # create collection popover
$('#create_collection_popover').popover $('#create_collection_popover').popover
html: true html: true
...@@ -9,128 +6,132 @@ $ -> ...@@ -9,128 +6,132 @@ $ ->
$('#create_collection_popover_content').html() $('#create_collection_popover_content').html()
title: -> title: ->
$('#create_collection_popover_title').html() $('#create_collection_popover_title').html()
return
# 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]', ->
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) ->
id = if (params.collection == undefined) then "all" else encodeURIComponent(params.collection)
url = '/collections/' + id + '/list?type=' + params.type
url += '&learning_objects_ids=' + encodeURIComponent(params.learning_object) if params.learning_object != undefined
$.get url, (d) ->
$('#collections-modal').remove()
$('body').append d
$('#collections-modal').modal('show')
return
# add/remove learning object to collection mount_modal_params = (e) ->
$(document).on 'click', '.add-to-collection', -> array = $(e).serializeArray()
$(document).trigger('open_collections_modal', [learning_object: [$(this).data('loid')], type: 'add']) object = {}
collection = ""
# change collection privacy type = ""
$(document).on 'click', 'input[name=privacy]', -> collections = []
url = '/collections/' + encodeURIComponent($(this).data('cid')) + '/change_privacy' learning_objects = []
value = $('input[name=privacy]:checked').val()
$.post url, {'privacy':value}, (d) -> i = 0
d length = array.length
while i < length
$(document).on 'open_collections_modal', (evt, params) -> switch array[i].name
url = '/collections/list?type=' + params.type when 'collection' then collections.push(array[i].value)
url += '&id=' + encodeURIComponent(params.collection) if params.collection != undefined when 'learning_object' then learning_objects.push(array[i].value)
url += '&learning_objects_ids=' + encodeURIComponent(JSON.stringify(params.learning_object)) if params.learning_object != undefined when 'collection_id' then collection = array[i].value
$.get url, (d) -> when 'type' then type = array[i].value
$('#collections-modal').remove() ++i
$('body').append d
$('#collections-modal').modal('show') object.collection_id = collection
return object.collections_ids = collections if collections.length > 0
object.learning_objects_ids = learning_objects if learning_objects.length > 0
mount_modal_params = (e) -> object.type = type
array = $(e).serializeArray() return object
object = {}
collection = "" $(document).on 'submit', '#collections-modal-form', (evt, params) ->
type = "" evt.preventDefault()
collections = []
learning_objects = [] refreshable_type = ['move']
i = 0 data = mount_modal_params(this)
length = array.length
while i < length return false if (data.learning_objects_ids.length < 1) || (data.collections_ids.length < 1) || (permitted_types.indexOf(data.type) < 0)
switch array[i].name
when 'collection' then collections.push(array[i].value) id = if (data.collections_ids == undefined) then data.collection_id else data.collections_ids
when 'learning_object' then learning_objects.push(array[i].value) url = '/collections/' + encodeURIComponent(id) + '/learning_objects/' + encodeURIComponent(data.learning_objects_ids.join())
when 'collection_id' then collection = array[i].value $.ajax {method: "POST", url: url, data: data }
when 'type' then type = array[i].value .done () ->
++i if (data.type == "move")
url = '/collections/' + encodeURIComponent(data.collection_id) + '/learning_objects/' + encodeURIComponent(data.learning_objects_ids.join())
object.collection_id = collection $.ajax {method: "DELETE", url: url, data: data }
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) location.reload(true) if !(refreshable_type.indexOf(data.type) < 0)
$('#collections-modal').modal('hide') $('#collections-modal').modal('hide')
return return
# manipulate collections # manipulate collections
$(document).on 'ready page:load', -> # $(document).on 'ready page:load', ->
if $('.collection-show-page').val() != undefined # if $('.collection-show-page').val() != undefined
# array with selected objects ids # array with selected objects ids
selected_objects = [] selected_objects = []
collection = $('.collection-show-page').data('cid') # valid types of operations in collections
permitted_types = ['add', 'download', 'copy', 'move', 'remove']
# add selectors
$('.learning-object-vertical').each (e) -> $(document).on 'ready page:load', ->
loid = $(this).data('loid') if $('.collection-show-page').val() != undefined
$('.learning-object-thumbnail', this).append '<input class="collection-selector" type="checkbox" value="' + loid + '"></input>' # add selectors
return $('.learning-object-vertical').each (e) ->
loid = $(this).data('loid')
# add/remove object to array when click checkbox $('.learning-object-thumbnail', this).append '<input class="collection-selector" type="checkbox" value="' + loid + '"></input>'
$(document).on 'click', '.collection-selector', ->
if this.checked
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_objects.indexOf({ name: 'learning_object', value: this.value })
if !!(~index)
selected_objects.splice(index, 1)
$(document).trigger('check_selected_collection');
return
# clear selected objects
$(document).on 'clear_collections', ->
selected_objects = []
$('.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', ->
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 an object
$(document).on 'check_selected_collection', (e) ->
length = selected_objects.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')
return return
# add/remove object to array when click checkbox
$(document).on 'click', '.collection-selector', ->
if this.checked
if selected_objects.indexOf(this.value) < 0
selected_objects.push this.value
$(document).trigger('check_selected_collection')
return
else
index = selected_objects.indexOf(this.value)
if !!(~index)
selected_objects.splice(index, 1)
$(document).trigger('check_selected_collection')
return
# clear selected objects
$(document).on 'clear_collections', ->
selected_objects = []
$('.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', ->
collection = $('.collection-show-page').data('cid')
index = permitted_types.indexOf($(this).data('action'))
if permitted_types[index] == 'remove'
url = '/collections/' + encodeURIComponent(collection) + '/learning_objects/' + encodeURIComponent(selected_objects.join())
$.ajax { method: 'DELETE', url: url } if confirm('Você tem certeza?')
location.reload(true)
else
$(document).trigger('open_collections_modal', [collection: collection, type: permitted_types[index], learning_object: selected_objects ]) if !!(~index)
# update interface when add/remove an object
$(document).on 'check_selected_collection', (e) ->
length = selected_objects.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')
$ ->
$('.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', '.add_to_collection', (e) ->
$('.add_to_collection').removeClass 'active'
$('.add_to_collection').not(this).popover 'hide'
return
$ -> $ ->
$(document).on 'ajax:success', 'a.vote', (status, data, xhr) -> $(document).on 'ajax:success', 'a.vote', (status, data, xhr) ->
$('.votes-count[data-id=\'' + data.id + '\']').text data.count $('.votes-count[data-id=\'' + data.id + '\']').text data.count
......
class CollectionsController < ApplicationController 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 :set_collection, only: [:show, :update, :destroy, :like, :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_collections, only: [:list, :add_learning_object, :remove_learning_object]
before_action :authenticate_user!, only: [:update, :destroy, :like, :list, :add_learning_object, :remove_learning_object, :change_privacy]
# GET /collections # GET /collections
# GET /collections.json # GET /collections.json
...@@ -74,47 +75,61 @@ class CollectionsController < ApplicationController ...@@ -74,47 +75,61 @@ class CollectionsController < ApplicationController
end end
def list def list
@collection = @collections.first
# list all
@collection = nil if @collection == 'all'
@collections = collection_repository.all(Collections::UserContext.new(current_user)) @collections = collection_repository.all(Collections::UserContext.new(current_user))
@collections.select!{|c| c.id != @collection.id} unless @collection.blank? @collections.select! { |c| c.id != @collection.id } unless @collection.blank?
unless params[:type].blank?
@type = params[:type]
@type = params[:type] unless params[:type].blank? @send = case @type
when 'add' then 'Adicionar'
when 'copy' then 'Copiar'
when 'move' then 'Mover'
else 'Enviar'
end
@title = (@send == 'Enviar') ? 'Coleções' : @send + ' objeto(s) às coleções'
end
render layout: false render layout: false
end end
# POST /collections/1/learning_object # POST /collections/1/learning_object
def add_learning_object def add_learning_object
response = learning_objects_to_collections(@learning_objects, @collection) @collections.each do |collection|
if collection.owner?(current_user)
@learning_objects.each do |learning_object|
collection.add learning_object
end
if request.xhr? collection_repository.save_learning_objects(collection)
render json: {status: response} end
end end
end
# DELETE /collections/1/learning_object
def remove_learning_object
response = remove_learning_objects_from_collection(@learning_objects, @collection)
if request.xhr? if request.xhr?
render json: {status: response} render json: {status: true}
end end
end end
def copy_learning_objects # DELETE /collections/1/learning_object
response = learning_objects_to_collections(@learning_objects, @target_collections) def remove_learning_object
@collections.each do |collection|
if collection.owner?(current_user)
@learning_objects.each do |learning_object|
collection.remove learning_object
end
if request.xhr? collection_repository.save_learning_objects(collection)
render json: {status: response} end
end end
end
def move_learning_objects
response = learning_objects_to_collections(@learning_objects, @target_collections)
remove_learning_objects_from_collection(@learning_objects, @collection) if response
if request.xhr? if request.xhr?
render json: {status: response} render json: {status: true}
end end
end end
...@@ -133,25 +148,7 @@ class CollectionsController < ApplicationController ...@@ -133,25 +148,7 @@ class CollectionsController < ApplicationController
end end
def learning_objects_to_collections(learning_objects, collections) 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
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 end
def check_collection_privacy!(collection) def check_collection_privacy!(collection)
...@@ -161,12 +158,24 @@ class CollectionsController < ApplicationController ...@@ -161,12 +158,24 @@ class CollectionsController < ApplicationController
end end
def set_collection def set_collection
@collection = collection_repository.find params[:id] unless params[:id].blank? @collection = collection_repository.find params[:id]
end
def set_collections
if params[:id] == "all" || params[:id].blank?
@collections = ['all']
else
@collections = (params[:id].class == String) ? [collection_repository.find(params[:id])] : params[:id].map{|id| collection_repository.find id}
end
@target_collections = params[:collections_ids].map{|id| collection_repository.find id} unless params[:collections_ids].blank? unless params[:learning_objects_ids].blank?
@learning_objects = []
params[:learning_objects_ids] = JSON.parse(params[:learning_objects_ids]) if params[:learning_objects_ids].class == String params[:learning_objects_ids].split(',').each do |id|
@learning_objects = params[:learning_objects_ids].map{|id| learning_object_repository.find id || false} unless params[:learning_objects_ids].blank? object = learning_object_repository.find id
@learning_objects << object unless object.blank?
end
end
end end
# Never trust parameters from the scary internet, only allow the white list through. # Never trust parameters from the scary internet, only allow the white list through.
......
...@@ -12,8 +12,8 @@ class LearningObject ...@@ -12,8 +12,8 @@ class LearningObject
validates_presence_of :name, :created_at, :type validates_presence_of :name, :created_at, :type
delegate :get_retrievelink, to: :file delegate :get_retrievelink, to: :attachment
delegate :get_filename, to: :file delegate :get_filename, to: :attachment
def initialize(params={}) def initialize(params={})
super(params.merge(defaults)) super(params.merge(defaults))
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<div class="modal-content"> <div class="modal-content">
<div class="modal-header"> <div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title">Coleções</h4> <h4 class="modal-title"><%= @title %></h4>
</div> </div>
<form id="collections-modal-form"> <form id="collections-modal-form">
<div class="modal-body"> <div class="modal-body">
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button> <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
<button type="submit" class="btn btn-primary">Enviar</button> <button type="submit" class="btn btn-primary"><%= @send %></button>
</div> </div>
</form> </form>
</div> </div>
......
...@@ -65,21 +65,17 @@ Rails.application.routes.draw do ...@@ -65,21 +65,17 @@ Rails.application.routes.draw do
end end
end end
# collection list
get '/collections/list' => "collections#list", as: 'collection_list'
resources :collections do resources :collections do
member do member do
# collection list
get :list
# change privacy # change privacy
post :change_privacy post :change_privacy
# add/remove a learning object for some collection # add/remove a learning object for some collection
post '/add', as: :add_learning_object, action: :add_learning_object post '/learning_objects/:learning_objects_ids', as: :add_learning_object, action: :add_learning_object
delete '/remove', as: :remove_learning_object, action: :remove_learning_object delete '/learning_objects/:learning_objects_ids', 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
end end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment