Difference between revisions of "Module:Animated"

From Minetest Wiki
Jump to navigation Jump to search
(Created page with "local p = {} local getArgs = require('Module:Arguments').getArgs function p.images(frame) local args = getArgs(frame, {inherited = true}) return p._images(args) end ...")
 
m (Replaced getArgs with makeInvokeFunc)
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
local p = {}
 
local p = {}
  
local getArgs = require('Module:Arguments').getArgs
+
local HtmlBuilder = require('Module:HtmlBuilder')
 +
local TextUtil = require('Module:TextUtil')
 +
local makeInvokeFunc = require('Module:Arguments').makeInvokeFunc
  
function p.images(frame)
+
local function createAnimatedDiv(args)
     local args = getArgs(frame, {inherited = true})
+
     local size = args.size
     return p._images(args)
+
    local background = args.background
 +
    local border = args.border
 +
    local padding = args.padding
 +
 
 +
    local div = HtmlBuilder.create('div'):addClass('animated')
 +
    if size then
 +
        div:css('width', size)
 +
        div:css('height', size)
 +
    end
 +
    if background then
 +
        div:css('background-color', background)
 +
    end
 +
    if border then
 +
        div:css('border', border)
 +
     end
 +
    if padding then
 +
        div:css('padding', padding)
 +
    end
 +
 
 +
    return div
 
end
 
end
  
function p.grid(frame)
+
local function createImageDiv()
     local args = getArgs(frame, {inherited = true})
+
     return HtmlBuilder.create('div'):addClass('image')
    return p._grid(args)
 
 
end
 
end
  
 
function p._images(args)
 
function p._images(args)
 
     local size = args.size or args[2]
 
     local size = args.size or args[2]
     local animatedSpan = p.createAnimatedSpan({
+
     local animatedDiv = createAnimatedDiv({
 
         size = size,
 
         size = size,
 
         background = args.background,
 
         background = args.background,
Line 22: Line 42:
 
     })
 
     })
  
     local input = mw.text.trim(args[1])
+
     local input = args[1]
     for name in mw.text.gsplit(input, '%s*,%s*') do
+
     for name in TextUtil.gsplit(input, '%s*,%s*') do
         local span = createImageSpan()
+
         local div = createImageDiv()
         animatedSpan:node(span)
+
         animatedDiv:node(div)
  
         name = mw.text.trim(name)
+
         name = TextUtil.trim(name)
 
         if name ~= '' then
 
         if name ~= '' then
             span:wikitext('[[File:', name)
+
             div:wikitext('[[File:', name)
 
             if size then
 
             if size then
                 span:wikitext('|', size)
+
                 div:wikitext('|', size)
 
             end
 
             end
             span:wikitext('|link=File:', name, ']]')
+
             div:wikitext('|link=File:', name, ']]')
 
         end
 
         end
 
     end
 
     end
  
     return tostring(animatedSpan)
+
     return tostring(animatedDiv)
 
end
 
end
  
 
function p._grid(args)
 
function p._grid(args)
     local animatedSpan = p.createAnimatedSpan({
+
     local animatedDiv = createAnimatedDiv({
 
         size = '32px',
 
         size = '32px',
 
         background = '#888',
 
         background = '#888',
Line 48: Line 68:
 
     })
 
     })
  
     local names = args[1] and mw.text.split(mw.text.trim(args[1]), '%s*,%s*') or {}
+
     local names = args[1] and TextUtil.split(args[1], '%s*,%s*') or {}
     local counts = args[2] and mw.text.split(mw.text.trim(args[2]), '%s*,%s*') or {}
+
     local counts = args[2] and TextUtil.split(args[2], '%s*,%s*') or {}
 
     for i, name in ipairs(names) do
 
     for i, name in ipairs(names) do
         local span = createImageSpan()
+
         local div = createImageDiv()
         animatedSpan:node(span)
+
         animatedDiv:node(div)
  
         name = mw.text.trim(name)
+
         name = TextUtil.trim(name)
 
         if name ~= '' then
 
         if name ~= '' then
             span:wikitext('[[File:', name, '.png|32px|link=', name, ']]')
