(42 tussenliggende versies door dezelfde gebruiker niet weergegeven) | |||
Regel 72: | Regel 72: | ||
FlexForm documentation: https://www.open-csp.org/DevOps:Doc/FlexForm/1.1/token | FlexForm documentation: https://www.open-csp.org/DevOps:Doc/FlexForm/1.1/token | ||
]]-- | ]]-- | ||
− | p.token= function(frame) | + | p.token = function(frame) |
-- nowiki for query? | -- nowiki for query? | ||
local query = frame.args.query or "" -- nowiki? | local query = frame.args.query or "" -- nowiki? | ||
Regel 114: | Regel 114: | ||
local checked = frame.args.checked or "" --all values checked | local checked = frame.args.checked or "" --all values checked | ||
local sep = frame.args.sep or "," | local sep = frame.args.sep or "," | ||
+ | local name = frame.args.name or "" | ||
+ | local displayprop = frame.args.displayproperty or nil | ||
local valTbl = mw.text.split( values, sep ) | local valTbl = mw.text.split( values, sep ) | ||
local checkedvalTbl = mw.text.split( checked, sep ) | local checkedvalTbl = mw.text.split( checked, sep ) | ||
Regel 120: | Regel 122: | ||
for k,v in ipairs( valTbl ) do | for k,v in ipairs( valTbl ) do | ||
− | local isChecked = | + | local v = mw.text.trim(v) |
+ | local isChecked = '' | ||
for index,checkedVal in ipairs( checkedvalTbl ) do | for index,checkedVal in ipairs( checkedvalTbl ) do | ||
+ | checkedVal = mw.text.trim(checkedVal) | ||
if v == checkedVal then | if v == checkedVal then | ||
− | + | isChecked = 'checked' | |
end | end | ||
end | end | ||
− | local id = | + | local idName = 'ff-' .. string.gsub( name, "%[%]", "" ) |
− | -- checkboxes[k] = p. | + | local idName = string.lower( idName ) |
+ | local id = idName .. "-" .. k | ||
+ | |||
+ | -- Optionally, use semantic property value as label | ||
+ | if displayprop == nil then | ||
+ | labelName = v | ||
+ | else | ||
+ | labelName = p.smwgetlabel( v, displayprop ) | ||
+ | end | ||
+ | |||
+ | checkboxes[k] = p.createCheckbox( v, isChecked, id, name, labelName ) | ||
end | end | ||
− | return | + | |
+ | local res = '<div class="ff-checkboxes">' .. table.concat( checkboxes, "" ) .. '</div>' | ||
+ | return res | ||
+ | |||
end | end | ||
− | p.createCheckbox = function( val, isChecked, id, name ) | + | --[[ |
− | + | ]]-- | |
− | + | p.createCheckbox = function( val, isChecked, id, name, labelName ) | |
− | + | frame = mw.getCurrentFrame() | |
− | |||
-- create input | -- create input | ||
− | local checkboxArgs = { type = 'checkbox', value = val, checked = isChecked, name = name } | + | local checkboxArgs = { type = 'checkbox', value = val, checked = isChecked, name = name, id = id } |
− | local checkboxInput = frame:extensionTag( '_input', labelInput, labelArgs ) | + | local checkboxInput = frame:extensionTag( '_input', "", checkboxArgs ) |
+ | |||
+ | -- create label | ||
+ | local labelArgs = {} | ||
+ | labelArgs['for'] = id -- 'for' has special meaning | ||
+ | local labelInput = frame:extensionTag( 'label', labelName, labelArgs ) | ||
+ | |||
+ | local res = '<span class="ff-labelled-input">' .. checkboxInput .. labelInput .. '</span>' | ||
+ | return res | ||
end | end | ||
Regel 178: | Regel 202: | ||
p.smwgetlabel = function( pagename, propname ) | p.smwgetlabel = function( pagename, propname ) | ||
+ | if ( propname == nil ) then | ||
+ | return pagename | ||
+ | end | ||
if not mw.smw then | if not mw.smw then | ||
return "mw.smw module not found" | return "mw.smw module not found" | ||
end | end | ||
+ | |||
local qarg = "[[" .. pagename .. "]][[Modification date::+]]|?" .. propname .. "=title|link=none" | local qarg = "[[" .. pagename .. "]][[Modification date::+]]|?" .. propname .. "=title|link=none" | ||
local qres = mw.smw.ask( qarg ) | local qres = mw.smw.ask( qarg ) | ||
− | local label = qres[1].title or | + | |
+ | local res = '' | ||
+ | if ( qres ~= nil ) then | ||
+ | label = qres[1].title or '' | ||
+ | end | ||
+ | |||
return label | return label | ||
end | end | ||
Regel 198: | Regel 231: | ||
local qarg = "[[" .. page .. "]]|?" .. prop.. "=title|link=none" | local qarg = "[[" .. page .. "]]|?" .. prop.. "=title|link=none" | ||
local qres = mw.smw.ask( qarg ) | local qres = mw.smw.ask( qarg ) | ||
− | local res = qres[1].title | + | |
+ | local res = '' | ||
+ | if ( qres ~= nil ) then | ||
+ | res = qres[1].title | ||
+ | end | ||
return res | return res | ||
+ | end | ||
+ | |||
+ | --[[ | ||
+ | work in progress. Conviennce method for select + options | ||
+ | |||
+ | {{#invoke:... | ||
+ | |array=Page1=pears;;Page23=apples // or | ||
+ | |selected=Page23 | ||
+ | }} | ||
+ | |||
+ | - use case 1: simple list of values. Name and label are the same | ||
+ | - use case 2: associative array of values and labels | ||
+ | |||
+ | better yet: support ArrayFunctions | ||
+ | |||
+ | ]]-- | ||
+ | p.selectdropdown = function( frame ) | ||
+ | local values = frame.args.values or "" --all possible values | ||
+ | return "nothing to see here" | ||
end | end | ||
Regel 229: | Regel 285: | ||
return str | return str | ||
end | end | ||
− | |||
local slotdata = mw.slots.slotTemplates( slot, page ) | local slotdata = mw.slots.slotTemplates( slot, page ) | ||
+ | -- fail silently if nothing gets returned | ||
+ | if ( slotdata[template] == nil or slotdata[template][1][param] == nil ) then | ||
+ | return "" | ||
+ | end | ||
local str = slotdata[template][1][param]["_text"] | local str = slotdata[template][1][param]["_text"] | ||
--local str = slotdata["Book"][1]["TitelNr"]["_text"] | --local str = slotdata["Book"][1]["TitelNr"]["_text"] |
Huidige versie van 20 nov 2024 om 15:41
Module:FF
invoke within form
{{#invoke:FF|input |type=text |name=myname |value=Value here |id=fff-443434 |class=form-control |label=Name |template=Data item |slot= |param= |template= }}
Please log in first.
Getdata
Kikkers en tongzoenen - Sarah Mlynowski (2007), 1ᵉ druk, Uitgeverij Van Goor Unieboek BV, Houten:
Titel/-787178247
- Works with parser function though
Titel/-787178247
- getdatatest
Titel/-787178247
--[[ FF is for FlexForm ]]-- local p = {} p.input = function(frame) res = p.inputHandler( frame, '_input' ) return res end p.textarea = function( frame ) res = p.inputHandler( frame, 'textarea' ) return res end p.label = function( frame ) res = p.inputHandler( frame, 'label' ) return res end --[[ ]]-- p.inputHandler = function( frame, tag ) local tag = tag or "_input" local content = frame.args.content or frame.args[1] or "" local content = mw.text.nowiki( content ) local frompage = frame.args.page or "" local fromslot = frame.args.slot or "main" local fromtemplate = frame.args.template or "" --template name local fromparam = frame.args.param or "" --template parameter local label = frame.args.label or "" local wrappertemplate = frame.args.wrappertemplate or "" -- Important! Assign everything from protected frame.args to new table flexargs = {} for k,v in pairs( frame.args ) do flexargs[k] = v end -- slot -- Remove anything from args that should not be sent to FF local nonffargs = { 'content', 'page', 'slot', 'template', 'param', 'label' } for name in ipairs( nonffargs ) do if ( flexargs[name] ~= nil ) then flexargs[name] = nil end end --add value from slot if ( frompage ~= "" ) then slotval = p.getdatafromslot( frompage, fromslot, fromtemplate, fromparam ) if ( flexargs["type"] == 'textarea' ) or ( tag == 'textarea' ) then content = mw.text.nowiki( slotval ) else flexargs.value = "" -- likely superfluous flexargs.value = mw.text.nowiki( slotval ) end end -- FF local forminput = frame:extensionTag( tag, content, flexargs ) -- Should be optional. Usually "Data item" if ( mw.text.trim( wrappertemplate ) == "" ) then return forminput else local res = frame:expandTemplate{ title = wrappertemplate, args = { label, forminput } } return res end end --[[ Token field with options FlexForm documentation: https://www.open-csp.org/DevOps:Doc/FlexForm/1.1/token ]]-- p.token = function(frame) -- nowiki for query? local query = frame.args.query or "" -- nowiki? local selectedvalues = frame.args.selected or "" local sep = frame.args.separator or "," local displayprop = frame.args.returntext or "" local inputname = frame.args.name or "" -- @todo returnid if you don't want to retrieve the pages as values local wrappertemplate = frame.args.wrappertemplate or "" local query = frame.args.query or "" local optionfor = string.gsub( inputname, "%[%]", "" ) local options = p.createOptions( selectedvalues, sep, displayprop, optionfor ) -- string local flexargs = {} for k, v in pairs( frame.args ) do flexargs[k] = v or "" end -- flexargs.query = mw.text.nowiki( query ) -- nowiki version -- ... -- FlexForm local forminput = frame:extensionTag( '_token', options, flexargs ) if ( mw.text.trim( wrappertemplate ) == "" ) then return forminput else local res = frame:expandTemplate{ title = wrappertemplate, args = { label, forminput } } return res end return res end --[[ Checkboxes utility values, checked, sep ]]-- p.checkboxes = function( frame ) local values = frame.args.values or "" --all possible values local checked = frame.args.checked or "" --all values checked local sep = frame.args.sep or "," local name = frame.args.name or "" local displayprop = frame.args.displayproperty or nil local valTbl = mw.text.split( values, sep ) local checkedvalTbl = mw.text.split( checked, sep ) local checkboxes = {} for k,v in ipairs( valTbl ) do local v = mw.text.trim(v) local isChecked = '' for index,checkedVal in ipairs( checkedvalTbl ) do checkedVal = mw.text.trim(checkedVal) if v == checkedVal then isChecked = 'checked' end end local idName = 'ff-' .. string.gsub( name, "%[%]", "" ) local idName = string.lower( idName ) local id = idName .. "-" .. k -- Optionally, use semantic property value as label if displayprop == nil then labelName = v else labelName = p.smwgetlabel( v, displayprop ) end checkboxes[k] = p.createCheckbox( v, isChecked, id, name, labelName ) end local res = '<div class="ff-checkboxes">' .. table.concat( checkboxes, "" ) .. '</div>' return res end --[[ ]]-- p.createCheckbox = function( val, isChecked, id, name, labelName ) frame = mw.getCurrentFrame() -- create input local checkboxArgs = { type = 'checkbox', value = val, checked = isChecked, name = name, id = id } local checkboxInput = frame:extensionTag( '_input', "", checkboxArgs ) -- create label local labelArgs = {} labelArgs['for'] = id -- 'for' has special meaning local labelInput = frame:extensionTag( 'label', labelName, labelArgs ) local res = '<span class="ff-labelled-input">' .. checkboxInput .. labelInput .. '</span>' return res end --[[ Accept a delimited string of values and return option inputs as concatenated string ]] p.createOptions = function( values, sep, displayprop, inputname ) local valTbl = mw.text.split( values, sep ) local str = '' for k, val in ipairs( valTbl ) do local label = p.smwgetlabel( val, displayprop ) or val local inputTbl = { ['type'] = 'option', ['value'] = mw.text.trim( val ), ['for'] = inputname, ['selected'] = 'selected' } frame = mw.getCurrentFrame() local input = frame:extensionTag( '_input', label, inputTbl ); str = str .. input end return str end --[[ test ]]-- p.options = function( frame ) local values = frame.args.values or "" local sep = frame.args.sep or "," local displayprop = frame.args.displayprop or "" local name = frame.args.name or "" local options = p.createOptions( values, sep, displayprop, name ) return options end p.smwgetlabel = function( pagename, propname ) if ( propname == nil ) then return pagename end if not mw.smw then return "mw.smw module not found" end local qarg = "[[" .. pagename .. "]][[Modification date::+]]|?" .. propname .. "=title|link=none" local qres = mw.smw.ask( qarg ) local res = '' if ( qres ~= nil ) then label = qres[1].title or '' end return label end --[[ testing ]]-- p.smwlabel = function( frame ) local page = frame.args.page or "" local prop = frame.args.prop or "Display title of" if not mw.smw then return "mw.smw module not found" end local qarg = "[[" .. page .. "]]|?" .. prop.. "=title|link=none" local qres = mw.smw.ask( qarg ) local res = '' if ( qres ~= nil ) then res = qres[1].title end return res end --[[ work in progress. Conviennce method for select + options {{#invoke:... |array=Page1=pears;;Page23=apples // or |selected=Page23 }} - use case 1: simple list of values. Name and label are the same - use case 2: associative array of values and labels better yet: support ArrayFunctions ]]-- p.selectdropdown = function( frame ) local values = frame.args.values or "" --all possible values return "nothing to see here" end --[[ test ]]-- p.getdata = function(frame) -- todo without pagename, get current page local page = frame.args.page or mw.title.getCurrentTitle() local slot = frame.args.slot or "main" local template = frame.args.template or "" --template name local param = frame.args.param or nil --template parameter -- getdatafromslot( page, slot, param ) local str = p.getdatafromslot( page, slot, template, param ) return str --local pagepropsparams = { slot, page, '@' .. slot } --local slotdatapf = frame:callParserFunction( '#slottemplates', pagepropsparams ) --local slotdata = frame:preprocess( slotdatapf ) end --[[ ]]-- p.getdatafromslot = function( page, slot, template, param ) local str = "Something went wrong" -- if ( template == "" ) then --todo local str = mw.slots.slotContent( slot, page ) return str end local slotdata = mw.slots.slotTemplates( slot, page ) -- fail silently if nothing gets returned if ( slotdata[template] == nil or slotdata[template][1][param] == nil ) then return "" end local str = slotdata[template][1][param]["_text"] --local str = slotdata["Book"][1]["TitelNr"]["_text"] return str end -- does not work p.getdatatest = function() local slotdata = mw.slots.slotTemplates( "ws-page-props", "Boek/-100102193" ) mw.logObject( slotdata ); local str = slotdata["Book"][1]["TitelNr"]["_text"] return str end --[[ test to show comparison callParserFunction and extensionTag ]]-- p.form1 = function(frame) -- test of frame:callParserFunction -- {{#tag:form||id=bla|action=get}} local content = 'Test' local formparams = { 'form', content, id = 'bla', action = 'get' } local str = frame:callParserFunction( '#tag', formparams ) return str end --[[ Makes more sense than using callParserFunction but "equivalent to a call to frame:callParserFunction() with function name '#tag' " ]]-- p.form2 = function(frame) -- frame:extensionTag( name, content, args ) local content = 'Test' local tag = 'form' local args = { id = 'bla', action = 'get' } local str = frame:extensionTag( tag, content, args ) return str end p.printarray = function( mytable ) str = table.concat( mytable, ", " ) return str end return p