Skip to content

Commit 698ca59

Browse files
committed
added highlight for keyword in searchcodes
1 parent d24b722 commit 698ca59

9 files changed

Lines changed: 147 additions & 6 deletions

File tree

‎cache.manifest‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CACHE MANIFEST
2-
# Build: 20160216070953
2+
# Build: 20160216095328
33

44
CACHE:
55
resources/fonts/Dressedless_Three.svg
@@ -15,6 +15,7 @@ resources/images/paypal.png
1515
resources/images/twohardtings.jpg
1616
resources/images/wechatpay.jpg
1717
resources/images/zhifubao.png
18+
src/lib/all.1a4577ad.js
1819
src/lib/all.f851782e.js
1920

2021
NETWORK:

‎index.html‎

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

‎src/lib/all.1a4577ad.js‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎static/app/resources/css/style.css‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ html, body {
2323
.jumbotron {
2424
margin-bottom: 0; }
2525

26+
.highlight {
27+
background-color: #FFFF88; }
28+
2629
.modal-header .close {
2730
padding-left: .65rem; }
2831

‎static/app/scss/_util.scss‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
.jumbotron{
1919
margin-bottom: 0;
2020
}
21+
.highlight {
22+
background-color: #FFFF88;
23+
}
2124
.modal-header .close {
2225
padding-left: .65rem;
2326
}

‎static/app/src/AppBundle.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@
215215
function showSourceCode() {
216216
renderSourceCode();
217217
Model.searchcodeModel.requestSourceCode(this.dataset.id, renderSourceCode);
218+
els.lastVariableKeyword = this.dataset.val || els.lastVariableKeyword;
218219
this.dataset.val && renderRelatedProperty(this.dataset.val);
219220
els.sourceCodeModal.modal('show');
220221
}
@@ -593,11 +594,17 @@
593594
els.sourceCodeContentHd.hide();
594595
els.sourceCodeContent.text(data.code);
595596
setTimeout(function () {
596-
PR.prettyPrint();
597+
PR.prettyPrint(renderHighlightVariableKeyword);
597598
}, 100);
598599
renderAnalytics('vc&q=' + els.lastInputVal);
599600
}
600601
}
602+
function renderHighlightVariableKeyword(){
603+
els.sourceCodeContent.unhighlight();
604+
setTimeout(function(){
605+
els.sourceCodeContent.highlight(els.lastVariableKeyword);
606+
},1000);
607+
}
601608