+
             div:wikitext('[[File:', name, '.png|32px|link=', name, ']]')
  
 
             local count = counts[i]
 
             local count = counts[i]
 
             if count then
 
             if count then
                 span:node(
+
                 count = string.match(count, '^%s*(.-)%s*$')
                    mw.html.create('span')
+
 
                        :css('position', 'relative')
+
                div:tag('br')
                        :css('top', '-16px')
+
                div:tag('span')
                        :css('left', '25px')
+
                    :css('position', 'relative')
                        :css('font-weight', 'bold')
+
                    :css('top', '-11px')
                        :css('color', 'white')
+
                    :css('left', '25px')
                        :css('text-shadow', '1px 1px black')
+
                    :css('font-weight', 'bold')
                        :wikitext(count))
+
                    :css('color', 'white')
 +
                    :css('text-shadow', '1px 1px black')
 +
                    :wikitext(count)
 
             end
 
             end
 
         end
 
         end
 
     end
 
     end
  
     return tostring(animatedSpan)
+
     return tostring(animatedDiv)
end
 
 
 
function p.createAnimatedSpan(args)
 
    local size = args.size
 
    local background = args.background
 
    local border = args.border
 
    local padding = args.padding
 
 
 
    local span = mw.html.create('span'):addClass('animated')
 
    if size then
 
        span:css('width', size)
 
        span:css('height', size)
 
    end
 
    if background then
 
        span:css('background-color', background)
 
    end
 
    if border then
 
        span:css('border', border)
 
    end
 
    if padding then
 
        span:css('padding', padding)
 
    end
 
 
 
    return span
 
 
end
 
end
  
function p.createImageSpan()
+
p.images = makeInvokeFunc(p._images, {inherited = true})
    return mw.html.create('span'):addClass('image')
+
p.grid = makeInvokeFunc(p._grid, {inherited = true})
end
 
  
 
return p
 
return p

Revision as of 18:36, 7 May 2017

local p = {}

local HtmlBuilder = require('Module:HtmlBuilder') local TextUtil = require('Module:TextUtil') local makeInvokeFunc = require('Module:Arguments').makeInvokeFunc

local function createAnimatedDiv(args)

   local size = args.size
   local background = args.background
   local border = args.border
   local padding = args.padding
   local div = HtmlBuilder.create('div'):addClass('animated')
   if size then
       div:css('width', size)
       div:css('height', size)
   end
   if background then
       div:css('background-color', background)
   end
   if border then
       div:css('border', border)
   end
   if padding then
       div:css('padding', padding)
   end
   return div

end

local function createImageDiv()

   return HtmlBuilder.create('div'):addClass('image')

end

function p._images(args)

   local size = args.size or args[2]
   local animatedDiv = createAnimatedDiv({
       size = size,
       background = args.background,
       border = args.border,
       padding = args.padding
   })
   local input = args[1]
   for name in TextUtil.gsplit(input, '%s*,%s*') do
       local div = createImageDiv()
       animatedDiv:node(div)
       name = TextUtil.trim(name)
       if name ~=  then
           div:wikitext('[[File:', name)
           if size then
               div:wikitext('|', size)
           end
           div:wikitext('|link=File:', name, ']]')
       end
   end
   return tostring(animatedDiv)

end

function p._grid(args)

   local animatedDiv = createAnimatedDiv({
       size = '32px',
       background = '#888',
       border = '1px solid #333',
       padding = args.padded and '10px' or nil
   })
   local names = args[1] and TextUtil.split(args[1], '%s*,%s*') or {}
   local counts = args[2] and TextUtil.split(args[2], '%s*,%s*') or {}
   for i, name in ipairs(names) do
       local div = createImageDiv()
       animatedDiv:node(div)
       name = TextUtil.trim(name)
       if name ~=  then
           div:wikitext('32px')
           local count = counts[i]
           if count then
               count = string.match(count, '^%s*(.-)%s*$')
               div:tag('br')
               div:tag('span')
                   :css('position', 'relative')
                   :css('top', '-11px')
                   :css('left', '25px')
                   :css('font-weight', 'bold')
                   :css('color', 'white')
                   :css('text-shadow', '1px 1px black')
                   :wikitext(count)
           end
       end
   end
   return tostring(animatedDiv)

end

p.images = makeInvokeFunc(p._images, {inherited = true}) p.grid = makeInvokeFunc(p._grid, {inherited = true})

return p