Widget:CategoryPages: Difference between revisions

From Nottinghack Wiki
Jump to navigation Jump to search
No edit summary
Add inline categories, if they are being used
 
Line 2: Line 2:
   <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:<!--{$category|escape:'html'}-->&format=json&cmlimit=40&cmtype=page';
    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;
  console.log(pages);
         if (pages.length == 0) {
         if (pages.length == 0) {
          wrapper.appendChild(document.createTextNode("No pages found in this category"));
            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];
            const page = pages[pageIdx];
          const pageLink = document.createElement('a');
      if (category != page.sortkeyprefix) {
          pageLink.href = `/index.php?curid=${page.pageid}`;
    category = page.sortkeyprefix || 'Uncategorized';
          pageLink.appendChild(document.createTextNode(page.title));
    const label = document.createElement('i');
          wrapper.appendChild(pageLink);
    const labelOuter = document.createElement('b');
          if (pageIdx < (pages.length - 1)) {
    label.appendChild(document.createTextNode(' (' + category.replace(/\^/g, '', true) + ') '));
            wrapper.appendChild(document.createTextNode(' • '));
    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) {
    }).catch(function(err) {
         wrapper.appendChild(document.createTextNode("There was an error loading this information."));
         wrapper.appendChild(document.createTextNode("There was an error loading this information." + err));
      });
    });
    })();
})();
   </script>
   </script>
</includeonly><noinclude>
</includeonly><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.