Module:Stamp datatable

From Sekaipedia
local getArgs = require('Module:Arguments').getArgs
local DatatableBuilder = require('Module:DatatableBuilder')
local DisplayFns       = require('Module:DisplayFunctions')
local QueryBuilder = require('Module:QueryBuilder')

local p = {}

local function splitStringWithDelim(delim)
	return function(str)
		if str == nil then
			return nil
		end
		
		local list = mw.text.split(str, delim)
		for i=1,#list do
			list[i] = mw.text.trim(list[i])
		end
		
		return list
	end
end


function p.main(frame)
	local args = getArgs(frame)
	
	local columns = args['columns']
	
	local qb = QueryBuilder.new()
	qb
		:setTables('stamps')
		:setFields([[
			stamp_id,
			image,
			japanese,
			romaji,
			english,
			acquire,
			notes
		]])
		:setOrderBy('stamps.stamp_id ASC')
	qb
		:addWhere('stamps.type', '=', args['types'])
		:addWhereList(
			'stamps.stamp_characters',
			'HOLDS',
			args['characters'],
			',',
			args['characters operator'] or 'OR'
		)
		:addWhereList(
			'stamps.acquire',
			'=',
			args['acquire'],
			',',
			'OR'
		)
	
	local datatable = DatatableBuilder.new()
	datatable
		:setColumns{
			{
				name = 'id',
				header = 'ID',
				dataFields = { 'stamp_id' },
				visible = false
			},
			{
				name = 'image',
				header = 'Stamp',
				dataFields = { 'image' },
				fn = DisplayFns.image('128px'),
				sortable = false
			},
			{
				name = 'japanese',
				header = 'Japanese',
				dataFields = { 'japanese' },
				sortable = false,
				visible = false,
				css = {
					['min-width'] = '100px',
					['width'] = '200px'
				}
			},
			{
				name = 'romaji',
				header = 'Romaji',
				dataFields = { 'romaji' },
				sortable = false,
				visible = false,
				css = {
					['min-width'] = '100px',
					['width'] = '200px'
				}
			},
			{
				name = 'english',
				header = 'English',
				dataFields = { 'english' },
				sortable = false,
				visible = false,
				css = {
					['min-width'] = '100px',
					['width'] = '200px'
				}
			},
			{
				name = 'type',
				header = 'Type',
				dataFields = { 'type' },
				visible = false
			},
			{
				name = 'character',
				header = 'Character',
				dataFields = { 'stamp_character' },
				visible = false
			},
			{
				name = 'acquire',
				header = 'Acquisition method',
				dataFields = { 'acquire' },
				visible = false,
				css = {
					['min-width'] = '100px',
					['width'] = '200px'
				}
			},
			{
				name = 'notes',
				header = 'Acquisition notes',
				dataFields = { 'notes' },
				visible = false,
				sortable = false,
				css = {
					['min-width'] = '100px',
					['width'] = '200px'
				}
			}
		}
		:setData(qb:query())
		
	columns = splitStringWithDelim(',')(columns)
	if columns and #columns > 0 then
		for _, column in ipairs(columns) do
			datatable:setColumnVisibility(column, true)
		end
	end
	
	return datatable:tostring()
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.