1<!DOCTYPE html>
2<!--
3Copyright (c) 2014 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
8<link rel="import" href="/tracing/base/extension_registry.html">
9
10<script>
11'use strict';
12
13/**
14 * @fileoverview Provides the EventRegistry class.
15 */
16tr.exportTo('tr.model', function() {
17  // Create the type registry.
18  function EventRegistry() {
19  }
20
21  var options = new tr.b.ExtensionRegistryOptions(tr.b.BASIC_REGISTRY_MODE);
22  tr.b.decorateExtensionRegistry(EventRegistry, options);
23
24  // Enforce all options objects have the right fields.
25  EventRegistry.addEventListener('will-register', function(e) {
26    var metadata = e.typeInfo.metadata;
27
28    if (metadata.name === undefined)
29      throw new Error('Registered events must provide name metadata');
30    var i = tr.b.findFirstInArray(
31      EventRegistry.getAllRegisteredTypeInfos(),
32      function(x) { return x.metadata.name === metadata.name; });
33    if (i !== undefined)
34      throw new Error('Event type with that name already registered');
35
36    if (metadata.pluralName === undefined)
37      throw new Error('Registered events must provide pluralName metadata');
38    if (metadata.singleViewElementName === undefined) {
39      throw new Error('Registered events must provide ' +
40                      'singleViewElementName metadata');
41    }
42    if (metadata.multiViewElementName === undefined) {
43      throw new Error('Registered events must provide ' +
44                      'multiViewElementName metadata');
45    }
46  });
47
48  // Helper: lookup Events indexed by type name.
49  var eventsByTypeName = undefined;
50  EventRegistry.getEventTypeInfoByTypeName = function(typeName) {
51    if (eventsByTypeName === undefined) {
52      eventsByTypeName = {};
53      EventRegistry.getAllRegisteredTypeInfos().forEach(function(typeInfo) {
54        eventsByTypeName[typeInfo.metadata.name] = typeInfo;
55      });
56    }
57    return eventsByTypeName[typeName];
58  }
59
60  // Ensure eventsByTypeName stays current.
61  EventRegistry.addEventListener('registry-changed', function() {
62    eventsByTypeName = undefined;
63  });
64
65  function convertCamelCaseToTitleCase(name) {
66    var result = name.replace(/[A-Z]/g, ' $&');
67    result = result.charAt(0).toUpperCase() + result.slice(1);
68    return result;
69  }
70
71  EventRegistry.getUserFriendlySingularName = function(typeName) {
72    var typeInfo = EventRegistry.getEventTypeInfoByTypeName(typeName);
73    var str = typeInfo.metadata.name;
74    return convertCamelCaseToTitleCase(str);
75  };
76
77  EventRegistry.getUserFriendlyPluralName = function(typeName) {
78    var typeInfo = EventRegistry.getEventTypeInfoByTypeName(typeName);
79    var str = typeInfo.metadata.pluralName;
80    return convertCamelCaseToTitleCase(str);
81  };
82
83  return {
84    EventRegistry: EventRegistry
85  };
86});
87</script>
88