602609
function renderRelatedProperty(name) {
603610
var htm = [],

‎static/app/src/View.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ function init() {
160160
function showSourceCode() {
161161
renderSourceCode();
162162
Model.searchcodeModel.requestSourceCode(this.dataset.id, renderSourceCode);
163+
els.lastVariableKeyword = this.dataset.val || els.lastVariableKeyword;
163164
this.dataset.val && renderRelatedProperty(this.dataset.val);
164165
els.sourceCodeModal.modal('show');
165166
}
@@ -538,11 +539,17 @@ function renderSourceCode(data) {
538539
els.sourceCodeContentHd.hide();
539540
els.sourceCodeContent.text(data.code);
540541
setTimeout(function () {
541-
PR.prettyPrint();
542+
PR.prettyPrint(renderHighlightVariableKeyword);
542543
}, 100);
543544
renderAnalytics('vc&q=' + els.lastInputVal);
544545
}
545546
}
547+
function renderHighlightVariableKeyword(){
548+
els.sourceCodeContent.unhighlight();
549+
setTimeout(function(){
550+
els.sourceCodeContent.highlight(els.lastVariableKeyword);
551+
},1000);
552+
}
546553

547554
function renderRelatedProperty(name) {
548555
var htm = [],

‎static/app/src/lib/all.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@@include("jquery.min.js")
2+
@@include("jquery.highlight.js")
23
@@include("tether.min.js")
34
@@include("bootstrap.min.js")
45
@@include("prettify.js")
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* jQuery Highlight plugin
3+
*
4+
* Based on highlight v3 by Johann Burkard
5+
* http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html
6+
*
7+
* Code a little bit refactored and cleaned (in my humble opinion).
8+
* Most important changes:
9+
* - has an option to highlight only entire words (wordsOnly - false by default),
10+
* - has an option to be case sensitive (caseSensitive - false by default)
11+
* - highlight element tag and class names can be specified in options
12+
*
13+
* Usage:
14+
* // wrap every occurrance of text 'lorem' in content
15+
* // with <span class='highlight'> (default options)
16+
* $('#content').highlight('lorem');
17+
*
18+
* // search for and highlight more terms at once
19+
* // so you can save some time on traversing DOM
20+
* $('#content').highlight(['lorem', 'ipsum']);
21+
* $('#content').highlight('lorem ipsum');
22+
*
23+
* // search only for entire word 'lorem'
24+
* $('#content').highlight('lorem', { wordsOnly: true });
25+
*
26+
* // don't ignore case during search of term 'lorem'
27+
* $('#content').highlight('lorem', { caseSensitive: true });
28+
*
29+
* // wrap every occurrance of term 'ipsum' in content
30+
* // with <em class='important'>
31+
* $('#content').highlight('ipsum', { element: 'em', className: 'important' });
32+
*
33+
* // remove default highlight
34+
* $('#content').unhighlight();
35+
*
36+
* // remove custom highlight
37+
* $('#content').unhighlight({ element: 'em', className: 'important' });
38+
*
39+
*
40+
* Copyright (c) 2009 Bartek Szopka
41+
*
42+
* Licensed under MIT license.
43+
*
44+
*/
45+
46+
jQuery.extend({
47+
highlight: function (node, re, nodeName, className) {
48+
if (node.nodeType === 3) {
49+
var match = node.data.match(re);
50+
if (match) {
51+
var highlight = document.createElement(nodeName || 'span');
52+
highlight.className = className || 'highlight';
53+
var wordNode = node.splitText(match.index);
54+
wordNode.splitText(match[0].length);
55+
var wordClone = wordNode.cloneNode(true);
56+
highlight.appendChild(wordClone);
57+
wordNode.parentNode.replaceChild(highlight, wordNode);
58+
return 1; //skip added node in parent
59+
}
60+
} else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children
61+
!/(script|style)/i.test(node.tagName) && // ignore script and style nodes
62+
!(node.tagName === nodeName.toUpperCase() && node.className === className)) { // skip if already highlighted
63+
for (var i = 0; i < node.childNodes.length; i++) {
64+
i += jQuery.highlight(node.childNodes[i], re, nodeName, className);
65+
}
66+
}
67+
return 0;
68+
}
69+
});
70+
71+
jQuery.fn.unhighlight = function (options) {
72+
var settings = { className: 'highlight', element: 'span' };
73+
jQuery.extend(settings, options);
74+
75+
return this.find(settings.element + "." + settings.className).each(function () {
76+
var parent = this.parentNode;
77+
parent.replaceChild(this.firstChild, this);
78+
parent.normalize();
79+
}).end();
80+
};
81+
82+
jQuery.fn.highlight = function (words, options) {
83+
var settings = { className: 'highlight', element: 'span', caseSensitive: false, wordsOnly: false };
84+
jQuery.extend(settings, options);
85+
86+
if (words.constructor === String) {
87+
words = [words];
88+
}
89+
words = jQuery.grep(words, function(word, i){
90+
return word != '';
91+
});
92+
words = jQuery.map(words, function(word, i) {
93+
return word.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
94+
});
95+
if (words.length == 0) { return this; };
96+
97+
var flag = settings.caseSensitive ? "" : "i";
98+
var pattern = "(" + words.join("|") + ")";
99+
if (settings.wordsOnly) {
100+
pattern = "\\b" + pattern + "\\b";
101+
}
102+
var re = new RegExp(pattern, flag);
103+
104+
return this.each(function () {
105+
jQuery.highlight(this, re, settings.element, settings.className);
106+
});
107+
};
108+

0 commit comments

Comments
 (0)