function stristr (haystack, needle, bool) { // http://kevin.vanzonneveld.net // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + bugfxied by: Onno Marsman // * example 1: stristr('Kevin van Zonneveld', 'Van'); // * returns 1: 'van Zonneveld' // * example 2: stristr('Kevin van Zonneveld', 'VAN', true); // * returns 2: 'Kevin ' var pos = 0; haystack += ''; pos = haystack.toLowerCase().indexOf( (needle+'').toLowerCase() ); if (pos == -1){ return false; } else{ if (bool) { return haystack.substr( 0, pos ); } else{ return haystack.slice( pos ); } } } function stripos ( f_haystack, f_needle, f_offset ){ // http://kevin.vanzonneveld.net // + original by: Martijn Wieringa // + revised by: Onno Marsman // * example 1: stripos('ABC', 'a'); // * returns 1: 0 var haystack = (f_haystack+'').toLowerCase(); var needle = (f_needle+'').toLowerCase(); var index = 0; if ((index = haystack.indexOf(needle, f_offset)) !== -1) { return index; } return false; } String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }; Ext.namespace( 'RS' ); RS.TextFinder = { // init init : function() { RS.TextFinder.table = Ext.select( 'table[tabPrice]' ); //console.dir( RS.TextFinder.table ); RS.TextFinder.textNodes = Ext.query( 'td[class*=item]' ); RS.TextFinder.lastHiddenNodes = []; RS.TextFinder.input = Ext.get( Ext.DomQuery.selectNode( 'input[name=search]' ) ); RS.TextFinder.input.dom.value = ''; RS.TextFinder.input.on( 'keyup', RS.TextFinder.doSearch ); Ext.fly( 'butSearch' ).on( 'click', RS.TextFinder.searchText ); // если к нам пришли по ссылке из поисковика, то попробуем подстветить нужную строчку в прайсе RS.TextFinder.highlightRefererItem(); } // showLastHiddenNode , showLastHiddenNode : function() { Ext.each( RS.TextFinder.lastHiddenNodes, function( item ) { //item.clearOpacity(); //item.setOpacity(1); item.setDisplayed( true ); } ); } // doSearch , doSearch : function( aEvent, aTarget ) { //console.log( '>>> ' + RS.TextFinder.input.getValue() ); //console.log( aEvent.getCharCode() ); var charCode = aEvent.getCharCode(); if ( charCode == 8 /*BackSpace*/ && Ext.isEmpty( RS.TextFinder.input.dom.value ) || charCode == 27 /*Esc*/ ) { RS.TextFinder.input.dom.value = ''; RS.TextFinder.searchText(); return; } if ( charCode != 13 ) { return; } try { clearTimeout( RS.TextFinder.keyupTimeout ); } catch(e){} RS.TextFinder.keyupTimeout = RS.TextFinder.searchText.defer( 500 ); } // searchText , searchText : function( aText ) { RS.TextFinder.showLastHiddenNode(); RS.TextFinder.lastHiddenNodes = []; if ( !Ext.isString( aText ) ) { aText = RS.TextFinder.input.dom.value; } if ( aText.trim() == '' ) { document.location.hash = ''; RS.TextFinder.input.focus(); return; } var hiddenCount = 0 , firstElement = null , lastH2 = null , lastH3 = null , h2HasItem = false , h3HasItem = false , i = 0 , n = RS.TextFinder.textNodes.length ; if ( n > 0 ) { do { item = Ext.get( RS.TextFinder.textNodes[i] ); if ( !Ext.isEmpty( item.child( 'h2' ) ) || i == n - 1 ) { // оставляем заголовочные строки в таблице if ( !h2HasItem && lastH2 ) { RS.TextFinder.lastHiddenNodes[ hiddenCount++ ] = lastH2.parent( 'tr' ); } if ( !h3HasItem && lastH3 ) { RS.TextFinder.lastHiddenNodes[ hiddenCount++ ] = lastH3.parent( 'tr' ); } lastH2 = item; lastH3 = null; h2HasItem = false; h3HasItem = false; if ( i < n - 1 ) { continue; } } if ( !Ext.isEmpty( item.child( 'h3' ) ) || i == n - 1 ) { // оставляем заголовочные строки в таблице if ( !h3HasItem && lastH3 ) { RS.TextFinder.lastHiddenNodes[ hiddenCount++ ] = lastH3.parent( 'tr' ); } lastH3 = item; h3HasItem = false; if ( i < n - 1 ) { continue; } } if ( stripos( item.dom.innerHTML, aText ) !== false ) { // строка содержит искомый текст if ( Ext.isEmpty( firstElement ) ) { firstElement = item; } h2HasItem = true; h3HasItem = true; //console.log( item.dom.innerHTML ); continue; } RS.TextFinder.lastHiddenNodes[ hiddenCount++ ] = item.parent( 'tr' ); } while( ++i < n ); } if ( Ext.isEmpty( firstElement ) ) { RS.TextFinder.lastHiddenNodes = []; document.location.hash = ''; alert( 'Ничего не найдено...' ); return; } // гасим строчки, которые не содержат искомого текста Ext.each( RS.TextFinder.lastHiddenNodes, function( item ) { item.setDisplayed( false ); //hide() ;//setOpacity( 0.1 ); } ); // Перемещаемся на первый найденный узел firstElement = firstElement.parent( 'tr' ).child( 'a[class=nr]' ); if ( !Ext.isEmpty( firstElement ) ) { //console.log( firstElement.getAttribute( 'name' ) ); document.location.hash = firstElement.getAttribute( 'name' ); //RS.TextFinder.input.focus(); } /* //console.log( '<<< ' + aText ); // ищем первый узел, который содержит нужный нам текст var firstElement = RS.TextFinder.table.child( 'td:contains(' + aText + ')' ); if ( Ext.isEmpty( firstElement ) ) { alert( 'Ничего не найдено...' ); return; } //console.dir( firstElement ); // Перемещаемся на первый найденный узел firstElement = firstElement.parent( 'tr' ).child( 'a[class=nr]' ); if ( !Ext.isEmpty( firstElement ) ) { //console.log( firstElement.getAttribute( 'name' ) ); document.location.hash = firstElement.getAttribute( 'name' ); } // Отбираем узлы, которые НЕ содержат искомый текст var noTextNodes = Ext.DomQuery.filter( RS.TextFinder.textNodes, 'td:contains(' + aText + ')', true ); // убираем из этого списка заголовки, чтобы они остались на экране noTextNodes = Ext.DomQuery.filter( noTextNodes, 'td[class*=hd]', true ); // гасим строки, не содержащие искомый текст Ext.each( noTextNodes, function( item, i ) { var node = Ext.get( item ); parent = node.parent( 'tr' ) ; RS.TextFinder.lastHiddenNodes[i] = node; } ); */ } // highlightPriceItem , highlightPriceItem : function( aItemNum ) { document.location.hash = '0' + aItemNum; var itemNode = Ext.fly( Ext.DomQuery.selectNode( 'a[name=' + aItemNum + ']' ) ); if ( itemNode ) { itemNode.parent( 'tr' ).frame( '#ff0000', 2 ).highlight(); } } // highlightRefererItem , highlightRefererItem : function() { if ( Ext.isEmpty( window.document.referrer ) ) return; var ref = decodeURIComponent( window.document.referrer ).toLowerCase() , ref2item = { 'шитрок' : '9010', 'sheetrock' : '9010' , 'штрок' : '9010', 'шетрок' : '9010', 'щитрок' : '9010' , 'про-форм' : '9012', 'proform' : '9012', 'проформ' : '9012' , 'шпатлевка' : '9010', 'шпаклевка' : '9010' , 'модель 15' : '1200', 'форпост 15' : '1200' , 'модель+15' : '1200', 'форпост+15' : '1200' , 'модель15' : '1200', 'форпост15' : '1200' , 'модель 11' : '1201', 'форпост 11' : '1201' , 'модель+11' : '1201', 'форпост+11' : '1201' , 'модель11' : '1201', 'форпост11' : '1201' , 'модель 21' : '1202', 'форпост 21' : '1202' , 'модель+21' : '1202', 'форпост+21' : '1202' , 'модель21' : '1202', 'форпост21' : '1202' , 'модель 58' : '1203', 'форпост 58' : '1203' , 'модель+58' : '1203', 'форпост+58' : '1203' , 'модель58' : '1203', 'форпост58' : '1203' , 'модель 18' : '1204', 'форпост 18' : '1204' , 'модель+18' : '1204', 'форпост+18' : '1204' , 'модель18' : '1204', 'форпост18' : '1204' , 'модель 17' : '1205', 'форпост 17' : '1205' , 'модель+17' : '1205', 'форпост+17' : '1205' , 'модель17' : '1205', 'форпост17' : '1205' , 'модель 111' : '1206', 'форпост 111' : '1206' , 'модель+111' : '1206', 'форпост+111' : '1206' , 'модель111' : '1206', 'форпост111' : '1206' , 'модель 12' : '1207', 'форпост 12' : '1207' , 'модель+12' : '1207', 'форпост+12' : '1207' , 'модель12' : '1207', 'форпост12' : '1207' , 'модель 17ВК' : '1208', 'модель 17 ВК' : '1208', 'модель 17BK' : '1208', 'модель 17 BK' : '1208' , 'форпост 17ВК' : '1208', 'форпост 17 ВК' : '1208', 'форпост 17BK' : '1208', 'форпост 17 BK' : '1208' , 'модель17ВК' : '1208', 'модель 17ВК' : '1208', 'модель17BK' : '1208', 'модель 17BK' : '1208' , 'форпост17ВК' : '1208', 'форпост 17ВК' : '1208', 'форпост17BK' : '1208', 'форпост 17BK' : '1208' , 'модель+17ВК' : '1208', 'модель+17+ВК' : '1208', 'модель+17BK' : '1208', 'модель+17+BK' : '1208' , 'форпост+17ВК' : '1208', 'форпост+17+ВК' : '1208', 'форпост+17BK' : '1208', 'форпост+17+BK' : '1208' , 'модель+17ВК' : '1208', 'модель+17BK' : '1208' , 'форпост+17ВК' : '1208', 'форпост+17BK' : '1208' , 'модель 37' : '1209', 'форпост 37' : '1209' , 'модель+37' : '1209', 'форпост+37' : '1209' , 'модель37' : '1209', 'форпост37' : '1209' , 'модель 58CC' : '1210', 'модель 58 CC' : '1210', 'модель 58СС' : '1210', 'модель 58 СС' : '1210' , 'форпост 58CC' : '1210', 'форпост 58 CC' : '1210', 'форпост 58СС' : '1210', 'форпост 58 СС' : '1210' , 'модель+58CC' : '1210', 'модель+58+CC' : '1210', 'модель+58СС' : '1210', 'модель+58+СС' : '1210' , 'форпост+58CC' : '1210', 'форпост+58+CC' : '1210', 'форпост+58СС' : '1210', 'форпост+58+СС' : '1210' , 'модель 27' : '1211', 'форпост 27' : '1211' , 'модель+27' : '1211', 'форпост+27' : '1211' , 'модель27' : '1211', 'форпост27' : '1211' , 'модель 32' : '1212', 'форпост 32' : '1212' , 'модель+32' : '1212', 'форпост+32' : '1212' , 'модель32' : '1212', 'форпост32' : '1212' , 'модель СМ' : '1213', 'модель CM' : '1213', 'модель SM' : '1213' , 'модель+СМ' : '1213', 'модель+CM' : '1213', 'модель+SM' : '1213' , 'модельСМ' : '1213', 'модельCM' : '1213', 'модельSM' : '1213' , 'модель 128' : '1214', 'форпост 128' : '1214' , 'модель+128' : '1214', 'форпост+128' : '1214' , 'модель128' : '1214', 'форпост128' : '1214' , 'двер' : '1200', 'форпост' : '1200' } ; //console.log( ref ); for ( var key in ref2item ) { if ( ref.indexOf( key ) != -1 ) { (function() { var anchorName = ref2item[ key ]; RS.TextFinder.highlightPriceItem( anchorName ); }).defer( 2000 ); break; } } } } Ext.onReady( RS.TextFinder.init );