Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: observatory_pub_packages/polymer/src/build/log_injector.dart

Issue 816693004: Add observatory_pub_packages snapshot to third_party (Closed) Base URL: http://dart.googlecode.com/svn/third_party/
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 /// This library provides a single function called injectLogs which when called
6 /// will request a logs json file and build a small widget out of them which
7 /// groups the logs by level.
8 library polymer.build.log_injector;
9
10 import 'dart:async';
11 import 'dart:convert';
12 import 'dart:html';
13
14 import 'package:path/path.dart' as path;
15 import 'package:source_span/source_span.dart';
16 import 'package:code_transformers/messages/messages.dart';
17
18 class LogInjector {
19 Element selectedMenu;
20 Element selectedContent;
21
22 // Gets the logs from a url and inject them into the dom.
23 Future injectLogsFromUrl(String url) =>
24 HttpRequest.getString(url).then((data) => injectLogs(data));
25
26 // Builds the html for the logs element given some logs, and injects that
27 // into the dom. Currently, we do not use Polymer just to ensure that the
28 // page works regardless of the state of the app. Ideally, we could have
29 // multiple scripts running independently so we could ensure that this would
30 // always be running.
31 injectLogs(String data) {
32 var logs = new LogEntryTable.fromJson(JSON.decode(data));
33 if (logs.entries.isEmpty) return;
34
35 // Group all logs by level.
36 var logsByLevel = {
37 };
38 logs.entries.values.forEach((list) => list.forEach((log) {
39 logsByLevel.putIfAbsent(log.level, () => []);
40 logsByLevel[log.level].add(log);
41 }));
42 if (logsByLevel.isEmpty) return;
43
44 // Build the wrapper, menu, and content divs.
45
46 var menuWrapper = new DivElement()
47 ..classes.add('menu');
48 var contentWrapper = new DivElement()
49 ..classes.add('content');
50 var wrapperDiv = new DivElement()
51 ..classes.add('build-logs')
52 ..append(menuWrapper)
53 ..append(contentWrapper);
54
55 // For each log level, add a menu item, content section, and all the logs.
56 logsByLevel.forEach((level, logs) {
57 var levelClassName = level.toLowerCase();
58
59 // Add the menu item and content item.
60 var menuItem = new Element.html(
61 '<div class="$levelClassName">'
62 '$level <span class="num">(${logs.length})</span>'
63 '</div>');
64 menuWrapper.append(menuItem);
65 var contentItem = new DivElement()
66 ..classes.add(levelClassName);
67 contentWrapper.append(contentItem);
68
69 // Set up the click handlers.
70 menuItem.onClick.listen((_) {
71 if (selectedMenu == menuItem) {
72 selectedMenu = null;
73 selectedContent = null;
74 } else {
75 if (selectedMenu != null) {
76 selectedMenu.classes.remove('active');
77 selectedContent.classes.remove('active');
78 }
79
80 selectedMenu = menuItem;
81 selectedContent = contentItem;
82 }
83
84 menuItem.classes.toggle('active');
85 contentItem.classes.toggle('active');
86 });
87
88 // Add the logs to the content item.
89 for (var log in logs) {
90 var logHtml = new StringBuffer();
91 logHtml.write('<div class="log">');
92
93 var id = log.message.id;
94 var hashTag = '${id.package}_${id.id}';
95 var message = new HtmlEscape().convert(log.message.snippet);
96 message.replaceAllMapped(_urlRegex,
97 (m) => '<a href="${m.group(0)}" target="blank">${m.group(0)}</a>');
98 logHtml.write('<div class="message $levelClassName">$message '
99 '<a target="blank" href='
100 '"/packages/polymer/src/build/generated/messages.html#$hashTag">'
101 '(more details)</a></div>');
102 var span = log.span;
103 if (span != null) {
104 logHtml.write('<div class="location">');
105 var text = new HtmlEscape().convert(span.text);
106 logHtml.write(
107 ' <span class="location">${span.start.toolString}</span></div>'
108 ' <span class="text">$text</span>''</div>');
109 logHtml.write('</div>');
110 }
111 logHtml.write('</div>');
112
113 var logElement = new Element.html(logHtml.toString(),
114 validator: new NodeValidatorBuilder.common()
115 ..allowNavigation(new _OpenUriPolicy()));
116 contentItem.append(logElement);
117 var messageElement = logElement.querySelector('.message');
118 messageElement.onClick.listen((e) {
119 if (e.target == messageElement) {
120 messageElement.classes.toggle('expanded');
121 }
122 });
123 };
124 });
125
126 document.body.append(wrapperDiv);
127 }
128
129 }
130
131 final _urlRegex = new RegExp('http://[^ ]*');
132 class _OpenUriPolicy implements UriPolicy {
133 bool allowsUri(String uri) => true;
134 }
OLDNEW
« no previous file with comments | « observatory_pub_packages/polymer/src/build/log_injector.css ('k') | observatory_pub_packages/polymer/src/build/messages.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698