Template:APIRef

<%
// Creates a link to the interface on which this page is a method or property.
//
// Parameters:
//  $0  A specific tag to releated APIs (or the API name for backward compatibility)

function containsTag(tagList, tag) {
    if (!tagList || typeof tagList == 'undefined') return 0;
    tag = tag.toLowerCase();
    for (var i = 0, len = tagList.length; i < len; i++) {
        if (tagList[i].toLowerCase() === tag) return 1;
    }
    return 0;
}

function containsAPI(APIList, name) {
    if (!name || typeof name == 'undefined') return 0;
    name = name.toLowerCase();
    name = name.split('.')[0];
    for (var i = 0, len = APIList.length; i < len; i++) {
        if (APIList[i].toLowerCase() === name) {
            return 1;
        }
    }
    return 0;
}

var s_api_tag  = $0;
var s_api_href = '/' + env.locale + '/docs/Web/API';

var s_api_title_methods = mdn.localString({
  "en-US": "Methods",
  "fr"   : "Méthodes",
  "de"   : "Methoden"
});

var s_api_title_properties = mdn.localString({
  "en-US": "Properties",
  "fr"   : "Propriétés",
  "de"   : "Eigenschaften"
});

var s_api_title_seealso = mdn.localString({
  "en-US": "Related APIs",
  "fr"   : "API connexes",
  "de"   : "Verwandte APIs"
});

var aquo = mdn.localString({
  "en-US": "&laquo;", // "»"
 
  // For RTL locales (ar, he, ...)
  "ar"   : "&raquo;", // "«"
  "he"   : "&raquo;", // "«"
});

/* Find the API Name from the URL: the part after the last / and before the first . */
var pageURL = env.path;
    
var pageFolders = pageURL.split('/');
if (pageFolders) {
    var pageName = pageFolders[pageFolders.length-1]; // We are only interested in the last part
    var mainIF = pageName.split('.')[0];
    var APIList = [];
    APIList.push(mainIF);

    var pageList = page.subpagesExpand(s_api_href);   // Get subpages, including tags
    
    /* List properties of this API */
    var resultProps = [];
    
    /* List methods of this API */
    var resultMethods = [];

    /* List related APIs */
    var resultSeealso = [];

    for (aPage in pageList) {
        if (containsAPI(APIList, pageList[aPage].title)) {
            if (containsTag(pageList[aPage].tags, "Property")) { // We only want properties
                pageList[aPage].domxref = template("domxref", [pageList[aPage].title]);
                resultProps.push(pageList[aPage]);
            }
            
            if (containsTag(pageList[aPage].tags, "Method") &&
                        !containsTag(pageList[aPage].tags, "Constructor")) { // We only want methods
                pageList[aPage].domxref = template("domxref", [pageList[aPage].title]);
                resultMethods.push(pageList[aPage]);
            }
        }
        
        if (containsTag(pageList[aPage].tags, s_api_tag) &&
                    pageList[aPage].title.indexOf('.') === -1 &&
                    pageList[aPage].title !== mainIF) {
            pageList[aPage].domxref = template("domxref", [pageList[aPage].title]);
            resultSeealso.push(pageList[aPage]);
        }
    }
}

var badges = {
    ExperimentalBadge : '<span class="sidebar-icon">' + template("ExperimentalBadge", [1]) + '</span>',
    NonStandardBadge  : '<span class="sidebar-icon">' + template("NonStandardBadge", [1]) + '</span>',
    DeprecatedBadge   : '<span class="sidebar-icon">' + template("DeprecatedBadge", [1]) + '</span>',
    ObsoleteBadge     : '<span class="sidebar-icon">' + template("ObsoleteBadge", [1]) + '</span>',
}

function buildSublist(pages, title) {
    var result = '<li data-default-state="open"><a href="#"><strong>' + title + '</strong></a><ol>';
    
    for (aPage in pages) {
        result += '<li>';
        
        if (containsTag(pages[aPage].tags, 'Experimental')) {
            result += badges.ExperimentalBadge;
        }
        
        if (containsTag(pages[aPage].tags, 'Non-standard') ||
                    containsTag(pages[aPage].tags, 'Non Standard')) {
            result += badges.NonStandardBadge;
        }
        
        if (containsTag(pages[aPage].tags, 'Deprecated')) {
            result += badges.DeprecatedBadge;
        }
        
        if (containsTag(pages[aPage].tags, 'Obsolete')) {
            result += badges.ObsoleteBadge;
        }
        
        result += '<em>' + pages[aPage].domxref + '</em></li>';
    }
    
    result += '</ol></li>';
    
    return result;
}
    
%>
<span class="breadcrumbs themeUpdated"><%- aquo %>&nbsp;<a href="<%- s_api_href + '/' + mainIF %>"><%= mainIF %></a></span>
<section class="Quick_links" id="Quick_Links">
  <ol>
    <%
    /* Display ancestor chain */
    // TBD: the chain, we only displays the lowest one!
    %>
    <li><strong><%- template("domxref", [mainIF]) %></a></strong></li>
    <% 
    
    /* Display properties */ 
    if (resultProps.length > 0) { 
        %><%-buildSublist(resultProps, s_api_title_properties)%><%
    }
    
    /* Display methods */ 
    if (resultMethods.length > 0) {
        %><%-buildSublist(resultMethods, s_api_title_methods)%><%
    }
    
    /* Display related APIs */
    if (resultSeealso.length > 0) {
        %><%-buildSublist(resultSeealso, s_api_title_seealso)%><%
    }
    %>
  </ol>
</section>
Search for pages that use Template:APIRef to see example use cases and how many pages use this macro.

Document Tags and Contributors

Contributors to this page: Sheppy, Jeremie, fscholz, teoli
Last updated by: fscholz,