docs(readme): Start adding documentation for React version
This commit is contained in:
1 parent
ec51cb7a6c
commit
6658623f1f
365 files changed
+52155
-15
No files matched your search
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* lunr - http://lunrjs.com - A bit like Solr, but much smaller and not as bright - 0.5.12
|
||||
* Copyright (C) 2015 Oliver Nightingale
|
||||
* MIT Licensed
|
||||
* @license
|
||||
*/
|
||||
!function(){var t=function(e){var n=new t.Index;return n.pipeline.add(t.trimmer,t.stopWordFilter,t.stemmer),e&&e.call(n,n),n};t.version="0.5.12",t.utils={},t.utils.warn=function(t){return function(e){t.console&&console.warn&&console.warn(e)}}(this),t.EventEmitter=function(){this.events={}},t.EventEmitter.prototype.addListener=function(){var t=Array.prototype.slice.call(arguments),e=t.pop(),n=t;if("function"!=typeof e)throw new TypeError("last argument must be a function");n.forEach(function(t){this.hasHandler(t)||(this.events[t]=[]),this.events[t].push(e)},this)},t.EventEmitter.prototype.removeListener=function(t,e){if(this.hasHandler(t)){var n=this.events[t].indexOf(e);this.events[t].splice(n,1),this.events[t].length||delete this.events[t]}},t.EventEmitter.prototype.emit=function(t){if(this.hasHandler(t)){var e=Array.prototype.slice.call(arguments,1);this.events[t].forEach(function(t){t.apply(void 0,e)})}},t.EventEmitter.prototype.hasHandler=function(t){return t in this.events},t.tokenizer=function(t){return arguments.length&&null!=t&&void 0!=t?Array.isArray(t)?t.map(function(t){return t.toLowerCase()}):t.toString().trim().toLowerCase().split(/[\s\-]+/):[]},t.Pipeline=function(){this._stack=[]},t.Pipeline.registeredFunctions={},t.Pipeline.registerFunction=function(e,n){n in this.registeredFunctions&&t.utils.warn("Overwriting existing registered function: "+n),e.label=n,t.Pipeline.registeredFunctions[e.label]=e},t.Pipeline.warnIfFunctionNotRegistered=function(e){var n=e.label&&e.label in this.registeredFunctions;n||t.utils.warn("Function is not registered with pipeline. This may cause problems when serialising the index.\n",e)},t.Pipeline.load=function(e){var n=new t.Pipeline;return e.forEach(function(e){var i=t.Pipeline.registeredFunctions[e];if(!i)throw new Error("Cannot load un-registered function: "+e);n.add(i)}),n},t.Pipeline.prototype.add=function(){var e=Array.prototype.slice.call(arguments);e.forEach(function(e){t.Pipeline.warnIfFunctionNotRegistered(e),this._stack.push(e)},this)},t.Pipeline.prototype.after=function(e,n){t.Pipeline.warnIfFunctionNotRegistered(n);var i=this._stack.indexOf(e);if(-1==i)throw new Error("Cannot find existingFn");i+=1,this._stack.splice(i,0,n)},t.Pipeline.prototype.before=function(e,n){t.Pipeline.warnIfFunctionNotRegistered(n);var i=this._stack.indexOf(e);if(-1==i)throw new Error("Cannot find existingFn");this._stack.splice(i,0,n)},t.Pipeline.prototype.remove=function(t){var e=this._stack.indexOf(t);-1!=e&&this._stack.splice(e,1)},t.Pipeline.prototype.run=function(t){for(var e=[],n=t.length,i=this._stack.length,o=0;n>o;o++){for(var r=t[o],s=0;i>s&&(r=this._stack[s](r,o,t),void 0!==r);s++);void 0!==r&&e.push(r)}return e},t.Pipeline.prototype.reset=function(){this._stack=[]},t.Pipeline.prototype.toJSON=function(){return this._stack.map(function(e){return t.Pipeline.warnIfFunctionNotRegistered(e),e.label})},t.Vector=function(){this._magnitude=null,this.list=void 0,this.length=0},t.Vector.Node=function(t,e,n){this.idx=t,this.val=e,this.next=n},t.Vector.prototype.insert=function(e,n){this._magnitude=void 0;var i=this.list;if(!i)return this.list=new t.Vector.Node(e,n,i),this.length++;if(e<i.idx)return this.list=new t.Vector.Node(e,n,i),this.length++;for(var o=i,r=i.next;void 0!=r;){if(e<r.idx)return o.next=new t.Vector.Node(e,n,r),this.length++;o=r,r=r.next}return o.next=new t.Vector.Node(e,n,r),this.length++},t.Vector.prototype.magnitude=function(){if(this._magnitude)return this._magnitude;for(var t,e=this.list,n=0;e;)t=e.val,n+=t*t,e=e.next;return this._magnitude=Math.sqrt(n)},t.Vector.prototype.dot=function(t){for(var e=this.list,n=t.list,i=0;e&&n;)e.idx<n.idx?e=e.next:e.idx>n.idx?n=n.next:(i+=e.val*n.val,e=e.next,n=n.next);return i},t.Vector.prototype.similarity=function(t){return this.dot(t)/(this.magnitude()*t.magnitude())},t.SortedSet=function(){this.length=0,this.elements=[]},t.SortedSet.load=function(t){var e=new this;return e.elements=t,e.length=t.length,e},t.SortedSet.prototype.add=function(){var t,e;for(t=0;t<arguments.length;t++)e=arguments[t],~this.indexOf(e)||this.elements.splice(this.locationFor(e),0,e);this.length=this.elements.length},t.SortedSet.prototype.toArray=function(){return this.elements.slice()},t.SortedSet.prototype.map=function(t,e){return this.elements.map(t,e)},t.SortedSet.prototype.forEach=function(t,e){return this.elements.forEach(t,e)},t.SortedSet.prototype.indexOf=function(t){for(var e=0,n=this.elements.length,i=n-e,o=e+Math.floor(i/2),r=this.elements[o];i>1;){if(r===t)return o;t>r&&(e=o),r>t&&(n=o),i=n-e,o=e+Math.floor(i/2),r=this.elements[o]}return r===t?o:-1},t.SortedSet.prototype.locationFor=function(t){for(var e=0,n=this.elements.length,i=n-e,o=e+Math.floor(i/2),r=this.elements[o];i>1;)t>r&&(e=o),r>t&&(n=o),i=n-e,o=e+Math.floor(i/2),r=this.elements[o];return r>t?o:t>r?o+1:void 0},t.SortedSet.prototype.intersect=function(e){for(var n=new t.SortedSet,i=0,o=0,r=this.length,s=e.length,a=this.elements,h=e.elements;;){if(i>r-1||o>s-1)break;a[iLine truncated
|
||||
@@ -0,0 +1,50 @@
|
||||
require([
|
||||
'gitbook',
|
||||
'jquery'
|
||||
], function(gitbook, $) {
|
||||
// Global search objects
|
||||
var engine = null;
|
||||
var initialized = false;
|
||||
|
||||
// Set a new search engine
|
||||
function setEngine(Engine, config) {
|
||||
initialized = false;
|
||||
engine = new Engine(config);
|
||||
|
||||
init(config);
|
||||
}
|
||||
|
||||
// Initialize search engine with config
|
||||
function init(config) {
|
||||
if (!engine) throw new Error('No engine set for research. Set an engine using gitbook.research.setEngine(Engine).');
|
||||
|
||||
return engine.init(config)
|
||||
.then(function() {
|
||||
initialized = true;
|
||||
gitbook.events.trigger('search.ready');
|
||||
});
|
||||
}
|
||||
|
||||
// Launch search for query q
|
||||
function query(q, offset, length) {
|
||||
if (!initialized) throw new Error('Search has not been initialized');
|
||||
return engine.search(q, offset, length);
|
||||
}
|
||||
|
||||
// Get stats about search
|
||||
function getEngine() {
|
||||
return engine? engine.name : null;
|
||||
}
|
||||
|
||||
function isInitialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
// Initialize gitbook.search
|
||||
gitbook.search = {
|
||||
setEngine: setEngine,
|
||||
getEngine: getEngine,
|
||||
query: query,
|
||||
isInitialized: isInitialized
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
This CSS only styled the search results section, not the search input
|
||||
It defines the basic interraction to hide content when displaying results, etc
|
||||
*/
|
||||
#book-search-results .search-results {
|
||||
display: none;
|
||||
}
|
||||
#book-search-results .search-results ul.search-results-list {
|
||||
list-style-type: none;
|
||||
padding-left: 0;
|
||||
}
|
||||
#book-search-results .search-results ul.search-results-list li {
|
||||
margin-bottom: 1.5rem;
|
||||
padding-bottom: 0.5rem;
|
||||
/* Highlight results */
|
||||
}
|
||||
#book-search-results .search-results ul.search-results-list li p em {
|
||||
background-color: rgba(255, 220, 0, 0.4);
|
||||
font-style: normal;
|
||||
}
|
||||
#book-search-results .search-results .no-results {
|
||||
display: none;
|
||||
}
|
||||
#book-search-results.open .search-results {
|
||||
display: block;
|
||||
}
|
||||
#book-search-results.open .search-noresults {
|
||||
display: none;
|
||||
}
|
||||
#book-search-results.no-results .search-results .has-results {
|
||||
display: none;
|
||||
}
|
||||
#book-search-results.no-results .search-results .no-results {
|
||||
display: block;
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
require([
|
||||
'gitbook',
|
||||
'jquery'
|
||||
], function(gitbook, $) {
|
||||
var MAX_RESULTS = 15;
|
||||
var MAX_DESCRIPTION_SIZE = 500;
|
||||
|
||||
var usePushState = (typeof history.pushState !== 'undefined');
|
||||
|
||||
// DOM Elements
|
||||
var $body = $('body');
|
||||
var $bookSearchResults;
|
||||
var $searchInput;
|
||||
var $searchList;
|
||||
var $searchTitle;
|
||||
var $searchResultsCount;
|
||||
var $searchQuery;
|
||||
|
||||
// Throttle search
|
||||
function throttle(fn, wait) {
|
||||
var timeout;
|
||||
|
||||
return function() {
|
||||
var ctx = this, args = arguments;
|
||||
if (!timeout) {
|
||||
timeout = setTimeout(function() {
|
||||
timeout = null;
|
||||
fn.apply(ctx, args);
|
||||
}, wait);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function displayResults(res) {
|
||||
$bookSearchResults.addClass('open');
|
||||
|
||||
var noResults = res.count == 0;
|
||||
$bookSearchResults.toggleClass('no-results', noResults);
|
||||
|
||||
// Clear old results
|
||||
$searchList.empty();
|
||||
|
||||
// Display title for research
|
||||
$searchResultsCount.text(res.count);
|
||||
$searchQuery.text(res.query);
|
||||
|
||||
// Create an <li> element for each result
|
||||
res.results.forEach(function(res) {
|
||||
var $li = $('<li>', {
|
||||
'class': 'search-results-item'
|
||||
});
|
||||
|
||||
var $title = $('<h3>');
|
||||
|
||||
var $link = $('<a>', {
|
||||
'href': gitbook.state.basePath + '/' + res.url,
|
||||
'text': res.title
|
||||
});
|
||||
|
||||
var content = res.body.trim();
|
||||
if (content.length > MAX_DESCRIPTION_SIZE) {
|
||||
content = content.slice(0, MAX_DESCRIPTION_SIZE).trim()+'...';
|
||||
}
|
||||
var $content = $('<p>').html(content);
|
||||
|
||||
$link.appendTo($title);
|
||||
$title.appendTo($li);
|
||||
$content.appendTo($li);
|
||||
$li.appendTo($searchList);
|
||||
});
|
||||
}
|
||||
|
||||
function launchSearch(q) {
|
||||
// Add class for loading
|
||||
$body.addClass('with-search');
|
||||
$body.addClass('search-loading');
|
||||
|
||||
// Launch search query
|
||||
throttle(gitbook.search.query(q, 0, MAX_RESULTS)
|
||||
.then(function(results) {
|
||||
displayResults(results);
|
||||
})
|
||||
.always(function() {
|
||||
$body.removeClass('search-loading');
|
||||
}), 1000);
|
||||
}
|
||||
|
||||
function closeSearch() {
|
||||
$body.removeClass('with-search');
|
||||
$bookSearchResults.removeClass('open');
|
||||
}
|
||||
|
||||
function launchSearchFromQueryString() {
|
||||
var q = getParameterByName('q');
|
||||
if (q && q.length > 0) {
|
||||
// Update search input
|
||||
$searchInput.val(q);
|
||||
|
||||
// Launch search
|
||||
launchSearch(q);
|
||||
}
|
||||
}
|
||||
|
||||
function bindSearch() {
|
||||
// Bind DOM
|
||||
$searchInput = $('#book-search-input input');
|
||||
$bookSearchResults = $('#book-search-results');
|
||||
$searchList = $bookSearchResults.find('.search-results-list');
|
||||
$searchTitle = $bookSearchResults.find('.search-results-title');
|
||||
$searchResultsCount = $searchTitle.find('.search-results-count');
|
||||
$searchQuery = $searchTitle.find('.search-query');
|
||||
|
||||
// Launch query based on input content
|
||||
function handleUpdate() {
|
||||
var q = $searchInput.val();
|
||||
|
||||
if (q.length == 0) {
|
||||
closeSearch();
|
||||
}
|
||||
else {
|
||||
launchSearch(q);
|
||||
}
|
||||
}
|
||||
|
||||
// Detect true content change in search input
|
||||
// Workaround for IE < 9
|
||||
var propertyChangeUnbound = false;
|
||||
$searchInput.on('propertychange', function(e) {
|
||||
if (e.originalEvent.propertyName == 'value') {
|
||||
handleUpdate();
|
||||
}
|
||||
});
|
||||
|
||||
// HTML5 (IE9 & others)
|
||||
$searchInput.on('input', function(e) {
|
||||
// Unbind propertychange event for IE9+
|
||||
if (!propertyChangeUnbound) {
|
||||
$(this).unbind('propertychange');
|
||||
propertyChangeUnbound = true;
|
||||
}
|
||||
|
||||
handleUpdate();
|
||||
});
|
||||
|
||||
// Push to history on blur
|
||||
$searchInput.on('blur', function(e) {
|
||||
// Update history state
|
||||
if (usePushState) {
|
||||
var uri = updateQueryString('q', $(this).val());
|
||||
history.pushState({ path: uri }, null, uri);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
gitbook.events.on('page.change', function() {
|
||||
bindSearch();
|
||||
closeSearch();
|
||||
|
||||
// Launch search based on query parameter
|
||||
if (gitbook.search.isInitialized()) {
|
||||
launchSearchFromQueryString();
|
||||
}
|
||||
});
|
||||
|
||||
gitbook.events.on('search.ready', function() {
|
||||
bindSearch();
|
||||
|
||||
// Launch search from query param at start
|
||||
launchSearchFromQueryString();
|
||||
});
|
||||
|
||||
function getParameterByName(name) {
|
||||
var url = window.location.href;
|
||||
name = name.replace(/[\[\]]/g, '\\$&');
|
||||
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)', 'i'),
|
||||
results = regex.exec(url);
|
||||
if (!results) return null;
|
||||
if (!results[2]) return '';
|
||||
return decodeURIComponent(results[2].replace(/\+/g, ' '));
|
||||
}
|
||||
|
||||
function updateQueryString(key, value) {
|
||||
value = encodeURIComponent(value);
|
||||
|
||||
var url = window.location.href;
|
||||
var re = new RegExp('([?&])' + key + '=.*?(&|#|$)(.*)', 'gi'),
|
||||
hash;
|
||||
|
||||
if (re.test(url)) {
|
||||
if (typeof value !== 'undefined' && value !== null)
|
||||
return url.replace(re, '$1' + key + '=' + value + '$2$3');
|
||||
else {
|
||||
hash = url.split('#');
|
||||
url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, '');
|
||||
if (typeof hash[1] !== 'undefined' && hash[1] !== null)
|
||||
url += '#' + hash[1];
|
||||
return url;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (typeof value !== 'undefined' && value !== null) {
|
||||
var separator = url.indexOf('?') !== -1 ? '&' : '?';
|
||||
hash = url.split('#');
|
||||
url = hash[0] + separator + key + '=' + value;
|
||||
if (typeof hash[1] !== 'undefined' && hash[1] !== null)
|
||||
url += '#' + hash[1];
|
||||
return url;
|
||||
}
|
||||
else
|
||||
return url;
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user