// // fasza, nagyon fasza. blog.hu nem enged .js fajlt feltolteni, csak .txt. Akor azt kapjak. Be. // // A record in the dictionary function DictRec(line, prevRec) { // this.indexWord; // (amire ténylegesen keresni lehet autocomplete-tel) // this.indexText; //(ami megjelenik az autocomplete-ben, benne az (fn.) jellegű cucc formázva) // this.translation; // (a tab utáni rész) // Constructor parts = line.split(/\t/); this.indexWord = parts[0].replace(/\s*\(.*\)|\[.*\]/g,""); this.indexText = parts[0].replace(/\((.*)\)/g,"($1)"); this.translation = parts[1].replace(/=(.*?)=/g, "→ $1"); this.prevRec = prevRec; if (prevRec != null) { prevRec.nextRec = this; } this.toString = function() { return this.indexText+" -- "+this.indexWord+" -- "+this.translation; } } // Dictionary object function Dict() { var dbContent; var dbRecs = []; this.loadDb = function(dictUrlOrArray, callback) { if ($.isArray(dictUrlOrArray)) { parseDb(dictUrlOrArray); callback(dbRecs); } else { $.ajax({ url : dictUrlOrArray, success : function (data) { lines = data.split(/\n/); parseDb(lines); callback(dbRecs); } }); } } this.getRecords = function() { return dbRecs; } this.search = function(word) { $("#search-field").val(word); $("#search-field").search(); } function parseDb(lines) { prevRec = null; $.each(lines, function(index, value) { prevRec = dbRecs[index] = new DictRec(value, prevRec); //alert(index+": "+dbRecs[index]); }); } } function loadDict(dictUrlOrArray) { dict = new Dict(); dict.loadDb(dictUrlOrArray, function(recs) { $("#search-field").autocomplete(recs, { formatItem: function(item) { return item.indexText; }, formatResult: function(item) { //alert(item); return item.indexWord; }, multipleSeparator: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" }).result(function(event, item) { $("#search-result").show(); if (item == null) { $("#dict-entry").html("Nem található szó."); //window.location.hash = null; } else { $("#dict-entry").html(""+item.indexText+": "+item.translation+""); if (item.prevRec != null) { $("#search-prev").html("← "+item.prevRec.indexText) .unbind("click") .click(function(){$("#search-field").val(item.prevRec.indexWord).search()}) } else { $("#search-prev").html(null); } if (item.nextRec != null) { $("#search-next").html(item.nextRec.indexText+" →") .unbind("click") .click(function(){$("#search-field").val(item.nextRec.indexWord).search()}) } else { $("#search-next").html(null); } window.location.hash = "q="+item.indexWord; } }); function getHashParams() { var hashParams = {}; var e, a = /\+/g, // Regex for replacing addition symbol with a space r = /([^&;=]+)=?([^&;]*)/g, d = function (s) { return decodeURIComponent(s.replace(a, " ")); }, q = window.location.hash.substring(1); while (e = r.exec(q)) hashParams[d(e[1])] = d(e[2]); return hashParams; } params = getHashParams(); q = params.q; if (q != null) { setTimeout(function() { $("#search-field").val(q).search(); }, 50); } }); } $(document).ready(function() { // if text input field value is not empty show the "X" button $("#search-container #search-field").keyup(function() { $("#search-container #x").fadeIn(); if ($.trim($("#search-container #search-field").val()) == "") { $("#search-container #x").fadeOut(); } }); // on click of "X", delete input field value and hide "X" $("#search-container #x").click(function() { $("#search-container #search-field").val(""); $(this).hide(); }); });