Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
node-slate
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nguyễn Hải Sơn
node-slate
Commits
2384d9af
Commit
2384d9af
authored
Apr 26, 2014
by
bootstraponline
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reposition headers based on score
Enforce min score
parent
adcf6c70
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
3 deletions
+45
-3
search.js
source/javascripts/app/search.js
+45
-3
No files found.
source/javascripts/app/search.js
View file @
2384d9af
...
@@ -42,6 +42,23 @@
...
@@ -42,6 +42,23 @@
.
on
(
'blur'
,
inactive
);
.
on
(
'blur'
,
inactive
);
}
}
function
refToHeader
(
itemRef
)
{
return
$
(
'.tocify-item[data-unique='
+
itemRef
+
']'
).
closest
(
'.tocify-header'
);
}
function
sortDescending
(
obj2
,
obj1
)
{
var
s1
=
parseInt
(
obj1
.
id
.
replace
(
/
[^\d]
/g
,
''
),
10
);
var
s2
=
parseInt
(
obj2
.
id
.
replace
(
/
[^\d]
/g
,
''
),
10
);
return
s1
===
s2
?
0
:
s1
<
s2
?
-
1
:
1
;
}
function
resetHeaderLocations
()
{
var
headers
=
$
(
".tocify-header"
).
sort
(
sortDescending
);
$
.
each
(
headers
,
function
(
index
,
item
)
{
$
(
item
).
insertBefore
(
$
(
"#toc ul:first-child"
));
});
}
function
search
(
event
)
{
function
search
(
event
)
{
var
sections
=
$
(
'section, #toc .tocify-header'
);
var
sections
=
$
(
'section, #toc .tocify-header'
);
...
@@ -53,12 +70,37 @@
...
@@ -53,12 +70,37 @@
if
(
this
.
value
)
{
if
(
this
.
value
)
{
sections
.
hide
();
sections
.
hide
();
var
results
=
index
.
search
(
this
.
value
);
// results are sorted by score in descending order
var
tmpResults
=
index
.
search
(
this
.
value
);
var
results
=
[];
// remove low score matches
$
.
each
(
tmpResults
,
function
(
index
,
item
)
{
if
(
item
.
score
>=
0.0001
)
results
.
push
(
item
);
});
if
(
results
.
length
)
{
if
(
results
.
length
)
{
lastRef
=
null
;
resetHeaderLocations
();
$
.
each
(
results
,
function
(
index
,
item
)
{
$
.
each
(
results
,
function
(
index
,
item
)
{
$
(
'#section-'
+
item
.
ref
).
show
();
var
itemRef
=
item
.
ref
;
$
(
'.tocify-item[data-unique='
+
item
.
ref
+
']'
).
closest
(
'.tocify-header'
).
show
();
$
(
'#section-'
+
itemRef
).
show
();
// headers must be repositioned in the DOM
var
closestHeader
=
refToHeader
(
itemRef
);
if
(
lastRef
)
{
refToHeader
(
lastRef
).
insertBefore
(
closestHeader
);
}
closestHeader
.
show
();
lastRef
=
itemRef
;
});
});
// position first element. it wasn't positioned above if len > 1
if
(
results
.
length
>
1
)
{
var
firstRef
=
results
[
0
].
ref
;
var
secondRef
=
results
[
1
].
ref
refToHeader
(
firstRef
).
insertBefore
(
refToHeader
(
secondRef
));
}
highlight
.
call
(
this
);
highlight
.
call
(
this
);
}
else
{
}
else
{
sections
.
show
();
sections
.
show
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment