| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, 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 typedef void XhrCallback(XMLHttpRequest xhr); | |
| 6 | |
| 7 class Xhr { | |
| 8 static void post(Window window, String url, String data, String contentType, | |
| 9 XhrCallback onSuccess, XhrCallback onFailure) { | |
| 10 _request(window, 'POST', url, data, contentType, onSuccess, onFailure); | |
| 11 } | |
| 12 | |
| 13 static void _request(Window window, String method, String url, String data, | |
| 14 String contentType, XhrCallback onSuccess, XhrCallback onFailure) { | |
| 15 // TODO(knorton): Catch exceptions. | |
| 16 final xhr = new XMLHttpRequest(); | |
| 17 final onReadyStateChange = () { | |
| 18 if (xhr.readyState != 4) | |
| 19 return; | |
| 20 | |
| 21 xhr.removeEventListener('readystatechange', onReadyStateChange); | |
| 22 if (xhr.status == 200) { | |
| 23 if (onSuccess != null) | |
| 24 onSuccess(xhr); | |
| 25 } else { | |
| 26 if (onFailure != null) | |
| 27 onFailure(xhr); | |
| 28 } | |
| 29 }; | |
| 30 xhr.addEventListener('readystatechange', onReadyStateChange, true); | |
| 31 xhr.open(method, url, true); | |
| 32 if (contentType == null) { | |
| 33 xhr.setRequestHeader('Content-Type', contentType); | |
| 34 } | |
| 35 // TODO(knorton): This is a problem with dart_dom bindings. A fix will arriv
e | |
| 36 // soon. | |
| 37 // xhr.send(data); | |
| 38 xhr.send(null); | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 typedef void Callback(); | |
| 43 class Poller { | |
| 44 Window window; | |
| 45 String prefix; | |
| 46 Callback onSuccess; | |
| 47 | |
| 48 Poller(Window this.window, String this.prefix, Callback this.onSuccess) { } | |
| 49 | |
| 50 void schedule() { | |
| 51 window.setTimeout(() { | |
| 52 print('Pinging...'); | |
| 53 Xhr.post(window, prefix.concat('/ping'), null, null, | |
| 54 (XMLHttpRequest xhr) { | |
| 55 if (xhr.responseText == '{"status" : "si"}') { | |
| 56 onSuccess(); | |
| 57 return; | |
| 58 } | |
| 59 schedule(); | |
| 60 }, | |
| 61 (XMLHttpRequest xhr) { | |
| 62 schedule(); | |
| 63 }); | |
| 64 }, 1000 /* ms */); | |
| 65 } | |
| 66 | |
| 67 static void wait(Window window, String prefix, Callback onSuccess) { | |
| 68 new Poller(window, prefix, onSuccess).schedule(); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 class Adminz { | |
| 73 Window window; | |
| 74 Document document; | |
| 75 | |
| 76 Element notify; | |
| 77 Element status; | |
| 78 String prefix; | |
| 79 | |
| 80 Adminz(Window this.window, Document this.document, String this.prefix) { } | |
| 81 | |
| 82 void refresh() { | |
| 83 if (notify != null) | |
| 84 return; | |
| 85 | |
| 86 // Put up refresh UI. | |
| 87 notify = new Element.tag('div'); | |
| 88 status = new Element.tag('div'); | |
| 89 | |
| 90 notify.style.cssText = 'position:absolute;left:0;right:0;bottom:0;z-index:10
000;background:#000;opacity:0.8;height:0;-webkit-transition:height 300ms ease-in
-out;'; | |
| 91 status.style.cssText = 'padding:20px 40px;color:#fff;font-family:Helvetica,A
rial;font-size:24pt;'; | |
| 92 status.text = 'server is restarting'; | |
| 93 notify.nodes.add(status); | |
| 94 document.body.nodes.add(notify); | |
| 95 | |
| 96 notify.style.setProperty('height', status.offsetHeight.toString().concat('px
')); | |
| 97 | |
| 98 print(prefix); | |
| 99 Xhr.post(window, prefix.concat('/refresh'), null, null, | |
| 100 (XMLHttpRequest xhr) { | |
| 101 print(xhr); | |
| 102 }, null); | |
| 103 | |
| 104 print('Beging polling...'); | |
| 105 Poller.wait(window, prefix, () { | |
| 106 status.text = 'done'; | |
| 107 window.location.reload(true); | |
| 108 }); | |
| 109 } | |
| 110 | |
| 111 static void attach(Window window, Document document) { | |
| 112 String prefix = _getPrefix(window); | |
| 113 if (prefix == null) | |
| 114 return; | |
| 115 | |
| 116 Adminz a = new Adminz(window, document, prefix); | |
| 117 document.on.keyDown.add((MouseEvent event) { | |
| 118 if (event.ctrlKey && event.keyCode == 88 /* x */) { | |
| 119 a.refresh(); | |
| 120 return; | |
| 121 } | |
| 122 print(event); | |
| 123 }, true); | |
| 124 } | |
| 125 | |
| 126 static String _getPrefix(Window window) { | |
| 127 Document document = window.document; | |
| 128 String name = '/Adminz.app'; | |
| 129 NodeList items = document.queryAll('script'); | |
| 130 for (int i = 0, n = items.length; i < n; ++i) { | |
| 131 ScriptElement s = items[i]; | |
| 132 String src = s.src; | |
| 133 if (src.endsWith(name)) | |
| 134 return src.substring(0, src.length - name.length); | |
| 135 } | |
| 136 window.console.warn('Adminz was unable find itself in the DOM :-('); | |
| 137 return null; | |
| 138 } | |
| 139 | |
| 140 static void main() { | |
| 141 Adminz.attach(window, window.document); | |
| 142 } | |
| 143 } | |
| OLD | NEW |