1
0
mirror of https://github.com/lxsang/antd-web-apps synced 2024-11-20 18:28:22 +01:00
antd-web-apps/apps/assets/scripts/main.js

530 lines
14 KiB
JavaScript
Raw Normal View History

2018-09-21 01:34:44 +02:00
// Generated by CoffeeScript 1.12.7
2018-08-28 14:49:03 +02:00
(function() {
2018-09-17 19:43:24 +02:00
var APIManager, BaseObject, MarkOn, WVNC, require,
2018-08-28 14:49:03 +02:00
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
window.classes = {};
window.libraries = {};
window.myuri = "/";
window.mobilecheck = function() {
if (navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/Windows Phone/i)) {
return true;
}
return false;
};
window.makeclass = function(n, o) {
return window.classes[n] = o;
};
2018-09-17 19:43:24 +02:00
/*
window.require = (lib) ->
return new Promise (r, e) ->
return r() if window.libraries[lib]
$.getScript window.myuri + lib
.done (d) ->
window.libraries[lib] = true
r()
.fail (m, s) ->
e(m, s)
*/
require = function(lib) {
2018-08-28 14:49:03 +02:00
return new Promise(function(r, e) {
if (window.libraries[lib]) {
return r();
}
return $.getScript(window.myuri + lib).done(function(d) {
window.libraries[lib] = true;
return r();
}).fail(function(m, s) {
return e(m, s);
});
});
};
BaseObject = (function() {
function BaseObject(name) {
this.name = name;
}
BaseObject.prototype.ready = function() {
var me;
me = this;
return new Promise(function(r, e) {
return me.resolveDep().then(function() {
return r();
})["catch"](function(m, s) {
return e(m, s);
});
});
};
BaseObject.prototype.resolveDep = function() {
var me;
me = this;
return new Promise(function(r, e) {
var dep, fn;
dep = window.classes[me.name].dependencies;
if (!dep) {
r();
}
fn = function(l, i) {
if (i >= dep.length) {
return r();
}
return require(l[i]).then(function() {
return fn(l, i + 1);
})["catch"](function(m, s) {
return e(m, s);
});
};
return fn(dep, 0);
});
};
return BaseObject;
})();
makeclass("BaseObject", BaseObject);
APIManager = (function(superClass) {
extend(APIManager, superClass);
2018-09-17 19:43:24 +02:00
function APIManager(args) {
this.args = args;
2018-08-28 14:49:03 +02:00
APIManager.__super__.constructor.call(this, "APIManager");
}
2018-09-17 19:43:24 +02:00
APIManager.prototype.init = function() {
var cname, me;
me = this;
if (!(this.args && this.args.length > 0)) {
return console.error("No class found");
}
cname = (this.args.splice(0, 1))[0].trim();
2018-08-28 14:49:03 +02:00
return this.ready().then(function() {
if (mobilecheck()) {
mobileConsole.init();
}
if (!cname || cname === "") {
return;
}
if (!window.classes[cname]) {
return console.error("Cannot find class ", cname);
}
2018-09-17 19:43:24 +02:00
return (new window.classes[cname](me.args)).init();
2018-08-28 14:49:03 +02:00
})["catch"](function(m, s) {
return console.error(m, s);
});
};
return APIManager;
})(window.classes.BaseObject);
APIManager.dependencies = ["/assets/scripts/mobile_console.js"];
makeclass("APIManager", APIManager);
MarkOn = (function(superClass) {
extend(MarkOn, superClass);
2018-08-28 15:07:46 +02:00
function MarkOn() {
2018-08-28 14:49:03 +02:00
MarkOn.__super__.constructor.call(this, "MarkOn");
}
MarkOn.prototype.init = function() {
var me;
me = this;
return this.ready().then(function() {
return me.editor = new SimpleMDE({
2018-08-28 15:07:46 +02:00
element: $("#editor")[0]
2018-08-28 14:49:03 +02:00
});
})["catch"](function(m, s) {
return console.error(m, s);
});
};
return MarkOn;
})(window.classes.BaseObject);
MarkOn.dependencies = ["/rst/gscripts/mde/simplemde.min.js"];
makeclass("MarkOn", MarkOn);
2018-09-17 19:43:24 +02:00
WVNC = (function(superClass) {
extend(WVNC, superClass);
function WVNC(args) {
2018-09-20 20:02:22 +02:00
var me;
2018-09-17 19:43:24 +02:00
this.args = args;
WVNC.__super__.constructor.call(this, "WVNC");
this.socket = void 0;
this.uri = void 0;
if (this.args && this.args.length > 0) {
this.uri = this.args[0];
}
2018-09-18 19:53:00 +02:00
this.canvas = void 0;
if (this.args && this.args.length > 1) {
this.canvas = ($(this.args[1]))[0];
}
this.scale = 0.8;
2018-09-20 20:02:22 +02:00
this.decoder = new Worker('/assets/scripts/decoder.js');
me = this;
this.mouseMask = 0;
this.decoder.onmessage = function(e) {
return me.process(e.data);
};
2018-09-17 19:43:24 +02:00
}
WVNC.prototype.init = function() {
var me;
me = this;
return this.ready().then(function() {
2018-09-18 19:53:00 +02:00
$("#stop").click(function(e) {
if (me.socket) {
return me.socket.close();
}
});
2018-09-19 00:01:55 +02:00
$("#connect").click(function(e) {
2018-09-18 19:53:00 +02:00
return me.openSession();
});
2018-09-20 20:02:22 +02:00
return me.initInputEvent();
2018-09-17 19:43:24 +02:00
})["catch"](function(m, s) {
return console.error(m, s);
});
};
2018-09-20 20:02:22 +02:00
WVNC.prototype.initInputEvent = function() {
var getMousePos, hamster, me, sendMouseLocation;
me = this;
getMousePos = function(e) {
var pos, rect;
rect = me.canvas.getBoundingClientRect();
pos = {
x: Math.floor((e.clientX - rect.left) / me.scale),
y: Math.floor((e.clientY - rect.top) / me.scale)
};
return pos;
};
sendMouseLocation = function(e) {
var p;
p = getMousePos(e);
return me.sendPointEvent(p.x, p.y, me.mouseMask);
};
if (!me.canvas) {
return;
}
($(me.canvas)).css("cursor", "none");
2018-09-20 20:02:22 +02:00
($(me.canvas)).contextmenu(function(e) {
e.preventDefault();
return false;
});
($(me.canvas)).mousemove(function(e) {
return sendMouseLocation(e);
});
($(me.canvas)).mousedown(function(e) {
var state;
state = 1 << e.button;
me.mouseMask = me.mouseMask | state;
return sendMouseLocation(e);
});
($(me.canvas)).mouseup(function(e) {
var state;
state = 1 << e.button;
me.mouseMask = me.mouseMask & (~state);
return sendMouseLocation(e);
});
me.canvas.onkeydown = me.canvas.onkeyup = function(e) {
2018-09-21 01:34:44 +02:00
var code, keycode;
keycode = e.keyCode;
switch (keycode) {
case 8:
code = 0xFF08;
break;
case 9:
code = 0xff89;
break;
case 13:
code = 0xFF0D;
break;
case 27:
code = 0xFF1B;
break;
case 46:
code = 0xFFFF;
break;
case 38:
code = 0xFF52;
break;
case 40:
code = 0xFF54;
break;
case 37:
code = 0xFF51;
break;
case 39:
code = 0xFF53;
break;
case 91:
code = 0xFFE7;
break;
case 93:
code = 0xFFE8;
break;
case 16:
code = 0xFFE1;
break;
case 17:
code = 0xFFE3;
break;
case 18:
code = 0xFFE9;
break;
case 20:
code = 0xFFE5;
break;
case 113:
code = 0xFFBF;
break;
case 112:
code = 0xFFBE;
break;
case 114:
code = 0xFFC0;
break;
case 115:
code = 0xFFC1;
break;
case 116:
code = 0xFFC2;
break;
case 117:
code = 0xFFC3;
break;
case 118:
code = 0xFFC4;
break;
case 119:
code = 0xFFC5;
break;
case 120:
code = 0xFFC6;
break;
case 121:
code = 0xFFC7;
break;
case 122:
code = 0xFFC8;
break;
case 123:
code = 0xFFC9;
break;
default:
code = e.key.charCodeAt(0);
}
e.preventDefault();
if (!code) {
return;
2018-09-20 20:02:22 +02:00
}
if (e.type === "keydown") {
return me.sendKeyEvent(code, 1);
2018-09-20 20:02:22 +02:00
} else if (e.type === "keyup") {
return me.sendKeyEvent(code, 0);
2018-09-20 20:02:22 +02:00
}
};
hamster = Hamster(this.canvas);
return hamster.wheel(function(event, delta, deltaX, deltaY) {
var p;
p = getMousePos(event.originalEvent);
if (delta > 0) {
me.sendPointEvent(p.x, p.y, 8);
me.sendPointEvent(p.x, p.y, 0);
return;
}
me.sendPointEvent(p.x, p.y, 16);
return me.sendPointEvent(p.x, p.y, 0);
});
};
2018-09-18 19:53:00 +02:00
WVNC.prototype.initCanvas = function(w, h, d) {
var me;
2018-09-18 19:53:00 +02:00
me = this;
this.depth = d;
this.canvas.width = w;
this.canvas.height = h;
this.engine = {
2018-09-20 20:02:22 +02:00
w: w,
h: h,
depth: this.depth,
wasm: true
2018-09-20 20:02:22 +02:00
};
this.decoder.postMessage(this.engine);
return this.setScale(this.scale);
2018-09-18 19:53:00 +02:00
};
WVNC.prototype.process = function(msg) {
var ctx, data, imgData;
if (!this.socket) {
return;
2018-09-20 20:02:22 +02:00
}
data = new Uint8Array(msg.pixels);
ctx = this.canvas.getContext("2d", {
alpha: false
});
imgData = ctx.createImageData(msg.w, msg.h);
imgData.data.set(data);
return ctx.putImageData(imgData, msg.x, msg.y);
2018-09-20 00:03:53 +02:00
};
2018-09-20 20:02:22 +02:00
WVNC.prototype.setScale = function(n) {
this.scale = n;
this.canvas.style.transformOrigin = '0 0';
return this.canvas.style.transform = 'scale(' + n + ')';
2018-09-18 19:53:00 +02:00
};
2018-09-17 19:43:24 +02:00
WVNC.prototype.openSession = function() {
var me;
me = this;
if (this.socket) {
this.socket.close();
}
if (!this.uri) {
return;
}
this.socket = new WebSocket(this.uri);
this.socket.binaryType = "arraybuffer";
this.socket.onopen = function() {
console.log("socket opened");
return me.initConnection();
};
this.socket.onmessage = function(e) {
return me.consume(e);
};
return this.socket.onclose = function() {
me.socket = null;
return console.log("socket closed");
};
};
WVNC.prototype.initConnection = function() {
var data, vncserver;
2018-09-21 01:34:44 +02:00
vncserver = "192.168.1.20:5901";
data = new Uint8Array(vncserver.length + 3);
data[0] = 32;
2018-09-20 20:02:22 +02:00
/*
flag:
0: raw data no compress
1: jpeg no compress
2: raw data compressed by zlib
3: jpeg data compressed by zlib
*/
data[1] = 1;
data[2] = 40;
data.set((new TextEncoder()).encode(vncserver), 3);
2018-09-20 20:02:22 +02:00
return this.socket.send(this.buildCommand(0x01, data));
2018-09-17 19:43:24 +02:00
};
2018-09-19 00:01:55 +02:00
WVNC.prototype.sendPointEvent = function(x, y, mask) {
var data;
if (!this.socket) {
return;
}
data = new Uint8Array(5);
data[0] = x & 0xFF;
data[1] = x >> 8;
data[2] = y & 0xFF;
data[3] = y >> 8;
2018-09-20 20:02:22 +02:00
data[4] = mask;
2018-09-19 00:01:55 +02:00
return this.socket.send(this.buildCommand(0x05, data));
};
2018-09-20 20:02:22 +02:00
WVNC.prototype.sendKeyEvent = function(code, v) {
var data;
if (!this.socket) {
return;
}
data = new Uint8Array(3);
data[0] = code & 0xFF;
data[1] = code >> 8;
data[2] = v;
2018-09-21 01:34:44 +02:00
console.log(code, v);
2018-09-20 20:02:22 +02:00
return this.socket.send(this.buildCommand(0x06, data));
};
2018-09-17 19:43:24 +02:00
WVNC.prototype.buildCommand = function(hex, o) {
var cmd, data;
data = void 0;
switch (typeof o) {
case 'string':
data = (new TextEncoder()).encode(o);
break;
2018-09-18 19:53:00 +02:00
case 'number':
data = new Uint8Array([o]);
break;
2018-09-17 19:43:24 +02:00
default:
data = o;
}
cmd = new Uint8Array(data.length + 3);
cmd[0] = hex;
cmd[2] = data.length >> 8;
cmd[1] = data.length & 0x0F;
cmd.set(data, 3);
return cmd.buffer;
};
WVNC.prototype.consume = function(e) {
2018-09-20 20:02:22 +02:00
var arr, cmd, data, dec, depth, h, pass, user, w;
2018-09-18 01:35:13 +02:00
data = new Uint8Array(e.data);
cmd = data[0];
switch (cmd) {
case 0xFE:
data = data.subarray(1, data.length - 1);
dec = new TextDecoder("utf-8");
return console.log("Error", dec.decode(data));
2018-09-18 19:53:00 +02:00
case 0x81:
console.log("Request for password");
2018-09-21 01:34:44 +02:00
pass = "lxsan9";
2018-09-18 19:53:00 +02:00
return this.socket.send(this.buildCommand(0x02, pass));
2018-09-18 01:35:13 +02:00
case 0x82:
console.log("Request for login");
user = "mrsang";
pass = "!x$@n9";
arr = new Uint8Array(user.length + pass.length + 1);
arr.set((new TextEncoder()).encode(user), 0);
arr.set(['\0'], user.length);
arr.set((new TextEncoder()).encode(pass), user.length + 1);
return this.socket.send(this.buildCommand(0x03, arr));
case 0x83:
console.log("resize");
w = data[1] | (data[2] << 8);
h = data[3] | (data[4] << 8);
depth = data[5];
2018-09-18 19:53:00 +02:00
this.initCanvas(w, h, depth);
return this.socket.send(this.buildCommand(0x04, 1));
2018-09-18 01:35:13 +02:00
case 0x84:
2018-09-20 20:02:22 +02:00
return this.decoder.postMessage(data.buffer, [data.buffer]);
2018-09-18 01:35:13 +02:00
default:
return console.log(cmd);
}
2018-09-17 19:43:24 +02:00
};
return WVNC;
})(window.classes.BaseObject);
2018-09-20 20:02:22 +02:00
WVNC.dependencies = ["/assets/scripts/hamster.js"];
2018-09-17 19:43:24 +02:00
makeclass("WVNC", WVNC);
2018-08-28 14:49:03 +02:00
}).call(this);