RuneScape Wiki
Register
(a thing)
Tag: sourceedit
 
m (fix?)
 
(4 intermediate revisions by one other user not shown)
Line 16: Line 16:
 
--used internally but can also be an entry point
 
--used internally but can also be an entry point
 
function p.jsnotice(frame)
 
function p.jsnotice(frame)
  +
return p._jsnotice(frame:getParent().args)
page = mw.title.getCurrentTitle()
 
  +
end
return 'The calculator form will appear here soon. You will need Javascript enabled and cannot be using the mobile version of the Wiki.\n\nIf you are on a mobile device, you can load the full version of the site by [' .. page:fullUrl('useskin=oasis') .. ' clicking here].'
 
  +
function p._jsnotice(args)
 
local page = mw.title.getCurrentTitle()
  +
if args.forminit then
  +
return args.forminit
  +
else
 
return 'The calculator form will appear here soon. You will need Javascript enabled and cannot be using the mobile version of the Wiki.\n\nIf you are on a mobile device, you can load the full version of the site by [' .. page:fullUrl('useskin=oasis') .. ' clicking here].'
  +
end
 
end
 
end
   
 
--used internally but can also be an entry point
 
--used internally but can also be an entry point
 
function p.resnotice(frame)
 
function p.resnotice(frame)
  +
return p._resnotice(frame:getParent().args)
return 'The result will appear here when you submit the form.'
 
  +
end
  +
function p._resnotice(args)
  +
if args.resultinit then
  +
return args.resultinit
  +
else
 
return 'The result will appear here when you submit the form.'
  +
end
 
end
 
end
   
 
--main entry for constructing a calculator
 
--main entry for constructing a calculator
 
function p.main(frame)
 
function p.main(frame)
−
local args = frame.args --frame:getParent().args
+
return p._main(frame:getParent().args)
  +
end
  +
function p._main(args)
 
local template, form, result
 
local template, form, result
 
local err = ''
 
local err = ''
Line 46: Line 62:
 
else
 
else
 
err = err .. 'No result ID specified<br />'
 
err = err .. 'No result ID specified<br />'
  +
end
  +
  +
local cat = ''
  +
-- if the category is not overriden AND [the page param is missing OR the page param = the title]
  +
if defto(args.cat, 'yes') ~= 'no' and (not hascontent(args.page) or args.page == mw.title.getCurrentTitle().prefixedText) then
  +
cat = '[[Category:Form calculators]]'
 
end
 
end
 
 
 
if err ~= '' then
 
if err ~= '' then
  +
if cat ~= '' then
return '<span class="error">'.."'''Fatal error(s) found in form definition:'''<br />" .. err .. '</span> [[Category:Form calculators]] [[Category:Form calculator errors]]'
 
  +
cat = cat .. '[[Category:Form calculator errors]]'
  +
end
 
return '<span class="error">'.."'''Fatal error(s) found in form definition:'''<br />" .. err .. '</span>' .. cat
 
end
 
end
 
 
Line 83: Line 108:
 
out = ''
 
out = ''
 
elseif outtype == 'basic' or outtype == '1' then
 
elseif outtype == 'basic' or outtype == '1' then
−
out = '\n\n<div id="'..form..'">'..p.jsnotice(frame)..'</div>\n<div id="'..result..'">'..p.resnotice(frame)..'</div>'
+
out = '\n\n<div id="'..form..'">'..p._jsnotice(args)..'</div>\n<div id="'..result..'">'..p._resnotice(args)..'</div>'
 
elseif outtype == 'verticaltable' then
 
elseif outtype == 'verticaltable' then
−
out='\n\nvtable here'
+
out = makeVTable(args, form, result)
 
elseif outtype == 'horizontaltable' then
 
elseif outtype == 'horizontaltable' then
−
out='\n\nhtable here'
+
out = makeHTable(args, form, result)
 
end
 
end
 
 
Line 94: Line 119:
 
end
 
end
 
 
−
return tostring(conf) .. err .. out .. '[[Category:Form calculators]]'
+
return tostring(conf) .. tostring(err) .. tostring(out) .. tostring(cat)
 
end
 
end
   
Line 117: Line 142:
 
conf:wikitext('param = ' .. str .. '\n')
 
conf:wikitext('param = ' .. str .. '\n')
 
end
 
end
  +
  +
function makeHTable(args, form, result)
  +
return mw.html.create('table')
  +
:tag('tr')
  +
:tag('td')
  +
:attr('id', form)
  +
:wikitext(p._jsnotice(args))
  +
:done()
  +
:tag('td')
  +
:css('width', '10px')
  +
:done()
  +
:tag('td')
  +
:attr('id', result)
  +
:wikitext(p._resnotice(args))
  +
:done()
  +
:done()
  +
end
  +
  +
  +
function makeVTable(args, form, result)
  +
return mw.html.create('table')
  +
:addClass('wikitable')
  +
:tag('tr')
  +
:tag('th')
  +
:wikitext('Calculator')
  +
:done()
  +
:done()
  +
:tag('tr')
  +
:tag('td')
  +
:attr('id', form)
  +
:wikitext(p._jsnotice(args))
  +
:done()
  +
:done()
  +
:tag('tr')
  +
:tag('th')
  +
:wikitext('Result')
  +
:done()
  +
:done()
  +
:tag('tr')
  +
:tag('td')
  +
:attr('id', result)
  +
:wikitext(p._resnotice(args))
  +
:done()
  +
:done()
  +
end
  +
   
 
return p
 
return p

