diff --git a/app/assets/javascripts/application/learning_objects.coffee b/app/assets/javascripts/application/learning_objects.coffee index f114ec84c6886942ece8cac026ce8d4e9fe3cd9f..9730795c0383bf2153e389bbe3300bbf2dab6dfd 100644 --- a/app/assets/javascripts/application/learning_objects.coffee +++ b/app/assets/javascripts/application/learning_objects.coffee @@ -1,14 +1,42 @@ $ -> $('.default_btn').toggleClass 'active_btn' - $('.add_to_collection').popover - html: true - container: 'body' - content: -> - url = $(this).data('poload') + $('*[data-poload]').click -> + e = $(this) + e.popover({content: "Carregando..."}).popover('show') + all = undefined + objects = undefined + url = $(this).data('poload') + $.get '/collections.json', (d) -> + all = d $.get url, (d) -> - $.parseJSON 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" 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 + + $('.popover-content').html(html) return + return return + $ -> $('.add_to_collection').on 'click', (e) -> $('.add_to_collection').removeClass 'active' @@ -17,4 +45,4 @@ $ -> $ -> $(document).on 'ajax:success', 'a.vote', (status, data, xhr) -> $('.votes-count[data-id=\'' + data.id + '\']').text data.count - return \ No newline at end of file + return diff --git a/app/assets/javascripts/application/navigation-content.js b/app/assets/javascripts/application/navigation-content.js index 2d9f2e66e1021bf819e7b600e928c58450ee9883..fbc951f84d309954ba4af95f2dad00b06516ab64 100644 --- a/app/assets/javascripts/application/navigation-content.js +++ b/app/assets/javascripts/application/navigation-content.js @@ -44,4 +44,15 @@ var nav_content = function () { $(document).ready(function () { nav_content(); + + // fix bootstrap popover click to dismiss - http://stackoverflow.com/a/14857326 + $('body').on('click', function (e) { + $('[data-toggle="popover"]').each(function () { + //the 'is' for buttons that trigger popups + //the 'has' for icons within a button that triggers a popup + if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) { + $(this).popover('hide'); + } + }); + }); }); diff --git a/app/views/learning_objects/_collections_button.html.erb b/app/views/learning_objects/_collections_button.html.erb index be15549ca0ff1950529da7cb8cd6407c4bf880c5..b9c2aeaf7c804073bd20349b94f0a7e62950c6d6 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" class="btn btn-default btn-xs add_to_collection" role="button" data-toggle="popover" data-placement="left" data-trigger="focus" title="Adicionar as coleções" data-poload="/learning_objects/<%= learning_object.id %>/collections.json"> +<a tabindex="0" class="btn btn-default btn-xs add_to_collection" role="button" data-toggle="popover" data-placement="left" title="Adicionar as coleções" data-poload="/learning_objects/<%= ERB::Util.url_encode(learning_object.id) %>/collections.json"> <span class="glyphicon glyphicon-list" aria-hidden="true"></span> </a>