-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.js
152 lines (135 loc) · 4.74 KB
/
example.js
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
function moduleDidLoad() {
common.hideModule();
}
// Called by the common.js module.
function domContentLoaded(name, tc, config, width, height) {
navigator.webkitPersistentStorage.requestQuota(1024 * 1024,
function(bytes) {
common.updateStatus(
'Allocated ' + bytes + ' bytes of persistant storage.');
common.attachDefaultListeners();
common.createNaClModule(name, tc, config, width, height);
},
function(e) { alert('Failed to allocate space') });
}
// Called by the common.js module.
function attachListeners() {
var radioEls = document.querySelectorAll('input[type="radio"]');
for (var i = 0; i < radioEls.length; ++i) {
radioEls[i].addEventListener('click', onRadioClicked);
}
function addEventListenerToButton(parentId, func) {
document.querySelector('#' + parentId + ' button')
.addEventListener('click', func);
}
addEventListenerToButton('saveFile', saveFile);
addEventListenerToButton('loadFile', loadFile);
addEventListenerToButton('delete', deleteFileOrDirectory);
addEventListenerToButton('listDir', listDir);
addEventListenerToButton('makeDir', makeDir);
}
function onRadioClicked(e) {
var divId = this.id.slice(6); // skip "radio_"
var functionEls = document.querySelectorAll('.function');
for (var i = 0; i < functionEls.length; ++i) {
var visible = functionEls[i].id === divId;
if (functionEls[i].id === divId)
functionEls[i].removeAttribute('hidden');
else
functionEls[i].setAttribute('hidden', '');
}
}
function makeMessage(command, path) {
// Package a message using a simple protocol containing:
// command <path length> <path> <space-separated extra args>
var msg = command;
msg += ' ';
msg += path.length;
msg += ' ';
msg += path;
// Maybe add extra args
for (var i = 2; i < arguments.length; ++i) {
msg += ' ' + arguments[i];
}
return msg;
}
function saveFile() {
if (common.naclModule) {
var fileName = document.querySelector('#saveFile input').value;
var fileText = document.querySelector('#saveFile textarea').value;
common.naclModule.postMessage(makeMessage('sv', fileName, fileText));
// clear the editor.
fileText.value = '';
}
}
function loadFile() {
if (common.naclModule) {
var fileName = document.querySelector('#loadFile input').value;
// clear the editor first (in case there is an error and there is no
// output).
document.querySelector('#loadFile textarea').value = '';
common.naclModule.postMessage(makeMessage('ld', fileName));
}
}
function deleteFileOrDirectory() {
if (common.naclModule) {
var fileName = document.querySelector('#delete input').value;
common.naclModule.postMessage(makeMessage('de', fileName));
}
}
function listDir() {
if (common.naclModule) {
var dirName = document.querySelector('#listDir input').value;
common.naclModule.postMessage(makeMessage('ls', dirName));
}
}
function makeDir() {
if (common.naclModule) {
var dirName = document.querySelector('#makeDir input').value;
common.naclModule.postMessage(makeMessage('md', dirName));
}
}
// Called by the common.js module.
function handleMessage(message_event) {
var msg = message_event.data;
var parts = msg.split('|');
var command = parts[0];
var args = parts.slice(1);
if (command == 'ERR') {
common.logMessage('Error: ' + args[0]);
} else if (command == 'STAT') {
common.logMessage(args[0]);
} else if (command == 'READY') {
common.logMessage('Filesystem ready!');
} else if (command == 'DISP') {
// Find the file editor that is currently visible.
var fileEditorEl =
document.querySelector('.function:not([hidden]) > textarea');
// Rejoin args with pipe (|) -- there is only one argument, and it can
// contain the pipe character.
fileEditorEl.value = args.join('|');
} else if (command == 'LIST') {
var listDirOutputEl = document.getElementById('listDirOutput');
// NOTE: files with | in their names will be incorrectly split. Fixing this
// is left as an exercise for the reader.
// Remove all children of this element...
while (listDirOutputEl.firstChild) {
listDirOutputEl.removeChild(listDirOutputEl.firstChild);
}
if (args.length) {
// Add new <li> elements for each file.
for (var i = 0; i < args.length; ++i) {
var itemEl = document.createElement('li');
itemEl.textContent = args[i];
listDirOutputEl.appendChild(itemEl);
}
} else {
var itemEl = document.createElement('li');
itemEl.textContent = '<empty directory>';
listDirOutputEl.appendChild(itemEl);
}
}
}