UNPKG

responsivewebframework

Version:

Jalasoft Foundation Front End Framework ========================================

367 lines (316 loc) 13.3 kB
frameworkObject.searchBox = new function() { var InputSearcherBox = function(idJsearchBox){ var varCurrentKey = ""; var selectArrowOn = false; var idSearchBox=idJsearchBox; var idContent = idJsearchBox+"Content"; var suggestion; var indexSuggestion = -1; var arraySuccess = []; var word; var indexDatas = -1; var sizeDatas = 0; var placeholder = $('#'+idSearchBox).attr("dataplaceholder");; var width = ""+$('#'+idSearchBox).attr("size"); var errorMargin = 15; var matches = function(word) { var numSuccess; var numAlgoritm; var word = word; this.getWord = function() { return word; }; this.getNumSuccess= function(){ return numSuccess; }; this.getNumAroritm = function(){ return numAlgoritm; } this.successes = function(s1) { this.successesForSuggestion(s1); var s2 = word; if (s1 == s2) { numAlgoritm = 0; } var s1_len = s1.length; var s2_len = s2.length; if (s1_len === 0) { numAlgoritm = s2_len; } if (s2_len === 0) { numAlgoritm = s1_len; } var split = false; try { split = !('0')[0]; } catch (e) { split = true; } if (split) { s1 = s1.split(''); s2 = s2.split(''); } var v0 = new Array(s1_len + 1); var v1 = new Array(s1_len + 1); var s1_idx = 0, s2_idx = 0, cost = 0; for (s1_idx = 0; s1_idx < s1_len + 1; s1_idx++) { v0[s1_idx] = s1_idx; } var char_s1 = '', char_s2 = ''; for (s2_idx = 1; s2_idx <= s2_len; s2_idx++) { v1[0] = s2_idx; char_s2 = s2[s2_idx - 1]; for (s1_idx = 0; s1_idx < s1_len; s1_idx++) { char_s1 = s1[s1_idx]; cost = (char_s1 == char_s2) ? 0 : 1; var m_min = v0[s1_idx + 1] + 1; var b = v1[s1_idx] + 1; var c = v0[s1_idx] + cost; if (b < m_min) { m_min = b; } if (c < m_min) { m_min = c; } v1[s1_idx + 1] = m_min; } var v_tmp = v0; v0 = v1; v1 = v_tmp; } numAlgoritm = v0[s1_len]; } this.successesForSuggestion = function (textToSearch){ if(word.toLowerCase().indexOf(textToSearch.toLowerCase())!==-1 && textToSearch!==" ") { numSuccess=textToSearch.length; } else { numSuccess=0; } } } $('#'+idSearchBox).attr("id",idContent); var datas=[]; { $('#'+idContent).find('.item').each(function(index, element) { datas.push($(element).val()); }); selectArrowOn = true; var addText = ''+ '<div class="container-searcher-element" id="'+idContent+'">' + '<div class="ico-searcher-list"><span class="icon-lupa"></span></div>' + '<div class="container-searcher-box">' + '<div class="search-box">' + '<div class="searcher-container ">' + '<span id="searcher-placeholder" class="searcher-placeholder "></span>' + '<div id="searcher-text" class="searcher-text" >' + '<input type="text" value="" name="" id="'+idSearchBox+'" style="width:'+width+'" class="input-searcher-text" onKeyUp="frameworkObject.searchBox.runShowPlaceHolder('+"'"+idSearchBox+"'"+');" onFocus="frameworkObject.searchBox.focus('+"'"+idSearchBox+"'"+');" onBlur="frameworkObject.searchBox.focusOff('+"'"+idSearchBox+"'"+');">' + '</div>' + '</div>' + '</div>' + '</div>' + '<div class="ico-exit-searcher"><span onClick=" frameworkObject.searchBox.showsearchexit('+"'"+idSearchBox+"'"+');" class="icon-equis"></span></div>' + '</div>' + '<div class="searchlist" style="width:'+width+'" >' + '</div>'; $('#'+idContent).append(addText); $('#'+idContent).find(".searcher-placeholder").text( placeholder); $('#'+idContent).find('.searcher-container').css("display","inherit"); var maxCaracterResult = (width.substring(0,width.length-2)/8); datas = datas.sort(); for(var i =0; i<datas.length ; i++) { arraySuccess[i] = new matches(datas[i].substring(0,maxCaracterResult)); } } this.focus = function(){ $('#'+idContent).find('.container-searcher-element').css("border","solid blue 1px"); }; this.focusOff = function(){ $('#'+idContent).find('.container-searcher-element').css("border","solid rgb(155, 155, 155) 1px"); }; this.showText = function() { if(word.length == 0) { $('#'+idContent).find('.exit-search').css("opacity",0); } if(word.length > 0) { $('#'+idContent).find('.exit-search').css("opacity",1); } }; this.showSearchList = function(arraySuccess,div) { indexDatas = -1; $('#'+idContent).find('.searchlist').css("display","none"); $('#'+idContent).find(div).text(""); var itemId = ""; sizeDatas = 0; if(word.trim().length < 2 ) { return 0; } if(indexSuggestion>-1) { $('#'+idContent).find('.searchlist').css("display","inherit"); itemId = "itemsearch"+sizeDatas; $('#'+idContent).find(div).append("<div class='searcher-item' id='"+itemId+"' onClick='frameworkObject.searchBox.changevalue("+'"'+idSearchBox+'",this)'+"'> <p class= 'searcher-item-text'>"+arraySuccess[indexSuggestion].getWord()+"</p></div>"); sizeDatas++; } for(var i=0; i<arraySuccess.length; i++) { if(arraySuccess[i].getNumAroritm() < errorMargin && i !== indexSuggestion) { $('#'+idContent).find('.searchlist').css("display","inherit"); itemId = "itemsearch"+sizeDatas; $('#'+idContent).find(div).append("<div class='searcher-item' id='"+itemId+"' onClick='frameworkObject.searchBox.changevalue("+'"'+idSearchBox+'",this)'+"'><p class= 'searcher-item-text'>"+arraySuccess[i].getWord()+"</p></div>"); sizeDatas++; } } }; this.changevalue = function(id){ $('#'+idContent).find('.searchlist').css("display","none"); $('#'+idSearchBox).val($(id).text()); $('#'+idContent).find('.searcher-placeholder').text(""); $('#'+idSearchBox).focus(); suggestion=""; }; this.search = function(datas,textToSearch) { for( var i = 0; i<datas.length; i++) { datas[i].successes(textToSearch) } }; this.maxSuccesses = function(arraySuccess){ var max=0; var position = 0; var result = ""; indexSuggestion = -1; for( var i = 0; i<arraySuccess.length; i++) { if(max<arraySuccess[i].getNumSuccess()) { max=arraySuccess[i].getNumSuccess(); position = i; } } if (0 + max > 0) { result = arraySuccess[position].getWord(); if(word.toLowerCase()==result.substring(0,word.length).toLowerCase()) { result = result.substring(word.length, result.length); result = word + result; indexSuggestion = position; } else { result=""; } } return result; }; this.showsearchexit = function(idDiv,idWord) { $('#'+idSearchBox).val(""); $('#'+idContent).find('.ico-exit-searcher').css("opacity",0); $('#'+idContent).find('.searcher-placeholder').text(placeholder); $('#'+idContent).find('.searchlist').css("display","none"); }; this.runShowPlaceHolder = function() { selectArrowOn = true; //window.onkeydown = selectArrow; if (varCurrentKey !== "down" && varCurrentKey !== "up") { suggestion =this.maxSuccesses(arraySuccess); word = $('#'+idSearchBox).val(); $('#'+idContent).find('.searchlist').css("display","none"); this.showSearchList(arraySuccess, '.searchlist'); this.showText(); this.search(arraySuccess, word); suggestion = this.maxSuccesses(arraySuccess); this.showSearchList(arraySuccess, '.searchlist'); } if(word.length==0) { $('#'+idContent).find(".searcher-placeholder").text( placeholder); $('#'+idContent).find('.ico-exit-searcher').css("opacity",0); } else { $('#'+idContent).find('.ico-exit-searcher').css("opacity",1); $('#'+idContent).find(".searcher-placeholder").text( suggestion); } if (varCurrentKey == "enter") { varCurrentKey = ""; $('#'+idContent).find('.searchlist').css("display","none"); sizeDatas=0; } if (varCurrentKey == "right") { varCurrentKey = ""; sizeDatas=0; if (suggestion != "") { $('#'+idSearchBox).val(suggestion); $('#'+idContent).find('.searchlist').css("display","none"); } } if (varCurrentKey == "down" || varCurrentKey == "up" ) { suggestion=""; if(varCurrentKey == "down") { indexDatas++; if(indexDatas>sizeDatas-1) { indexDatas=0; } } if(varCurrentKey == "up") { indexDatas--; if(indexDatas<0) { indexDatas=sizeDatas-1; } } for(var i=0; i<sizeDatas; i++){ $('#'+idContent).find("#itemsearch"+i).css("background","#fff"); } var itemId; itemId = "#itemsearch" + indexDatas; var textIndex = $(itemId).text(); textIndex = textIndex.trim(); $('#'+idContent).find(".searcher-placeholder").text(textIndex); $('#'+idContent).find(itemId).css("background","#c0c0c0"); $('#'+idContent).find('#'+idSearchBox).val(textIndex); } } $('#'+idSearchBox).keydown(function(e){ var keyCode = document.all ? e.which : e.keyCode; switch(keyCode) { case 39: varCurrentKey = "right"; break; case 40: varCurrentKey = "down"; break; case 38: varCurrentKey = "up"; break; case 13: varCurrentKey = "enter"; break; default: varCurrentKey = ""; } }); }; var searchBox = []; this.add = function(id){ searchBox[id] = new InputSearcherBox(id); }; this.runShowPlaceHolder = function(id) { searchBox[id].runShowPlaceHolder(); }; this.focus = function(id){ searchBox[id].focus(); }; this.focusOff = function(id) { searchBox[id].focusOff(); }; this.showsearchexit = function(id) { searchBox[id].showsearchexit(); }; this.changevalue = function(id,idsearch) { searchBox[id].changevalue(idsearch); }; } //frameworkObject.searchBox = new ManagerSearchBox(); jQuery.fn.extend({ frameworkSearchBox: function() { $(this).each(function(index, element) { var id = $(element).attr('id'); frameworkObject.searchBox.add(id); }) } });