Added JS files.
[nit.git] / contrib / online_ide / www / js / functions.js
1 // This file is part of NIT ( http://www.nitlanguage.org )
2 //
3 // Copyright 2014 Johan Kayser <kayser.johan@gmail.com>
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 // Initializes the terminal, set it to send messages to PNaCl.
18 function init_terminal() {
19 terminal = $('#terminal').terminal(function(command, term) {
20 pnacl_nitModule.postMessage(command + '\n');
21 }, { greetings: false, name: 'terminal' });
22 }
23
24 // Edit current file name.
25 $('#filename').bind('click', function() {
26 $(this).attr('contentEditable', true);
27 }).blur(
28 function() {
29 $(this).attr('contentEditable', false);
30 });
31
32 // To launch the interpret process.
33 function interpret() {
34 var dictionary = {
35 operation: 'interpret',
36 args: /*"-v -v " + */document.getElementById('filename').innerText + ".nit",
37 content: editor.getValue()
38 }
39 pnacl_nitModule.postMessage(dictionary);
40 }
41
42 // To save the current editor lines in a file.
43 function save() {
44 var blob = new Blob([editor.getValue()], {type: "text/plain;charset=utf-8"});
45 saveAs(blob, document.getElementById('filename').innerText + ".nit");
46 }
47
48 // To load nit lib
49 function load_nit_lib() {
50 if (!lib_files_loaded)
51 {
52 // We get the 'nit' folder content on github.
53 $.get("https://api.github.com/repos/privat/nit/contents/?access_token=" + github_acces_token, function(data) {
54 for (var i = 0; i < data.length; i++) {
55 if (data[i].name == "lib") {
56 // We get the list of all files in the 'lib' folder.
57 $.get("https://api.github.com/repos/privat/nit/git/trees/" + data[i].sha + "?recursive=1&access_token=" + github_acces_token, function(data) {
58 for (var i = 0; i < data.tree.length; i++) {
59 if (data.tree[i].type == "blob") {
60 lib_files_number++;
61 sha_filename_map[data.tree[i].sha] = data.tree[i].path.split("/").pop();
62
63 // We get the content of each file and send it to PNaCl.
64 $.get(data.tree[i].url + "?access_token=" + github_acces_token, function(data) {
65 var dictionary = {
66 operation: 'load',
67 filename: sha_filename_map[data.sha],
68 content: Base64.decode(data.content),
69 }
70 pnacl_nitModule.postMessage(dictionary);
71 lib_files.push(dictionary);
72 });
73 }
74 }
75 });
76 }
77 }
78 });
79 }
80 else {
81 // If we already have the files stored in JS, we just send them.
82 for (var i = 0; i < lib_files.length; i++) {
83 pnacl_nitModule.postMessage(lib_files[i]);
84 }
85 }
86 }
87
88 // To code/decode base64
89 var Base64 = {
90 _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
91
92 encode: function(input) {
93 var output = "";
94 var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
95 var i = 0;
96
97 input = Base64._utf8_encode(input);
98 while (i < input.length) {
99 chr1 = input.charCodeAt(i++);
100 chr2 = input.charCodeAt(i++);
101 chr3 = input.charCodeAt(i++);
102 enc1 = chr1 >> 2;
103 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
104 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
105 enc4 = chr3 & 63;
106 if (isNaN(chr2)) {
107 enc3 = enc4 = 64;
108 } else if (isNaN(chr3)) {
109 enc4 = 64;
110 }
111 output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
112 }
113 return output;
114 },
115
116
117 decode: function(input) {
118 var output = "";
119 var chr1, chr2, chr3;
120 var enc1, enc2, enc3, enc4;
121 var i = 0;
122
123 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
124 while (i < input.length) {
125 enc1 = this._keyStr.indexOf(input.charAt(i++));
126 enc2 = this._keyStr.indexOf(input.charAt(i++));
127 enc3 = this._keyStr.indexOf(input.charAt(i++));
128 enc4 = this._keyStr.indexOf(input.charAt(i++));
129 chr1 = (enc1 << 2) | (enc2 >> 4);
130 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
131 chr3 = ((enc3 & 3) << 6) | enc4;
132 output = output + String.fromCharCode(chr1);
133 if (enc3 != 64) {
134 output = output + String.fromCharCode(chr2);
135 }
136 if (enc4 != 64) {
137 output = output + String.fromCharCode(chr3);
138 }
139 }
140 output = Base64._utf8_decode(output);
141 return output;
142 },
143
144 _utf8_encode: function(string) {
145 string = string.replace(/\r\n/g, "\n");
146 var utftext = "";
147
148 for (var n = 0; n < string.length; n++) {
149 var c = string.charCodeAt(n);
150
151 if (c < 128) {
152 utftext += String.fromCharCode(c);
153 }
154 else if ((c > 127) && (c < 2048)) {
155 utftext += String.fromCharCode((c >> 6) | 192);
156 utftext += String.fromCharCode((c & 63) | 128);
157 }
158 else {
159 utftext += String.fromCharCode((c >> 12) | 224);
160 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
161 utftext += String.fromCharCode((c & 63) | 128);
162 }
163 }
164 return utftext;
165 },
166
167 _utf8_decode: function(utftext) {
168 var string = "";
169 var i = 0;
170 var c = c1 = c2 = 0;
171
172 while (i < utftext.length) {
173 c = utftext.charCodeAt(i);
174 if (c < 128) {
175 string += String.fromCharCode(c);
176 i++;
177 }
178 else if ((c > 191) && (c < 224)) {
179 c2 = utftext.charCodeAt(i + 1);
180 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
181 i += 2;
182 }
183 else {
184 c2 = utftext.charCodeAt(i + 1);
185 c3 = utftext.charCodeAt(i + 2);
186 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
187 i += 3;
188 }
189 }
190 return string;
191 }
192 }