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 from a url and inject them into the dom. |
| 21 Future injectLogsFromUrl([String url]) { |
| 22 if (url == null) url = '${Uri.base.path}._buildLogs'; |
| 23 return HttpRequest.getString(url).then((data) => injectLogs(data)); |
| 24 } |
| 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 // Group all logs by level. |
| 33 var logsByLevel = { |
| 34 }; |
| 35 JSON.decode(data).forEach((log) { |
| 36 logsByLevel.putIfAbsent(log['level'], () => []); |
| 37 logsByLevel[log['level']].add(log); |
| 38 }); |
| 39 if (logsByLevel.isEmpty) return; |
| 40 |
| 41 // Build the wrapper, menu, and content divs. |
| 42 |
| 43 var menuWrapper = new DivElement() |
| 44 ..classes.add('menu'); |
| 45 var contentWrapper = new DivElement() |
| 46 ..classes.add('content'); |
| 47 var wrapperDiv = new DivElement() |
| 48 ..classes.add('build-logs') |
| 49 ..append(menuWrapper) |
| 50 ..append(contentWrapper); |
| 51 |
| 52 // For each log level, add a menu item, content section, and all the logs. |
| 53 logsByLevel.forEach((level, logs) { |
| 54 var levelClassName = level.toLowerCase(); |
| 55 |
| 56 // Add the menu item and content item. |
| 57 var menuItem = new Element.html( |
| 58 '<div class="$levelClassName">' |
| 59 '$level <span class="num">(${logs.length})</span>' |
| 60 '</div>'); |
| 61 menuWrapper.append(menuItem); |
| 62 var contentItem = new DivElement() |
| 63 ..classes.add(levelClassName); |
| 64 contentWrapper.append(contentItem); |
| 65 |
| 66 // Set up the click handlers. |
| 67 menuItem.onClick.listen((_) { |
| 68 if (selectedMenu == menuItem) { |
| 69 selectedMenu = null; |
| 70 selectedContent = null; |
| 71 } else { |
| 72 if (selectedMenu != null) { |
| 73 selectedMenu.classes.remove('active'); |
| 74 selectedContent.classes.remove('active'); |
| 75 } |
| 76 |
| 77 selectedMenu = menuItem; |
| 78 selectedContent = contentItem; |
| 79 } |
| 80 |
| 81 menuItem.classes.toggle('active'); |
| 82 contentItem.classes.toggle('active'); |
| 83 }); |
| 84 |
| 85 // Add the logs to the content item. |
| 86 for (var log in logs) { |
| 87 var logHtml = new StringBuffer(); |
| 88 logHtml.write('<div class="log">'); |
| 89 logHtml.write( |
| 90 '<div class="message $levelClassName">${log['message']}</div>'); |
| 91 var assetId = log['assetId']; |
| 92 if (assetId != null) { |
| 93 logHtml.write( |
| 94 '<div class="asset">' |
| 95 ' <span class="package">${assetId['package']}</span>:' |
| 96 ' <span class="path">${assetId['path']}</span>''</div>'); |
| 97 } |
| 98 var span = log['span']; |
| 99 if (span != null) { |
| 100 logHtml.write( |
| 101 '<div class="span">' |
| 102 ' <div class="location">${span['location']}</div>' |
| 103 ' <code class="text">${span['text']}</code>''</div>'); |
| 104 } |
| 105 logHtml.write('</div>'); |
| 106 |
| 107 contentItem.append(new Element.html(logHtml.toString())); |
| 108 }; |
| 109 }); |
| 110 |
| 111 document.body.append(wrapperDiv); |
| 112 } |
| 113 |
| 114 } |
OLD | NEW |