| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, 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 #import("dart:html", prefix:"html"); | |
| 6 #import("../../frog/lang.dart", prefix:"lang"); | |
| 7 #import("../../frog/file_system.dart", prefix:"file_system"); | |
| 8 | |
| 9 class Dartboard { | |
| 10 final List<Object> markers; | |
| 11 | |
| 12 Dartboard() : markers = new List() {} | |
| 13 | |
| 14 void clearOutput() { | |
| 15 setEditorText("warningEditor", ""); | |
| 16 setEditorText("jsEditor", ""); | |
| 17 html.document.query("#resultFrame").attributes["src"] = "about:blank"; | |
| 18 for (Object marker in markers) { | |
| 19 clearMarker(marker); | |
| 20 } | |
| 21 markers.clear(); | |
| 22 } | |
| 23 | |
| 24 void run() { | |
| 25 setEditorText("dartEditor", SampleCode.dart); | |
| 26 setEditorText("htmlEditor", SampleCode.getHtml()); | |
| 27 | |
| 28 html.document.query("#clearButton").on.click.add(void _(html.Event e) { | |
| 29 clearOutput(); | |
| 30 }); | |
| 31 | |
| 32 html.document.query("#runButton").on.click.add(void _(html.Event e) { | |
| 33 clearOutput(); | |
| 34 String warnings = ""; | |
| 35 HtmlFileSystem fs = new HtmlFileSystem(); | |
| 36 | |
| 37 | |
| 38 List<String> args = ["dummyArg1", "dummyArg2", "user.dart"]; | |
| 39 lang.options = new lang.FrogOptions("dartdir/frog", args, fs); | |
| 40 lang.options.useColors = false; | |
| 41 lang.options.warningsAsErrors = | |
| 42 html.document.query("#warningCheckbox").dynamic.checked; | |
| 43 | |
| 44 int millis1 = new Date.now().value; | |
| 45 if (lang.world === null) { | |
| 46 lang.initializeWorld(fs); | |
| 47 } else { | |
| 48 lang.world.reset(); | |
| 49 } | |
| 50 | |
| 51 lang.world.messageHandler = (String prefix, String message, lang.SourceSpa
n span) { | |
| 52 warnings += prefix + message + span.locationText + "\n"; | |
| 53 if (span.file.filename == "user.dart") { | |
| 54 int startLine = SpanHelper.startLine(span); | |
| 55 int startCol = SpanHelper.startCol(span); | |
| 56 int endLine = SpanHelper.endLine(span); | |
| 57 int endCol = SpanHelper.endCol(span); | |
| 58 String cssClass = null; | |
| 59 if (prefix.startsWith("error") || prefix.startsWith("fatal")) { | |
| 60 cssClass = "compile_error"; | |
| 61 } else if (prefix.startsWith("warning")) { | |
| 62 cssClass = "compile_warning"; | |
| 63 } | |
| 64 markers.add(markText(startLine, startCol, endLine, endCol, cssClass)); | |
| 65 } | |
| 66 }; | |
| 67 bool success = lang.world.compile(); | |
| 68 if (success) { | |
| 69 String code = lang.world.getGeneratedCode(); | |
| 70 setEditorText("jsEditor", code); | |
| 71 } | |
| 72 int millis2 = new Date.now().value; | |
| 73 warnings += "\ncompile time: ${millis2 - millis1}ms\n"; | |
| 74 setEditorText("warningEditor", warnings); | |
| 75 }); | |
| 76 } | |
| 77 | |
| 78 // TODO - remove native | |
| 79 static String markText(int startLine, int startCol, | |
| 80 int endLine, int endCol, String cssClass) native | |
| 81 'return window.markText(startLine, startCol, endLine, endCol, cssClass);'; | |
| 82 | |
| 83 // TODO - remove native | |
| 84 static void clearMarker(Object marker) native 'marker.clear();'; | |
| 85 | |
| 86 // TODO - remove native | |
| 87 static String getEditorText(String id) native | |
| 88 'return window.getEditorText(id)'; | |
| 89 | |
| 90 // TODO - remove native | |
| 91 static String setEditorText(String id, String text) native | |
| 92 'window.setEditorText(id, text);'; | |
| 93 } | |
| 94 | |
| 95 class HtmlFileSystem implements file_system.FileSystem { | |
| 96 final Object frameDocument; | |
| 97 | |
| 98 HtmlFileSystem() : frameDocument = getFrameDocument() {} | |
| 99 | |
| 100 // TODO - remove native | |
| 101 static Object getFrameDocument() native | |
| 102 'return document.getElementById("dartlibFrame").contentDocument;'; | |
| 103 | |
| 104 // TODO - remove native | |
| 105 static String getElementText(Object frame, String id) native | |
| 106 'return frame.getElementById(id).text;'; | |
| 107 | |
| 108 String readAll(String filename) { | |
| 109 filename = resolveDartColon(filename); | |
| 110 if (filename == "user.dart") { | |
| 111 return Dartboard.getEditorText("dartEditor"); | |
| 112 } | |
| 113 String id = filename.replaceAll(".", "_").replaceAll("/", "_"); | |
| 114 return getElementText(frameDocument, id); | |
| 115 } | |
| 116 | |
| 117 static String resolveDartColon(String filename) { | |
| 118 if (!filename.startsWith("dart:")) { | |
| 119 return filename; | |
| 120 } | |
| 121 String symbol = filename.substring("dart:".length); | |
| 122 String relPath = symbolToRelPath[symbol]; | |
| 123 if (relPath === null) { | |
| 124 throw new Exception("dart:$symbol not recognized"); | |
| 125 } | |
| 126 return "dartdir/" + relPath; | |
| 127 } | |
| 128 | |
| 129 void writeString(String outfile, String text) { | |
| 130 throw new UnsupportedOperationException(); | |
| 131 } | |
| 132 | |
| 133 bool fileExists(String filename) { | |
| 134 return true; | |
| 135 } | |
| 136 | |
| 137 void createDirectory(String path, [bool recursive]) { | |
| 138 throw new UnsupportedOperationException(); | |
| 139 } | |
| 140 void removeDirectory(String path, [bool recursive]) { | |
| 141 throw new UnsupportedOperationException(); | |
| 142 } | |
| 143 | |
| 144 static final Map<String, String> symbolToRelPath = const <String>{ | |
| 145 "core" : "frog/lib/corelib.dart", | |
| 146 "dom" : "lib/dom/frog/dom_frog.dart", | |
| 147 "html" : "lib/html/html_frog.dart", | |
| 148 "json" : "lib/json/json_frog.dart", | |
| 149 "uri" : "lib/uri/uri.dart", | |
| 150 "utf" : "lib/utf/utf.dart" | |
| 151 }; | |
| 152 } | |
| 153 | |
| 154 void main() { | |
| 155 // TODO - temporary, the if(false) here is so that tree-shaking doesn't | |
| 156 // delete frogPondMain. (We want to be able to call frogPondMain at | |
| 157 // the appropriate time after js setup.) | |
| 158 if (false) { | |
| 159 dartboardMain(); | |
| 160 } | |
| 161 } | |
| 162 | |
| 163 void dartboardMain() { | |
| 164 new Dartboard().run(); | |
| 165 } | |
| 166 | |
| 167 /** Slightly friendlier interface to SourceSpan */ | |
| 168 class SpanHelper { | |
| 169 static int startLine(lang.SourceSpan span) { | |
| 170 return span.file.getLine(span.start); | |
| 171 } | |
| 172 | |
| 173 static int startCol(lang.SourceSpan span) { | |
| 174 return span.file.getColumn(span.file.getLine(span.start), span.start); | |
| 175 } | |
| 176 | |
| 177 static int endLine(lang.SourceSpan span) { | |
| 178 return span.file.getLine(span.end); | |
| 179 } | |
| 180 | |
| 181 static int endCol(lang.SourceSpan span) { | |
| 182 return span.file.getColumn(span.file.getLine(span.end), span.end); | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 class SampleCode { | |
| 187 | |
| 188 static final String dart = ''' | |
| 189 #import("dart:html"); | |
| 190 void main() { | |
| 191 window.on.load.add(void handler(Event e) { | |
| 192 Element element = document.query("#status"); | |
| 193 if (element === null) { | |
| 194 throw "can't find status element"; | |
| 195 } | |
| 196 element.innerHTML = "hello, dart"; | |
| 197 element.on.click.add( | |
| 198 void handler(Event e) { | |
| 199 if (element.classes.remove("highlight")) { | |
| 200 return; | |
| 201 } | |
| 202 element.classes.add("highlight"); | |
| 203 }); | |
| 204 }); | |
| 205 } | |
| 206 '''; | |
| 207 | |
| 208 static String getHtml() { | |
| 209 return ''' | |
| 210 <html> | |
| 211 <head> | |
| 212 <style type="text/css"> | |
| 213 .highlight { | |
| 214 background: #003300; | |
| 215 } | |
| 216 </style> | |
| 217 <scrip_t type="application/dart"> | |
| 218 {{DART}} | |
| 219 </scrip_t> | |
| 220 </head> | |
| 221 <body> | |
| 222 <h2 id="status">not running</h2> | |
| 223 </body> | |
| 224 </html> | |
| 225 '''.replaceAll("scrip_t", "script"); | |
| 226 } | |
| 227 } | |
| 228 | |
| OLD | NEW |