summaryrefslogtreecommitdiffstats
path: root/theme/javascript/core/glossary.js
diff options
context:
space:
mode:
authorAaron O'Mullan <aaron.omullan@gmail.com>2014-08-19 16:09:47 -0700
committerAaron O'Mullan <aaron.omullan@gmail.com>2014-08-19 16:09:47 -0700
commit71aff100921f7fcf94c9d75e0bd17c4fcb4669b6 (patch)
treecfefdd61394268bd6f4255eff7414c8a03460c3a /theme/javascript/core/glossary.js
parentf77404afe180e97692897237dca7598eae33a761 (diff)
parent4b390667e574ab46842821f029f3941d797a8b1a (diff)
downloadgitbook-71aff100921f7fcf94c9d75e0bd17c4fcb4669b6.zip
gitbook-71aff100921f7fcf94c9d75e0bd17c4fcb4669b6.tar.gz
gitbook-71aff100921f7fcf94c9d75e0bd17c4fcb4669b6.tar.bz2
Merge pull request #413 from GitbookIO/feature/glossary
Glossary terms highlight in page
Diffstat (limited to 'theme/javascript/core/glossary.js')
-rw-r--r--theme/javascript/core/glossary.js82
1 files changed, 82 insertions, 0 deletions
diff --git a/theme/javascript/core/glossary.js b/theme/javascript/core/glossary.js
new file mode 100644
index 0000000..6e300e3
--- /dev/null
+++ b/theme/javascript/core/glossary.js
@@ -0,0 +1,82 @@
+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 pregQuote = function( str ) {
+ // http://kevin.vanzonneveld.net
+ // + original by: booeyOH
+ // + improved by: Ates Goral (http://magnetiq.com)
+ // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
+ // + bugfixed by: Onno Marsman
+ // * example 1: preg_quote("$40");
+ // * returns 1: '\$40'
+ // * example 2: preg_quote("*RRRING* Hello?");
+ // * returns 2: '\*RRRING\* Hello\?'
+ // * example 3: preg_quote("\\.+*?[^]$(){}=!<>|:");
+ // * returns 3: '\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:'
+
+ return (str+'').replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, "\\$1");
+ };
+
+ 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) {
+ var r = new RegExp( "\\b(" + pregQuote(term.name.toLowerCase()) + ")\\b" , 'gi' );
+
+ $el.find("p:contains('"+term.name+"')").each( function( i, element ) {
+ element = $(element);
+ var content = $(element).html();
+
+ content = content.replace(r, '<span class="glossary-term" data-glossary-term="' + term.id + '" title="' + term.description + '">$1</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
+ };
+}); \ No newline at end of file