Latest revision as of 01:17, 19 August 2018

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

-- implements [[Template:Form calculator]]
--    for generating form calculators
-- [[RuneScape:Calculators/Form calculators]]
-- 
-- performs some minor checks on the input; the javascript still does most of it, though
-- 
-- <nowiki>
local pt = require('Module:Paramtest')
local hascontent = pt.has_content
local defto = pt.default_to

-- possible parameter types
local param_types = {string = true, article = true, number = true, int = true, select = true, check = true, hs = true, fixed = true, hidden = true, semihidden = true}

local p = {}
--used internally but can also be an entry point
function p.jsnotice(frame)
	return p._jsnotice(frame:getParent().args)
end
function p._jsnotice(args)
    local page = mw.title.getCurrentTitle()
    if args.forminit then
        return args.forminit
    else
        return 'The calculator form will appear here soon. You will need Javascript enabled and cannot be using the mobile version of the Wiki.\n\nIf you are on a mobile device, you can load the full version of the site by [' .. page:fullUrl('useskin=oasis') .. ' clicking here].'
    end
end

--used internally but can also be an entry point
function p.resnotice(frame)
	return p._resnotice(frame:getParent().args)
end
function p._resnotice(args)
    if args.resultinit then
        return args.resultinit
    else
        return 'The result will appear here when you submit the form.'
    end
end

--main entry for constructing a calculator
function p.main(frame)
	return p._main(frame:getParent().args)
end
function p._main(args)
    local template, form, result
    local err = ''
    if hascontent(args.template) then
        template = args.template
    else
        err = 'No template specified<br />'
    end
    
    if hascontent(args.form) then
        form = args.form
    else
        err = err .. 'No form ID specified<br />'
    end
    
    if hascontent(args.result) then
        result = args.result
    else
        err = err .. 'No result ID specified<br />'
    end
    
    local cat = ''
    -- if the category is not overriden AND [the page param is missing OR the page param = the title]
    if defto(args.cat, 'yes') ~= 'no' and (not hascontent(args.page) or args.page == mw.title.getCurrentTitle().prefixedText) then
        cat = '[[Category:Form calculators]]'
    end
    
    if err ~= '' then
        if cat ~= '' then
            cat = cat .. '[[Category:Form calculator errors]]'
        end
        return '<span class="error">'.."'''Fatal error(s) found in form definition:'''<br />" .. err .. '</span>' .. cat
    end
    
    local conf = mw.html.create('div')
    conf:addClass('hidden jcConfig')
        :wikitext('\n')
        :wikitext('template = ' .. template .. '\n')
        :wikitext('form = ' .. form .. '\n')
        :wikitext('result = ' .. result .. '\n')
        :done()
        
    if hascontent(args.suggestns) then
        --check ns stuff
        for i,v in pairs(mw.text.split(args.suggestns, ',', true)) do
            if not mw.site.namespaces(tonumber(v)) then
                err = err .. 'Invalid namespace(s) detected in suggestns<br />'
                break
            end
        end
        
        conf:wikitext('suggestns = ' .. args.suggestns)
    end
    
    local i = 1
    while hascontent(args['param' .. i]) do
        process_param(args, conf, i)
        i = i + 1
    end
    
    local outtype = defto(args.outputtype, 'none')
    local out
    if outtype == 'none' or outtype == '0' then
        out = ''
    elseif outtype == 'basic' or outtype == '1'  then
        out = '\n\n<div id="'..form..'">'..p._jsnotice(args)..'</div>\n<div id="'..result..'">'..p._resnotice(args)..'</div>'
    elseif outtype == 'verticaltable' then
        out = makeVTable(args, form, result)
    elseif outtype == 'horizontaltable' then
        out = makeHTable(args, form, result)
    end
    
    if err ~= '' then
        err = '<span class="error">' .. "'''Minor error(s) found in form definition:'''<br />" .. err .. '</span>'
    end
    
    return tostring(conf) .. tostring(err) .. tostring(out) .. tostring(cat)
end

function type_check(arg)
    if hascontent(arg) and param_types[arg] then
        return arg
    else
        return 'string'
    end
end

function process_param(args, conf, num)
    local param = args['param' .. num]
    local label = defto(args['label' .. num], param)
    local type = type_check(args['type' .. num])
    local default = defto(args['default' .. num], '')
    local range = defto(args['range' .. num], '')
    
    --for clarity, do this here
    local str = param .. '|' .. label .. '|' .. default .. '|' .. type .. '|' .. range
    
    conf:wikitext('param = ' .. str .. '\n')
end

function makeHTable(args, form, result)
    return mw.html.create('table')
            :tag('tr')
                :tag('td')
                    :attr('id', form)
                    :wikitext(p._jsnotice(args))
                :done()
                :tag('td')
                    :css('width', '10px')
                :done()
                :tag('td')
                    :attr('id', result)
                    :wikitext(p._resnotice(args))
                :done()
            :done()
end


function makeVTable(args, form, result)
    return mw.html.create('table')
            :addClass('wikitable')
            :tag('tr')
                :tag('th')
                    :wikitext('Calculator')
                :done()
            :done()
            :tag('tr')
                :tag('td')
                    :attr('id', form)
                    :wikitext(p._jsnotice(args))
                :done()
            :done()
            :tag('tr')
                :tag('th')
                    :wikitext('Result')
                :done()
            :done()
            :tag('tr')
                :tag('td')
                    :attr('id', result)
                    :wikitext(p._resnotice(args))
                :done()
            :done()
end


return p