-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCokeFrame.js
More file actions
137 lines (127 loc) · 3.39 KB
/
Copy pathCokeFrame.js
File metadata and controls
137 lines (127 loc) · 3.39 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
import {EventEmitter} from "./JAXEvents.js";
var CokeFrame,cokeFrame,liveFrames,nextSeq;
liveFrames={};
nextSeq=1;
//***************************************************************************
//Coke Frame
//***************************************************************************
{
CokeFrame=function(){
let ifm,seq;
EventEmitter.call(this);
seq=""+(nextSeq++);
this.frame=ifm=document.createElement("iframe");
ifm.src=`${document.location.origin}//-terminal/frame.html?${seq}`;
this.frameStub=liveFrames[seq]={
cokeFrame:this,seq:seq,frame:ifm,client:null
};
this.inCmd=false;
};
cokeFrame=CokeFrame.prototype=new EventEmitter();
//-----------------------------------------------------------------------
//Bind CokeFrame to a div:
cokeFrame.createFrame=function(div){
let ifm;
ifm=this.frame;
this.frameDiv=div;
ifm.style.position="absolute";
ifm.style.left="0px";
ifm.style.top="0px";
//ifm.style.width=div.offsetWidth+"px";
//ifm.style.height=div.offsetHeight+"px";
ifm.style.width="100%";
ifm.style.height="100%";
ifm.style.border="none";
div.appendChild(ifm);
};
//-----------------------------------------------------------------------
//Exec a command line in client iframe:
cokeFrame.execCmd=function(text){
return new Promise((resolve,reject)=>{
let doneCbk,errorCbk;
doneCbk=function(data){
this.inCmd=false;
resolve(data);
this.off("ExecCommandDone",doneCbk);
this.off("ExecCommandError",errorCbk);
}
errorCbk=function(data){
this.inCmd=false;
resolve(data);
this.off("ExecCommandDone",doneCbk);
this.off("ExecCommandError",errorCbk);
}
if(this.inCmd){
reject(new Error("CokeFrame is executing another command."));
return;
}
if(!this.frameStub.client){
reject(new Error("CokeFrame is not ready yet."));
return;
}
this.inCmd=true;
this.on("ExecCommandDone",doneCbk);
this.on("ExecCommandError",errorCbk);
this.frameStub.client.postMessage({
msgCatalog:"CokeFrameH2C",
msg:"ExecCommand",
cmd:text
});
});
};
//-----------------------------------------------------------------------
//Start user input command:
cokeFrame.startInputCmd=function(){
this.inCmd=true;
this.frameStub.client.postMessage({
msgCatalog:"CokeFrameH2C",
msg:"StartInputCmd",
});
};
//-----------------------------------------------------------------------
//Handle client2Host messages:
window.addEventListener("message",async (evt)=>{
let seq,stub,frame;
//Filter messages:
if(evt.data.msgCatalog!=="CokeFrameC2H"){
return;
}
seq=evt.data.frameSeq;
stub=liveFrames[seq];
if(!stub){
return;
}
frame=stub.frame;
if(!frame){
return;
}
switch(evt.data.msg){
case "ClientReady": {
stub.client=evt.source;
stub.cokeFrame.emit("ClientReady")
break;
}
case "ExecCommandDone": {
if(stub.cokeFrame.execError) {
stub.cokeFrame.emit("ExecCommandError",evt.data.msg.error);
}else {
stub.cokeFrame.emit("ExecCommandDone",evt.data.msg.result);
}
break;
}
}
},false);
}
//---------------------------------------------------------------------------
//Make a i-frame inside [div] that runs a cokeEnv with tty:
var makeCokeFrame=function(div){
return new Promise((resolve,reject)=>{
let cfm;
cfm=new CokeFrame();
cfm.on("ClientReady",()=>{
resolve(cfm);
});
cfm.createFrame(div);
});
};
export {makeCokeFrame};