Module:BehaviorInstance

From ema
Jump to navigation Jump to search

Documentation for this module may be created at Module:BehaviorInstance/doc

local p = {}

function p.getBehaviorInstanceHierarchy(frame)
	result1 = mw.ext.cargo.query(
		'Behaviors,Pages',
		'CONCAT(Pages._pageName),Pages.Name',
		{
			join = 'Behaviors._pageName=Pages._pageName',
			orderBy = 'Pages.Name',
			limit = 500
		}
	)
	s = ''
	for k1,v1 in pairs(result1) do
		behavior = v1['CONCAT(Pages._pageName)']
		s = s .. '* [[' .. behavior  .. ']]\n'
		result2 = mw.ext.cargo.query(
			'Behavior_Instances,Pages',
			'CONCAT(Pages._pageName),Pages.Name',
			{
				where = 'Behavior_Instances.Associated_Behavior="' .. behavior .. '"',
				join = 'Behavior_Instances._pageName=Pages._pageName',
				orderBy = 'Pages.Name',
				limit = 500
			}
		)
		for k2,v2 in pairs(result2) do
			behavior_instance = v2['CONCAT(Pages._pageName)']
			s = s .. '** [[' .. behavior_instance  .. ']]\n'
		end
	end
	return s
end

function p.getPlatformIcon(frame)
  items = mw.text.split(frame.args[1], ",", true)
  icon = ""
  platforms = {
    "Windows",
    "Android",
    "iOS",
    "Mac OS X",
    "Linux"
  }
  for k1,v1 in pairs(items) do
  	for k2,v2 in pairs(platforms) do
    	platformFilename = "BehaviorInstance-" .. v2 .. ".png"
    	if (string.sub(v1, 1, #v2) == v2) then
    		if (#icon == 0) then
        		icon = platformFilename
        		break
      		elseif (icon ~= platformFilename) then
        		return "BehaviorInstance-multiple.png"
      		end
    	end
  	end
    if (#icon == 0) then
        icon = "BehaviorInstance.png"
    elseif (icon == "BehaviorInstance.png") then
        return "BehaviorInstance-multiple.png"
    end
  end
  if (#icon == 0) then
    return "BehaviorInstance.png"
  else
    return icon
  end
end

function p.getAssociatedBehaviorName(frame)
	if ( #frame.args[1] > 0 ) then
		result = mw.ext.cargo.query(
			'Pages',
			'Name',
			{
				where = '_pageName="' .. frame.args[1] .. '"'
			}
		)
		return result[1]['Name']
	else
		result = mw.ext.cargo.query(
			'Behavior_Instances,Pages',
			'Pages.Name',
			{
				join = 'Behavior_Instances.Associated_Behavior=Pages._pageName',
				where = 'Behavior_Instances._pageName="' .. frame:getParent():getTitle() .. '"'
			}
		)
		return result[1]['Pages.Name']
	end
end

function p.createBehaviorInstanceFields(frame)
	if ( #frame.args[1] > 0 ) then
		behavior = frame.args[1]
	else
		result = mw.ext.cargo.query(
			'Behavior_Instances,Pages',
			'Pages._pageName',
			{
				join = 'Behavior_Instances.Associated_Behavior=Pages._pageName',
				where = 'Behavior_Instances._pageName="' .. frame:getParent():getTitle() .. '"'
			}
		)
		behavior = result[1]['Pages._pageName']
	end
	attributes = mw.ext.cargo.query(
		'Associations,Attributes,Pages',
		'CONCAT(Attributes._pageName),Pages.Name',
		{
			where = 'Associations._pageName="' .. behavior .. '" AND ' ..
				'Associations.Association_Type="Attribute" AND ' ..
				'Attributes.Attribute_Category="Common"',
			join = 'Associations.Associated_Page=Attributes._pageName,Attributes._pageName=Pages._pageName'
		}
	)
	names = {}
	count = 0
	for k,v in pairs(attributes) do
		name = v['Pages.Name']
		attribute_page = v['CONCAT(Attributes._pageName)']
		names[name] = attribute_page
		count = count + 1
	end
	if (count > 0) then
		table.sort(names)
		s = ''
		for k,v in pairs(names) do
			s = s .. p.createBehaviorInstanceField(v, k)
		end
		s = '<br /><b>Inherited Attributes:</b><table class="wikitable" style="width:100%;margin:0;">' .. s .. '</table>'
		return s
	end
	return ''
end

function p.createBehaviorInstanceField(attribute, name)
  result = mw.ext.cargo.query(
    'Attributes',
    'Type',
    {
      where = '_pageName="' .. attribute .. '"'
    }
  )
  type = result[1]['Type']
  if (type == 'Enumerable List') then
    values = mw.ext.cargo.query(
      'Attribute_Values',
      'Name',
      {
      	where = '_pageName="' .. attribute .. '"'
  	  }
  	)
    valuetable = {}
    for k,value in pairs(values) do
      value = 
      table.insert(valuetable, value['Name'])
    end
    size = #valuetable
    if (size > 10) then
      size = 10
    end
    field = '{{{field|' .. name .. '|input type=listbox|mapping cargo table=Pages|mapping cargo field=Name|values=' .. table.concat(valuetable, ',') .. '|size=' .. size .. '}}}'
  else
    field = '{{{field|' .. name .. '|input type=textarea}}}'
  end
  return '<tr><th style="width:200px;text-align:left;padding-left:10px;padding-right:10px;">' .. name .. ':</th><td>' .. field .. '</td></tr>'
end

function p.getCommonAttributes(frame)
  result = mw.ext.cargo.query(
    'Attributes,Pages',
    'Pages.Name',
    {
      where = 'Attributes.Attribute_Category="Common"',
      join = 'Attributes._pageName=Pages._pageName',
      orderBy = 'Pages.Name',
      limit = 500
    }
  )
  s = ''
  for k,v in pairs(result) do
    if s ~= '' then
      s = s .. ','
    end
    s = s .. v['Pages.Name']
  end
  return s
end

function p.getAttributeType(frame)
  result = mw.ext.cargo.query(
    'Attributes,Pages',
    'Attributes.Type',
    {
      where = 'Pages.Name="' .. frame.args[1] .. '"',
      join = 'Attributes._pageName=Pages._pageName',
      limit = 1
    }
  )
  return result[1]['Attributes.Type']
end

return p