Basculer le menu
Changer de menu des préférences
Basculer le menu personnel
Non connecté(e)
Votre adresse IP sera visible au public si vous faites des modifications.

« Module:Infobox/Core » : différence entre les versions

De Nefald
Hiob (discussion | contributions)
Ajout catégorisation automatique (namespace 0) selon le type
Balises : Modification par mobile Modification par le web mobile
Hiob (discussion | contributions)
m map
 
(5 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
local p = {}
local p = {}
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
-- Fonction pour générer le lien de carte dynamique
local function buildMapLink(args)
    -- Vérifier les champs obligatoires
    if not args.x or args.x == '' then return nil end
    if not args.y or args.y == '' then return nil end
    if not args.z or args.z == '' then return nil end
    if not args.worldname or args.worldname == '' then return nil end
   
    -- Valeurs par défaut pour les champs optionnels
    local mapname = args.mapname or 'flat'
    local zoom = args.zoom or '4'
   
    -- Construction de l'URL
    local url = string.format(
        'https://map.nefald.fr/?worldname=%s&mapname=%s&zoom=%s&x=%s&y=%s&z=%s',
        mw.uri.encode(args.worldname, 'PATH'),
        mw.uri.encode(mapname, 'PATH'),
        zoom,
        args.x,
        args.y,
        args.z
    )
   
    -- Retourner le lien formaté
    return string.format('[%s %s, %s, %s]', url, args.x, args.y, args.z)
end


-- Fonction de validation du champ "type"
-- Fonction de validation du champ "type"
Ligne 44 : Ligne 71 :


-- Fonction pour gérer la catégorisation automatique
-- Fonction pour gérer la catégorisation automatique
local function addTypeCategories(args, config)
local function addCategories(args, config, frame)
     -- Vérifier qu'on est dans le namespace principal (0)
     -- Vérifier qu'on est dans le namespace principal (0)
     local title = mw.title.getCurrentTitle()
     local title = mw.title.getCurrentTitle()
Ligne 51 : Ligne 78 :
     end
     end
      
      
     -- Pas de configuration de type, pas de catégorie
     local categoryString = ''
    if not config.typeField or not config.typeField.allowedValues then
        return ''
    end
      
      
     local typeValue = args.type or args['type']
     -- Nouvelles catégories configurables (fonction categories)
     if not typeValue or typeValue == '' then
     if type(config.categories) == 'function' then
         return ''
         local cats = config.categories(args, config, frame)
    end
        if cats then
   
            for _, cat in ipairs(cats) do
    -- Nettoyer la valeur
                categoryString = categoryString .. '[[Catégorie:' .. cat .. ']]'
    local cleanType = mw.text.trim(mw.ustring.lower(typeValue))
            end
   
        end
    -- Récupérer la catégorie correspondante
    local typeConfig = config.typeField.allowedValues[cleanType]
    if typeConfig and typeConfig.category then
        return '[[Catégorie:' .. typeConfig.category .. ']]'
     end
     end
      
      
     return ''
     return categoryString
end
end


Ligne 126 : Ligne 146 :
     local body = mw.html.create('div')
     local body = mw.html.create('div')
         :addClass('infobox-body')
         :addClass('infobox-body')
    -- Vérifier si on a des coordonnées complètes
    local mapLink = buildMapLink(args)
    local coordinatesDisplayed = false


     for _, section in ipairs(config.sections or {}) do
     for _, section in ipairs(config.sections or {}) do
Ligne 134 : Ligne 158 :
             body:node(sectionDiv)
             body:node(sectionDiv)
         end
         end
        -- Vérifier si on doit afficher les coordonnées automatiquement
        local hasCoordinates = args.x and args.x ~= '' and args.y and args.y ~= '' and args.z and args.z ~= ''
        local coordinatesDisplayed = false


         for _, champ in ipairs(section.champs or {}) do
         for _, champ in ipairs(section.champs or {}) do
             -- Détection automatique des coordonnées
             -- Gestion spéciale pour les coordonnées
             if hasCoordinates and not coordinatesDisplayed and (champ.cle == 'x' or champ.cle == 'y' or champ.cle == 'z' or champ.cle == 'spawn' or champ.cle == 'coordonnees') then
             if mapLink and not coordinatesDisplayed and (champ.cle == 'x' or champ.cle == 'y' or champ.cle == 'z' or champ.cle == 'worldname' or champ.cle == 'coordonnees') then
                 local rowDiv = mw.html.create('div')
                 local rowDiv = mw.html.create('div')
                     :addClass('infobox-row')
                     :addClass('infobox-row')
Ligne 151 : Ligne 171 :
                 local valueDiv = mw.html.create('div')
                 local valueDiv = mw.html.create('div')
                     :addClass('infobox-value')
                     :addClass('infobox-value')
               
                     :wikitext(mapLink)
                -- Construction du lien vers la carte si le paramètre monde existe
                if args.monde and args.monde ~= '' then
                     local mapUrl = string.format('[https://map.nefald.fr/?worldname=%s&mapname=surface&zoom=4&x=%s&y=%s&z=%s %s, %s, %s]',
                        args.monde, args.x, args.y, args.z, args.x, args.y, args.z)
                    valueDiv:wikitext("''" .. mapUrl .. "''")
                else
                    -- Affichage simple sans lien
                    valueDiv:wikitext(string.format('%s, %s, %s', args.x, args.y, args.z))
                end
                  
                  
                 rowDiv:node(labelDiv):node(valueDiv)
                 rowDiv:node(labelDiv):node(valueDiv)
Ligne 166 : Ligne 177 :
                 coordinatesDisplayed = true
                 coordinatesDisplayed = true
                  
                  
             -- Traitement normal des autres champs avec formatage spécial pour le type
             -- Traitement normal des autres champs (ignorer x, y, z, worldname s'ils sont déjà affichés comme coordonnées)
             elseif not (hasCoordinates and (champ.cle == 'x' or champ.cle == 'y' or champ.cle == 'z')) and args[champ.cle] and args[champ.cle] ~= '' then
             elseif not (mapLink and (champ.cle == 'x' or champ.cle == 'y' or champ.cle == 'z' or champ.cle == 'worldname' or champ.cle == 'mapname' or champ.cle == 'zoom')) then
                local rowDiv = mw.html.create('div')
                local rawValue = args[champ.cle]
                    :addClass('infobox-row')
               
                -- Si le champ a une valeur OU une fonction process qui peut générer une valeur
                if (rawValue and rawValue ~= '') or champ.process then
                    local finalValue = rawValue
                   
                    -- Appliquer la fonction process si elle existe
                    if champ.process and type(champ.process) == 'function' then
                        finalValue = champ.process(rawValue, args, frame)
                    end
                   
                    -- N'afficher que si on a une valeur finale
                    if finalValue and finalValue ~= '' then
                        local rowDiv = mw.html.create('div')
                            :addClass('infobox-row')
 
                        local labelDiv = mw.html.create('div')
                            :addClass('infobox-label')
                            :wikitext(champ.label)
                       
                        local valueDiv = mw.html.create('div')
                            :addClass('infobox-value')
                       
                        -- Formatage spécial pour le champ "type"
                        if champ.cle == 'type' and typeValidation.displayValue then
                            valueDiv:wikitext(typeValidation.displayValue)
                        else
                            valueDiv:wikitext(finalValue)
                        end


                local labelDiv = mw.html.create('div')
                        rowDiv:node(labelDiv):node(valueDiv)
                    :addClass('infobox-label')
                        body:node(rowDiv)
                    :wikitext(champ.label)
                     end
               
                local valueDiv = mw.html.create('div')
                    :addClass('infobox-value')
               
                -- Formatage spécial pour le champ "type"
                if champ.cle == 'type' and typeValidation.displayValue then
                     valueDiv:wikitext(typeValidation.displayValue)
                else
                    valueDiv:wikitext(args[champ.cle])
                 end
                 end
                rowDiv:node(labelDiv):node(valueDiv)
                body:node(rowDiv)
             end
             end
         end
         end
Ligne 219 : Ligne 245 :
      
      
     -- Ajouter les catégories automatiques
     -- Ajouter les catégories automatiques
     local categories = addTypeCategories(args, config)
     local categories = addCategories(args, config, frame)
      
      
     return finalResult .. categories
     return finalResult .. categories
Les témoins (''cookies'') nous aident à fournir nos services. En utilisant nos services, vous acceptez notre utilisation de témoins.