Widget:CategoryPages: Difference between revisions

From Nottinghack Wiki
Jump to navigation Jump to search
Created page with " <script type="text/javascript"> (function() { console.log('it loads?'); })(); </script>"
 
Add inline categories, if they are being used
 
(27 intermediate revisions by the same user not shown)
Line 1: Line 1:
<includeonly>
  <div id="categoryPages"></div>
   <script type="text/javascript">
   <script type="text/javascript">
     (function() {
(function() {
      console.log('it loads?');
     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();
    }).then(function(data) {
        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) {
            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>
</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 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
:The name of the category.
[[Category:Widget]]
</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.