]> git.proxmox.com Git - pve-xtermjs.git/blame - xterm.js/src/util.js
split upstream and our native into different source packages
[pve-xtermjs.git] / xterm.js / src / util.js
CommitLineData
dcf3d43b
DC
1function urlEncode(object) {
2 var i,value, params = [];
3
4 for (i in object) {
5 if (object.hasOwnProperty(i)) {
6 value = object[i];
7 if (value === undefined) value = '';
8 params.push(encodeURIComponent(i) + '=' + encodeURIComponent(String(value)));
9 }
10 }
11
12 return params.join('&');
13}
14
15var msgtimeout;
16var severities = {
17 normal: 1,
18 warning: 2,
19 error: 3,
20};
21
22function showMsg(message, timeout, severity) {
23 var status_bar = document.getElementById('status_bar');
24 clearTimeout(msgtimeout);
25
26 status_bar.classList.remove('normal');
27 status_bar.classList.remove('warning');
28 status_bar.classList.remove('error');
29
30 status_bar.textContent = message;
31
32 severity = severity || severities.normal;
33
34 switch (severity) {
35 case severities.normal:
36 status_bar.classList.add('normal');
37 break;
38 case severities.warning:
39 status_bar.classList.add('warning');
40 break;
41 case severities.error:
42 status_bar.classList.add('error');
43 break;
44 default:
45 throw "unknown severity";
46 }
47
48 status_bar.classList.add('open');
49
50 if (timeout !== 0) {
51 msgtimeout = setTimeout(hideMsg, timeout || 1500);
52 }
53}
54
55function hideMsg() {
56 clearTimeout(msgtimeout);
57 status_bar.classList.remove('open');
58}
59
60function getQueryParameter(name) {
61 var params = location.search.slice(1).split('&');
62 var result = "";
63 params.forEach(function(param) {
64 var components = param.split('=');
65 if (components[0] === name) {
66 result = components.slice(1).join('=');
67 }
68 });
69 return result;
70}
71
dcf3d43b
DC
72function API2Request(reqOpts) {
73 var me = this;
74
75 reqOpts.method = reqOpts.method || 'GET';
76
77 var xhr = new XMLHttpRequest();
78
79 xhr.onload = function() {
80 var scope = reqOpts.scope || this;
81 var result;
82 var errmsg;
83
84 if (xhr.readyState === 4) {
85 var ctype = xhr.getResponseHeader('Content-Type');
86 if (xhr.status === 200) {
87 if (ctype.match(/application\/json;/)) {
88 result = JSON.parse(xhr.responseText);
89 } else {
90 errmsg = 'got unexpected content type ' + ctype;
91 }
92 } else {
93 errmsg = 'Error ' + xhr.status + ': ' + xhr.statusText;
94 }
95 } else {
96 errmsg = 'Connection error - server offline?';
97 }
98
99 if (errmsg !== undefined) {
100 if (reqOpts.failure) {
101 reqOpts.failure.call(scope, errmsg);
102 }
103 } else {
104 if (reqOpts.success) {
105 reqOpts.success.call(scope, result);
106 }
107 }
108 if (reqOpts.callback) {
109 reqOpts.callback.call(scope, errmsg === undefined);
110 }
111 }
112
113 var data = urlEncode(reqOpts.params || {});
114
115 if (reqOpts.method === 'GET') {
116 xhr.open(reqOpts.method, "/api2/json" + reqOpts.url + '?' + data);
117 } else {
118 xhr.open(reqOpts.method, "/api2/json" + reqOpts.url);
119 }
120 xhr.setRequestHeader('Cache-Control', 'no-cache');
121 if (reqOpts.method === 'POST' || reqOpts.method === 'PUT') {
122 xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
123 xhr.setRequestHeader('CSRFPreventionToken', PVE.CSRFPreventionToken);
124 xhr.send(data);
125 } else if (reqOpts.method === 'GET') {
126 xhr.send();
127 } else {
128 throw "unknown method";
129 }
130}
37445374
DC
131
132function getTerminalSettings() {
133 var res = {};
134 var settings = ['fontSize', 'fontFamily', 'letterSpacing', 'lineHeight'];
135 if(localStorage) {
136 settings.forEach(function(setting) {
137 var val = localStorage.getItem('pve-xterm-' + setting);
138 if (val !== undefined && val !== null) {
139 res[setting] = val;
140 }
141 });
142 }
143 return res;
144}