mozilla
Your Search Results

    Template:APIRef

    <%
    // API Sidebar
    //
    // Parameters:
    //  $0  A tag for the API group (searches for related APIs)
    
    // Variables and data
    var slug = env.slug;
    var locale = env.locale;
    var APIHref = '/' + locale + '/docs/Web/API';
    var output = "";
    // slug is not available in preview mode.
    if (slug) {
    var mainIF = slug.replace('Web/API/','').split('/')[0];
    mainIF = mainIF.charAt(0).toUpperCase() + mainIF.slice(1);
    var group = $0;
    var hasTag = page.hasTag;
    var escapeQuotes = mdn.escapeQuotes;
    var htmlEscape = kuma.htmlEscape;
    var rtlLocales = ['ar', 'he', 'fa'];
    
    var webAPIData = string.deserialize(template("InterfaceData"));
    if (group) {
      var webAPIGroups = string.deserialize(template("GroupData"));
    }
    
    var commonl10n = string.deserialize(template('L10n:Common'));
    
    var text = {
        'Methods': mdn.getLocalString(commonl10n, 'Methods'),
        'Properties': mdn.getLocalString(commonl10n, 'Properties'),
        'Constructor': mdn.getLocalString(commonl10n, 'Constructor'),
        'Inheritance': mdn.getLocalString(commonl10n, 'Inheritance'),
        'Implemented_by': mdn.getLocalString(commonl10n, 'Implemented_by'),
        'Related': mdn.getLocalString(commonl10n, 'Related_pages').replace('$1', group),
        'translate': mdn.getLocalString(commonl10n, '[Translate]'),
        'title': mdn.getLocalString(commonl10n, 'TranslationCTA'),
    };
    
    
    // Collect all the things
    var mainIFPages = page.subpagesExpand('/en-US/docs/Web/API/' + mainIF);
    var impl = webAPIData[0][mainIF] != undefined ? webAPIData[0][mainIF].impl : [];
    var inh = webAPIData[0][mainIF] != undefined ? webAPIData[0][mainIF].inh : '';
    var groups = [];
    if (group) {
      groups = webAPIGroups[0][group] || [];
      var self = groups.indexOf(mainIF);
      groups.splice(self, 1);
      groups.sort();
    }
        
    var pageList = mainIFPages;
    
    var properties = [];
    var methods = [];
    var ctors = [];
    var inheritedIF = [];
    var implementedBy = [];
    
    function collect(pageList) {
      for (var i in pageList) {
        var aPage = pageList[i];
        if (hasTag(aPage, "Property")) {
            properties.push(aPage);
        }
        if (hasTag(aPage, "Method")) {
            methods.push(aPage);
        }
        if (hasTag(aPage, "Constructor")) {
            ctors.push(aPage);
        }
      }
    }
    
    collect(mainIFPages);
    
    if (impl.length > 0) {
      for (var i = 0; i < impl.length; i++) {
        var implementPages = page.subpagesExpand('/en-US/docs/Web/API/' + impl[i]);
        collect(implementPages);
      }
    }
    
    function APISort(a, b) {
      var aSplit = a.title.split('.');
      var a = aSplit[aSplit.length - 1];
      var bSplit = b.title.split('.');
      var b = bSplit[bSplit.length - 1];
      return a.toLowerCase().localeCompare(b.toLowerCase());
    }
    
    properties.sort(APISort);
    methods.sort(APISort);
    ctors.sort(APISort);
    
    function getInheritance(inh) {
      if (inh.length > 0) {
        inheritedIF.push(inh);
        if (webAPIData[0].hasOwnProperty(inh)) {
            var inh = webAPIData[0][inh].inh;
            getInheritance(inh);
        }
      }
    }
    getInheritance(inh);
    
    function getImplementedBy(data) {
      for (var key in data) {
        if (data.hasOwnProperty(key) && data[key].impl.indexOf(mainIF) != -1) {
          implementedBy.push(key);
        }
      }
      implementedBy.sort();  
    }
    getImplementedBy(webAPIData[0]);
    
    // output helpers
    
    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 (var i in pages) {
        var aPage = pages[i];
        var summary = escapeQuotes(aPage.summary) || '';
        var url = aPage.url.replace('en-US', locale);
        var title = htmlEscape(aPage.title);
        
        var translated = false;
        if (locale != 'en-US') {
            aPage.translations.forEach(function(translation){
                if(translation.locale === locale) {
                    summary = escapeQuotes(translation.summary) || '';
                    url = translation.url;
                    title = htmlEscape(translation.title);
                    translated = true;
                }
            });
        }
        
        var cta = '';
         if (!translated && locale != 'en-US') {
            cta += ' <a href="'+ url +'$translate" style="opacity:0.5" title="'+ text['title'] + '">' + text['translate'] + '</a>';
        }
        
        
        result += '<li>';
        
        if (hasTag(aPage, 'Experimental')) {
            result += badges.ExperimentalBadge;
        }
    
        if (hasTag(aPage, 'Non-standard') || hasTag(aPage, 'Non Standard')) {
            result += badges.NonStandardBadge;
        }
    
        if (hasTag(aPage, 'Deprecated')) {
            result += badges.DeprecatedBadge;
        }
    
        if (hasTag(aPage, 'Obsolete')) {
            result += badges.ObsoleteBadge;
            result += '<s class="obsoleteElement">';
        }
        
        if (rtlLocales.indexOf(locale) != -1) {
            result += '<bdi>';
        }
        
        if (slug == aPage.slug) {
            result += '<em><code>' + aPage.title + '</code></em>'
        } else {
            result += '<a href="' + url + '" title="' + summary + '"><code>' + title + '</code></a>' + cta;
        }
        
        if (rtlLocales.indexOf(locale) != -1) {
            result += '</bdi>';
        }
        
        if (hasTag(aPage, 'Obsolete')) {
            result += '</s>';
        }
        
        result += '</li>';
      }
    
      result += '</ol></li>';
    
      return result;
    }
    
    function buildIFList(interfaces, title) {
      var result = '<li data-default-state="open"><a href="#"><strong>' + title + '</strong></a><ol>';
    
      for (var i = 0; i < interfaces.length; i++) {
        var url = interfaces[i].replace('()', '').replace('.', '/');
        result += '<li><a href="' + APIHref + '/' + url + '"><code>' + interfaces[i] + '</code></a></li>';
      }
    
      result += '</ol></li>';
    
      return result;
    }
    
    
    // output
    output = '<section class="Quick_links" id="Quick_Links"><ol>';
    output += '<li><strong><a href="' +  APIHref + '/' + mainIF + '"><code>' + mainIF + '</a></code></strong></li>';
    
    if (ctors.length > 0) { 
      output += buildSublist(ctors, text['Constructor']);
    }
    
    if (properties.length > 0) { 
      output += buildSublist(properties, text['Properties']);
    }
    if (methods.length > 0) { 
      output += buildSublist(methods, text['Methods']);
    }
    if (inh.length > 0) {
      output += buildIFList(inheritedIF, text['Inheritance']);
    }
    if (implementedBy.length > 0) {
      output += buildIFList(implementedBy, text['Implemented_by']);
    }
    if (groups.length > 0) {
      output += buildIFList(groups, text['Related']);
    }
    
    output += '</ol></section>';
    }
    %>
    
    <%-output%>
    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, fscholz, Jeremie, teoli, ziyunfei
    Last updated by: fscholz,