From faea3a7564720a1e33dd590abc2e2c943722b7be Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 4 Dec 2015 18:42:02 +0530 Subject: [PATCH] Content server: Implement search --- src/pyj/book_list/search.pyj | 30 +++++++++++++++++++++++++++++- src/pyj/widgets.pyj | 4 +++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/pyj/book_list/search.pyj b/src/pyj/book_list/search.pyj index 89365a2ed4..0e2dc23426 100644 --- a/src/pyj/book_list/search.pyj +++ b/src/pyj/book_list/search.pyj @@ -2,6 +2,9 @@ # License: GPL v3 Copyright: 2015, Kovid Goyal from elementmaker import E +from gettext import gettext as _ +from widgets import create_button, BUTTON_VPADDING +from book_list.globals import get_boss sp_counter = 0 @@ -15,10 +18,31 @@ class SearchPanel: style = '' div = E.div( id=self.container_id, style='display:none', - E.style(style, type='text/css') + E.style(style, type='text/css'), + E.div(style="text-align:center; padding:1ex 1em; border-bottom: solid 1px currentColor; margin-bottom: 0.5ex"), # search input container + E.div( + E.div(), + E.div() + ) ) book_list_container.appendChild(div) + # Build search input + search_container = div.firstChild.nextSibling + search_container.appendChild(E.div(style="display: flex; width: 100%;", + E.input( + type='text', autocomplete='on', autosave='search', inputmode='latin', name='search-books', + title=_('Search for books'), placeholder=_('Enter the search query'), spellcheck='false', + style=str.format("flex-grow: 10; padding: {} 0.5em; margin-right: 0.5em", BUTTON_VPADDING) + ), + create_button(_('Search'), icon='search', action=self.execute_search.bind(self), tooltip=_('Do the search')) + )) + search_container.firstChild.firstChild.addEventListener('keypress', def(event): + if event.keyCode == 13: + self.execute_search() + event.preventDefault(), event.stopPropagation() + ) + @property def container(self): return document.getElementById(self.container_id) @@ -30,3 +54,7 @@ class SearchPanel: @is_visible.setter def is_visible(self, val): self.container.style.display = 'block' if val else 'none' + + def execute_search(self): + text = self.container.querySelector('input[name="search-books"]').value or '' + get_boss().ui.books_view.change_search(text) diff --git a/src/pyj/widgets.pyj b/src/pyj/widgets.pyj index 66f49ea125..b59b597606 100644 --- a/src/pyj/widgets.pyj +++ b/src/pyj/widgets.pyj @@ -6,6 +6,8 @@ from elementmaker import E from book_list.theme import get_color +BUTTON_VPADDING = '0.5ex' + def create_button(text, icon=None, action=None, tooltip=None): cls = '' if icon: @@ -19,7 +21,7 @@ def create_button(text, icon=None, action=None, tooltip=None): create_button.style = build_rule('button.calibre-push-button', border_radius='1em', background_clip='padding-box', background_color=get_color('button-start'), background_image=str.format('linear-gradient(to bottom, {}, {})', get_color('button-start'), get_color('button-end')), - padding='1ex 1em', color=get_color('button-text'), cursor='pointer', font_size='inherit' + padding=BUTTON_VPADDING + ' 1em', color=get_color('button-text'), cursor='pointer', font_size='inherit' ) create_button.style += build_rule('button.calibre-push-button:hover', transform='scale(1.2)') create_button.style += build_rule('button.calibre-push-button:active', transform='scale(2)')