Skip to content
Snippets Groups Projects
Commit 600a3b9c authored by Mateus Rambo Strey's avatar Mateus Rambo Strey
Browse files

fix collections coffee multiple load

parent 6158c80b
No related branches found
No related tags found
No related merge requests found
......@@ -11,13 +11,13 @@
// about supported directives.
//
//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require jquery-ui/autocomplete
//= require bootstrap-sprockets
//= require select2
//= require select2_locale_pt-BR
//= require bootstrap-tagsinput
//= require jquery.turbolinks
//= require turbolinks
//= require_tree ./application
......
$ ->
# valid types of operations in collections
permitted_types = ['add', 'download', 'copy', 'move', 'remove']
# create collection popover
$('#create_collection_popover').popover
html: true
......@@ -9,128 +6,132 @@ $ ->
$('#create_collection_popover_content').html()
title: ->
$('#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
$(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')
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.collections_ids == undefined) then data.collection_id else data.collections_ids
url = '/collections/' + encodeURIComponent(id) + '/learning_objects/' + encodeURIComponent(data.learning_objects_ids.join())
$.ajax {method: "POST", url: url, data: data }
.done () ->
if (data.type == "move")
url = '/collections/' + encodeURIComponent(data.collection_id) + '/learning_objects/' + encodeURIComponent(data.learning_objects_ids.join())
$.ajax {method: "DELETE", 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 objects ids
selected_objects = []
# valid types of operations in collections
permitted_types = ['add', 'download', 'copy', 'move', 'remove']
$(document).on 'ready page:load', ->
if $('.collection-show-page').val() != undefined
# 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
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.collections_ids == undefined) then data.collection_id else data.collections_ids
url = '/collections/' + encodeURIComponent(id) + '/learning_objects/' + encodeURIComponent(data.learning_objects_ids.join())
$.ajax {method: "POST", url: url, data: data }
.done () ->
if (data.type == "move")
url = '/collections/' + encodeURIComponent(data.collection_id) + '/learning_objects/' + encodeURIComponent(data.learning_objects_ids.join())
$.ajax {method: "DELETE", url: url, data: data }
location.reload(true) if !(refreshable_type.indexOf(data.type) < 0)
$('#collections-modal').modal('hide')
# 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
# manipulate collections
$(document).on 'ready page:load', ->
if $('.collection-show-page').val() != undefined
# array with selected objects ids
selected_objects = []
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 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', ->
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')
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) ->
$('.votes-count[data-id=\'' + data.id + '\']').text data.count
......
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