Widget:Icon filter

 RLQ.push(function { iconFilter = {    fields : {},

toCamelCase : function( text ) { return text.toLowerCase.replace(/-([a-z])/g, function( match, group1 ) {       return group1.toUpperCase;      }); },

initialize : function { $( ".icon-filter-section" ).each( function {       var section = $( this );        var id = section.data( "id" );        if ( typeof id !== "undefined" && id !== "" ) {          section.attr( "id", "icon-filter-" + id );          var show = false;          section.find( ".icon-filter-selects span" ).each( function { var data = $( this ).data; if ( "fieldName" in data && "anyValueName" in data ) { var values = iconFilter.uniqueDataValues( section, data ); if ( values.length > 1 ) { show = true; var select = $( ' ' ); select.data( "fieldName", data.fieldName ); select.append( iconFilter.newOption( true, '', data.anyValueName ) ); for ( var i = 0; i < values.length; ++i ) { select.append( iconFilter.newOption( false, values[i], values[i] ) ); }                  select.change( function {                   iconFilter.filter( id );                } ); $( this ).replaceWith( select ); } else { $( this ).remove; }           }          });          if ( show ) {            section.find( ".icon-filter-selects" ).show;          }        }      }); },

uniqueDataValues : function( section, data ) { var fieldName = iconFilter.toCamelCase( data.fieldName ); // mildly hackish but efficient way to get the unique values var unique = {}; section.find( ".tagged-icon" ).each( function {       var value = $( this ).data( fieldName );        if ( typeof value !== 'undefined' && value !== '') {          unique[value] = true;        }      } ); // custom sorts required, but keep only values present in the set values = []; if ( "valueOrder" in data ) { order = data.valueOrder.split( "/" ); for ( var i = 0; i < order.length; ++i ) { if ( order[i] in unique ) { values.push( order[i] ); delete unique[order[i]]; }       }      }      // also keep any values in the set even if they aren't in the specified order, just kick them to the end return values.concat( Object.keys( unique ).sort ); },

newOption : function( selected, value, content ) { var option = $( ' ' ); if ( selected ) { option.attr( 'selected', 'selected' ); }     option.attr( 'value', value ); option.html( content ); return option; },

isSelected : function( section, icon ) { var data = icon.data; var selects = $.makeArray( section.find( ".icon-filter-selects select" ) ); var select, name; while ( selects.length > 0 ) { select = $( selects.shift ); name = iconFilter.toCamelCase( select.data( "fieldName" ) ); if ( select.val !== "" && !( name in data && data[name] === select.val ) ) { return false; }     }      return true; },

filter : function( sectionName ) { $( "#icon-filter-" + sectionName ).each( function {       var section = $( this );        $( this ).find( ".tagged-icon" ).each( function { if ( iconFilter.isSelected( section, $( this ) ) ) { $( this ).css( "opacity", "1.0" ); } else { $( this ).css( "opacity", "0.25" ); }       });      });    }  };

window.iconFilter = iconFilter; $( document ).ready( iconFilter.initialize ); });