improved form validation
This commit is contained in:
@@ -22,29 +22,26 @@ public.config = {
|
||||
}
|
||||
|
||||
function private.color_accessor(callback)
|
||||
return aux.index_function({callback=callback}, function(self, key)
|
||||
self.private.table = m.config.color
|
||||
return aux.index_function(self.private, function(self, key)
|
||||
self.private.table = self.private.table[key]
|
||||
if getn(self.private.table) == 0 then
|
||||
return self.public
|
||||
else
|
||||
return callback(aux.util.copy(self.private.table))
|
||||
end
|
||||
end)[key]
|
||||
return aux.index_function({callback=callback, table=m.config.color}, function(self, key)
|
||||
self.private.table = self.private.table[key]
|
||||
if getn(self.private.table) == 0 then
|
||||
return self.public
|
||||
else
|
||||
local color = aux.util.copy(self.private.table)
|
||||
self.private.table = m.config.color
|
||||
return callback(color)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
public.color = m.color_accessor(function(color)
|
||||
for i=1,3 do
|
||||
color[i] = color[i]/255
|
||||
end
|
||||
return color
|
||||
local r, g, b, a = unpack(color)
|
||||
return {r/255, g/255, b/255, a}
|
||||
end)
|
||||
|
||||
public.inline_color = m.color_accessor(function(color)
|
||||
tinsert(color, 1, tremove(color))
|
||||
return format('|c%02X%02X%02X%02X', unpack(color))
|
||||
local r, g, b, a = unpack(color)
|
||||
return format('|c%02X%02X%02X%02X', a, r, g, b)
|
||||
end)
|
||||
|
||||
do
|
||||
@@ -356,6 +353,17 @@ function public.editbox(parent)
|
||||
end)
|
||||
end
|
||||
|
||||
function editbox:Enable()
|
||||
editbox:EnableMouse(true)
|
||||
editbox:SetTextColor(unpack(m.color.text.enabled))
|
||||
end
|
||||
|
||||
function editbox:Disable()
|
||||
editbox:EnableMouse(false)
|
||||
editbox:SetTextColor(unpack(m.color.text.disabled))
|
||||
editbox:ClearFocus()
|
||||
end
|
||||
|
||||
return editbox
|
||||
end
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ function private.item_column_init(rt, cell)
|
||||
local icon = iconBtn:CreateTexture(nil, 'ARTWORK')
|
||||
icon:SetPoint('TOPLEFT', 2, -2)
|
||||
icon:SetPoint('BOTTOMRIGHT', -2, 2)
|
||||
icon:SetTexCoord(.08, .94, .08, .94)
|
||||
icon:SetTexCoord(.07, .93, .08, .94)
|
||||
|
||||
cell.iconBtn = iconBtn
|
||||
cell.icon = icon
|
||||
|
||||
+88
-27
@@ -1,5 +1,85 @@
|
||||
local m, public, private = aux.module'search_tab'
|
||||
|
||||
function private.valid_level_input(str)
|
||||
local number = tonumber(str)
|
||||
local bounded = number and aux.util.bound(1, 60, number)
|
||||
if number ~= bounded or strfind(str, '^0') then
|
||||
return false, bounded
|
||||
else
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
private.blizzard_query = {}
|
||||
|
||||
private.blizzard_filter = setmetatable({}, {
|
||||
__newindex = function(_, key, value)
|
||||
m.update_form(key, value)
|
||||
m.blizzard_query[key] = value
|
||||
end,
|
||||
})
|
||||
|
||||
function private.update_form(key, value)
|
||||
|
||||
if key == 'class' and value ~= m.blizzard_query.class then
|
||||
m.blizzard_query.class = value
|
||||
UIDropDownMenu_ClearAll(m.subclass_dropdown)
|
||||
UIDropDownMenu_Initialize(m.subclass_dropdown, m.initialize_subclass_dropdown)
|
||||
m.blizzard_query.subclass = nil
|
||||
if value and GetAuctionItemSubClasses(value) then
|
||||
m.subclass_dropdown.button:Enable()
|
||||
else
|
||||
m.subclass_dropdown.button:Disable()
|
||||
end
|
||||
UIDropDownMenu_ClearAll(m.slot_dropdown)
|
||||
UIDropDownMenu_Initialize(m.slot_dropdown, m.initialize_slot_dropdown)
|
||||
m.blizzard_query.slot = nil
|
||||
m.slot_dropdown.button:Disable()
|
||||
elseif key == 'subclass' and value ~= m.blizzard_query.subclass then
|
||||
m.blizzard_query.subclass = value
|
||||
UIDropDownMenu_ClearAll(m.slot_dropdown)
|
||||
UIDropDownMenu_Initialize(m.slot_dropdown, m.initialize_slot_dropdown)
|
||||
m.blizzard_query.slot = nil
|
||||
if value and GetAuctionInvTypes(m.blizzard_query.class, value) then
|
||||
m.slot_dropdown.button:Enable()
|
||||
else
|
||||
m.slot_dropdown.button:Disable()
|
||||
end
|
||||
else
|
||||
m.blizzard_query[key] = value
|
||||
end
|
||||
|
||||
if key == 'exact' then
|
||||
if value then
|
||||
for _, key in {'class', 'subclass', 'slot', 'quality'} do
|
||||
m[key..'_dropdown'].button:Disable()
|
||||
end
|
||||
else
|
||||
m.class_dropdown.button:Enable()
|
||||
end
|
||||
for _, key in {'min_level', 'max_level'} do
|
||||
if value then
|
||||
m[key..'_input']:Disable()
|
||||
else
|
||||
m[key..'_input']:Enable()
|
||||
end
|
||||
end
|
||||
if value then
|
||||
m.usable_checkbox:Disable()
|
||||
else
|
||||
m.usable_checkbox:Enable()
|
||||
end
|
||||
end
|
||||
|
||||
if aux.util.set('min_level', 'max_level', 'usable', 'class', 'subclass', 'slot', 'quality')[key] then
|
||||
if value then
|
||||
m.exact_checkbox:Disable()
|
||||
elseif not aux.util.any({'min_level', 'max_level', 'usable', 'class', 'subclass', 'slot', 'quality'}, function(key) return m.blizzard_query[key] end) then
|
||||
m.exact_checkbox:Enable()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function private.get_form_query()
|
||||
local query_string = ''
|
||||
|
||||
@@ -306,14 +386,8 @@ end
|
||||
|
||||
function private.initialize_class_dropdown()
|
||||
local function on_click()
|
||||
local old_value = UIDropDownMenu_GetSelectedValue(m.class_dropdown)
|
||||
UIDropDownMenu_SetSelectedValue(m.class_dropdown, this.value)
|
||||
if this.value ~= old_value then
|
||||
UIDropDownMenu_ClearAll(m.subclass_dropdown)
|
||||
UIDropDownMenu_Initialize(m.subclass_dropdown, m.initialize_subclass_dropdown)
|
||||
UIDropDownMenu_ClearAll(m.slot_dropdown)
|
||||
UIDropDownMenu_Initialize(m.slot_dropdown, m.initialize_slot_dropdown)
|
||||
end
|
||||
m.blizzard_filter.class = this.value
|
||||
end
|
||||
|
||||
if not m.exact_checkbox:GetChecked() then
|
||||
@@ -338,58 +412,47 @@ function private.initialize_class_dropdown()
|
||||
end
|
||||
|
||||
function private.initialize_subclass_dropdown()
|
||||
|
||||
local function on_click()
|
||||
local old_value = UIDropDownMenu_GetSelectedValue(m.subclass_dropdown)
|
||||
UIDropDownMenu_SetSelectedValue(m.subclass_dropdown, this.value)
|
||||
if this.value ~= old_value then
|
||||
UIDropDownMenu_ClearAll(m.slot_dropdown)
|
||||
UIDropDownMenu_Initialize(m.slot_dropdown, m.initialize_slot_dropdown)
|
||||
end
|
||||
m.blizzard_filter.subclass = this.value
|
||||
end
|
||||
|
||||
local class_index = UIDropDownMenu_GetSelectedValue(m.class_dropdown)
|
||||
|
||||
if class_index and GetAuctionItemSubClasses(class_index) and not m.exact_checkbox:GetChecked() then
|
||||
m.subclass_dropdown.button:Enable()
|
||||
|
||||
if class_index and GetAuctionItemSubClasses(class_index) then
|
||||
UIDropDownMenu_AddButton{
|
||||
text = ALL,
|
||||
value = 0,
|
||||
func = on_click,
|
||||
}
|
||||
|
||||
for i, subclass in { GetAuctionItemSubClasses(class_index) } do
|
||||
for i, subclass in {GetAuctionItemSubClasses(class_index)} do
|
||||
UIDropDownMenu_AddButton{
|
||||
text = subclass,
|
||||
value = i,
|
||||
func = on_click,
|
||||
}
|
||||
end
|
||||
else
|
||||
m.subclass_dropdown.button:Disable()
|
||||
end
|
||||
end
|
||||
|
||||
function private.initialize_slot_dropdown()
|
||||
|
||||
local function on_click()
|
||||
UIDropDownMenu_SetSelectedValue(m.slot_dropdown, this.value)
|
||||
m.blizzard_filter.slot = this.value
|
||||
end
|
||||
|
||||
local class_index = UIDropDownMenu_GetSelectedValue(m.class_dropdown)
|
||||
local subclass_index = UIDropDownMenu_GetSelectedValue(m.subclass_dropdown)
|
||||
|
||||
if class_index and subclass_index and GetAuctionInvTypes(class_index, subclass_index) and not m.exact_checkbox:GetChecked() then
|
||||
m.slot_dropdown.button:Enable()
|
||||
|
||||
if subclass_index and GetAuctionInvTypes(class_index, subclass_index) then
|
||||
UIDropDownMenu_AddButton{
|
||||
text = ALL,
|
||||
value = 0,
|
||||
func = on_click,
|
||||
}
|
||||
|
||||
for _, slot in { GetAuctionInvTypes(class_index, subclass_index) } do
|
||||
for _, slot in {GetAuctionInvTypes(class_index, subclass_index)} do
|
||||
local slot_name = getglobal(slot)
|
||||
UIDropDownMenu_AddButton{
|
||||
text = slot_name,
|
||||
@@ -397,15 +460,13 @@ function private.initialize_slot_dropdown()
|
||||
func = on_click,
|
||||
}
|
||||
end
|
||||
else
|
||||
m.slot_dropdown.button:Disable()
|
||||
end
|
||||
end
|
||||
|
||||
function private.initialize_quality_dropdown()
|
||||
|
||||
local function on_click()
|
||||
UIDropDownMenu_SetSelectedValue(m.quality_dropdown, this.value)
|
||||
m.blizzard_filter.quality = this.value
|
||||
end
|
||||
|
||||
UIDropDownMenu_AddButton{
|
||||
|
||||
+23
-8
@@ -366,6 +366,9 @@ function private.create_frames()
|
||||
end
|
||||
end)
|
||||
editbox:SetScript('OnTabPressed', function()
|
||||
if m.blizzard_query.exact then
|
||||
return
|
||||
end
|
||||
if IsShiftKeyDown() then
|
||||
m.max_level_input:SetFocus()
|
||||
else
|
||||
@@ -382,12 +385,7 @@ function private.create_frames()
|
||||
local checkbox = aux.gui.checkbox(m.frame.filter)
|
||||
checkbox:SetPoint('TOPLEFT', m.name_input, 'TOPRIGHT', 16, 0)
|
||||
checkbox:SetScript('OnClick', function()
|
||||
UIDropDownMenu_ClearAll(m.class_dropdown)
|
||||
UIDropDownMenu_Initialize(m.class_dropdown, m.initialize_class_dropdown)
|
||||
UIDropDownMenu_ClearAll(m.subclass_dropdown)
|
||||
UIDropDownMenu_Initialize(m.subclass_dropdown, m.initialize_subclass_dropdown)
|
||||
UIDropDownMenu_ClearAll(m.slot_dropdown)
|
||||
UIDropDownMenu_Initialize(m.slot_dropdown, m.initialize_slot_dropdown)
|
||||
m.blizzard_filter.exact = this:GetChecked()
|
||||
end)
|
||||
local label = aux.gui.label(checkbox, aux.gui.config.small_font_size)
|
||||
label:SetPoint('BOTTOMLEFT', checkbox, 'TOPLEFT', -2, 1)
|
||||
@@ -399,7 +397,6 @@ function private.create_frames()
|
||||
editbox:SetPoint('TOPLEFT', m.name_input, 'BOTTOMLEFT', 0, -26)
|
||||
editbox:SetWidth(125)
|
||||
editbox:SetNumeric(true)
|
||||
editbox:SetMaxLetters(2)
|
||||
editbox:SetScript('OnTabPressed', function()
|
||||
if IsShiftKeyDown() then
|
||||
m.name_input:SetFocus()
|
||||
@@ -408,6 +405,14 @@ function private.create_frames()
|
||||
end
|
||||
end)
|
||||
editbox:SetScript('OnEnterPressed', aux._(editbox.ClearFocus, editbox))
|
||||
editbox:SetScript('OnTextChanged', function()
|
||||
local valid, number = m.valid_level_input(this:GetText())
|
||||
if valid then
|
||||
m.blizzard_filter.min_level = number
|
||||
else
|
||||
this:SetText(number)
|
||||
end
|
||||
end)
|
||||
local label = aux.gui.label(editbox, aux.gui.config.small_font_size)
|
||||
label:SetPoint('BOTTOMLEFT', editbox, 'TOPLEFT', -2, 1)
|
||||
label:SetText('Level Range')
|
||||
@@ -418,7 +423,6 @@ function private.create_frames()
|
||||
editbox:SetPoint('TOPLEFT', m.min_level_input, 'TOPRIGHT', 10, 0)
|
||||
editbox:SetWidth(125)
|
||||
editbox:SetNumeric(true)
|
||||
editbox:SetMaxLetters(2)
|
||||
editbox:SetScript('OnTabPressed', function()
|
||||
if IsShiftKeyDown() then
|
||||
m.min_level_input:SetFocus()
|
||||
@@ -427,6 +431,14 @@ function private.create_frames()
|
||||
end
|
||||
end)
|
||||
editbox:SetScript('OnEnterPressed', aux._(editbox.ClearFocus, editbox))
|
||||
editbox:SetScript('OnTextChanged', function()
|
||||
local valid, number = m.valid_level_input(this:GetText())
|
||||
if valid then
|
||||
m.blizzard_filter.max_level = number
|
||||
else
|
||||
this:SetText(number)
|
||||
end
|
||||
end)
|
||||
local label = aux.gui.label(editbox, aux.gui.config.medium_font_size)
|
||||
label:SetPoint('RIGHT', editbox, 'LEFT', -3, 0)
|
||||
label:SetText('-')
|
||||
@@ -435,6 +447,9 @@ function private.create_frames()
|
||||
do
|
||||
local checkbox = aux.gui.checkbox(m.frame.filter)
|
||||
checkbox:SetPoint('TOPLEFT', m.max_level_input, 'TOPRIGHT', 16, 0)
|
||||
checkbox:SetScript('OnClick', function()
|
||||
m.blizzard_filter.usable = this:GetChecked()
|
||||
end)
|
||||
local label = aux.gui.label(checkbox, aux.gui.config.small_font_size)
|
||||
label:SetPoint('BOTTOMLEFT', checkbox, 'TOPLEFT', -2, 1)
|
||||
label:SetText('Usable')
|
||||
|
||||
Reference in New Issue
Block a user