Skip to content
This repository was archived by the owner on Feb 1, 2018. It is now read-only.

Commit ca8fd7a

Browse files
committed
Fix highlighter issue : break on 100 match found
1 parent 36368d4 commit ca8fd7a

4 files changed

Lines changed: 402 additions & 4 deletions

File tree

dist/js/main.js

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,205 @@
11641164
};
11651165
}(jQuery));
11661166

1167+
(function($) {
1168+
$.fn.highlight = function(pat) {
1169+
var successCount = 0;
1170+
function innerHighlight(node, pat) {
1171+
var skip = 0;
1172+
if (node.nodeType == 3) {
1173+
var pos = node.data.toUpperCase().indexOf(pat);
1174+
if (pos >= 0) {
1175+
successCount++;
1176+
var spannode = document.createElement('span');
1177+
spannode.className = 'highlight';
1178+
var middlebit = node.splitText(pos);
1179+
var endbit = middlebit.splitText(pat.length);
1180+
var middleclone = middlebit.cloneNode(true);
1181+
spannode.appendChild(middleclone);
1182+
middlebit.parentNode.replaceChild(spannode, middlebit);
1183+
skip = 1;
1184+
}
1185+
} else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
1186+
for (var i = 0; i < node.childNodes.length; ++i) {
1187+
if(successCount > 100) {
1188+
break;
1189+
}
1190+
i += innerHighlight(node.childNodes[i], pat);
1191+
}
1192+
}
1193+
return skip;
1194+
}
1195+
return this.length && pat && pat.length ? this.each(function() {
1196+
innerHighlight(this, pat.toUpperCase());
1197+
}) : this;
1198+
};
1199+
}(jQuery));
1200+
1201+
(function($) {
1202+
$.fn.removeHighlight = function() {
1203+
return $(this).find('span.highlight').each(function(){
1204+
$(this).replaceWith($(this).text());
1205+
}).end().each(function() {
1206+
this.normalize();
1207+
});
1208+
};
1209+
}(jQuery));(function($) {
1210+
$.fn.searchAppbase = function(searchIndexUrl, htmlMode) {
1211+
1212+
//Create the search input element and insert it into html
1213+
var $search = $('<input>').attr({
1214+
'class':"search_field form-control dropdown-toggle",
1215+
'type':'text',
1216+
'placeholder':'search'
1217+
});
1218+
$(this).html($search);
1219+
$search.addClass('appbase-search');
1220+
1221+
function searchTag(data) {
1222+
var singleId = data.singleId;
1223+
var sectionId = singleId.substring(singleId.indexOf('"')+1, singleId.lastIndexOf('"'));
1224+
var filesplit = data.link.split('/');
1225+
var fileName = htmlMode ? filesplit[filesplit.length - 1].replace('.html','') : filesplit[filesplit.length - 2];
1226+
var link_part = data.link.split('/');
1227+
data.version = link_part.length > 1 ? '<span class="result_record_version">'+link_part[1]+'</span>' : null;
1228+
data.folder = link_part.length > 2 ? '<span class="result_record_folder">'+fileName+'</span>' : null;
1229+
var result_info = link_part.length > 1 ? $("<div>").addClass('result_record_info').append(data.folder).append(data.version) : null;
1230+
var result_a = $('<a>').addClass('result_record_a pointer').attr({'link':data.link, 'sectionId':sectionId, 'spaLink': data.spaLink}).text(data.title).append(result_info);
1231+
var result_div = $('<div>').addClass('result_record').append(result_a);
1232+
result_a.on('click',function(){
1233+
gotoLink(this);
1234+
});
1235+
return result_div;
1236+
}
1237+
var fail = function(e) {
1238+
console.error("Your search index wasn't loaded, please check the following error", e);
1239+
};
1240+
var success = function(searchData) {
1241+
searchData.forEach(function(searchSingle) {
1242+
var content = searchSingle.content;
1243+
searchSingle.singleId = content.substring(content.indexOf('<'), content.indexOf('>'));
1244+
searchSingle.content = content.replace(/<\/?[^>]+(>|$)/g, " ");
1245+
});
1246+
1247+
var posts = new Bloodhound({
1248+
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('title', 'content'),
1249+
queryTokenizer: Bloodhound.tokenizers.whitespace,
1250+
local: searchData
1251+
});
1252+
1253+
$search.typeahead({
1254+
minLength: 1
1255+
}, {
1256+
name: 'titles',
1257+
displayKey: 'title',
1258+
source: posts,
1259+
templates: {
1260+
pending: true,
1261+
suggestion: function(data) {
1262+
if (data) {
1263+
var single_record = searchTag(data);
1264+
return single_record;
1265+
} else
1266+
return;
1267+
}
1268+
}
1269+
});
1270+
1271+
// $search.bind('typeahead:select', function(ev, suggestion) {
1272+
// window.location.href = suggestion.link;
1273+
// });
1274+
$search.bind('typeahead:open', function(ev, suggestion) {
1275+
$search.parents('.search-form').addClass('open');
1276+
});
1277+
$search.bind('typeahead:close', function(ev, suggestion) {
1278+
$search.parents('.search-form').removeClass('open');
1279+
});
1280+
$search.on('keyup', function() {
1281+
var searchText = $(this).val();
1282+
$('.content').removeHighlight().highlight(searchText);
1283+
});
1284+
setQueryText();
1285+
};
1286+
//goto page with query string
1287+
var gotoLink = function(eve) {
1288+
var linkMode = htmlMode ? $(eve).attr('link') : $(eve).attr('spaLink');
1289+
var fullLink = linkMode+'?q='+$search.val()+'#'+$(eve).attr('sectionId');
1290+
window.location.href = fullLink;
1291+
};
1292+
//set initial higlhight according to previous page query
1293+
var setQueryText = function(){
1294+
var winhref = window.location.href;
1295+
if(winhref.indexOf('?q=') != -1){
1296+
var queryText = winhref.substring(winhref.indexOf('?q=')+3,winhref.lastIndexOf('#')).replace(/%20/g,' ');
1297+
$search.val(queryText);
1298+
$search.trigger('keyup');
1299+
}
1300+
};
1301+
//Fetch search index json data
1302+
var intializeCall = function() {
1303+
$.get(searchIndexUrl)
1304+
.then(success)
1305+
.fail(fail);
1306+
};
1307+
1308+
//Load typeahead.js
1309+
var Loader = function() {};
1310+
Loader.prototype = {
1311+
require: function(scripts, callback) {
1312+
this.loadCount = 0;
1313+
this.totalRequired = scripts.length;
1314+
this.callback = callback;
1315+
1316+
for (var i = 0; i < scripts.length; i++) {
1317+
var split_name = scripts[i].split('.');
1318+
if (split_name[split_name.length - 1] == 'js')
1319+
this.writeScript(scripts[i]);
1320+
if (split_name[split_name.length - 1] == 'css')
1321+
this.writeStylesheet(scripts[i]);
1322+
}
1323+
},
1324+
loaded: function(evt) {
1325+
this.loadCount++;
1326+
1327+
if (this.loadCount == this.totalRequired && typeof this.callback == 'function') this.callback.call();
1328+
},
1329+
writeScript: function(src) {
1330+
var self = this;
1331+
var s = document.createElement('script');
1332+
s.type = "text/javascript";
1333+
s.async = true;
1334+
s.src = src;
1335+
s.addEventListener('load', function(e) {
1336+
self.loaded(e);
1337+
}, false);
1338+
var head = document.getElementsByTagName('head')[0];
1339+
head.appendChild(s);
1340+
},
1341+
writeStylesheet: function(src) {
1342+
var self = this;
1343+
var s = document.createElement('link');
1344+
s.type = "text/css";
1345+
s.rel = "stylesheet";
1346+
s.href = src;
1347+
s.addEventListener('load', function(e) {
1348+
self.loaded(e);
1349+
}, false);
1350+
var head = document.getElementsByTagName('head')[0];
1351+
head.appendChild(s);
1352+
}
1353+
};
1354+
1355+
var jquery_js = new Loader();
1356+
jquery_js.require([
1357+
"http://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"
1358+
],
1359+
function() {
1360+
intializeCall();
1361+
});
1362+
1363+
};
1364+
}(jQuery));
1365+
11671366
(function($) {
11681367
$.fn.highlight = function(pat) {
11691368
function innerHighlight(node, pat) {

0 commit comments

Comments
 (0)