Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added onAfterSearch and ignoreDiacritics #20

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ This plugin provides the following configuration options:
| onSearchEmpty | `false` | Allows you to define a function to be called when the search input is empty. This function will be called once when the search input is empty or cleared. The searchable element will be passed to the function. |
| onSearchFocus | `false` | Allows you to define a function to be called when the search input is focussed. The `this` context of this function will be the search input element. |
| onSearchBlur | `false` | Allows you to define a function to be called when the search input is blurred. The `this` context of this function will be the search input element. |
| onAfterSearch | `false` | Allows you to define a function to be called after searchable has hidden/shown the elements. This can be used to count the number of elements being hidden/shown. |
| clearOnLoad | `false` | Determines whether the search input should be cleared on page load (either `true` or `false`). |
| ignoreDiacritics | `false` | Determines if diacritic letters should be ignore. For example, when searching for `u` -> `ú` and `ü` will also be found. |

### Example usage

Expand Down Expand Up @@ -89,7 +91,11 @@ $( '#element' ).searchable({
onSearchBlur: function() {
$( '#feedback' ).hide();
},
clearOnLoad: true
onAfterSearch: function() {
// i.e. count number of items shown
},
clearOnLoad: true,
ignoreDiacritics: true
});
```

Expand All @@ -104,6 +110,11 @@ $( '#element' ).searchable({
- Added some events that allow you to call custom functions during the search lifecycle: onSearchActive, onSearchEmpty, onSearchFocus, onSearchBlur (view the [configuration](#configuration) for more information).
- Added the `clearOnLoad` setting which allows you to clear the search input on page load / refresh.

**Version 1.1.1:**

- Added onAfterSearch callback.
- Added ignoreDiacritics option. Default is false so it should be backwards compatible.

## Contributing & Issues

Please feel free to submit any issues or pull requests, they are more then welcome. When submitting an issue, please specify the version number and describe the issue in detail so that it can be solved as soon as possible!
Expand Down
2 changes: 2 additions & 0 deletions dist/jquery.searchable-1.1.1.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions dist/jquery.searchable-ie-1.1.1.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 37 additions & 5 deletions jquery.searchable-ie.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*!
/*!
* jQuery Searchable Plugin v1.0.0
* https://github.com/stidges/jquery-searchable
*
Expand All @@ -22,12 +22,15 @@
onSearchEmpty: false,
onSearchFocus: false,
onSearchBlur: false,
clearOnLoad: false
clearOnLoad: false,
onAfterSearch: false,
ignoreDiacritics: false
},
searchActiveCallback = false,
searchEmptyCallback = false,
searchFocusCallback = false,
searchBlurCallback = false;
searchBlurCallback = false,
afterSearchCallback = false;

function isFunction(value) {
return typeof value === 'function';
Expand Down Expand Up @@ -98,6 +101,28 @@
searchEmptyCallback = isFunction( this.settings.onSearchEmpty );
searchFocusCallback = isFunction( this.settings.onSearchFocus );
searchBlurCallback = isFunction( this.settings.onSearchBlur );
afterSearchCallback = isFunction( this.settings.onAfterSearch );
},

removeDiacritics : function (text) {
if (this.settings.ignoreDiacritics) {
text = text
.replace(/[ÀÁÂÃÄÅ]/g, 'A')
.replace(/[àáâãäå]/g, 'a')
.replace(/[ÈÉÊË]/g, 'E')
.replace(/[èéêë]/g, 'e')
.replace(/[Í]/g, 'I')
.replace(/[í]/g, 'i')
.replace(/[ÓÖ]/g, 'O')
.replace(/[óö]/g, 'o')
.replace(/[ÚÜ]/g, 'U')
.replace(/[úü]/g, 'u')
.replace(/[Ñ]/g, 'N')
.replace(/[ñ]/g, 'n')
.replace(/[ß]/g, 's')
;
}
return text;
},

bindEvents: function() {
Expand All @@ -107,6 +132,10 @@
that.search( $( this ).val() );

that.updateStriping();

if ( afterSearchCallback ) {
that.settings.onAfterSearch();
}
});

if ( searchFocusCallback ) {
Expand Down Expand Up @@ -142,7 +171,7 @@
},

search: function( term ) {
var matcher, elemCount, children, childCount, hide, $elem, i, x;
var matcher, elemCount, children, childCount, hide, $elem, i, x, contentText;

if ( $.trim( term ).length === 0 ) {
this.$searchElems.css( 'display', '' );
Expand All @@ -158,6 +187,7 @@
}

elemCount = this.$searchElems.length;
term = this.removeDiacritics(term);
matcher = this.matcherFunc( term );

for ( i = 0; i < elemCount; i++ ) {
Expand All @@ -167,7 +197,9 @@
hide = true;

for ( x = 0; x < childCount; x++ ) {
if ( matcher( $( children[ x ] ).text() ) ) {
contentText = $( children[ x ] ).text();
contentText = this.removeDiacritics(contentText);
if ( matcher( contentText ) ) {
hide = false;
break;
}
Expand Down
42 changes: 37 additions & 5 deletions jquery.searchable.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*!
/*!
* jQuery Searchable Plugin v1.0.0
* https://github.com/stidges/jquery-searchable
*
Expand All @@ -22,12 +22,15 @@
onSearchEmpty: false,
onSearchFocus: false,
onSearchBlur: false,
clearOnLoad: false
clearOnLoad: false,
onAfterSearch: false,
ignoreDiacritics: false
},
searchActiveCallback = false,
searchEmptyCallback = false,
searchFocusCallback = false,
searchBlurCallback = false;
searchBlurCallback = false,
afterSearchCallback = false;

function isFunction(value) {
return typeof value === 'function';
Expand Down Expand Up @@ -56,6 +59,28 @@
searchEmptyCallback = isFunction( this.settings.onSearchEmpty );
searchFocusCallback = isFunction( this.settings.onSearchFocus );
searchBlurCallback = isFunction( this.settings.onSearchBlur );
afterSearchCallback = isFunction( this.settings.onAfterSearch );
},

removeDiacritics : function (text) {
if (this.settings.ignoreDiacritics) {
text = text
.replace(/[ÀÁÂÃÄÅ]/g, 'A')
.replace(/[àáâãäå]/g, 'a')
.replace(/[ÈÉÊË]/g, 'E')
.replace(/[èéêë]/g, 'e')
.replace(/[Í]/g, 'I')
.replace(/[í]/g, 'i')
.replace(/[ÓÖ]/g, 'O')
.replace(/[óö]/g, 'o')
.replace(/[ÚÜ]/g, 'U')
.replace(/[úü]/g, 'u')
.replace(/[Ñ]/g, 'N')
.replace(/[ñ]/g, 'n')
.replace(/[ß]/g, 's')
;
}
return text;
},

bindEvents: function() {
Expand All @@ -65,6 +90,10 @@
that.search( $( this ).val() );

that.updateStriping();

if ( afterSearchCallback ) {
that.settings.onAfterSearch();
}
});

if ( searchFocusCallback ) {
Expand Down Expand Up @@ -100,7 +129,7 @@
},

search: function( term ) {
var matcher, elemCount, children, childCount, hide, $elem, i, x;
var matcher, elemCount, children, childCount, hide, $elem, i, x, contentText;

if ( $.trim( term ).length === 0 ) {
this.$searchElems.css( 'display', '' );
Expand All @@ -116,6 +145,7 @@
}

elemCount = this.$searchElems.length;
term = this.removeDiacritics(term);
matcher = this.matcherFunc( term );

for ( i = 0; i < elemCount; i++ ) {
Expand All @@ -125,7 +155,9 @@
hide = true;

for ( x = 0; x < childCount; x++ ) {
if ( matcher( $( children[ x ] ).text() ) ) {
contentText = $( children[ x ] ).text();
contentText = this.removeDiacritics(contentText);
if ( matcher( contentText ) ) {
hide = false;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jquery.searchable",
"title": "jQuery Searchable",
"version": "1.1.0",
"version": "1.1.1",
"description": "Tiny, fast jQuery plugin to search through elements as you type.",
"main": "jquery.searchable.js",
"scripts": {},
Expand Down