prelim search support

This commit is contained in:
Alex Yatskov 2017-03-04 11:45:55 -08:00
parent ca6d12dd62
commit b1950cf47e
6 changed files with 96 additions and 36 deletions

View File

@ -17,3 +17,62 @@
*/ */
function onSearch(e) {
e.preventDefault();
instYomi().termsFind($('#query').val()).then(({length, definitions}) => {
const options = instYomi().options;
const params = {
definitions,
grouped: options.general.groupResults,
addable: options.anki.enabled,
playback: options.general.audioPlayback
};
return instYomi().textRender('terms.html', params);
}).then(content => {
$('#content').html(content);
});
// const sequence = ++this.sequence;
// const params = {
// definitions,
// grouped: options.general.groupResults,
// addable: options.ankiMethod !== 'disabled',
// playback: options.general.audioPlayback
// };
// definitions.forEach(definition => {
// definition.sentence = context.sentence;
// definition.url = context.url;
// });
// this.definitions = definitions;
// this.showSpinner(false);
// window.scrollTo(0, 0);
// bgTextRender(params, 'terms.html').then(content => {
// $('#content').html(content);
// $('.action-add-note').click(this.onAddNote.bind(this));
// $('.kanji-link').click(e => {
// e.preventDefault();
// const character = $(e.target).text();
// bgKanjiFind(character).then(definitions => this.api_showKanjiDefs({definitions, options, context}));
// });
// $('.action-play-audio').click(e => {
// e.preventDefault();
// const index = $(e.currentTarget).data('index');
// this.playAudio(this.definitions[index]);
// });
// this.updateAddNoteButtons(['term_kanji', 'term_kana'], sequence);
// }).catch(error => {
// this.handleError(error);
// });
}
$(document).ready(() => {
$('#search').click(onSearch);
});

View File

