OLD | NEW |
(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 |
| 16 class LogInjector { |
| 17 Element selectedMenu; |
| 18 Element selectedContent; |
| 19 |
| 20 // Gets the logs and builds the html for the logs element, and injects that |
| 21 // into the dom. Currently, we do not use Polymer just to ensure that the |
| 22 // page works regardless of the state of the app. Ideally, we could have |
| 23 // multiple scripts running independently so we could ensure that this would |
| 24 // always be running. |
| 25 Future injectLogs() { |
| 26 var buildLogsUrl = '${Uri.base.path}._buildLogs'; |
| 27 return HttpRequest.getString(buildLogsUrl).then((data) { |
| 28 // Group all logs by level. |
| 29 var logsByLevel = { |
| 30 }; |
| 31 JSON.decode(data).forEach((log) { |
| 32 logsByLevel.putIfAbsent(log['level'], () => []); |
| 33 logsByLevel[log['level']].add(log); |
| 34 }); |
| 35 if (logsByLevel.isEmpty) return; |
| 36 |
| 37 // Build the wrapper, menu, and content divs. |
| 38 |
| 39 var menuWrapper = new DivElement() |
| 40 ..classes.add('menu'); |
| 41 var contentWrapper = new DivElement() |
| 42 ..classes.add('content'); |
| 43 var wrapperDiv = new DivElement() |
| 44 ..classes.add('build-logs') |
| 45 ..append(menuWrapper) |
| 46 ..append(contentWrapper); |
| 47 |
| 48 // For each log level, add a menu item, content section, and all the logs. |
| 49 logsByLevel.forEach((level, logs) { |
| 50 var levelClassName = level.toLowerCase(); |
| 51 |
| 52 // Add the menu item and content item. |
| 53 var menuItem = new Element.html( |
| 54 '<div class="$levelClassName">' |
| 55 '$level <span class="num">(${logs.length})</span>' |
| 56 '</div>'); |
| 57 menuWrapper.append(menuItem); |
| 58 var contentItem = new DivElement() |
| 59 ..classes.add(levelClassName); |
| 60 contentWrapper.append(contentItem); |
| 61 |
| 62 // Set up the click handlers. |
| 63 menuItem.onClick.listen((_) { |
| 64 if (selectedMenu == menuItem) { |
| 65 selectedMenu = null; |
| 66 selectedContent = null; |
| 67 } else { |
| 68 if (selectedMenu != null) { |
| 69 selectedMenu.classes.remove('active'); |
| 70 selectedContent.classes.remove('active'); |
| 71 } |
| 72 |
| 73 selectedMenu = menuItem; |
| 74 selectedContent = contentItem; |
| 75 } |
| 76 |
| 77 menuItem.classes.toggle('active'); |
| 78 contentItem.classes.toggle('active'); |
| 79 }); |
| 80 |
| 81 // Add the logs to the content item. |
| 82 for (var log in logs) { |
| 83 var logHtml = new StringBuffer(); |
| 84 logHtml.write('<div class="log">'); |
| 85 logHtml.write( |
| 86 '<div class="message $levelClassName">${log['message']}</div>'); |
| 87 var assetId = log['assetId']; |
| 88 if (assetId != null) { |
| 89 logHtml.write( |
| 90 '<div class="asset">' |
| 91 ' <span class="package">${assetId['package']}</span>:' |
| 92 ' <span class="path">${assetId['path']}</span>''</div>'); |
| 93 } |
| 94 var span = log['span']; |
| 95 if (span != null) { |
| 96 logHtml.write( |
| 97 '<div class="span">' |
| 98 ' <div class="location">${span['location']}</div>' |
| 99 ' <code class="text">${span['text']}</code>''</div>'); |
| 100 } |
| 101 logHtml.write('</div>'); |
| 102 |
| 103 contentItem.append(new Element.html(logHtml.toString())); |
| 104 }; |
| 105 }); |
| 106 |
| 107 document.body.append(wrapperDiv); |
| 108 }); |
| 109 } |
| 110 |
| 111 } |
OLD | NEW |