Back to Home

ESO Lua File v101044

common/textsearch/textsearchobject.lua

[◄ back to folders ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
ZO_TextSearchObject = ZO_InitializingObject:Subclass()
function ZO_TextSearchObject:Initialize(searchContext, searchEditBox)
    self.searchContext = searchContext
    self.searchEditBox = searchEditBox
end
function ZO_TextSearchObject:SetupContextTextSearch()
    -- Can be overridden
    -- Context can be setup elsewhere in the code to be used here.
    -- The TEXT_SEARCH_MANAGER function will override the filter target descriptors for a context that already exists.
    --[[
Expected format for filterTargetDescriptors:
filterTargetDescriptors =
{
[<FilterTarget>] =
{
searchFilterList =
{
BACKGROUND_LIST_FILTER_TYPE_<FilterType>,
...
},
primaryKeys =
{
<List of keys> (ie. BAG_BACKPACK, BAG_BANK for FILTER_TARGET_BAG_SLOT or filterFunction() for filter like slottable in FILTER_TARGET_COLLECTIBLE)
},
},
}
TEXT_SEARCH_MANAGER:SetupContextTextSearch(self.searchContext, filterTargetDescriptor)
]]
--
end
function ZO_TextSearchObject:SetupOnTextChangedHandler()
    local currentSearchEditBox = self:GetSearchEditBox()
    if currentSearchEditBox then
        local function OnTextSearchTextChanged(editBox)
            self:OnTextSearchTextChanged(editBox)
        end
        currentSearchEditBox:SetHandler("OnTextChanged", OnTextSearchTextChanged)
    end
end
function ZO_TextSearchObject:SetTextSearchContext(context)
    self.searchContext = context
end
function ZO_TextSearchObject:SetSearchEditBox(searchEditBox)
    if searchEditBox ~= self.searchEditBox then
        self.searchEditBox = searchEditBox
        self:SetupOnTextChangedHandler()
    end
end
function ZO_TextSearchObject:GetSearchEditBox()
    return self.searchEditBox
end
function ZO_TextSearchObject:SetSearchCriteria(filterType, context, editBox)
    local previousContextActive = TEXT_SEARCH_MANAGER:IsActiveTextSearch(self.searchContext)
    if previousContextActive then
        self:DeactivateTextSearch()
    end
    self.searchFilterType = filterType
    self.searchContext = context
    if editBox then
        self:SetSearchEditBox(editBox)
    end
    if previousContextActive then
        self:ActivateTextSearch()
    end
end
function ZO_TextSearchObject:OnTextSearchTextChanged(editBox)
    if self.searchContext then
        TEXT_SEARCH_MANAGER:SetSearchText(self.searchContext, editBox:GetText())
    end
end
function ZO_TextSearchObject:OnUpdateSearchResults()
    -- To be overridden
end
function ZO_TextSearchObject:SetSearchText()
    if self.searchContext then
        TEXT_SEARCH_MANAGER:SetSearchText(self.searchContext, self:GetTextSearchText())
    end
end
function ZO_TextSearchObject:GetTextSearchText()
    local searchEditBox = self:GetSearchEditBox()
    if searchEditBox then
        return searchEditBox:GetText()
    end
    return ""
end
function ZO_TextSearchObject:UpdateSearchText()
    if self.searchEditBox and self.searchContext then
        self.searchEditBox:SetText(TEXT_SEARCH_MANAGER:GetSearchText(self.searchContext))
    end
end
function ZO_TextSearchObject:ActivateTextSearch()
    if self.searchContext and not TEXT_SEARCH_MANAGER:IsActiveTextSearch(self.searchContext) then
        self:UpdateSearchText()
        local function OnTextSearchResults()
            self.dirty = true
            self:OnUpdateSearchResults()
        end
        self.onTextSearchResults = OnTextSearchResults
        TEXT_SEARCH_MANAGER:ActivateTextSearch(self.searchContext)
        TEXT_SEARCH_MANAGER:RegisterCallback("UpdateSearchResults", OnTextSearchResults)
    end
end
function ZO_TextSearchObject:DeactivateTextSearch()
    if self.searchContext then
        TEXT_SEARCH_MANAGER:DeactivateTextSearch(self.searchContext)
        TEXT_SEARCH_MANAGER:UnregisterCallback("UpdateSearchResults", self.onTextSearchResults)
    end
end
function ZO_TextSearchObject:HasSearchFilter()
    return TEXT_SEARCH_MANAGER:HasSearchFilter(self.searchContext)
end
function ZO_TextSearchObject:SetSearchFilterType(searchFilterType)
    self.searchFilterType = searchFilterType
end
function ZO_TextSearchObject:IsDataInSearchTextResults(dataId)
    if self.searchContext and self.searchFilterType then
        if type(self.searchFilterType) == "table" then
            for i, searchFilterType in ipairs(self.searchFilterType) do
                if TEXT_SEARCH_MANAGER:IsDataInSearchTextResults(self.searchContext, searchFilterType, dataId) then
                    return true
                end
            end
            return false
        else
            return TEXT_SEARCH_MANAGER:IsDataInSearchTextResults(self.searchContext, self.searchFilterType, dataId)
        end
    end
    -- Return true for every result if we don't have a context search
    return true
end