@ -126,39 +126,40 @@ class Yomichan {
return note; return note;
} }
termsFind(text) {
const searcher = this.options.general.groupResults ?
this.translator.findTermsGrouped.bind(this.translator) :
this.translator.findTerms.bind(this.translator);
return searcher(text, dictEnabled(this.options), this.options.general.softKatakana).then(({definitions, length}) => {
return {length, definitions: definitions.slice(0, this.options.general.maxResults)};
});
}
kanjiFind(text) {
return this.translator.findKanji(text, dictEnabled(this.options)).then(definitions => {
return definitions.slice(0, this.options.general.maxResults);
});
}
textRender(template, data) {
return Handlebars.templates[template](data);
}
api_optionsGet({callback}) { api_optionsGet({callback}) {
promiseCallback(optionsLoad(), callback); promiseCallback(optionsLoad(), callback);
} }
api_kanjiFind({text, callback}) { api_kanjiFind({text, callback}) {
promiseCallback( promiseCallback(this.kanjiFind(text), callback);
this.translator.findKanji(text, dictEnabled(this.options)).then(definitions => {
return definitions.slice(0, this.options.general.maxResults);
}),
callback
);
} }
api_termsFind({text, callback}) { api_termsFind({text, callback}) {
promiseCallback( promiseCallback(this.termsFind(text), callback);
this.translator.findTerms(text, dictEnabled(this.options), this.options.general.softKatakana).then(({definitions, length}) => {
return {length, definitions: definitions.slice(0, this.options.general.maxResults)};
}),
callback
);
}
api_termsFindGrouped({text, callback}) {
promiseCallback(
this.translator.findTermsGrouped(text, dictEnabled(this.options), this.options.general.softKatakana).then(({definitions, length}) => {
return {length, definitions: definitions.slice(0, this.options.general.maxResults)};
}),
callback
);
} }
api_textRender({template, data, callback}) { api_textRender({template, data, callback}) {
callback({result: Handlebars.templates[template](data)}); callback({result: this.textRender(template, data)});
} }
api_definitionAdd({definition, mode, callback}) { api_definitionAdd({definition, mode, callback}) {

View File

@ -5,19 +5,24 @@
<title>Yomichan Search</title> <title>Yomichan Search</title>
<link rel="stylesheet" type="text/css" href="../lib/bootstrap-3.3.7-dist/css/bootstrap.min.css"> <link rel="stylesheet" type="text/css" href="../lib/bootstrap-3.3.7-dist/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../lib/bootstrap-3.3.7-dist/css/bootstrap-theme.min.css"> <link rel="stylesheet" type="text/css" href="../lib/bootstrap-3.3.7-dist/css/bootstrap-theme.min.css">
<link rel="stylesheet" type="text/css" href="../fg/css/frame.css">
</head> </head>
<body> <body>
<div class="container-fluid"> <div class="container-fluid">
<p> <p>
<div class="input-group"> <form class="input-group">
<input type="text" class="form-control" placeholder="Search for..."> <input type="text" class="form-control" placeholder="Search for..." id="query">
<span class="input-group-btn"> <span class="input-group-btn">
<button class="btn btn-default form-control" type="button">Search</button> <input type="submit" class="btn btn-default form-control" id="search" value="Search">
</span> </span>
</div> </form>
</p> </p>
<div id="content"></div>
</div> </div>
<script src="../lib/jquery-3.1.1.min.js"></script>
<script src="js/gecko.js"></script> <script src="js/gecko.js"></script>
<script src="js/util.js"></script>
<script src="js/search.js"></script> <script src="js/search.js"></script>
</body> </body>
</html> </html>

View File

@ -139,8 +139,7 @@ class Driver {
searchTerms(textSource) { searchTerms(textSource) {
textSource.setEndOffset(this.options.scanning.length); textSource.setEndOffset(this.options.scanning.length);
const findFunc = this.options.general.groupResults ? bgTermsFindGrouped : bgTermsFind; return bgTermsFind(textSource.text()).then(({definitions, length}) => {
return findFunc(textSource.text()).then(({definitions, length}) => {
if (definitions.length === 0) { if (definitions.length === 0) {
return false; return false;
} else { } else {

View File

@ -36,7 +36,7 @@ class Frame {
const params = { const params = {
definitions, definitions,
grouped: options.general.groupResults, grouped: options.general.groupResults,
addable: options.ankiMethod !== 'disabled', addable: options.anki.enabled,
playback: options.general.audioPlayback playback: options.general.audioPlayback
}; };
@ -49,7 +49,7 @@ class Frame {
this.showSpinner(false); this.showSpinner(false);
window.scrollTo(0, 0); window.scrollTo(0, 0);
bgTextRender(params, 'terms.html').then(content => { bgTextRender('terms.html', params).then(content => {
$('#content').html(content); $('#content').html(content);
$('.action-add-note').click(this.onAddNote.bind(this)); $('.action-add-note').click(this.onAddNote.bind(this));
@ -87,7 +87,7 @@ class Frame {
this.showSpinner(false); this.showSpinner(false);
window.scrollTo(0, 0); window.scrollTo(0, 0);
bgTextRender(params, 'kanji.html').then(content => { bgTextRender('kanji.html', params).then(content => {
$('#content').html(content); $('#content').html(content);
$('.action-add-note').click(this.onAddNote.bind(this)); $('.action-add-note').click(this.onAddNote.bind(this));

View File

@ -46,15 +46,11 @@ function bgTermsFind(text) {
return bgInvoke('termsFind', {text}); return bgInvoke('termsFind', {text});
} }
function bgTermsFindGrouped(text) {
return bgInvoke('termsFindGrouped', {text});
}
function bgKanjiFind(text) { function bgKanjiFind(text) {
return bgInvoke('kanjiFind', {text}); return bgInvoke('kanjiFind', {text});
} }
function bgTextRender(data, template) { function bgTextRender(template, data) {
return bgInvoke('textRender', {data, template}); return bgInvoke('textRender', {data, template});
} }