]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/filterlist.lua
filterlist api cleanup
[dragonfireclient.git] / builtin / filterlist.lua
1 --------------------------------------------------------------------------------
2 -- Generic implementation of a filter/sortable list                           --
3 --------------------------------------------------------------------------------
4 filterlist = {}
5
6 --------------------------------------------------------------------------------
7 function filterlist.refresh(this)
8         this.m_raw_list = this.m_raw_list_fct(this.m_fetch_param)
9         filterlist.process(this)
10 end
11
12 --------------------------------------------------------------------------------
13 function filterlist.create(raw_fct,compare_fct,uid_match_fct,filter_fct,fetch_param)
14
15         assert((raw_fct ~= nil) and (type(raw_fct) == "function"))
16         assert((compare_fct ~= nil) and (type(compare_fct) == "function"))
17         
18         local this = {}
19         
20         this.m_raw_list_fct  = raw_fct
21         this.m_compare_fct   = compare_fct
22         this.m_filter_fct    = filter_fct
23         this.m_uid_match_fct = uid_match_fct
24         
25         this.m_filtercriteria = nil
26         this.m_fetch_param = fetch_param
27         
28         this.m_sortmode = "none"
29         this.m_sort_list = {}
30
31         this.m_processed_list = nil
32         this.m_raw_list = this.m_raw_list_fct(this.m_fetch_param)
33
34         filterlist.process(this)
35         
36         return this
37 end
38
39 --------------------------------------------------------------------------------
40 function filterlist.add_sort_mechanism(this,name,fct)
41         this.m_sort_list[name] = fct
42 end
43
44 --------------------------------------------------------------------------------
45 function filterlist.set_filtercriteria(this,criteria)
46         if criteria == this.m_filtercriteria and
47                 type(criteria) ~= "table" then
48                 return
49         end
50         this.m_filtercriteria = criteria
51         filterlist.process(this)
52 end
53
54 --------------------------------------------------------------------------------
55 function filterlist.get_filtercriteria(this)
56         return this.m_filtercriteria
57 end
58
59 --------------------------------------------------------------------------------
60 --supported sort mode "alphabetic|none"
61 function filterlist.set_sortmode(this,mode)
62         if (mode == this.m_sortmode) then
63                 return
64         end
65         this.m_sortmode = mode
66         filterlist.process(this)
67 end
68
69 --------------------------------------------------------------------------------
70 function filterlist.get_list(this)
71         return this.m_processed_list
72 end
73
74 --------------------------------------------------------------------------------
75 function filterlist.get_raw_list(this)
76         return this.m_raw_list
77 end
78
79 --------------------------------------------------------------------------------
80 function filterlist.get_raw_element(this,idx)
81         if type(idx) ~= "number" then
82                 idx = tonumber(idx)
83         end
84         
85         if idx ~= nil and idx > 0 and idx < #this.m_raw_list then
86                 return this.m_raw_list[idx]
87         end
88         
89         return nil
90 end
91
92 --------------------------------------------------------------------------------
93 function filterlist.get_raw_index(this,listindex)
94         assert(this.m_processed_list ~= nil)
95         
96         if listindex ~= nil and listindex > 0 and
97                 listindex <= #this.m_processed_list then
98                 local entry = this.m_processed_list[listindex]
99                 
100                 for i,v in ipairs(this.m_raw_list) do
101                 
102                         if this.m_compare_fct(v,entry) then
103                                 return i
104                         end
105                 end
106         end
107         
108         return 0
109 end
110
111 --------------------------------------------------------------------------------
112 function filterlist.get_current_index(this,listindex)
113         assert(this.m_processed_list ~= nil)
114         
115         if listindex ~= nil and listindex > 0 and
116                 listindex <= #this.m_raw_list then
117                 local entry = this.m_raw_list[listindex]
118                 
119                 for i,v in ipairs(this.m_processed_list) do
120                 
121                         if this.m_compare_fct(v,entry) then
122                                 return i
123                         end
124                 end
125         end
126         
127         return 0
128 end
129
130 --------------------------------------------------------------------------------
131 function filterlist.process(this)
132         assert(this.m_raw_list ~= nil)
133
134         if this.m_sortmode == "none" and
135                 this.m_filtercriteria == nil then
136                 this.m_processed_list = this.m_raw_list
137                 return
138         end
139         
140         this.m_processed_list = {}
141         
142         for k,v in pairs(this.m_raw_list) do
143                 if this.m_filtercriteria == nil or 
144                         this.m_filter_fct(v,this.m_filtercriteria) then
145                         table.insert(this.m_processed_list,v)
146                 end
147         end
148         
149         if this.m_sortmode == "none" then
150                 return
151         end
152         
153         if this.m_sort_list[this.m_sortmode] ~= nil and 
154                 type(this.m_sort_list[this.m_sortmode]) == "function" then
155                 
156                 this.m_sort_list[this.m_sortmode](this)
157         end
158 end
159
160 --------------------------------------------------------------------------------
161 function filterlist.size(this)
162         if this.m_processed_list == nil then
163                 return 0
164         end
165         
166         return #this.m_processed_list
167 end
168
169 --------------------------------------------------------------------------------
170 function filterlist.uid_exists_raw(this,uid)
171         for i,v in ipairs(this.m_raw_list) do
172                 if this.m_uid_match_fct(v,uid) then
173                         return true
174                 end
175         end
176         return false
177 end
178
179 --------------------------------------------------------------------------------
180 function filterlist.raw_index_by_uid(this, uid)
181         local elementcount = 0
182         local elementidx = 0
183         for i,v in ipairs(this.m_raw_list) do
184                 if this.m_uid_match_fct(v,uid) then
185                         elementcount = elementcount +1
186                         elementidx = i
187                 end
188         end
189         
190         
191         -- If there are more elements than one with same name uid can't decide which
192         -- one is meant. This shouldn't be possible but just for sure.
193         if elementcount > 1 then
194                 elementidx=0
195         end
196
197         return elementidx
198 end
199
200 --------------------------------------------------------------------------------
201 -- COMMON helper functions                                                    --
202 --------------------------------------------------------------------------------
203
204 --------------------------------------------------------------------------------
205 function compare_worlds(world1,world2)
206
207         if world1.path ~= world2.path then
208                 return false
209         end
210         
211         if world1.name ~= world2.name then
212                 return false
213         end
214         
215         if world1.gameid ~= world2.gameid then
216                 return false
217         end
218
219         return true
220 end
221
222 --------------------------------------------------------------------------------
223 function sort_worlds_alphabetic(this) 
224
225         table.sort(this.m_processed_list, function(a, b) 
226                         local n1 = a.name 
227                         local n2 = b.name 
228                         local count = math.min(#n1, #n2) 
229                         
230                         for i=1,count do 
231                                 if n1:sub(i, i):lower() < n2:sub(i, i):lower() then 
232                                         return true 
233                                 elseif n1:sub(i, i):lower() > n2:sub(i, i):lower() then 
234                                         return false 
235                                 end 
236                         end 
237                         return (#n1 <= #n2) 
238                 end) 
239 end