-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.js
64 lines (52 loc) · 1.57 KB
/
runtime.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
console = {
log: function(x) {
call_python('log', x)
}
}
document = {
querySelectorAll: function(s) {
var handles = call_python("querySelectorAll", s);
return handles.map(function(handle) { return new Node(handle) })
}
}
var listeners = {}
function Node(handle) { this.handle = handle; }
Node.prototype.getAttribute = function(attr) {
return call_python("getAttribute", this.handle, attr);
}
Node.prototype.addEventListener = function(type, listener) {
if (!listeners[this.handle]) listeners[this.handle] = {}
var dict = listeners[this.handle]
if (!(type in dict)) dict[type] = []
dict[type].push(listener)
}
Node.prototype.dispatchEvent = function(event) {
var type = event.type;
var handle = this.handle
var list = (listeners[handle] && listeners[handle][type]) || []
for (var i = 0; i < list.length; i++) {
list[i].call(this, event);
}
return event.do_default
}
Object.defineProperty(Node.prototype, 'innerHTML', {
set: function(s) {
call_python('innerHTML_set', this.handle, s.toString())
}
})
function Event(type) {
this.type = type
this.do_default = true
}
Event.prototype.preventDefault = function() {
this.do_default = false
}
function XMLHttpRequest() {}
XMLHttpRequest.prototype.open = function(method, url, is_async) {
if (is_async) throw new Error('async not implemented yet')
this.method = method
this.url = url
}
XMLHttpRequest.prototype.send = function(body) {
this.responseText = call_python('XMLHttpRequest_send', this.method, this.url, body)
}