Skip to content

Instantly share code, notes, and snippets.

@DeskWOW
Last active December 27, 2015 14:09
Show Gist options
  • Save DeskWOW/7338514 to your computer and use it in GitHub Desktop.
Save DeskWOW/7338514 to your computer and use it in GitHub Desktop.
Below is the JavaScript code for real-time article suggest in your Desk.com support center contact form. This is to be placed within your theme's "Email (New)" section of Advanced Themes within Desk.com Admin.
<div id="autosuggest" style="display: none;"></div>
<div id="common">
<h4>Common Questions</h4>
<p>This will show on page-load and if no results are found based on what the user is typing. You can optionally put links to common articles here.</p>
</div>
<script>
var as_count = 0;
$(function() {
// Skip pre-create
$('#new_email').attr('action','/customer/portal/emails');
// Real-time auto-suggest
$('#email_subject').on("keyup paste",function() {
if ($('#email_subject').val().length > 3 && $('#email_subject').val().length <= 250) {
clearTimeout(window.timer);
window.timer=setTimeout(function(){ // setting the delay for each keypress
articleSuggest();
}, 500);
}
});
});
articleSuggest = function() {
as_count = 0;
var search_query = $('#email_subject').val();
var systemLanguageDesk = '{{system.language}}';
$.ajax({
url: '//' + document.domain.toString() + '/customer/' + systemLanguageDesk + '/portal/articles/autocomplete?term=' + search_query,
dataType: 'json'
}).done(apiSuccess).fail(apiFail);
}
apiSuccess = function(data) {
auto_suggest_content = "";
auto_suggest_articles = "";
auto_suggest_questions = "";
$('.autosuggest').html('<h2 class="muted">Do these help?</h4><ul class="unstyled"></ul>');
$.each(data, function() {
var html = $(this.label);
article_title = html.find(".article-autocomplete-subject").html();
if (this.id.indexOf("questions") !== -1) {
auto_suggest_questions += '<li><a target="_blank" href="' + this.id + '" class="discussion">' + article_title + '</a></li>';
} else {
auto_suggest_articles += '<li><a target="_blank" href="' + this.id + '" class="article">' + article_title + '</a></li>';
}
as_count++;
});
if (as_count > 0) {
$('#autosuggest ul').append(auto_suggest_articles + auto_suggest_questions);
$("#common").hide();
$(".autosuggest").removeClass('hide');
} else {
$("#autosuggest").addClass('hide');
$("#common").show();
}
};
apiFail = function(data) {
};
</script>
@DeskWOW
Copy link
Author

DeskWOW commented Jul 22, 2014

This code snippet requires jQuery. You can use a CDN and quickly insert it into your code by adding this to your code:

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment