/* Tipue Search 5.0 Copyright (c) 2015 Tipue Tipue Search is released under the MIT License http://www.tipue.com/search */ (function ($) { var originalTitle = document.title; // Stop words (list from http://www.ranks.nl/stopwords) var tipuesearch_stop_words = ["a", "about", "above", "after", "again", "against", "all", "am", "an", "and", "any", "are", "aren't", "as", "at", "be", "because", "been", "before", "being", "below", "between", "both", "but", "by", "can't", "cannot", "could", "couldn't", "did", "didn't", "do", "does", "doesn't", "doing", "don't", "down", "during", "each", "few", "for", "from", "further", "had", "hadn't", "has", "hasn't", "have", "haven't", "having", "he", "he'd", "he'll", "he's", "her", "here", "here's", "hers", "herself", "him", "himself", "his", "how", "how's", "i", "i'd", "i'll", "i'm", "i've", "if", "in", "into", "is", "isn't", "it", "it's", "its", "itself", "let's", "me", "more", "most", "mustn't", "my", "myself", "no", "nor", "not", "of", "off", "on", "once", "only", "or", "other", "ought", "our", "ours", "ourselves", "out", "over", "own", "same", "shan't", "she", "she'd", "she'll", "she's", "should", "shouldn't", "so", "some", "such", "than", "that", "that's", "the", "their", "theirs", "them", "themselves", "then", "there", "there's", "these", "they", "they'd", "they'll", "they're", "they've", "this", "those", "through", "to", "too", "under", "until", "up", "very", "was", "wasn't", "we", "we'd", "we'll", "we're", "we've", "were", "weren't", "what", "what's", "when", "when's", "where", "where's", "which", "while", "who", "who's", "whom", "why", "why's", "with", "won't", "would", "wouldn't", "you", "you'd", "you'll", "you're", "you've", "your", "yours", "yourself", "yourselves"]; // Internal strings var tipuesearch_string_one_result = window.searchTranslation.Search_one_result; var tipuesearch_string_results = window.searchTranslation.Search_results; var tipuesearch_string_prev = window.searchTranslation.Link_previous; var tipuesearch_string_next = window.searchTranslation.Link_next; var tipuesearch_string_no_results = window.searchTranslation.Search_no_results; var tipuesearch_string_common_words_ignored = window.searchTranslation.Search_common_words_ignored; var tipuesearch_string_too_short = window.searchTranslation.Search_too_short; var tipuesearch_string_one_character_or_more = window.searchTranslation.Search_one_character_or_more; var tipuesearch_string_should_be_x_or_more = window.searchTranslation.Search_should_be_x_or_more; // Main containers var tipue_container, tipue_backdrop; function getURLP(name) { return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null; } function closeSearch() { document.title = originalTitle; $(document).off("keyup", keyUpHandler); $("body").removeClass("with-search"); tipue_container.hide(); tipue_backdrop.hide(); } function keyUpHandler(e) { if (e.which == 27) { //escape closeSearch(); } } function getSearchString(searchFor) { var standard = true; var hasStopWords = false; if ((searchFor.match("^\"") && searchFor.match("\"$")) || (searchFor.match("^'") && searchFor.match("'$"))) { standard = false; } if (standard) { var d_w = searchFor.split(' '); searchFor = ''; for (var i = 0; i < d_w.length; i++) { var isStopWord = false; for (var f = 0; f < tipuesearch_stop_words.length; f++) { if (d_w[i] == tipuesearch_stop_words[f]) { isStopWord = true; hasStopWords = true; } } if (!isStopWord) { searchFor = searchFor + ' ' + d_w[i]; } } searchFor = $.trim(searchFor); } else { searchFor = searchFor.substring(1, searchFor.length - 1); } return { hasStopWords: hasStopWords, isStandard: standard, searchFor: searchFor }; } function getScore(searchFor, page) { var score = 0; var pat = new RegExp(searchFor, 'gi'); if (page.title.search(pat) != -1) { score += (20 * page.title.match(pat).length); } if (page.text.search(pat) != -1) { score += (20 * page.text.match(pat).length); } if (page.tags.search(pat) != -1) { score += (10 * page.tags.match(pat).length); } if (page.url.search(pat) != -1) { score += 20; } return score; } function makeResult(score, page, text) { return { "score": score, "title": page.title, "desc": text, "url": page.url } } window.tipuesearch = function (options) { var settings = $.extend( { 'field': $('#tipue_search_input'), 'show': 10, 'showURL': true, 'showTitleCount': true, 'minimumLength': 3, 'descriptiveWords': 25, 'highlightTerms': true, 'highlightEveryTerm': false, 'contentLocation': 'tipuesearch/tipuesearch_content.json', 'debug': false }, options); var tipuesearch_in = { pages: [] }; $.ajax( { dataType: "json", url: settings.base_url + settings.contentLocation, async: false }) .done( function (json) { tipuesearch_in = $.extend({}, json); }); if (getURLP('q')) { settings.field.val(getURLP('q')); getTipueSearch(0, true); } settings.field.keyup( function (event) { if (event.keyCode == '13') { getTipueSearch(0, true); } }); function highlightText(search, text) { if (settings.highlightTerms) { var pattern = new RegExp('(' + search + ')', settings.highlightEveryTerm ? 'gi' : 'i'); text = text.replace(pattern, "$1"); } return text; } function getResults(searchFor, standard) { var found = []; if (standard) { var d_w = searchFor.split(' '); for (var i = 0; i < tipuesearch_in.pages.length; i++) { var score = 0; var text = tipuesearch_in.pages[i].text; for (var f = 0; f < d_w.length; f++) { if (d_w[f].match('^-')) { var pat = new RegExp(d_w[f].substring(1), 'i'); if (tipuesearch_in.pages[i].title.search(pat) != -1 || tipuesearch_in.pages[i].text.search(pat) != -1 || tipuesearch_in.pages[i].tags.search(pat) != -1) { score = 0; } } else { score += getScore(d_w[f], tipuesearch_in.pages[i]); text = highlightText(d_w[f], text); } } if (score != 0) { found.push(makeResult(score, tipuesearch_in.pages[i], text)); } } } else { for (var i = 0; i < tipuesearch_in.pages.length; i++) { var score = getScore(searchFor, tipuesearch_in.pages[i]); if (score != 0) { found.push(makeResult(score, tipuesearch_in.pages[i], highlightText(searchFor, tipuesearch_in.pages[i].text))); } } } found.sort(function (a, b) { return b.score - a.score }); return found } function getTipueSearch(start, replace) { if (!tipue_container) { tipue_container = $(document.createElement("div")); tipue_container.addClass('SearchResults'); document.body.appendChild(tipue_container.get(0)); tipue_backdrop = $(document.createElement("div")); tipue_backdrop.addClass("SearchResultsBackdrop"); document.body.appendChild(tipue_backdrop.get(0)); tipue_container.on('click', '.SearchResults__close', closeSearch); tipue_container.on('click', '.SearchResults__footer__link', function () { var id_v = $(this).attr('id'); var id_a = id_v.split('_'); getTipueSearch(parseInt(id_a[0]), id_a[1]); tipue_container.scrollTop(0); }); tipue_container.on('keyup paste', '.Search__field', function(event) { settings.field.val($(this).val()); if (event.keyCode == '13') { getTipueSearch(0, true); } }) } $(document).keyup(keyUpHandler); var output = ''; var search = getSearchString($.trim(settings.field.val().toLowerCase())); var searchFor = search.searchFor; if (searchFor.length >= settings.minimumLength) { var found = getResults(search.searchFor, search.isStandard); var counter = found.length; if (counter == 0) { output += '