mirror of https://github.com/freeCodeCamp/devdocs
parent
bdfea4b186
commit
0c3f1814aa
@ -1,207 +1,180 @@
|
|||||||
// TODO: This file was created by bulk-decaffeinate.
|
app.views.SearchScope = class SearchScope extends app.View {
|
||||||
// Sanity-check the conversion and remove this comment.
|
static SEARCH_PARAM = app.config.search_param;
|
||||||
/*
|
|
||||||
* decaffeinate suggestions:
|
|
||||||
* DS102: Remove unnecessary code created because of implicit returns
|
|
||||||
* DS103: Rewrite code to no longer use __guard__, or convert again using --optional-chaining
|
|
||||||
* DS206: Consider reworking classes to avoid initClass
|
|
||||||
* DS207: Consider shorter variations of null checks
|
|
||||||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
|
|
||||||
*/
|
|
||||||
(function () {
|
|
||||||
let SEARCH_PARAM = undefined;
|
|
||||||
let HASH_RGX = undefined;
|
|
||||||
app.views.SearchScope = class SearchScope extends app.View {
|
|
||||||
static initClass() {
|
|
||||||
SEARCH_PARAM = app.config.search_param;
|
|
||||||
|
|
||||||
this.elements = {
|
|
||||||
input: "._search-input",
|
|
||||||
tag: "._search-tag",
|
|
||||||
};
|
|
||||||
|
|
||||||
this.events = {
|
|
||||||
click: "onClick",
|
|
||||||
keydown: "onKeydown",
|
|
||||||
textInput: "onTextInput",
|
|
||||||
};
|
|
||||||
|
|
||||||
this.routes = { after: "afterRoute" };
|
|
||||||
|
|
||||||
HASH_RGX = new RegExp(`^#${SEARCH_PARAM}=(.+?) .`);
|
|
||||||
}
|
|
||||||
|
|
||||||
init() {
|
static elements = {
|
||||||
this.placeholder = this.input.getAttribute("placeholder");
|
input: "._search-input",
|
||||||
|
tag: "._search-tag",
|
||||||
|
};
|
||||||
|
|
||||||
this.searcher = new app.SynchronousSearcher({
|
static events = {
|
||||||
fuzzy_min_length: 2,
|
click: "onClick",
|
||||||
max_results: 1,
|
keydown: "onKeydown",
|
||||||
});
|
textInput: "onTextInput",
|
||||||
this.searcher.on("results", (results) => this.onResults(results));
|
};
|
||||||
}
|
|
||||||
|
|
||||||
getScope() {
|
static routes = { after: "afterRoute" };
|
||||||
return this.doc || app;
|
|
||||||
}
|
|
||||||
|
|
||||||
isActive() {
|
static HASH_RGX = new RegExp(`^#${SearchScope.SEARCH_PARAM}=(.+?) .`);
|
||||||
return !!this.doc;
|
|
||||||
}
|
init() {
|
||||||
|
this.placeholder = this.input.getAttribute("placeholder");
|
||||||
|
|
||||||
|
this.searcher = new app.SynchronousSearcher({
|
||||||
|
fuzzy_min_length: 2,
|
||||||
|
max_results: 1,
|
||||||
|
});
|
||||||
|
this.searcher.on("results", (results) => this.onResults(results));
|
||||||
|
}
|
||||||
|
|
||||||
|
getScope() {
|
||||||
|
return this.doc || app;
|
||||||
|
}
|
||||||
|
|
||||||
|
isActive() {
|
||||||
|
return !!this.doc;
|
||||||
|
}
|
||||||
|
|
||||||
name() {
|
name() {
|
||||||
return this.doc != null ? this.doc.name : undefined;
|
return this.doc != null ? this.doc.name : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
search(value, searchDisabled) {
|
||||||
|
if (searchDisabled == null) {
|
||||||
|
searchDisabled = false;
|
||||||
|
}
|
||||||
|
if (this.doc) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.searcher.find(app.docs.all(), "text", value);
|
||||||
|
if (!this.doc && searchDisabled) {
|
||||||
|
this.searcher.find(app.disabledDocs.all(), "text", value);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
search(value, searchDisabled) {
|
searchUrl() {
|
||||||
if (searchDisabled == null) {
|
let value;
|
||||||
searchDisabled = false;
|
if ((value = this.extractHashValue())) {
|
||||||
}
|
this.search(value, true);
|
||||||
if (this.doc) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.searcher.find(app.docs.all(), "text", value);
|
|
||||||
if (!this.doc && searchDisabled) {
|
|
||||||
this.searcher.find(app.disabledDocs.all(), "text", value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
searchUrl() {
|
onResults(results) {
|
||||||
let value;
|
let doc;
|
||||||
if ((value = this.extractHashValue())) {
|
if (!(doc = results[0])) {
|
||||||
this.search(value, true);
|
return;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if (app.docs.contains(doc)) {
|
||||||
|
this.selectDoc(doc);
|
||||||
|
} else {
|
||||||
|
this.redirectToDoc(doc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onResults(results) {
|
selectDoc(doc) {
|
||||||
let doc;
|
const previousDoc = this.doc;
|
||||||
if (!(doc = results[0])) {
|
if (doc === previousDoc) {
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
if (app.docs.contains(doc)) {
|
|
||||||
this.selectDoc(doc);
|
|
||||||
} else {
|
|
||||||
this.redirectToDoc(doc);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
this.doc = doc;
|
||||||
|
|
||||||
selectDoc(doc) {
|
this.tag.textContent = doc.fullName;
|
||||||
const previousDoc = this.doc;
|
this.tag.style.display = "block";
|
||||||
if (doc === previousDoc) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.doc = doc;
|
|
||||||
|
|
||||||
this.tag.textContent = doc.fullName;
|
this.input.removeAttribute("placeholder");
|
||||||
this.tag.style.display = "block";
|
this.input.value = this.input.value.slice(this.input.selectionStart);
|
||||||
|
this.input.style.paddingLeft = this.tag.offsetWidth + 10 + "px";
|
||||||
|
|
||||||
this.input.removeAttribute("placeholder");
|
$.trigger(this.input, "input");
|
||||||
this.input.value = this.input.value.slice(this.input.selectionStart);
|
this.trigger("change", this.doc, previousDoc);
|
||||||
this.input.style.paddingLeft = this.tag.offsetWidth + 10 + "px";
|
}
|
||||||
|
|
||||||
$.trigger(this.input, "input");
|
redirectToDoc(doc) {
|
||||||
this.trigger("change", this.doc, previousDoc);
|
const { hash } = location;
|
||||||
}
|
app.router.replaceHash("");
|
||||||
|
location.assign(doc.fullPath() + hash);
|
||||||
|
}
|
||||||
|
|
||||||
redirectToDoc(doc) {
|
reset() {
|
||||||
const { hash } = location;
|
if (!this.doc) {
|
||||||
app.router.replaceHash("");
|
return;
|
||||||
location.assign(doc.fullPath() + hash);
|
|
||||||
}
|
}
|
||||||
|
const previousDoc = this.doc;
|
||||||
|
this.doc = null;
|
||||||
|
|
||||||
reset() {
|
this.tag.textContent = "";
|
||||||
if (!this.doc) {
|
this.tag.style.display = "none";
|
||||||
return;
|
|
||||||
}
|
|
||||||
const previousDoc = this.doc;
|
|
||||||
this.doc = null;
|
|
||||||
|
|
||||||
this.tag.textContent = "";
|
this.input.setAttribute("placeholder", this.placeholder);
|
||||||
this.tag.style.display = "none";
|
this.input.style.paddingLeft = "";
|
||||||
|
|
||||||
this.input.setAttribute("placeholder", this.placeholder);
|
this.trigger("change", null, previousDoc);
|
||||||
this.input.style.paddingLeft = "";
|
}
|
||||||
|
|
||||||
this.trigger("change", null, previousDoc);
|
doScopeSearch(event) {
|
||||||
|
this.search(this.input.value.slice(0, this.input.selectionStart));
|
||||||
|
if (this.doc) {
|
||||||
|
$.stopEvent(event);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
doScopeSearch(event) {
|
onClick(event) {
|
||||||
this.search(this.input.value.slice(0, this.input.selectionStart));
|
if (event.target === this.tag) {
|
||||||
if (this.doc) {
|
this.reset();
|
||||||
$.stopEvent(event);
|
$.stopEvent(event);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onClick(event) {
|
onKeydown(event) {
|
||||||
if (event.target === this.tag) {
|
if (event.which === 8) {
|
||||||
|
// backspace
|
||||||
|
if (this.doc && this.input.selectionEnd === 0) {
|
||||||
this.reset();
|
this.reset();
|
||||||
$.stopEvent(event);
|
$.stopEvent(event);
|
||||||
}
|
}
|
||||||
}
|
} else if (!this.doc && this.input.value && !$.isChromeForAndroid()) {
|
||||||
|
if (event.ctrlKey || event.metaKey || event.altKey || event.shiftKey) {
|
||||||
onKeydown(event) {
|
|
||||||
if (event.which === 8) {
|
|
||||||
// backspace
|
|
||||||
if (this.doc && this.input.selectionEnd === 0) {
|
|
||||||
this.reset();
|
|
||||||
$.stopEvent(event);
|
|
||||||
}
|
|
||||||
} else if (!this.doc && this.input.value && !$.isChromeForAndroid()) {
|
|
||||||
if (event.ctrlKey || event.metaKey || event.altKey || event.shiftKey) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
event.which === 9 || // tab
|
|
||||||
(event.which === 32 && app.isMobile())
|
|
||||||
) {
|
|
||||||
// space
|
|
||||||
this.doScopeSearch(event);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
onTextInput(event) {
|
|
||||||
if (!$.isChromeForAndroid()) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!this.doc && this.input.value && event.data === " ") {
|
if (
|
||||||
|
event.which === 9 || // tab
|
||||||
|
(event.which === 32 && app.isMobile())
|
||||||
|
) {
|
||||||
|
// space
|
||||||
this.doScopeSearch(event);
|
this.doScopeSearch(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
extractHashValue() {
|
onTextInput(event) {
|
||||||
let value;
|
if (!$.isChromeForAndroid()) {
|
||||||
if ((value = this.getHashValue())) {
|
return;
|
||||||
const newHash = $.urlDecode(location.hash).replace(
|
|
||||||
`#${SEARCH_PARAM}=${value} `,
|
|
||||||
`#${SEARCH_PARAM}=`,
|
|
||||||
);
|
|
||||||
app.router.replaceHash(newHash);
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if (!this.doc && this.input.value && event.data === " ") {
|
||||||
|
this.doScopeSearch(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
getHashValue() {
|
extractHashValue() {
|
||||||
try {
|
let value;
|
||||||
return __guard__(
|
if ((value = this.getHashValue())) {
|
||||||
HASH_RGX.exec($.urlDecode(location.hash)),
|
const newHash = $.urlDecode(location.hash).replace(
|
||||||
(x) => x[1],
|
`#${SearchScope.SEARCH_PARAM}=${value} `,
|
||||||
);
|
`#${SearchScope.SEARCH_PARAM}=`,
|
||||||
} catch (error) {}
|
);
|
||||||
|
app.router.replaceHash(newHash);
|
||||||
|
return value;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
afterRoute(name, context) {
|
getHashValue() {
|
||||||
if (!app.isSingleDoc() && context.init && context.doc) {
|
try {
|
||||||
this.selectDoc(context.doc);
|
return SearchScope.HASH_RGX.exec($.urlDecode(location.hash))?.[1];
|
||||||
}
|
} catch (error) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
afterRoute(name, context) {
|
||||||
|
if (!app.isSingleDoc() && context.init && context.doc) {
|
||||||
|
this.selectDoc(context.doc);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
app.views.SearchScope.initClass();
|
};
|
||||||
return app.views.SearchScope;
|
|
||||||
})();
|
|
||||||
|
|
||||||
function __guard__(value, transform) {
|
|
||||||
return typeof value !== "undefined" && value !== null
|
|
||||||
? transform(value)
|
|
||||||
: undefined;
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in new issue