|
| 1 | +export default { |
| 2 | + |
| 3 | + name: "discourse-autocomplete", |
| 4 | + initialize() {}, |
| 5 | + _initialize(options) { |
| 6 | + //getlogin status |
| 7 | + var currentUser = Discourse.User.current(); |
| 8 | + if (!currentUser) { |
| 9 | + $('.Typeahead-spinner').css("left","390px"); |
| 10 | + } |
| 11 | + //ajax function |
| 12 | + var autocomplete = function(query, processSync, processAsync){ |
| 13 | + //Get text from the input field |
| 14 | + var text = $('#search-box').val(), |
| 15 | + //ES Query |
| 16 | + json = { |
| 17 | + "query":{ |
| 18 | + "multi_match": |
| 19 | + {"query":text, |
| 20 | + "fields":[], |
| 21 | + "type":"best_fields" |
| 22 | + } |
| 23 | + } |
| 24 | + }; |
| 25 | + // ajax var init |
| 26 | + var url = options.elasticsearch_address, |
| 27 | + data = json, |
| 28 | + formatType = "json", |
| 29 | + type = "post", |
| 30 | + contentType = "application/json; charset=utf-8"; |
| 31 | + console.log(url); |
| 32 | + if(type === "post" && formatType === "json"){ |
| 33 | + data = JSON.stringify(data); |
| 34 | + } |
| 35 | + if(formatType === "json"){ |
| 36 | + contentType="application/json"; |
| 37 | + } |
| 38 | + return $.ajax({ |
| 39 | + dataType : "json", |
| 40 | + async : true, |
| 41 | + type:type, |
| 42 | + cache: false, |
| 43 | + url:url, |
| 44 | + data:data, |
| 45 | + contentType:contentType, |
| 46 | + timeout:30000 |
| 47 | + }).then(function(rs){ |
| 48 | + var dfd = $.Deferred(); |
| 49 | + var results = $.map([0], function() { |
| 50 | + |
| 51 | + //Parse the results and return them |
| 52 | + var resultsData = rs.hits, |
| 53 | + resultsLength = Object.keys(resultsData.hits).length, |
| 54 | + datum = []; |
| 55 | + |
| 56 | + for (var i = 0; i < resultsLength; i++) { |
| 57 | + var resultsArray = resultsData.hits[i]._source, |
| 58 | + resultsIndex = resultsData.hits[i]._index; |
| 59 | + if (resultsIndex == "discourse-users") { |
| 60 | + var user_avatar = resultsArray.avatar_template.replace("\{size}", 50); |
| 61 | + datum.push({ |
| 62 | + // user |
| 63 | + user_avatar_template: user_avatar, |
| 64 | + user_username: resultsArray.username, |
| 65 | + user_likes_received: resultsArray.likes_received, |
| 66 | + url: resultsArray.url |
| 67 | + }); |
| 68 | + }else if (resultsIndex == "discourse-tags") { |
| 69 | + datum.push({ |
| 70 | + // tag |
| 71 | + tag_name: resultsArray.name, |
| 72 | + tag_topic_count: resultsArray.topic_count, |
| 73 | + url:resultsArray.url |
| 74 | + }); |
| 75 | + }else if (resultsIndex == "discourse-posts") { |
| 76 | + var topic_name = resultsArray.topic.title, |
| 77 | + topic_view = resultsArray.topic.views, |
| 78 | + topic_url = resultsArray.topic.url, |
| 79 | + category = resultsArray.category.name, |
| 80 | + category_color = resultsArray.category.color, |
| 81 | + category_url = resultsArray.category.url, |
| 82 | + author = resultsArray.user.username, |
| 83 | + author_url = resultsArray.user.url, |
| 84 | + pre = resultsArray.content; |
| 85 | + |
| 86 | + datum.push({ |
| 87 | + // post |
| 88 | + post_topic_name: topic_name, |
| 89 | + post_topic_view: topic_view, |
| 90 | + url: topic_url, |
| 91 | + post_category: category, |
| 92 | + post_category_color: category_color, |
| 93 | + post_category_url: category_url, |
| 94 | + post_author: author, |
| 95 | + post_author_url: author_url, |
| 96 | + post_pre: pre |
| 97 | + |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + } |
| 102 | + return datum; |
| 103 | + }); |
| 104 | + processAsync(results); |
| 105 | + |
| 106 | + return dfd.promise(); |
| 107 | + },function(){ |
| 108 | + alert("网络异常"); |
| 109 | + }).always(function(rs){ |
| 110 | + |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + $('#search-box').typeahead({ |
| 119 | + highlight: true, |
| 120 | + minLength: 1 |
| 121 | + }, |
| 122 | + { |
| 123 | + name: 'posts', |
| 124 | + displayKey: 'value', |
| 125 | + source: autocomplete, |
| 126 | + async: true, |
| 127 | + limit: 6, |
| 128 | + templates: { |
| 129 | + empty: [ |
| 130 | + '<div class="empty-message">', |
| 131 | + '无结果', |
| 132 | + '</div>' |
| 133 | + ].join('\n'), |
| 134 | + footer: [ |
| 135 | + '<div class="show-more">', |
| 136 | + '<a class="advanced-search" onclick="document.location.href="/search"; document.reload();" href="/search">更多...</a>', |
| 137 | + '<div>' |
| 138 | + ].join('\n'), |
| 139 | + suggestion: function(value) { |
| 140 | + if (value.post_topic_name != undefined) { |
| 141 | + return '<div class="es-dataset-posts"><div class="hit-post"><div class="hit-post-title-holder"><span class="hit-post-topic-title"><a href="' + value.url + '">'+ value.post_topic_name + '</a></span><span class="hit-post-topic-views" title="Number of times the topic has been viewed">'+ value.post_topic_view + '</span></div><div class="hit-post-category-tags"><span class="hit-post-category"><span class="badge-wrapper bullet"><span class="badge-category-bg" style="background-color: #'+ value.post_category_color +';"></span><a class="badge-category hit-post-category-name" href="'+ value.post_category_url +'">'+ value.post_category + '</a></span></span></div><div class="hit-post-content-holder"><a class="hit-post-username" href="'+ value.post_author_url +'">'+ value.post_author + '</a>:<span class="hit-post-content">'+ value.post_pre +'</span></div></div></div>' |
| 142 | + }else{ |
| 143 | + return '<span></span>' |
| 144 | + |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + },{ |
| 149 | + name: 'users-tags', |
| 150 | + displayKey: 'value', |
| 151 | + source: autocomplete, |
| 152 | + async: true, |
| 153 | + limit: 6, |
| 154 | + templates: { |
| 155 | + empty: "", |
| 156 | + suggestion: function(value) { |
| 157 | + if (value.tag_name != undefined) { |
| 158 | + return '<div class="es-dataset-tags"><a href="'+ value.url +'"><div class="hit-tag"><span class="hit-tag-name">#'+ value.tag_name +' </span><span class="hit-tag-topic_count" title="Number of topics with this tag"> '+ value.tag_topic_count +'</span></div></a></div>' |
| 159 | + }else if (value.user_username != undefined){ |
| 160 | + return '<div class="es-dataset-users"><a href="'+ value.url +'"><div class="hit-user-left"><img class="hit-user-avatar" src="'+ value.user_avatar_template + '" /></div><div class="hit-user-right"><div class="hit-user-username-holder"><span class="hit-user-username">@'+ value.user_username + '</span><span class="hit-user-custom-ranking" title="Number of likes the user has received"><span class="hit-user-like-heart"> ❤ </span>' + value.user_likes_received + '</span></div></div></a></div>' |
| 161 | + }else{ |
| 162 | + return '<span></span>' |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + }).on('typeahead:selected', function(event, datum) { |
| 168 | + window.location = datum.url; |
| 169 | + }).on('typeahead:asyncrequest', function() { |
| 170 | + $('.Typeahead-spinner').show(); |
| 171 | + }).on('typeahead:asynccancel typeahead:asyncreceive', function() { |
| 172 | + $('.Typeahead-spinner').hide(); |
| 173 | + }); |
| 174 | + |
| 175 | + $("#search-box").on('focus', function (event) { |
| 176 | + $(this).select(); |
| 177 | + }); |
| 178 | + } |
| 179 | +} |
| 180 | + |
0 commit comments