mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-16 07:35:16 +00:00
Add action on glossary term in the page
This commit is contained in:
+1
-1
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,64 @@
|
||||
define([
|
||||
"jQuery",
|
||||
"lodash",
|
||||
"core/state"
|
||||
], function($, _, state) {
|
||||
var index = null;
|
||||
|
||||
// Use a specific idnex
|
||||
var useIndex = function(data) {
|
||||
index = data;
|
||||
};
|
||||
|
||||
// Load complete index
|
||||
var loadIndex = function() {
|
||||
return $.getJSON(state.basePath+"/glossary_index.json")
|
||||
.then(useIndex);
|
||||
};
|
||||
|
||||
// Get index and return a promise
|
||||
var getIndex = function() {
|
||||
var d = $.Deferred();
|
||||
|
||||
if (index) {
|
||||
d.resolve(index);
|
||||
} else {
|
||||
loadIndex().done(function(){
|
||||
d.resolve(index);
|
||||
}).fail(d.reject);
|
||||
}
|
||||
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
var init = function() {
|
||||
// Bind click on glossary item
|
||||
$(document).on("click", ".book-body .page-wrapper .page-inner .glossary-term", function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
location.href = state.basePath+"/GLOSSARY.html#"+$(e.currentTarget).data("glossary-term");
|
||||
});
|
||||
};
|
||||
|
||||
var replaceTerm = function($el, term) {
|
||||
$el.find("p:contains('"+term.name+"')").each( function( i, element ) {
|
||||
element = $(element);
|
||||
|
||||
var content = $(element).html();
|
||||
content = content.replace(term.name, '<span class="glossary-term" data-glossary-term="' + term.id + '" title="' + term.description + '">' + term.name + '</span>');
|
||||
element.html(content);
|
||||
});
|
||||
};
|
||||
|
||||
var prepare = function() {
|
||||
getIndex()
|
||||
.done(function() {
|
||||
_.each(index, _.partial(replaceTerm, $(".book-body .page-wrapper .page-inner")));
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
init: init,
|
||||
prepare: prepare
|
||||
};
|
||||
});
|
||||
@@ -33,7 +33,6 @@ define([
|
||||
};
|
||||
|
||||
return {
|
||||
init: init,
|
||||
search: search
|
||||
init: init
|
||||
};
|
||||
});
|
||||
@@ -5,8 +5,9 @@ define([
|
||||
"core/state",
|
||||
"core/progress",
|
||||
"core/loading",
|
||||
"core/search"
|
||||
], function($, URL, events, state, progress, loading, search) {
|
||||
"core/search",
|
||||
"core/glossary"
|
||||
], function($, URL, events, state, progress, loading, search, glossary) {
|
||||
var prev, next;
|
||||
|
||||
var usePushState = (typeof history.pushState !== "undefined");
|
||||
@@ -80,13 +81,16 @@ define([
|
||||
// Update navigation position
|
||||
updateNavigationPosition();
|
||||
|
||||
// Set glossary items
|
||||
glossary.prepare();
|
||||
|
||||
// Reset scroll
|
||||
$pageWrapper.scrollTop(0);
|
||||
|
||||
// Focus on content
|
||||
$pageWrapper.focus();
|
||||
|
||||
// Send to mixpanel
|
||||
// Notify
|
||||
events.trigger("page.change");
|
||||
};
|
||||
|
||||
|
||||
@@ -11,8 +11,9 @@ define([
|
||||
"core/navigation",
|
||||
"core/progress",
|
||||
"core/sidebar",
|
||||
"core/search"
|
||||
], function($, storage, sharing, dropdown, events, fontSettings, state, keyboard, navigation, progress, sidebar, search){
|
||||
"core/search",
|
||||
"core/glossary"
|
||||
], function($, storage, sharing, dropdown, events, fontSettings, state, keyboard, navigation, progress, sidebar, search, glossary){
|
||||
var start = function(config) {
|
||||
var $book;
|
||||
$book = state.$book;
|
||||
@@ -28,6 +29,9 @@ define([
|
||||
// Load search
|
||||
search.init();
|
||||
|
||||
// Load glossary
|
||||
glossary.init();
|
||||
|
||||
// Init keyboard
|
||||
keyboard.init();
|
||||
|
||||
|
||||
@@ -8,6 +8,11 @@
|
||||
pre, code {
|
||||
@import "./highlight/white.less";
|
||||
}
|
||||
|
||||
.glossary-term {
|
||||
cursor: help;
|
||||
text-decoration: underline;
|
||||
}
|
||||
}
|
||||
|
||||
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal {
|
||||
|
||||
Reference in New Issue
Block a user