mirror of https://gitlab.com/mayx/mayx.gitlab.io
56 lines
5.8 KiB
JavaScript
56 lines
5.8 KiB
JavaScript
/*!
|
|
* Simple-Jekyll-Search
|
|
* Copyright 2015-2020, Christian Fei
|
|
* Licensed under the MIT License.
|
|
*/
|
|
String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"")}),Array.prototype.forEach||(Array.prototype.forEach=function(t,e){var r,n
|
|
if(null==this)throw new TypeError(" this is null or not defined")
|
|
var i=Object(this),o=i.length>>>0
|
|
if("function"!=typeof t)throw new TypeError(t+" is not a function")
|
|
for(arguments.length>1&&(r=e),n=0;o>n;){var u
|
|
n in i&&(u=i[n],t.call(r,u,n,i)),n++}}),Array.prototype.filter||(Array.prototype.filter=function(t,e){if("Function"!=typeof t&&"function"!=typeof t||!this)throw new TypeError
|
|
var r=this.length>>>0,n=Array(r),i=this,o=0,u=-1
|
|
if(void 0===e)for(;++u!==r;)u in this&&t(i[u],u,i)&&(n[o++]=i[u])
|
|
else for(;++u!==r;)u in this&&t.call(e,i[u],u,i)&&(n[o++]=i[u])
|
|
return n.length=o,n}),Array.prototype.indexOf||(Array.prototype.indexOf=function(t,e){var r
|
|
if(null==this)throw new TypeError('"this" is null or not defined')
|
|
var n=Object(this),i=n.length>>>0
|
|
if(0===i)return-1
|
|
var o=+e||0
|
|
if(Math.abs(o)===1/0&&(o=0),o>=i)return-1
|
|
for(r=Math.max(o>=0?o:i-Math.abs(o),0);i>r;){if(r in n&&n[r]===t)return r
|
|
r++}return-1}),Object.prototype.toString||(Object.prototype.toString=function(){return"[object "+(this===window?"Window":this===document?"Document":"Object")+"]"}),function(){function t(t){j.pattern=t.pattern||j.pattern,j.template=t.template||j.template,"function"==typeof t.middleware&&(j.middleware=t.middleware)}function e(t){return j.template.replace(j.pattern,function(e,r){var n=j.middleware(r,t[r],j.template)
|
|
return void 0!==n?n:t[r]||e})}function r(t,e){var r=e.length,n=t.length
|
|
if(n>r)return!1
|
|
if(n===r)return t===e
|
|
t:for(var i=0,o=0;n>i;i++){for(var u=t.charCodeAt(i);r>o;)if(e.charCodeAt(o++)===u)continue t
|
|
return!1}return!0}function n(){this.matches=function(t,e){return T(e.toLowerCase(),t.toLowerCase())}}function i(){this.matches=function(t,e){if(!t)return!1
|
|
t=t.trim().toLowerCase(),e=e.trim().toLowerCase()
|
|
for(var r=e.split(" "),n=!0,i=0;i<r.length;i++)if(t.indexOf(r[i])<0){n=!1
|
|
break}return n}}function o(){return 0}function u(t){return c(t)?f(t):s(t)?l(t):void 0}function a(){return A.length=0,A}function c(t){return t&&"[object Object]"===Object.prototype.toString.call(t)}function s(t){return t&&"[object Array]"===Object.prototype.toString.call(t)}function f(t){return A.push(t),A}function l(t){var e=[]
|
|
a()
|
|
for(var r=0,n=t.length;n>r;r++)c(t[r])&&e.push(f(t[r]))
|
|
return e}function p(t){return t?d(A,t,M.searchStrategy,M).sort(M.sort):[]}function h(t){M=t||{},M.fuzzy=t.fuzzy||!1,M.limit=t.limit||10,M.searchStrategy=t.fuzzy?x:E,M.sort=t.sort||o,M.exclude=t.exclude||[]}function d(t,e,r,n){for(var i=[],o=0;o<t.length&&i.length<n.limit;o++){var u=y(t[o],e,r,n)
|
|
u&&i.push(u)}return i}function y(t,e,r,n){for(var i in t)if(t.hasOwnProperty(i)&&!v(t[i],n.exclude)&&r.matches(t[i],e))return t}function v(t,e){for(var r=0,n=e.length;n>r;r++){var i=e[r]
|
|
if(RegExp(i).test(t))return!0}return!1}function m(t,e){var r=g()
|
|
r.open("GET",t,!0),r.onreadystatechange=w(r,e),r.send()}function w(t,e){return function(){if(4===t.readyState&&200===t.status)try{e(null,JSON.parse(t.responseText))}catch(r){e(r,null)}}}function g(){return window.XMLHttpRequest?new window.XMLHttpRequest:new ActiveXObject("Microsoft.XMLHTTP")}function O(t,e){var r={}
|
|
for(var n in t)t.hasOwnProperty(n)&&(r[n]=t[n],void 0!==e[n]&&(r[n]=e[n]))
|
|
return r}function S(t){try{return t instanceof Object&&JSON.parse(JSON.stringify(t))?!0:!1}catch(e){return!1}}var b={compile:e,setOptions:t},j={}
|
|
j.pattern=/\{(.*?)\}/g,j.template="",j.middleware=function(){}
|
|
var T=r,x=new n,E=new i,z={put:u,clear:a,search:p,setOptions:h},A=[],M={}
|
|
M.fuzzy=!1,M.limit=10,M.searchStrategy=M.fuzzy?x:E,M.sort=o,M.exclude=[]
|
|
var L={load:m},q=function F(t){function e(t){return t?void 0!==t.required&&t.required instanceof Array:!1}if(!e(t))throw Error("-- OptionsValidator: required options missing")
|
|
if(!(this instanceof F))return new F(t)
|
|
var r=t.required
|
|
this.getRequiredOptions=function(){return r},this.validate=function(t){for(var e=[],n=0;n<r.length;n++){var i=r[n]
|
|
void 0===t[i]&&e.push(i)}return e}},C={merge:O,isJSON:S}
|
|
!function(t){function e(t){z.put(t),o()}function r(t){L.load(t,function(r,n){r&&f("failed to get JSON ("+t+")"),e(n)})}function n(){p.resultsContainer.innerHTML=""}function i(t){p.resultsContainer.innerHTML+=t}function o(){function t(t){var e=t.target||t.srcElement
|
|
s(t.which)&&(n(),h(function(){u(e.value)},p.debounceTime))}var e=p.searchInput
|
|
e.addEventListener?e.addEventListener("input",t):e.attachEvent&&e.attachEvent("onpropertychange",function(e){"value"===e.propertyName&&t(e)})}function u(t){c(t)&&(n(),a(z.search(t),t))}function a(t,e){var r=t.length
|
|
if(0===r)return i(p.noResultsText)
|
|
for(var n=0;r>n;n++)t[n].query=e,i(b.compile(t[n]))}function c(t){return t&&t.length>0}function s(t){for(var e=[13,16,20,37,38,39,40,91],r=0;r<e.length;r++)if(e[r]===t)return!1
|
|
return!0}function f(t){throw Error("SimpleJekyllSearch --- "+t)}var l,p={searchInput:null,resultsContainer:null,json:[],success:Function.prototype,searchResultTemplate:'<li><a href="{url}" title="{desc}">{title}</a></li>',templateMiddleware:Function.prototype,sortMiddleware:function(){return 0},noResultsText:"No results found",limit:10,fuzzy:!1,debounceTime:null,exclude:[]},h=function(t,e){e?(clearTimeout(l),l=setTimeout(t,e)):t.call()},d=["searchInput","resultsContainer","json"],y=q({required:d})
|
|
t.SimpleJekyllSearch=function(t){var n=y.validate(t)
|
|
n.length>0&&f("You must specify the following required options: "+d),p=C.merge(p,t),b.setOptions({template:p.searchResultTemplate,middleware:p.templateMiddleware}),z.setOptions({fuzzy:p.fuzzy,limit:p.limit,sort:p.sortMiddleware,exclude:p.exclude}),C.isJSON(p.json)?e(p.json):r(p.json)
|
|
var i={search:u}
|
|
return"function"==typeof p.success&&p.success.call(i),i}}(window)}() |