2014-11-05 04:28:11 +00:00
|
|
|
local ButtonDialog = require("ui/widget/buttondialog")
|
2017-04-07 13:20:57 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
2014-11-05 04:28:11 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2016-12-29 08:10:38 +00:00
|
|
|
local logger = require("logger")
|
2014-11-05 04:28:11 +00:00
|
|
|
local _ = require("gettext")
|
|
|
|
|
|
|
|
local ReaderSearch = InputContainer:new{
|
|
|
|
direction = 0, -- 0 for search forward, 1 for search backward
|
2014-11-17 09:58:25 +00:00
|
|
|
case_insensitive = true, -- default to case insensitive
|
2014-11-05 04:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ReaderSearch:init()
|
|
|
|
self.ui.menu:registerToMainMenu(self)
|
|
|
|
end
|
|
|
|
|
2017-03-04 13:46:38 +00:00
|
|
|
function ReaderSearch:addToMainMenu(menu_items)
|
|
|
|
menu_items.fulltext_search = {
|
2014-11-05 04:28:11 +00:00
|
|
|
text = _("Fulltext search"),
|
|
|
|
tap_input = {
|
|
|
|
title = _("Input text to search for"),
|
2017-04-07 13:20:57 +00:00
|
|
|
ok_text = _("Search all text"),
|
2014-11-05 04:28:11 +00:00
|
|
|
type = "text",
|
|
|
|
callback = function(input)
|
|
|
|
self:onShowSearchDialog(input)
|
|
|
|
end,
|
|
|
|
},
|
2017-02-28 21:46:32 +00:00
|
|
|
}
|
2014-11-05 04:28:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderSearch:onShowSearchDialog(text)
|
2016-06-27 16:43:23 +00:00
|
|
|
local do_search = function(search_func, _text, param)
|
2014-11-05 04:28:11 +00:00
|
|
|
return function()
|
2016-06-27 16:43:23 +00:00
|
|
|
local res = search_func(self, _text, param)
|
2014-11-05 04:28:11 +00:00
|
|
|
if res then
|
2014-11-17 09:58:25 +00:00
|
|
|
if self.ui.document.info.has_pages then
|
|
|
|
self.ui.link:onGotoLink({page = res.page - 1})
|
|
|
|
self.view.highlight.temp[res.page] = res
|
|
|
|
else
|
|
|
|
self.ui.link:onGotoLink(res[1].start)
|
|
|
|
end
|
2014-11-05 04:28:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.search_dialog = ButtonDialog:new{
|
|
|
|
alpha = 0.5,
|
|
|
|
buttons = {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = "|<",
|
|
|
|
callback = do_search(self.searchFromStart, text),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = "<",
|
|
|
|
callback = do_search(self.searchNext, text, 1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = ">",
|
|
|
|
callback = do_search(self.searchNext, text, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = ">|",
|
|
|
|
callback = do_search(self.searchFromEnd, text),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
tap_close_callback = function()
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("highlight clear")
|
2014-11-05 04:28:11 +00:00
|
|
|
self.ui.highlight:clear()
|
|
|
|
end,
|
|
|
|
}
|
2016-02-16 07:10:07 +00:00
|
|
|
do_search(self.searchFromCurrent, text, 0)()
|
2014-11-05 04:28:11 +00:00
|
|
|
UIManager:show(self.search_dialog)
|
2014-11-30 00:12:00 +00:00
|
|
|
-- TODO: regional
|
2014-11-05 04:28:11 +00:00
|
|
|
UIManager:setDirty(self.dialog, "partial")
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderSearch:search(pattern, origin)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("search pattern", pattern)
|
2016-06-14 18:34:46 +00:00
|
|
|
if pattern == nil or pattern == '' then return end
|
2014-11-05 04:28:11 +00:00
|
|
|
local direction = self.direction
|
|
|
|
local case = self.case_insensitive
|
2014-11-17 09:58:25 +00:00
|
|
|
local page = self.view.state.page
|
|
|
|
return self.ui.document:findText(pattern, origin, direction, case, page)
|
2014-11-05 04:28:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderSearch:searchFromStart(pattern)
|
|
|
|
self.direction = 0
|
|
|
|
return self:search(pattern, -1)
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderSearch:searchFromEnd(pattern)
|
|
|
|
self.direction = 1
|
|
|
|
return self:search(pattern, -1)
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderSearch:searchFromCurrent(pattern, direction)
|
|
|
|
self.direction = direction
|
|
|
|
return self:search(pattern, 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- ignore current page and search next occurrence
|
|
|
|
function ReaderSearch:searchNext(pattern, direction)
|
|
|
|
self.direction = direction
|
|
|
|
return self:search(pattern, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
return ReaderSearch
|