diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d210807 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.project +.settings +*.swp +.DS_Store diff --git a/README.md b/README.md index 22ef4a4..2c9ce8a 100644 --- a/README.md +++ b/README.md @@ -10,11 +10,7 @@ javascript @@ -22,24 +18,24 @@ html

Table test.

- - - - - - - - - - - - - - - - - -
namevalue
SatoTaro
SatoHanako
SuzukiJiro
田中半兵衛
+ + + + + + + + + + + + + + + + + +
namevalue
SatoTaro
SatoHanako
SuzukiJiro
田中半兵衛
@@ -56,4 +52,4 @@ Contact UshioShugo #TODO list -1. Ignore table header. +2. Add select html element. diff --git a/jquery.listSearch.js b/jquery.listSearch.js new file mode 100644 index 0000000..e5426fa --- /dev/null +++ b/jquery.listSearch.js @@ -0,0 +1,186 @@ +(function(jQuery){ + var ListSearch = function() {}; + ListSearch.prototype = { + /** + * run + * start incremental search. + * @param {} input + * @param {} target + * @returns {} + */ + run: function(input, target){ + var _this = this; + var tagName = target.get(0).tagName; + + input.on('keyup', function(e){ + text = _this.getInputText(input); + + switch(tagName){ + case 'TABLE': + _this.listSearchTable(target, text); + break; + case 'UL': + _this.listSearchListUl(target, text); + break; + case 'OL': + _this.listSearchListOl(target, text); + break; + case 'DL': + _this.listSearchListDl(target, text); + break; + default: + throw new Error('Illegal tag name ' + targetObject.targetTagName); + } + }); + }, + + getInputText: function(input){ + return input.val(); + }, + + /** + * tag table + */ + listSearchTable: function(target, text){ + this.listSearchCommon('tr', target, text); + }, + + /** + * tag ul + */ + listSearchListUl: function(target, text){ + this.listSearchCommon('li', target, text); + }, + + /** + * tag ol + */ + listSearchListOl: function(target, text){ + return this.listSearchListUl(target, text); + }, + + /** + * tag dl + */ + listSearchListDl: function(target, text){ + this.listSearchCommon('dd dt', target, text); + }, + + /** + * commondSearchList + */ + listSearchCommon: function(tagName ,target, text){ + var _this = this; + var searchWords = this.searchWordToWords(text); + var wordLength = searchWords.length; + + target.find(tagName).each(function(){ + var thisJQuery = jQuery(this); + if (thisJQuery.data('ignore-list-search') === 'ignore') return true; + + var body = _this.getBody(thisJQuery); + var displayFlag = true; + + var wordCount = 0; + for(wordCount = 0; wordCount < wordLength; wordCount++){ + var word = searchWords[wordCount]; + + var pattern = new RegExp(word, 'i'); + if ( !body.match(pattern) ) { + displayFlag = false; + break; + } + } + + jQuery(this).css('display', _this.getDisplayProperty(tagName, displayFlag)); + return true; + }) + }, + + /** + * getDisplayProperty + * @param {} tagName + * @param {} flag + * @returns {} + */ + getDisplayProperty: function(tagName, flag){ + switch(tagName){ + case 'tr': + return flag?'table-row':'none'; + case 'li': + return flag?'list-item':'none'; + case 'dd dt': + return flag?'list-item':'none'; + default: + throw new Error('Illegal tag name ' + targetObject.targetTagName); + } + }, + + /** + * getBody + * @returns {} + */ + getBody: function(target){ + var body = target.text(); + return jQuery.trim(body); + }, + + /** + * searchWordToWords + * a search text split to search words. + * @param {} body + * @param {} searchWord + * @returns {} + */ + searchWordToWords: function(text){ + text = jQuery.trim(text); + var pattern = new RegExp(/[  \-\/]/); + var words = text.split(pattern); + + // delete empty element + var newWords = new Array(); + var wordsLength = words.length; + var wordsCount = 0; + for (wordsCount = 0; wordsCount < wordsLength; wordsCount++){ + var word = words[wordsCount]; + if (word != ""){ + newWords.push(words[wordsCount]); + } + } + return newWords; + } + } + + /** + * Main stream + */ + jQuery.fn.listSearch = function(input, options){ + var options = jQuery.extend(jQuery.fn.listSearch.defaults, options); + + // set using objects. + var target = jQuery(this); + switch (jQuery.type(input)){ + case 'string': + input = jQuery(input); + break; + case 'object': + input = input; + break; + default: + throw 'Argiment value "' + input + '" is invalid.'; + } + + // Event start + listSearch = new ListSearch(); + listSearch.run(input, target); + + return target; + }; + + /** + * default settings. + */ + jQuery.fn.listSearch.defaults = { + + }; +})(jQuery); diff --git a/jquery.listSearch.min.js b/jquery.listSearch.min.js new file mode 100644 index 0000000..b8e718b --- /dev/null +++ b/jquery.listSearch.min.js @@ -0,0 +1,4 @@ +(function(d){var f=function(){};f.prototype={run:function(a,b){var c=this,d=b.get(0).tagName;a.on("keyup",function(h){text=c.getInputText(a);switch(d){case "TABLE":c.listSearchTable(b,text);break;case "UL":c.listSearchListUl(b,text);break;case "OL":c.listSearchListOl(b,text);break;case "DL":c.listSearchListDl(b,text);break;default:throw Error("Illegal tag name "+targetObject.targetTagName);}})},getInputText:function(a){return a.val()},listSearchTable:function(a,b){this.listSearchCommon("tr",a,b)}, +listSearchListUl:function(a,b){this.listSearchCommon("li",a,b)},listSearchListOl:function(a,b){return this.listSearchListUl(a,b)},listSearchListDl:function(a,b){this.listSearchCommon("dd dt",a,b)},listSearchCommon:function(a,b,c){var e=this,h=this.searchWordToWords(c),f=h.length;b.find(a).each(function(){var b=d(this);if("ignore"===b.data("ignore-list-search"))return!0;for(var b=e.getBody(b),c=!0,g=0,g=0;g< configLength; configCnt ++){ - // chack target tag type - var target = $(config[configCnt]['target']); - - // get input jquery object; - var input = $(config[configCnt]['input']); - - // Event start - listSearch = new ListSearch(); - listSearch.start(input, target); - - // create new Object. - var targets = { - 'input': input, - 'target': target, - 'listSearchObject': listSearch - } - - config[configCnt] = targets; - } - - return $(this); - }; - - /** - * default settings. - */ - $.fn.listSearch.defaults = { - - }; -})(jQuery); \ No newline at end of file diff --git a/listsearch.jquery.json b/listsearch.jquery.json index 7f4ef97..cc5257d 100644 --- a/listsearch.jquery.json +++ b/listsearch.jquery.json @@ -1,6 +1,6 @@ { "name": "listsearch", - "version": "0.1.0", + "version": "0.4.1", "title": "jQuery List Search", "author": { "name": "Ushio Shugo", @@ -16,11 +16,12 @@ "url": "http://www.opensource.org/licenses/mit-license.php" } ], - "description": "Incremental search for table, ul and ol tags. Perfect match only.", + "bugs": "https://github.com/ushios/jQuery.ListSearch/issues", + "docs": "https://github.com/ushios/jQuery.ListSearch/blob/master/README.md", + "homepage": "https://github.com/ushios/jQuery.ListSearch", + "description": "Incremental search for table, ul and ol tags.", "keywords": ["incremental", "search", "list", "table", "ul", "li", "ol"], - "homepage": "", - "demo": "", "dependencies": { - "jquery": ">=1.9.0" + "jquery": ">=1.7.0" } } diff --git a/index.html b/sample.html similarity index 76% rename from index.html rename to sample.html index 558b31e..d31f37e 100644 --- a/index.html +++ b/sample.html @@ -3,8 +3,8 @@ jQuery.ListSearch - - + + @@ -12,20 +12,16 @@

Table test.

- + @@ -63,4 +59,4 @@

Number list test.

- \ No newline at end of file +
namevalue