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

Side by Side Diff: samples/pond/editors_stub.dart

Issue 10204007: Remove many things that depend on frog. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « samples/pond/editors.js ('k') | samples/pond/pond.css » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 // Stubs implementing the editors API.
6
7 #library("editor_stub");
8
9 #import("editors.dart");
10 #import("dart:html");
11
12 Map<String, Function> _listeners;
13
14 /**
15 * An [EditorFactory] stub that sends messages via window.postMessage to
16 * implement the editor factory functionality.
17 */
18 class EditorFactoryStub implements EditorFactory {
19
20 Future<Editor> newEditor(String id, String type, [Function changeListener]) {
21 if (changeListener != null) {
22 if (_listeners == null) {
23 _listeners = {};
24 }
25 _listeners[id] = changeListener;
26 }
27
28 return _callJs(["newEditor", [id, type, changeListener != null]]).
29 transform((ignoreValue) => new EditorStub(id));
30 }
31 }
32
33 /**
34 * An [Editor] stub that sends messages via window.postMessage to implement the
35 * editor factory functionality.
36 */
37 class EditorStub implements Editor {
38 /** Identifier for this editor. */
39 String _editorId;
40
41 EditorStub(this._editorId);
42
43 Future<String> getText() {
44 return _callJs(["getText", [_editorId]]);
45 }
46
47 Future setText(String value) {
48 return _callJs(["setText", [_editorId, value]]);
49 }
50
51 Future<Marker> mark(Position start, Position end, int kind) {
52 return _callJs(["mark",
53 [_editorId, start.line, start.column, end.line, end.column, kind]]).
54 transform((int markerId) => new MarkerStub(markerId));
55 }
56
57 Future refresh() {
58 return _callJs(["refresh", [_editorId]]);
59 }
60 }
61
62 /**
63 * An [Marker] stub that sends messages via window.postMessage to implement the
64 * editor factory functionality.
65 */
66 class MarkerStub implements Marker {
67 /** Identifier for this editor. */
68 String _markerId;
69
70 MarkerStub(this._markerId);
71
72 Future clear() => _callJs(["clearMark", [_markerId]]);
73 }
74
75 /** Sparse map of pending messages. */
76 Map<int, Completer> _pending = null;
77
78 /** Next available id for a pending completer/message. */
79 int _nextId = 0;
80
81 /** Whether we are already listening on window messages. */
82 bool _dispatcherAdded = false;
83
84 Future _callJs(var message) {
85 if (_pending == null) {
86 _pending = new Map<int, Completer>();
87 }
88 int id = _nextId++;
89 _pending[id] = new Completer();
90
91 // TODO(sigmund): use a map here, instead of an array. Unfortunately, dart
92 // maps can't be used yet to send them via postMessage (bug 1883)
93 window.postMessage(['dart-to-js', id, message].dynamic, '*');
94 if (!_dispatcherAdded) {
95 _dispatcherAdded = true;
96 window.on.message.add((e) => postMessageDispatcher(e.data));
97 }
98 return _pending[id].future;
99 }
100
101 void postMessageDispatcher(envelope) {
102 if (envelope[0] == 'js-to-dart') {
103 if (envelope[1] == 'update') {
104 final f = _listeners[envelope[2]];
105 if (f != null) f();
106 } else {
107 print("warning: unrecognized js-to-dart message: $envelope");
108 }
109 } else if (envelope[0] == 'js-to-dart-reply') {
110 int id = envelope[1];
111 _pending[id].complete(envelope[2]);
112 _pending.remove(id);
113 }
114 }
OLDNEW
« no previous file with comments | « samples/pond/editors.js ('k') | samples/pond/pond.css » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698