fpa.min.js 11 KB

1
  1. (function fpa_scope($,Drupal,window,document){"use strict";var Fpa=function(context,settings){this.init(context,settings);return this};Fpa.prototype.selector={form:"#user-admin-permissions",table:"#permissions"};Fpa.prototype.init=function(context,settings){this.drupal_html_class_cache={};this.dom={};this.attr=settings.attr;this.filter_timeout=null;this.filter_timeout_time=0;this.module_match="*=";this.filter_selector_cache={"*=":{},"~=":{}};this.selector.table_base_selector=".fpa-table-wrapper tr["+this.attr.module+"]";this.selector.list_counter_selector=".fpa-perm-counter";this.selector.list_base_selector=".fpa-left-section li["+this.attr.module+"]";if(this.select(context)){this.prepare();this.authenticated_role_behavior()}};Fpa.prototype.styles={module_active_style:"{margin-right:-1px; background-color: white; border-right: solid 1px transparent;}"};Fpa.prototype.select=function(context){this.dom.context=$(context);this.dom.form=this.dom.context.find(this.selector.form);if(this.dom.form.length===0){return false}this.dom.container=this.dom.form.find(".fpa-container");this.dom.perm_style=this.dom.container.find(".fpa-perm-styles style").get(0);this.dom.role_style=this.dom.container.find(".fpa-role-styles style").get(0);this.dom.section_left=this.dom.container.find(".fpa-left-section");this.dom.section_right=this.dom.container.find(".fpa-right-section");this.dom.table_wrapper=this.dom.section_right.find(".fpa-table-wrapper");this.dom.table=this.dom.table_wrapper.find(this.selector.table);this.dom.module_list=this.dom.section_left.find("ul");this.dom.filter_form=this.dom.container.find(".fpa-filter-form");this.dom.filter=this.dom.filter_form.find('input[type="text"]');this.dom.role_select=this.dom.filter_form.find("select");this.dom.checked_status=this.dom.filter_form.find('input[type="checkbox"]');return true};Fpa.prototype.drupal_clean_css_identifier=function(str){return str.replace(/[ _\/\[]/g,"-").replace(/\]/g,"").replace(/[^\u002D\u0030-\u0039\u0041-\u005A\u005F\u0061-\u007A\u00A1-\uFFFF]/,"")};Fpa.prototype.drupal_html_class=function(str){if(this.drupal_html_class_cache[str]===undefined){this.drupal_html_class_cache[str]=this.drupal_clean_css_identifier(str.toLowerCase())}return this.drupal_html_class_cache[str]};Fpa.prototype.set_style=function(){if($('<style type="text/css" />').get(0).styleSheet){return function(element,styles){element.styleSheet.cssText=styles}}return function(element,styles){element.innerHTML=styles}}();Fpa.prototype.filter_module=function(e){e.preventDefault();e.stopPropagation();var $this=$(e.currentTarget);this.dom.filter.val([this.dom.filter.val().replace(/(@.*)/,""),$this.attr(this.attr.module)!==""?"@"+$this.find("a[href]").text():""].join(""));this.module_match="~=";this.filter()};Fpa.prototype.build_filter_selectors=function(filter_string){var matches=filter_string.match(/^\s*([^@]*)@?(.*?)\s*$/i);matches.shift();var safe_matches=$.map(matches,$.proxy(this.drupal_html_class,this));this.filter_selector_cache[this.module_match][filter_string]=[safe_matches[0].length>0?"["+this.attr.permission+'*="'+safe_matches[0]+'"]':"",safe_matches[1].length>0?"["+this.attr.module+this.module_match+'"'+safe_matches[1]+'"]':""];return this.filter_selector_cache[this.module_match][filter_string]};Fpa.prototype.get_filter_selectors=function(filter_string){filter_string=filter_string||this.dom.filter.val();return this.filter_selector_cache[this.module_match][filter_string]||this.build_filter_selectors(filter_string)};Fpa.prototype.permission_grid_styles=function(filters){filters=filters||this.get_filter_selectors();var checked_filters=this.build_checked_selectors();var styles=[this.selector.table_base_selector,"{display: none;}"];for(var i=0;i<checked_filters.length;i++){styles=styles.concat([this.selector.table_base_selector,checked_filters[i],filters[0],filters[1],"{display: table-row;}"])}return styles.join("")};Fpa.prototype.counter_styles=function(filters){filters=filters||this.get_filter_selectors();return[this.selector.list_counter_selector,"{display: none;}",this.selector.list_counter_selector,filters[0],"{display: inline;}"].join("")};Fpa.prototype.module_list_styles=function(filters){filters=filters||this.get_filter_selectors();return[this.selector.list_base_selector,filters[1].length>0?filters[1]:"["+this.attr.module+'=""]',this.styles.module_active_style].join("")};Fpa.prototype.filter=function(){var perm=this.dom.filter.val();$.cookie("fpa_filter",perm,{path:"/"});$.cookie("fpa_module_match",this.module_match,{path:"/"});this.save_filters();var filter_selector=this.get_filter_selectors(perm);this.set_style(this.dom.perm_style,[this.permission_grid_styles(filter_selector),this.counter_styles(filter_selector),this.module_list_styles(filter_selector)].join(""))};Fpa.prototype.build_role_selectors=function(roles){roles=roles||this.dom.role_select.val();var selectors=["*"];if($.inArray("*",roles)===-1){selectors=$.map(roles,$.proxy(function(value,index){return"["+this.attr.role+'="'+value+'"]'},this))}return selectors};Fpa.prototype.build_checked_selectors=function(roles){roles=roles||this.dom.role_select.val();var checked_boxes=$.map(this.dom.checked_status,function(element,index){return element.checked?$(element).val():null});var selectors=[""];if($.inArray("*",roles)!==-1){roles=$.map(this.dom.role_select.find("option").not('[value="*"]'),$.proxy(function(element,index){return $(element).attr("value")},this))}if(checked_boxes.length!=this.dom.checked_status.length){selectors=$.map(roles,$.proxy(function(value,index){return $.map(checked_boxes,$.proxy(function(checked_attr,index){return"["+checked_attr+'~="'+value+'"]'},this))},this))}return selectors};Fpa.prototype.filter_roles=function(){this.save_filters();var values=this.dom.role_select.val()||[];var role_style_code=[];$.cookie("fpa_roles",JSON.stringify(values),{path:"/"});if($.inArray("*",values)===-1){role_style_code.push(".fpa-table-wrapper ["+this.attr.role+"] {display: none;}");if(values.length>0){var role_selectors=this.build_role_selectors(values);role_style_code=role_style_code.concat($.map(role_selectors,$.proxy(function(value,index){return".fpa-table-wrapper "+value+" {display: table-cell;}"},this)));role_style_code.push(".fpa-table-wrapper "+role_selectors.pop()+" {border-right: 1px solid #bebfb9;}")}else{role_style_code.push('td[class="permission"] {border-right: 1px solid #bebfb9;}')}}this.set_style(this.dom.role_style,role_style_code.join(""));this.filter()};Fpa.prototype.save_filters=function(){this.dom.filter.get(0).defaultValue=this.dom.filter.val();this.dom.role_select.find("option").each(function(index,element){element.defaultSelected=element.selected});this.dom.checked_status.each(function(index,element){element.defaultChecked=element.checked})};Fpa.prototype.prepare=function(){this.filter_timeout_time=Math.min(this.dom.table.find("tr").length,200);this.dom.form.delegate(".fpa-toggle-container a","click",$.proxy(function fpa_toggle(e){e.preventDefault();var toggle_class=$(e.currentTarget).attr("fpa-toggle-class");this.dom.container.toggleClass(toggle_class).hasClass(toggle_class)},this));this.dom.section_left.delegate("li","click",$.proxy(this.filter_module,this));this.dom.filter.keypress(function fpa_prevent_form_submission(e){if(e.which===13){e.preventDefault();e.stopPropagation()}}).keyup($.proxy(function fpa_filter_keyup(e){if($.inArray(e.which,[13,16,17,18])===-1){window.clearTimeout(this.filter_timeout);this.filter_timeout=window.setTimeout($.proxy(function(){this.dom.table_wrapper.detach().each($.proxy(function(index,element){this.module_match="*=";this.filter()},this)).appendTo(this.dom.section_right)},this),this.filter_timeout_time)}},this));this.dom.form.delegate('a[href*="admin/people/permissions#"]',"click",$.proxy(function fpa_inter_page_links_click(e){e.preventDefault();e.stopPropagation();this.dom.module_list.find("li["+this.attr.module+'~="'+this.drupal_html_class(e.currentTarget.hash.substring(8))+'"]').click();$("body").scrollTop(this.dom.container.position().top)},this));if(window.location.hash.indexOf("module-")===1){this.dom.module_list.find("li["+this.attr.module+'~="'+this.drupal_html_class(window.location.hash.substring(8))+'"]').click()}this.dom.form.bind("reset",$.proxy(function fpa_form_reset(e){window.setTimeout($.proxy(function fpa_fix_authenticated_behavior(){this.dom.table_wrapper.detach().each($.proxy(function(index,element){$(element).find('input[type="checkbox"].rid-2').each(this.dummy_checkbox_behavior)},this)).appendTo(this.dom.section_right)},this),0)},this));this.dom.section_right.delegate("th["+this.attr.role+'] input[type="checkbox"].fpa-checkboxes-toggle',"change",$.proxy(function fpa_role_permissions_toggle(e){var $this=$(e.currentTarget);var filters=this.get_filter_selectors(this.dom.filter.val());this.dom.table_wrapper.detach().each($.proxy(function(index,element){var rid=$this.closest("["+this.attr.role+"]").attr(this.attr.role);$(element).find(["tr"+filters.join(""),"td.checkbox["+this.attr.role+'="'+rid+'"]','input[type="checkbox"][name]'].join(" ")).attr("checked",$this.attr("checked")).filter(".rid-2").each(this.dummy_checkbox_behavior)},this)).appendTo(this.dom.section_right)},this));this.dom.section_right.delegate('td.permission input[type="checkbox"].fpa-checkboxes-toggle',"change",$.proxy(function fpa_role_permissions_toggle(e){var $row=$(e.currentTarget).closest("tr");$row.prev("tr").after($row.detach().each($.proxy(function(index,element){$(element).find("td.checkbox").filter(this.build_role_selectors().join(",")).find('input[type="checkbox"][name]').attr("checked",e.currentTarget.checked).filter(".rid-2").each(this.dummy_checkbox_behavior)},this)))},this));this.dom.section_right.delegate(".fpa-clear-search","click",$.proxy(function(e){this.dom.filter.val("");this.filter()},this));this.dom.role_select.bind("change blur",$.proxy(this.filter_roles,this));this.dom.checked_status.bind("change",$.proxy(function(e){this.save_filters();this.filter()},this));this.dom.table.delegate("tr["+this.attr.system_name+"]","click",$.proxy(function(e){var $target=$(e.target);if($target.is("tr["+this.attr.system_name+"]")){window.prompt("You can grab the system name here",$target.attr(this.attr.system_name))}},this));window.setTimeout($.proxy(function fpa_filter_focus(){this.dom.filter.focus()},this),0)};Fpa.prototype.dummy_checkbox_behavior=function(){$(this).closest("tr").toggleClass("fpa-authenticated-role-behavior",this.checked)};Fpa.prototype.authenticated_role_behavior=function(){this.dom.table_wrapper.delegate("input[type=checkbox].rid-2","mousedown",function(e){$(e.currentTarget).unbind("click.permissions")}).delegate("input[type=checkbox].rid-2","change.fpa_authenticated_role",this.dummy_checkbox_behavior)};Drupal.behaviors.fpa={attach:function(context,settings){if(document.documentElement.ontouchstart!==undefined){$(document.body).addClass("fpa-mobile")}$(window).bind("scroll",function fpa_fix_tableheader(e){$(e.currentTarget).triggerHandler("resize.drupal-tableheader")});new Fpa(context,settings.fpa)}};Drupal.behaviors.permissions.attach=$.noop})(jQuery,Drupal,window,document);