File: jslib.js

package info (click to toggle)
golly 3.3-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 20,176 kB
  • sloc: cpp: 72,638; ansic: 25,919; python: 7,921; sh: 4,245; objc: 3,721; java: 2,781; xml: 1,362; makefile: 530; javascript: 279; perl: 69
file content (464 lines) | stat: -rwxr-xr-x 16,834 bytes parent folder | download | duplicates (4)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// The following JavaScript routines can be called from C++ code (eg. webcalls.cpp).
// Makefile merges these routines into golly.js via the --js-library option.

var LibraryGOLLY = {

$GOLLY: {
    cancel_progress: false,     // cancel progress dialog?
    progstart: 0.0,             // time when jsBeginProgress is called
    statusbar: null             // status bar element
},

// -----------------------------------------------------------------------------

jsAlert: function(msg) {
    alert(Pointer_stringify(msg));
},

// -----------------------------------------------------------------------------

jsConfirm: function(query) {
    return confirm(Pointer_stringify(query));
},

// -----------------------------------------------------------------------------

jsSetBackgroundColor: function(id, color) {
    document.getElementById(Pointer_stringify(id)).style.backgroundColor = Pointer_stringify(color);
},

// -----------------------------------------------------------------------------

jsSetStatus: function(line1, line2, line3) {
    // check if the statusbar element lookup has already been done
    if (!GOLLY.statusbar) {
        // lookup and cache the statusbar element
        GOLLY.statusbar = document.getElementById('statusbar');
    }

    // set the statusbar
    GOLLY.statusbar.value =
        Pointer_stringify(line1) + '\n' +
        Pointer_stringify(line2) + '\n' +
        Pointer_stringify(line3);
},

// -----------------------------------------------------------------------------

jsSetMode: function(index) {
    document.getElementById('mode').selectedIndex = index;
},

// -----------------------------------------------------------------------------

jsSetState: function(state, numstates) {
    var list = document.getElementById('state');
    // may need to update number of options in list
    while (list.length < numstates) {
        // append another option
        var option = document.createElement('option');
        option.text = list.length.toString();
        list.add(option);
    };
    while (list.length > numstates) {
        // remove last option
        list.remove(list.length - 1);
    }
    list.selectedIndex = state;
},

// -----------------------------------------------------------------------------

jsSetRule: function(oldrule) {
    var newrule = prompt('Type in a new rule:', Pointer_stringify(oldrule));
    if (newrule == null) {
        return allocate(intArrayFromString('\0'), 'i8', ALLOC_STACK);
    } else {
        return allocate(intArrayFromString(newrule), 'i8', ALLOC_STACK);
    }
},

// -----------------------------------------------------------------------------

jsGetSaveName: function(currname) {
    var newname = prompt('Save current pattern in given file:', Pointer_stringify(currname));
    if (newname == null) {
        return allocate(intArrayFromString('\0'), 'i8', ALLOC_STACK);
    } else {
        return allocate(intArrayFromString(newname), 'i8', ALLOC_STACK);
    }
},

// -----------------------------------------------------------------------------

jsShowMenu: function(id, x, y) {
    var menu = document.getElementById(Pointer_stringify(id));
    var mrect = menu.getBoundingClientRect();
    // x,y coords are relative to canvas, so convert to window coords
    var crect = Module['canvas'].getBoundingClientRect();
    // note that scrolling is disabled so window.scrollX and window.scrollY are 0
    x += crect.left + 1;
    y += crect.top + 1;
    // if menu would be outside right/bottom window edge then move it
    if (x + mrect.width > window.innerWidth) x -= mrect.width + 2;
    if (y + mrect.height > window.innerHeight) y -= y + mrect.height - window.innerHeight;
    menu.style.top = y.toString() + 'px';
    menu.style.left = x.toString() + 'px';
    menu.style.visibility = 'visible';
},

// -----------------------------------------------------------------------------

jsSetClipboard: function(text) {
    document.getElementById('cliptext').value = Pointer_stringify(text);
},

// -----------------------------------------------------------------------------

jsGetClipboard: function() {
    var text = document.getElementById('cliptext').value;
    if (text == null) {
        return allocate(intArrayFromString('\0'), 'i8', ALLOC_STACK);
    } else {
        return allocate(intArrayFromString(text), 'i8', ALLOC_STACK);
    }
},

// -----------------------------------------------------------------------------

jsTextAreaIsActive: function() {
    if (document.activeElement.tagName == 'TEXTAREA') {
        return 1;
    } else {
        return 0;
    }
},

// -----------------------------------------------------------------------------

jsElementIsVisible: function(id) {
    if (document.getElementById(Pointer_stringify(id)).style.visibility == 'visible') {
        return true;
    } else {
        return false;
    }
},

// -----------------------------------------------------------------------------

jsEnableButton: function(id, enable) {
    var button = document.getElementById(Pointer_stringify(id));
    if (enable) {
        button.disabled = false;
        button.style.color = '#000';
    } else {
        button.disabled = true;
        button.style.color = '#888';
    }
},

// -----------------------------------------------------------------------------

jsEnableImgButton: function(id, enable) {
    var button = document.getElementById(Pointer_stringify(id));
    var img = document.getElementById('img' + Pointer_stringify(id));
    if (enable) {
        button.disabled = false;
        img.style.opacity = 1.0;
        // following is needed on IE???!!!
        // img.style.filter = 'alpha(opacity:100)';
    } else {
        button.disabled = true;
        img.style.opacity = 0.25;
        // following is needed on IE???!!!
        // img.style.filter = 'alpha(opacity:25)';
    }
},

// -----------------------------------------------------------------------------

jsTickMenuItem: function(id, tick) {
    var menuitem = document.getElementById(Pointer_stringify(id));
    if (tick) {
        menuitem.style.backgroundImage = 'url(images/item_tick.png)';
    } else {
        menuitem.style.backgroundImage = 'url(images/item_blank.png)';
    }
},

// -----------------------------------------------------------------------------

jsSetInputValue: function(id, num) {
    document.getElementById(Pointer_stringify(id)).value = num.toString();
},

// -----------------------------------------------------------------------------

jsGetInputValue: function(id) {
    var num = parseInt(document.getElementById(Pointer_stringify(id)).value, 10);
    if (isNaN(num)) return -1;
    return num;
},

// -----------------------------------------------------------------------------

jsSetCheckBox: function(id, flag) {
    document.getElementById(Pointer_stringify(id)).checked = flag;
},

// -----------------------------------------------------------------------------

jsGetCheckBox: function(id) {
    return document.getElementById(Pointer_stringify(id)).checked;
},

// -----------------------------------------------------------------------------

jsSetInnerHTML: function(id, text) {
    document.getElementById(Pointer_stringify(id)).innerHTML = Pointer_stringify(text);
},

// -----------------------------------------------------------------------------

jsMoveToAnchor: function(anchor) {
    window.location.hash = Pointer_stringify(anchor);
},

// -----------------------------------------------------------------------------

jsSetScrollTop: function(id, pos) {
    document.getElementById(Pointer_stringify(id)).scrollTop = pos;
},

// -----------------------------------------------------------------------------

jsGetScrollTop: function(id) {
    return document.getElementById(Pointer_stringify(id)).scrollTop;
},

// -----------------------------------------------------------------------------

jsBeep: function() {
    var beep = new Audio("beep.wav");
    beep.play();
},

// -----------------------------------------------------------------------------

jsDeleteFile: function(filepath) {
    FS.unlink(Pointer_stringify(filepath));
},

// -----------------------------------------------------------------------------

jsMoveFile: function(inpath, outpath) {
    try {
        FS.rename(Pointer_stringify(inpath), Pointer_stringify(outpath));
        return true;
    } catch (e) {
        alert('FS.rename failed!');
        return false;
    }
},

// -----------------------------------------------------------------------------

jsShowSaveDialog: function(filename, extensions) {
    document.getElementById('save_overlay').style.visibility = 'visible';
    document.getElementById('save_extensions').innerHTML =
        'Valid extensions: ' + Pointer_stringify(extensions);
    var namebox = document.getElementById('save_name');
    namebox.value = Pointer_stringify(filename);
    namebox.select();
    namebox.focus();
},

// -----------------------------------------------------------------------------

jsSaveFile: function(filenameptr) {
    var filename = Pointer_stringify(filenameptr);
    var contents, blob;
    var gzext = '.gz';
    if (filename.length >= gzext.length && filename.substr(filename.length - gzext.length) == gzext) {
        // treat .gz file as uninterpreted binary data
        contents = FS.readFile(filename, {encoding:'binary'});
        blob = new Blob([contents], {type:'application/octet-stream'});
    } else {
        // assume it's a text file (.rle or .mc)
        contents = FS.readFile(filename, {encoding:'utf8'});
        blob = new Blob([contents], {type:'text/plain'});
    }
    var alink = document.createElement('a');
    alink.download = filename;
    alink.innerHTML = 'Download File';
    if (window.webkitURL != null) {
        // Safari/Chrome allows the link to be clicked without actually adding it to the DOM
        alink.href = window.webkitURL.createObjectURL(blob);
    } else {
        // Firefox requires the link to be added to the DOM before it can be clicked
        alink.href = window.URL.createObjectURL(blob);
        alink.onclick = function(event) {
            document.body.removeChild(event.target);
        };
        alink.style.display = 'none';
        document.body.appendChild(alink);
    }
    alink.click();
},

// -----------------------------------------------------------------------------

jsStoreRule: function(rulepath) {
    // read contents of .rule file and save to local storage using rulepath as the key
    var filepath = Pointer_stringify(rulepath);
    try {
        var contents = FS.readFile(filepath, {encoding:'utf8'});
        localStorage.setItem(filepath, contents);
    } catch(e) {
        alert('Failed to store rule file:\n' + filepath);
    }
},

// -----------------------------------------------------------------------------

jsCancelProgress: function() {
    // user hit Cancel button in progress dialog
    GOLLY.cancel_progress = true;
    // best to hide the progress dialog immediately
    document.getElementById('progress_overlay').style.visibility = 'hidden';
},

// -----------------------------------------------------------------------------

jsBeginProgress: function(title) {
    // DEBUG: Module.printErr('jsBeginProgress');
    document.getElementById('progress_title').innerHTML = Pointer_stringify(title);
    document.getElementById('progress_percent').innerHTML = ' ';
    // don't show the progress dialog immediately
    // document.getElementById('progress_overlay').style.visibility = 'visible';
    GOLLY.cancel_progress = false;
    GOLLY.progstart = Date.now()/1000;
},

// -----------------------------------------------------------------------------

jsAbortProgress: function(percentage) {
    // DEBUG: Module.printErr('jsAbortProgress: ' + percentage);
    var secs = Date.now()/1000 - GOLLY.progstart;
    if (document.getElementById('progress_overlay').style.visibility == 'visible') {
        if (percentage < 0) {
            // -ve percentage is # of bytes downloaded so far (file size is unknown)
            percentage *= -1;
            document.getElementById('progress_percent').innerHTML = 'Bytes downloaded: '+percentage;
        } else {
            document.getElementById('progress_percent').innerHTML = 'Completed: '+percentage+'%';
        }
        return GOLLY.cancel_progress;
    } else {
        // note that percentage is not always an accurate estimator for how long
        // the task will take, especially when we use nextcell for cut/copy
        if ( (secs > 1.0 && percentage < 30) || secs > 2.0 ) {
            // task is probably going to take a while so show progress dialog
            document.getElementById('progress_overlay').style.visibility = 'visible';
        }
        return false;
    }
},

// -----------------------------------------------------------------------------

jsEndProgress: function() {
    // DEBUG: Module.printErr('jsEndProgress');
    // hide the progress dialog
    document.getElementById('progress_overlay').style.visibility = 'hidden';
},

// -----------------------------------------------------------------------------

jsDownloadFile: function(urlptr, filepathptr) {
    // download file from given url and store its contents in filepath
    var url = Pointer_stringify(urlptr);
    var filepath = Pointer_stringify(filepathptr);
    // DEBUG: Module.printErr('URL: '+url+' FILE: '+filepath);

    // prefix url with http://www.corsproxy.com/ so we can get file from another domain
    // (note that we assume url starts with "http://")
    url = 'http://www.corsproxy.com/' + url.substring(7);

    var xhr = new XMLHttpRequest();
    if (xhr) {
        // first send a request to get the file's size
        /* doesn't work!!! -- get error 400 (bad request) probably because corsproxy.com
           only supports GET requests
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    Module.printErr('response: '+xhr.getResponseHeader('Content-Length'));
                } else {
                    // some sort of error occurred
                    Module.printErr('status error: ' + xhr.status + ' ' + xhr.statusText);
                }
            }
        }
        xhr.open('HEAD', url, true);    // get only the header info
        xhr.send();
        */
        
        // note that we need to prefix jsBeginProgress/jsAbortProgress/jsEndProgress calls
        // with an underscore because webcalls.cpp declares them as extern C routines
        _jsBeginProgress(allocate(intArrayFromString('Downloading file...'), 'i8', ALLOC_STACK));

        xhr.onprogress = function updateProgress(evt) {
            var percent_complete = 0;
            if (evt.lengthComputable) {
                percent_complete = Math.floor((evt.loaded / evt.total) * 100);
                // DEBUG: Module.printErr('Percentage downloaded: ' + percent_complete);
            } else {
                // file size is unknown (this seems to happen for .mc/rle files)
                // so we pass -ve bytes loaded to indicate it's not a percentage
                percent_complete = -evt.loaded;
                // DEBUG: Module.printErr('Bytes downloaded: ' + evt.loaded);
            }
            if (_jsAbortProgress(percent_complete)) {
                // GOLLY.cancel_progress is true
                _jsEndProgress();
                xhr.abort();
            }
        }
        
        xhr.onreadystatechange = function() {
            // DEBUG: Module.printErr('readyState=' + xhr.readyState);
            if (xhr.readyState == 4) {
                _jsEndProgress();
                // DEBUG: Module.printErr('status=' + xhr.status);
                if (xhr.status == 200) {
                    // success, so save binary data to filepath
                    var uInt8Array = new Uint8Array(xhr.response);
                    FS.writeFile(filepath, uInt8Array, {encoding:'binary'});
                    filecreated = Module.cwrap('FileCreated', 'void', ['string']);
                    filecreated(filepath);
                } else if (!GOLLY.cancel_progress) {
                    // some sort of error occurred
                    alert('XMLHttpRequest error: ' + xhr.status);
                }
            }
        }
        
        xhr.open('GET', url, true);
        
        // setting the following responseType will treat all files as binary
        // (note that this is only allowed for async requests)
        xhr.responseType = 'arraybuffer';
        
        xhr.send(null);
    } else {
        alert('XMLHttpRequest failed!');
    }
},

// -----------------------------------------------------------------------------

}; // LibraryGOLLY

autoAddDeps(LibraryGOLLY, '$GOLLY');
mergeInto(LibraryManager.library, LibraryGOLLY);