/** * search jQuery Plug-in * * Copyright (c) 2011, Nikolay Martynenko * * Licensed under The MIT License which can be obtained from * * search is a jQuery Plug-in enables easy search and highlight/replace manipulation over the DOM text nodes. * * It uses jQuery textSelect plugin for highlighting of search result(otherwise it highlights whole element). * http://opensource.csimon.info/#page=textSelect * * It uses jQuery scrollTo plugin for smooth scrolling to selected element. * http://flesler.blogspot.com/2007/10/jqueryscrollto.html */ !function(e){e.expr[":"].containsi=function(e,t,n){return jQuery(e).text().toUpperCase().indexOf(n[3].toUpperCase())>=0},e.extend(e,{search:function(t,n){if("string"==typeof t||t.tagName?t=e(t):t.jquery||(t=!1),!t||!t.length)return void(n&&n.debug&&window.console&&console.warn("nothing selected, can't search, returning nothing"));var i=e.data(t,"searcher");return i||(i=new e.searcher(n,t),e.data(t,"searcher",i)),i.settings.createOnly===!1&&i.nextConcurrence(),i}}),e.extend(e.fn,{search:function(t){return e.search(this,t)}}),e.searcher=function(t,n){this.settings=e.extend(!0,{},e.searcher.defaults,t),this.currentContainer=n,this.currentPosition=this.settings.position,this.settings.onCreate.apply(this)},e.extend(e.searcher,{defaults:{debug:!1,createOnly:!0,caseSensitive:!1,searchSelector:"",position:-1,searchOrder:"down",searchType:"highlight",text:null,replaceBy:"",scrollTo:!1,onCreate:function(){},beforeSearch:function(){},afterSearch:function(){}},prototype:{reset:function(){this.currentPosition=-1},getText:function(){return this.settings.text},setText:function(e){this.settings.text=e,this.reset()},getPosition:function(){return this.settings.position},setPosition:function(e){e>=this.getConcurrencesNumber()&&(e=-1),this.settings.position=e},getSearchOrder:function(){return"up"===this.settings.searchOrder||this.settings.searchOrder===!0?"up":"down"},setSearchOrder:function(e){this.settings.position="up"===e?"up":"down"},getSearchType:function(){return this.settings.searchType},setSearchType:function(e){this.settings.searchType="replace"===e?"replace":"highlightSelected"===e?"highlightSelected":"highlight"},getConcurrencesNumber:function(){if(!this.settings.text)return void this._debug("Empty string so nothing found");var e=this._escape(this.settings.text);return this._filter(e).length},nextConcurrence:function(e){var t=this.getConcurrencesNumber();if(0==t)return void this._debug("Nothing has been found");var n=this.currentPosition,i=this._getProperty("searchOrder",e);1!=t&&(n=i===!0||"up"===i?0>=n?t-1:n-1:n==t-1?0:n+1),this.findConcurrence(n,this.settings.searchType)},findConcurrence:function(t,n){if(this.settings.beforeSearch.apply(this),!this.settings.text)return void this._debug("Text shouldn't be empty");t=this._getProperty("startPosition",t),n=this._getProperty("searchType",n);var i=this._escape(this.settings.text),s=this._filter(i).eq(t);"replace"===n?this._replace(s):"highlightSelected"===n&&e.fn.textSelect?this._highlightSelected(s):this._highlightAll(s),this.currentPosition=t,this.settings.scrollTo&&e.fn.scrollTo&&this.currentContainer.scrollTo(s.parent()),this.settings.afterSearch.apply(this,e.makeArray(s))},_filter:function(e){if(this.settings.searchSelector)var t=this.currentContainer.find(this.settings.searchSelector);else var t=this.currentContainer;var n=this.settings.caseSensitive?"contains":"containsi";return t.contents(":"+n+"('"+e+"')")},_highlightSelected:function(t){var n=t.text(),i=this.settings.text;this.settings.caseSensitive||(n=n.toUpperCase(),i=i.toUpperCase());var s=n.indexOf(i),r=s+i.length;e.textSelect("setRange",{start:s,startElement:t.parent(),end:r,endElement:t.parent()})},_highlightAll:function(t){e.fn.textSelect?t.parent().textSelect("select"):this._highlightAllInternal(t)},_highlightAllInternal:function(t){var n=t.parent()[0];if(e.browser.msie){var i=document.body.createTextRange();i.moveToElementText(n),i.select()}else if(e.browser.mozilla||e.browser.opera){var s=window.getSelection(),i=document.createRange();i.selectNodeContents(n),s.removeAllRanges(),s.addRange(i)}else if(e.browser.safari){var s=window.getSelection();s.setBaseAndExtent(n,0,n,1)}},_replace:function(e){var t=e.text().replace(this.settings.text,this.settings.replaceBy);e.parent().text(t)},_getProperty:function(e,t){return null==t||"undefined"==typeof t?this.settings[e]:t},_escape:function(e){return e.replace(/([#;&,\.\+\*\~':"\!\^$\[\]\(\)=>\|])/g,"\\$1")},_debug:function(e){this.settings.debug&&window.console&&console.debug(e)}}})}(jQuery);