1<!DOCTYPE html>
2<!--
3Copyright (c) 2013 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7<link rel="import" href="/tracing/base/base.html">
8
9<script>
10'use strict';
11
12/**
13 * @fileoverview Helper code for working with Polymer.
14 */
15tr.exportTo('tr.ui.b', function() {
16
17  function getPolymerElementNamed(tagName) {
18    for (var i = 0; i < Polymer.elements.length; i++) {
19      if (Polymer.elements[i].name === tagName)
20        return Polymer.elements[i];
21    }
22  }
23
24  function getPolymerElementsThatSubclass(tagName) {
25    if (Polymer.waitingFor().length) {
26      throw new Error('There are unresolved polymer elements. ' +
27        'Wait until Polymer.whenReady');
28    }
29
30    var baseElement;
31    var elementNamesThatExtend = {};
32    Polymer.elements.forEach(function(element) {
33      if (element.name === tagName)
34        baseElement = element;
35
36      if (element.extends) {
37        if (elementNamesThatExtend[element.extends] === undefined)
38          elementNamesThatExtend[element.extends] = [];
39        elementNamesThatExtend[element.extends].push(element.name);
40      }
41    });
42
43    if (!baseElement)
44      throw new Error(tagName + ' is not a polymer element');
45
46    var allFoundSubElementNames = [baseElement.name];
47    for (var i = 0; i < allFoundSubElementNames.length; i++) {
48      var elementName = allFoundSubElementNames[i];
49      allFoundSubElementNames.push.apply(
50          allFoundSubElementNames, elementNamesThatExtend[elementName]);
51    }
52
53    // Remove the base element tag name from the list.
54    allFoundSubElementNames.shift();
55
56    return allFoundSubElementNames;
57  }
58
59  return {
60    getPolymerElementNamed: getPolymerElementNamed,
61    getPolymerElementsThatSubclass: getPolymerElementsThatSubclass
62  };
63});
64</script>
65