Widget:CategoryPages: Difference between revisions

From Nottinghack Wiki
Jump to navigation Jump to search
mNo edit summary
Add inline categories, if they are being used
 
(20 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() {
(function() {
      const url = '/api.php?action=query&list=categorymembers&cmtitle=Category:Tools and Equipment in Electronics Area&format=json';
    const wrapper = document.getElementById('categoryPages');
      fetch(url).then(function(response) {
    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) {
    }).then(function(data) {
        const wrapper = document.getElementById('categoryPages');
         const pages = data.query.categorymembers;
         const pages = data.query.categorymembers;
        for (let page in pages) { console.log(page);
  console.log(pages);
          const pageSpan = document.createElement('span');
        if (pages.length == 0) {
          pageSpan.innerHTML = `<a href="/index.php?curid=${page.pageid}">${page.title}</a>`;
            wrapper.appendChild(document.createTextNode("No pages found in this category"));
          wrapper.appendChild(pageSpan);
         }
         }
       }).catch(function(err) {
 
         console.log('Fetch Error :-S', err);
  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.