Actions

Module

BuildingBreadcrumb: Difference between revisions

From Dune Awakening DB

Created page with "local p = {} function p.render(frame) local title = mw.title.getCurrentTitle().text local rows = mw.ext.externalData.getExternalData({ source = "externaldb", from = "data_buildings", where = "page_title='" .. title:gsub("'", "''") .. "'", data = "category_1=category_1,category_2=category_2,category_3=category_3" }) if not rows or #rows == 0 then return '' end local cat1 = rows[1].category_1 or '' local cat2 = rows[1].category_2 or ''..."
 
mNo edit summary
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:


function p.render(frame)
function p.render(frame)
   local title = mw.title.getCurrentTitle().text
   local Html = mw.html
  local titleObj = mw.title.getCurrentTitle()
  local currentTitle = titleObj.text


  -- Try to get categories from the data_buildings table
   local rows = mw.ext.externalData.getExternalData({
   local rows = mw.ext.externalData.getExternalData({
     source = "externaldb",
     source = "externaldb",
     from = "data_buildings",
     from = "data_buildings",
     where = "page_title='" .. title:gsub("'", "''") .. "'",
     where = "name='" .. currentTitle:gsub("'", "''") .. "'",
     data = "category_1=category_1,category_2=category_2,category_3=category_3"
     data = "category_1=category_1,category_2=category_2,category_3=category_3"
   })
   })


   if not rows or #rows == 0 then
  -- Default values if nothing is found
     return ''
  local cat1, cat2, cat3
   if rows and #rows > 0 then
    cat1 = rows[1].category_1 or ''
    cat2 = rows[1].category_2 or ''
    cat3 = rows[1].category_3 or currentTitle
  else
    cat1 = 'Buildings'
     cat2 = 'Uncategorized'
    cat3 = currentTitle
   end
   end


   local cat1 = rows[1].category_1 or ''
   -- Create HTML breadcrumb
  local cat2 = rows[1].category_2 or ''
  local cat3 = rows[1].category_3 or ''
 
  local Html = mw.html
   local html = Html.create('div'):addClass('dune-breadcrumb-nav')
   local html = Html.create('div'):addClass('dune-breadcrumb-nav')


Line 38: Line 45:
   :done()
   :done()


   local function addLink(label, link)
  -- Add each breadcrumb level
   local function addCrumb(label)
     html:tag('span'):addClass('dune-breadcrumb-separator'):wikitext('/'):done()
     html:tag('span'):addClass('dune-breadcrumb-separator'):wikitext('/'):done()
     html:tag('a'):attr('href', link):wikitext(label):done()
     html:tag('a')
      :attr('href', 'https://dunedb.com/wiki/' .. mw.uri.encode(label, 'PATH'))
      :wikitext(label)
    :done()
   end
   end


   if cat1 ~= '' then
   if cat1 ~= '' then addCrumb(cat1) end
    addLink(cat1, 'https://dunedb.com/wiki/' .. mw.uri.encode(cat1, 'PATH'))
   if cat2 ~= '' then addCrumb(cat2) end
  end
   if cat2 ~= '' then
    addLink(cat2, 'https://dunedb.com/wiki/' .. mw.uri.encode(cat2, 'PATH'))
  end
   if cat3 ~= '' then
   if cat3 ~= '' then
     addLink(cat3, 'https://dunedb.com/wiki/' .. mw.uri.encode(cat3, 'PATH'))
     html:tag('span'):addClass('dune-breadcrumb-separator'):wikitext('/'):done()
    html:tag('span'):wikitext(cat3):done()
   end
   end



Latest revision as of 23:00, 9 April 2025

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

local p = {}

function p.render(frame)
  local Html = mw.html
  local titleObj = mw.title.getCurrentTitle()
  local currentTitle = titleObj.text

  -- Try to get categories from the data_buildings table
  local rows = mw.ext.externalData.getExternalData({
    source = "externaldb",
    from = "data_buildings",
    where = "name='" .. currentTitle:gsub("'", "''") .. "'",
    data = "category_1=category_1,category_2=category_2,category_3=category_3"
  })

  -- Default values if nothing is found
  local cat1, cat2, cat3
  if rows and #rows > 0 then
    cat1 = rows[1].category_1 or ''
    cat2 = rows[1].category_2 or ''
    cat3 = rows[1].category_3 or currentTitle
  else
    cat1 = 'Buildings'
    cat2 = 'Uncategorized'
    cat3 = currentTitle
  end

  -- Create HTML breadcrumb
  local html = Html.create('div'):addClass('dune-breadcrumb-nav')

  html:tag('a')
    :attr('id', 'duneLogoBtn')
    :addClass('dune-logo-btn')
    :tag('img')
      :attr('src', 'https://dunedb.com/images/9/99/HomeNavIcon.png')
      :attr('alt', 'DuneDB Logo')
      :addClass('dune-logo')
    :done()
  :done()

  html:tag('a')
    :attr('href', 'https://dunedb.com/Main_Page')
    :addClass('breadcrumb-home-link')
    :wikitext('<span>Home</span>')
  :done()

  -- Add each breadcrumb level
  local function addCrumb(label)
    html:tag('span'):addClass('dune-breadcrumb-separator'):wikitext('/'):done()
    html:tag('a')
      :attr('href', 'https://dunedb.com/wiki/' .. mw.uri.encode(label, 'PATH'))
      :wikitext(label)
    :done()
  end

  if cat1 ~= '' then addCrumb(cat1) end
  if cat2 ~= '' then addCrumb(cat2) end
  if cat3 ~= '' then
    html:tag('span'):addClass('dune-breadcrumb-separator'):wikitext('/'):done()
    html:tag('span'):wikitext(cat3):done()
  end

  return tostring(html)
end

return p