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

Side by Side Diff: runtime/observatory/lib/src/elements/json_view.dart

Issue 2305693003: Converted Observatory json-view element (Closed)
Patch Set: Updated observatory_sources.gypi Created 4 years, 3 months 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
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library json_view_element; 5 library json_view_element;
6 6
7 import 'package:polymer/polymer.dart'; 7 import 'dart:async';
8 import 'observatory_element.dart'; 8 import 'dart:html';
9 import 'package:observatory/service.dart'; 9 import 'package:observatory/models.dart' as M;
10 import 'package:observatory/src/elements/helpers/rendering_scheduler.dart';
11 import 'package:observatory/src/elements/helpers/tag.dart';
12 import 'package:observatory/src/elements/nav/bar.dart';
13 import 'package:observatory/src/elements/nav/notify.dart';
14 import 'package:observatory/src/elements/nav/top_menu.dart';
15 import 'package:observatory/src/elements/view_footer.dart';
10 16
11 class JsonPrettyPrinter { 17 class JSONViewElement extends HtmlElement implements Renderable {
12 String prettyPrint(ServiceMap map) { 18 static const tag = const Tag<JSONViewElement>('json-view',
19 dependencies: const [
20 NavBarElement.tag,
21 NavTopMenuElement.tag,
22 NavNotifyElement.tag,
23 ViewFooterElement.tag
24 ]);
25
26 RenderingScheduler<JSONViewElement> _r;
27
28 Stream<RenderedEvent<JSONViewElement>> get onRendered => _r.onRendered;
29
30 M.NotificationRepository _notifications;
31 Map _map;
32
33
34 M.NotificationRepository get notifications => _notifications;
35 Map get map => _map;
36
37 factory JSONViewElement(Map map,
38 M.NotificationRepository notifications,
39 {RenderingQueue queue}) {
40 assert(notifications != null);
41 assert(map != null);
42 JSONViewElement e = document.createElement(tag.name);
43 e._r = new RenderingScheduler(e, queue: queue);
44 e._notifications = notifications;
45 e._map = map;
46 return e;
47 }
48
49 JSONViewElement.created() : super.created();
50
51 @override
52 attached() {
53 super.attached();
54 _r.enable();
55 }
56
57 @override
58 detached() {
59 super.detached();
60 _r.disable(notify: true);
61 children = [];
62 }
63
64 void render() {
65 children = [
66 new NavBarElement(queue: _r.queue)
67 ..children = [
68 new NavTopMenuElement(queue: _r.queue),
69 new NavNotifyElement(_notifications, queue: _r.queue)
70 ],
71 new DivElement()..classes = ['content-centered-big']
72 ..children = [
73 new HeadingElement.h2()..text = 'Object',
74 new HRElement(),
75 new PreElement()
76 ..text = JSONPretty.stringify(_map),
77 new HRElement(),
78 new ViewFooterElement(queue: _r.queue)
79 ]
80 ];
81 }
82 }
83
84
85 class JSONPretty {
86 JSONPretty._();
87
88 static String stringify(Map map) => new JSONPretty._()._stringify(map);
89
90 String _stringify(Map map) {
13 _buffer.clear(); 91 _buffer.clear();
14 _buffer.write('{\n'); 92 _buffer.write('{\n');
15 _printMap(map, 0); 93 _printMap(map, 0);
16 _buffer.write('}\n'); 94 _buffer.write('}\n');
17 return _buffer.toString(); 95 return _buffer.toString();
18 } 96 }
19 97
20 void _printMap(ObservableMap map, int depth) { 98 void _printMap(Map map, int depth) {
21 if (_seen.contains(map)) { 99 if (_seen.contains(map)) {
22 return; 100 return;
23 } 101 }
24 _seen.add(map); 102 _seen.add(map);
25 for (var k in map.keys) { 103 for (var k in map.keys) {
26 var v = map[k]; 104 var v = map[k];
27 if (v is Map) { 105 if (v is Map) {
28 _writeIndent(depth); 106 _writeIndent(depth);
29 _buffer.write('"$k": {\n'); 107 _buffer.write('"$k": {\n');
30 _printMap(v, depth + 1); 108 _printMap(v, depth + 1);
31 _writeIndent(depth); 109 _writeIndent(depth);
32 _buffer.write('}\n'); 110 _buffer.write('}\n');
33 } else if (v is List) { 111 } else if (v is List) {
34 _writeIndent(depth); 112 _writeIndent(depth);
35 _buffer.write('"$k": [\n'); 113 _buffer.write('"$k": [\n');
36 _printList(v, depth + 1); 114 _printList(v, depth + 1);
37 _writeIndent(depth); 115 _writeIndent(depth);
38 _buffer.write(']\n'); 116 _buffer.write(']\n');
39 } else { 117 } else {
40 _writeIndent(depth); 118 _writeIndent(depth);
41 _buffer.write('"$k": $v'); 119 _buffer.write('"$k": $v');
42 _buffer.write('\n'); 120 _buffer.write('\n');
43 } 121 }
44 } 122 }
45 _seen.remove(map); 123 _seen.remove(map);
46 } 124 }
47 125
48 void _printList(ObservableList list, int depth) { 126 void _printList(List list, int depth) {
49 if (_seen.contains(list)) { 127 if (_seen.contains(list)) {
50 return; 128 return;
51 } 129 }
52 _seen.add(list); 130 _seen.add(list);
53 for (var v in list) { 131 for (var v in list) {
54 if (v is Map) { 132 if (v is Map) {
55 _writeIndent(depth); 133 _writeIndent(depth);
56 _buffer.write('{\n'); 134 _buffer.write('{\n');
57 _printMap(v, depth + 1); 135 _printMap(v, depth + 1);
58 _writeIndent(depth); 136 _writeIndent(depth);
(...skipping 14 matching lines...) Expand all
73 } 151 }
74 152
75 void _writeIndent(int depth) { 153 void _writeIndent(int depth) {
76 const tab = ' '; // 2 spaces. 154 const tab = ' '; // 2 spaces.
77 _buffer.write(tab * depth); 155 _buffer.write(tab * depth);
78 } 156 }
79 157
80 final _buffer = new StringBuffer(); 158 final _buffer = new StringBuffer();
81 final _seen = new Set(); 159 final _seen = new Set();
82 } 160 }
83
84
85 @CustomTag('json-view')
86 class JsonViewElement extends ObservatoryElement {
87 @published ServiceMap map;
88 @observable String mapAsString;
89 JsonViewElement.created() : super.created();
90
91 void mapChanged(oldValue) {
92 var jpp = new JsonPrettyPrinter();
93 mapAsString = jpp.prettyPrint(map);
94 }
95 }
OLDNEW
« no previous file with comments | « runtime/observatory/lib/src/app/page.dart ('k') | runtime/observatory/lib/src/elements/json_view.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698