import Bloodhound from "ucb_rails_user/bloodhound" // the users table has width set to auto, but the pagination tries to stretch to full width - this // forces the pagination to match the table var resizePagination = function () { //var paginationDiv = $('.dataTables_info').parents('.row').first() //paginationDiv.width($('.dataTable').width()) } var addDatatablesToNewUserSearchResults = function () { $('.add-user-search-results-table').dataTable({ searching: true, order: [[ 2, "asc" ]], columnDefs: [ { targets: 3, orderable: false }] }) resizePagination() } var addDatatablesToUsersTable = function () { var unsorted_columns = $('.ucb-rails-users-table th.unsorted').map((i, e) => $(e).index()).toArray() if (unsorted_columns == []) { var unsorted_columns = $(".ucb-rails-users-table th:contains('Edit'),.ucb-rails-users-table th:contains('Delete')").map((i, e) => $(e).index()).toArray() } $('.ucb-rails-users-table').dataTable({ searching: true, order: [[ 3, "asc" ]], columnDefs: [ { targets: unsorted_columns, orderable: false }], }) var addNewHtml = '  Add New' $('.ucb-rails-users-table-wrapper #DataTables_Table_0_filter').append(addNewHtml) } var resetImpersonateButton = function() { var targetId = $('#ucb_rails_user_impersonation_target_id').val() if (targetId != null && targetId.toString().length > 0) { $('input[data-impersonate-button]').removeAttr('disabled') } else { $('input[data-impersonate-button]').attr('disabled', 'disabled') } } var clearImpersonateSelection = function() { $('#ucb_rails_user_impersonation_target_id').val('') resetImpersonateButton() } $( window ).on("load", function() { // the datatable calling was failing intermittently, but adding the timeout // seemed to fix it, so ¯\_(ツ)_/¯ window.setTimeout(addDatatablesToUsersTable, 100) $('.user-search-form').on('submit', function() { $('.search-results').hide() $('.ucb-rails-user-loader').show() }) var usersSource = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), queryTokenizer: Bloodhound.tokenizers.whitespace, remote: { url: '/admin/users/impersonate_search?q=%QUERY', wildcard: '%QUERY' } }); $('#ucb_rails_user_impersonation_target').typeahead(null, { name: 'users', source: usersSource, display: 'name', limit: 7, // any higher than this, and the results don't display properly templates: { empty: [ '
', 'No match found', '
' ].join('\n'), suggestion: function (data) { return '
' + data.name + ' (' + data.uid + ')
' } } }); $('#ucb_rails_user_impersonation_target').keyup(function(event) { if ($(event.target).val().length == 0) { clearImpersonateSelection() } }) $('#ucb_rails_user_impersonation_target').bind('typeahead:open', function(event, suggestion) { clearImpersonateSelection() }) $('#ucb_rails_user_impersonation_target').bind('typeahead:select', function(event, suggestion) { $('#ucb_rails_user_impersonation_target_id').val(suggestion.id) resetImpersonateButton() }) }) // hack to make this work better with ES modules window.resizePagination = resizePagination window.addDatatablesToNewUserSearchResults = addDatatablesToNewUserSearchResults window.resetImpersonateButton = resetImpersonateButton window.clearImpersonateSelection = clearImpersonateSelection