$(document).ready(function() { var authorization_type = "managers"; $("ul#authorization-types a").on("click",function(){ authorization_type = $(this).data("for"); switch(authorization_type){ case "managers": $("ul#card-list-submanagers li.check-item").removeClass("active"); $("ul#card-list-submanagers li.check-item input[type=checkbox]").removeAttr("checked"); break; case "sub_managers": $("ul#card-list-managers li.check-item").removeClass("active"); $("ul#card-list-managers li.check-item input[type=checkbox]").removeAttr("checked"); break; } hide_or_show_button(); }) $(document).on("change",".selected_user input[type=checkbox], .selected_role input[type=checkbox]",function(){ hide_or_show_button(); }) $("#select_all").on("click",function(){ switch(authorization_type){ case "managers": $("ul#card-list-managers li.check-item").addClass("active"); $("ul#card-list-managers li.check-item input[type=checkbox]").prop("checked","checked"); break; case "sub_managers": $("ul#card-list-submanagers li.check-item").addClass("active"); $("ul#card-list-submanagers li.check-item input[type=checkbox]").prop("checked","checked"); break; } $(".dropup").removeClass("open"); hide_or_show_button(); return false; }) $("#deselect_all").on("click",function(){ switch(authorization_type){ case "managers": $("ul#card-list-managers li.check-item").removeClass("active"); $("ul#card-list-managers li.check-item input[type=checkbox]").removeAttr("checked"); break; case "sub_managers": $("ul#card-list-submanagers li.check-item").removeClass("active"); $("ul#card-list-submanagers li.check-item input[type=checkbox]").removeAttr("checked"); break; } $(".dropup").removeClass("open"); hide_or_show_button(); return false; }) var hide_or_show_button = function(){ if($('.selected_role input[type="checkbox"]:checked').length == 0 && $('.selected_user input[type="checkbox"]:checked').length == 0){ $("#remove_users").addClass("hide"); }else{ $("#remove_users").removeClass("hide"); } } $("select[name=anything]").on("change",function(){ var value_to_filter = $(this).val(); if(value_to_filter != ""){ lis.each(function(){ var categories = $(this).data("categories"); if(categories.indexOf(value_to_filter) == -1){ $(this).hide(); }else{ $(this).show(); } }) }else{ lis.show(); } }) $(".select_user_modal").on('click', function(){ var data_to_send = {"authorization_type" : authorization_type}; if (authorization_type == "sub_managers"){ var category_id = $("select[name=anything]").val(); if(category_id == ""){ alert("Please select a category."); return false; } data_to_send.category_id = category_id; } $.ajax({ type: 'GET', url: $(this).attr("rel"), dataType: 'html', data : data_to_send, success: function (html) { $("#select_user_modal").html(html); $("#member-filter").modal('show'); $(".modal").on("hidden", function () { $("#select_user_modal").empty(); }); var current_role = $("#select_user_modal .current_role a").eq(0).attr("href"); $("#select_user_modal .current_role a").on("click",function(){ current_role = $(this).attr("href"); }) $("#select_all_modal").on("click",function(){ $(current_role).find(".check-item").addClass("active"); $(current_role).find(".check-item input[type=checkbox]").prop("checked","checked"); $(".dropup").removeClass("open"); return false; }) $("#deselect_all_modal").on("click",function(){ $(current_role).find(".check-item").removeClass("active"); $(current_role).find(".check-item input[type=checkbox]").removeAttr("checked"); $(".dropup").removeClass("open"); return false; }) }, }); return false; }); $(document).on('click', ".remove_user", function(){ $(this).parent().remove(); }); $("#remove_users").on('click', function(){ var user_ids = [], role_ids =[], users = $('.selected_user input[type="checkbox"]:checked'), roles = $('.selected_role input[type="checkbox"]:checked'); users.each(function() { user_ids.push($(this).parent().attr('id')); }); roles.each(function() { role_ids.push($(this).parent().attr('id')); }); var data_to_send = {"authorization_type" : authorization_type, "user_ids" : user_ids, "role_ids" : role_ids}; if (authorization_type == "sub_managers"){ var category_id = $("select[name=anything]").val(); data_to_send.category_id = (category_id == "" ? "all" : category_id); } if (user_ids.length > 0 || role_ids.length > 0) { if (confirm('Are you sure you want revoke persmission from these users?')) { // TODO: i18n in javascript $.ajax({ type: 'DELETE', url: $(this).attr("rel"), dataType: 'script', data: data_to_send, error: function(){ alert("ERROR"); }, success : function(){ hide_or_show_button(); } }); } } else { // TODO: i18n in javascript alert('You did not select anything to delete') } }); });