This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gf-hs-search.min.js
1 lines (1 loc) · 3.69 KB
/
gf-hs-search.min.js
1
jQuery(document).on("gform_post_render",function(){var e=jQuery.extend(GF_HS_Settings,{searching:!1,cancelled:!1,has_searched:!1,count:0,query:"",timeout:null,wrap:jQuery(".gform_wrapper .gfield.helpscout-docs"),form:jQuery(".gform_wrapper .gfield.helpscout-docs").closest(".gform_wrapper"),field:jQuery(".gform_wrapper .gfield.helpscout-docs").find('input[type="text"]'),onclick:"",keypress:"",results:{},init:function(){e.form.find(".gform_next_button").hide(),e.form.on("keypress",function(t){var r=t.which||t.keyCode;if(e.field.is(":focus"))return 13!=r||jQuery(t.target).is('textarea,input[type="submit"],input[type="button"]')?void 0:(t.preventDefault(),!1)}),e.wrap.append('<div class="'+GF_HS_Settings.template.wrap_class+'" style="display:none;" />'),e.field.attr("autocomplete","off").on("keydown keyup change",e.search_changed)},search_changed:function(t){var r=[9,13,16,17,18,20,32,33,34,37,38,91,93];if(r.indexOf(t.which)>-1)return void e.log("Ignored key press",t.which);e.log("Starting search countdown in %d ms",parseInt(GF_HS_Settings.searchDelay,10));var s=jQuery(this);clearTimeout(e.timeout),e.timeout=setTimeout(function(){return e.log("Performing search",t.which),e.query=s.val(),e.query.length<GF_HS_Settings.minLength||(8===t.which||46===t.which)&&0===e.query.length?(e.cancelled=!0,void e.set_results({})):void(e.searching||e.perform_search())},parseInt(GF_HS_Settings.searchDelay,10))},perform_search:function(){e.results={},e.cancelled=!1,e.fetch_results()},log:function(t,r){e.debug&&console&&console.log&&console.log(t,r)},get_results_html:function(){var t="",r=0;return"undefined"!=typeof e.results.articles&&e.results.articles.results.length&&(t=GF_HS_Settings.template.before,jQuery.each(e.results.articles.results,function(s,n){var o=!0;GF_HS_Settings.collections.length>0&&(o=!1,jQuery.each(GF_HS_Settings.collections,function(e,t){var r=new RegExp("^/docs/"+t);r.exec(n.docsUrl)&&(o=!0)})),r<e.limit&&o&&(r++,t+=e.get_article_html(n))}),t+=GF_HS_Settings.template.after),e.get_results_found(r)+t},get_article_html:function(e){var t=GF_HS_Settings.template.item;for(var r in e)e.hasOwnProperty(r)&&(t=t.replace("{"+r+"}",e[r]));return t},get_results_found:function(t){var r="",s="results-found";return 0===e.query.length?(r=GF_HS_Settings.text.enter_search,s+=" message-enter_search"):e.query.length<e.minLength?(r=GF_HS_Settings.text.not_long_enough.replace("{minLength}",e.minLength),s+=" message-minlength"):0===t?(r=GF_HS_Settings.text.no_results_found,s+=" message-no_results"):(r=1===t?GF_HS_Settings.text.result_found:GF_HS_Settings.text.results_found,s+=" message-results"),GF_HS_Settings.template.results_found.replace("{css_class}",s).replace("{text}",r).replace("{count}",t)},set_results:function(t){e.log("Adding results:",t),e.results=t,e.wrap.find(".docs-search-wrap").html(e.get_results_html()).not(":visible").slideDown()},sanitize_query:function(t){return t=t.replace(/[\{\}\[\]]/g," "),e.log("Searching for %s",t),t},fetch_results:function(){query=e.sanitize_query(e.query),$search_wrap=e.wrap.find(".docs-search-wrap"),jQuery.ajax({url:"https://"+GF_HS_Settings._subdomain+".helpscoutdocs.com/search/ajax?ref=gf&query="+encodeURIComponent(query),async:!0,dataType:"json",beforeSend:function(){e.searching=!0,$search_wrap.addClass("docs-searching"),e.wrap.find(".docs-search-wrap").show().html('<span class="gf-hs-spinner"></span>'),jQuery("body").trigger("gf_hs_search_started")},success:function(t){e.cancelled||(e.set_results(t),e.form.find(".gform_next_button").show(),jQuery("body").trigger("gf_hs_search_results_found"),e.has_searched=!0)},error:function(t){e.log("Error: %s",t),jQuery("body").trigger("gf_hs_search_error")}}).always(function(){e.searching=!1,$search_wrap.removeClass("docs-searching")})}});e.init()});