forked from jamesgao/ipython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.js
More file actions
149 lines (140 loc) · 5.21 KB
/
manager.js
File metadata and controls
149 lines (140 loc) · 5.21 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/***********************************************************************
#-----------------------------------------------------------------------------
# Copyright (c) 2010, IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
Manages messages that arrive via the CometGetter interface
Creates new messages if required, otherwise manages the communication between
COMET and the messages
***********************************************************************/
/***********************************************************************
* Process any payload objects into html
***********************************************************************/
function procPayload(payload) {
var data = null
if (typeof(payload['format']) != "undefined") {
var format = payload['format']
if (format == "svg") {
//Remove the doctype from the top, otherwise no way to embed
data = payload['data'].split("\n").slice(4).join("\n")
} else if (format == "png") {
data = $(document.createElement("img"))
data.attr("src", "data:image/png;"+payload['data'])
}
} else if (typeof(payload["text"]) != "undefined") {
data = fixConsole(payload["text"])
}
return data
}
/***********************************************************************
* Manages the messages and their ordering
***********************************************************************/
function Manager(obj) {
this.messages = {}
this.ordering = []
this.obj = "#"+obj
this.ondeck = null
this.cursor = 0
var thisObj = this
$(document).click(function() {
thisObj.deactivate(thisObj.ondeck)
})
this.buf_out = null
}
Manager.prototype.set = function (msg, msg_id) {
msg.msg_id = msg_id
if (this.ondeck == msg) {
manager.messages[msg_id] = msg
manager.ordering.push(msg)
manager.ondeck = null
} else {
manager.messages[msg_id] = msg
}
}
Manager.prototype.get = function (msg_id) {
if (typeof(msg_id) == "undefined") {
//Handle manager.get(), to return a new message on deck
if (this.ondeck == null) {
this.ondeck = new Message(-1, this.obj)
this.cursor = this.ordering.length
}
return this.ondeck
} else if (msg_id[0] == "+" || msg_id[0] == "-") {
//Handle the manager.get("+1") case, to advance the cursor
var idx = parseInt(msg_id)
if (this.cursor + idx <= this.ordering.length &&
this.cursor + idx >= 0)
this.cursor += idx
if (this.cursor >= this.ordering.length ||
this.ordering.length == 0)
return this.get()
return this.ordering[this.cursor]
} else if (typeof(this.messages[msg_id]) == "undefined") {
//Handle the manager.get(unknown) case, for messages from other clients
if (this.ondeck != null)
this.ondeck.remove()
this.messages[msg_id] = new Message(msg_id, this.obj)
this.ordering.push(this.messages[msg_id])
if (this.ondeck != null) {
this.ondeck = null
this.get().activate()
}
}
return this.messages[msg_id]
}
Manager.prototype.deactivate = function (current) {
for (var i in this.messages)
this.messages[i].deactivate()
if (this.ondeck != null) {
this.ondeck.deactivate()
if (this.ondeck != current) {
this.ondeck.remove()
this.ondeck = null
}
}
}
Manager.prototype.process = function (json, origin, immediate) {
if (typeof(json.content.execution_count) != "undefined")
exec_count = json.content.execution_count
var id = json.parent_header.msg_id
var type = json.msg_type
var msg = this.get(id)
var output = $(document.createElement("div"))
var head = false
if (type == "execute_reply") {
if (json.content.payload.length > 0) {
for (var i in json.content.payload)
output.append(procPayload(json.content.payload[i]))
}
this.get().activate()
} else if (type == "pyin") {
if (json.content.code != "") {
msg.setInput(json.content.code, true)
kernhistory.append(json.content.code)
}
} else if (type == "stream") {
output.append(fixConsole(json.content.data))
} else if (type == "pyout") {
exec_count = json.content.execution_count
msg.num = json.content.execution_count
output.append(fixConsole(json.content.data))
head = true
} else if (type == "pyerr") {
output.append(fixConsole(json.content.traceback.join("\n")))
}
if (output.html() != "")
msg.setOutput(output, head )
}
Manager.prototype.length = function () {
return this.ordering.length
}
Manager.prototype.getOrder = function(msg) {
for (var i = 0; i < this.ordering.length; i++)
if (this.ordering[i] == msg)
return i
//If it's not found, it's probably ondeck, return the last element
return this.ordering.length
}