Widget:CategoryPages: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
Add inline categories, if they are being used |
||
| (7 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
<includeonly> | |||
<div id="categoryPages"></div> | <div id="categoryPages"></div> | ||
<script type="text/javascript"> | <script type="text/javascript"> | ||
(function() { | |||
const wrapper = document.getElementById('categoryPages'); | |||
wrapper.innerHTML = ''; | |||
const url = '/api.php?action=query&list=categorymembers&cmtitle=Category:<!--{$category|escape:'html'}-->&format=json&cmlimit=40&cmtype=page&cmprop=sortkeyprefix|title|ids&cmsort=sortkey'; | |||
fetch(url).then(function(response) { | |||
return response.json(); | return response.json(); | ||
}).then(function(data) { | |||
const pages = data.query.categorymembers; | const pages = data.query.categorymembers; | ||
console.log(pages); | |||
if (pages.length == 0) { | |||
wrapper.appendChild(document.createTextNode("No pages found in this category")); | |||
} | |||
let category = ''; | |||
for (const pageIdx in pages) { | for (const pageIdx in pages) { | ||
const page = pages[pageIdx]; | |||
if (category != page.sortkeyprefix) { | |||
category = page.sortkeyprefix || 'Uncategorized'; | |||
const label = document.createElement('i'); | |||
const labelOuter = document.createElement('b'); | |||
label.appendChild(document.createTextNode(' (' + category.replace(/\^/g, '', true) + ') ')); | |||
labelOuter.appendChild(label); | |||
wrapper.appendChild(labelOuter); | |||
} else { | |||
if (pageIdx > 0) { | |||
wrapper.appendChild(document.createTextNode(' • ')); | |||
} | |||
} | |||
const pageLink = document.createElement('a'); | |||
pageLink.href = `/index.php?curid=${page.pageid}`; | |||
pageLink.appendChild(document.createTextNode(page.title)); | |||
wrapper.appendChild(pageLink); | |||
} | } | ||
}).catch(function(err) { | |||
wrapper.appendChild(document.createTextNode("There was an error loading this information." + err)); | |||
}); | |||
})(); | |||
</script> | </script> | ||
<noinclude> | </includeonly><noinclude> | ||
This widget uses the MediaWiki API to request a list of pages within a category, and spits them out into a div. It takes one parameter, which is the category name. | This widget uses the MediaWiki API to request a list of pages within a category, and spits them out into a div. It takes one parameter, which is the category name. This is done on the client side, using JavaScript, which is less than ideal. It can only be used once on a page. It is currently only used by [[:Template:Location Tools]]. | ||
==Example== | |||
<nowiki>{{#widget:CategoryPages|category=Tools and Equipment in Electronics Area}}</nowiki> | |||
{{#widget:CategoryPages|category=Tools and Equipment in Electronics Area}} | |||
==Parameters== | |||
;category | ;category | ||
:The name of the category. | :The name of the category. | ||
[[Category:Widget]] | |||
</noinclude> | </noinclude> | ||
Latest revision as of 22:46, 28 January 2025
This widget uses the MediaWiki API to request a list of pages within a category, and spits them out into a div. It takes one parameter, which is the category name. This is done on the client side, using JavaScript, which is less than ideal. It can only be used once on a page. It is currently only used by Template:Location Tools.
Example
{{#widget:CategoryPages|category=Tools and Equipment in Electronics Area}}
Parameters
- category
- The name of the category.