1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 function moduleDidLoad() {
9 // Called by the common.js module.
10 function domContentLoaded(name, tc, config, width, height) {
11 navigator.webkitPersistentStorage.requestQuota(1024 * 1024,
14 'Allocated ' + bytes + ' bytes of persistant storage.');
15 common.attachDefaultListeners();
16 common.createNaClModule(name, tc, config, width, height);
18 function(e) { alert('Failed to allocate space') });
21 // Called by the common.js module.
22 function attachListeners() {
23 var radioEls = document.querySelectorAll('input[type="radio"]');
24 for (var i = 0; i < radioEls.length; ++i) {
25 radioEls[i].addEventListener('click', onRadioClicked);
28 function addEventListenerToButton(parentId, func) {
29 document.querySelector('#' + parentId + ' button')
30 .addEventListener('click', func);
33 addEventListenerToButton('saveFile', saveFile);
34 addEventListenerToButton('loadFile', loadFile);
35 addEventListenerToButton('delete', deleteFileOrDirectory);
36 addEventListenerToButton('listDir', listDir);
37 addEventListenerToButton('makeDir', makeDir);
40 function onRadioClicked(e) {
41 var divId = this.id.slice(6); // skip "radio_"
42 var functionEls = document.querySelectorAll('.function');
43 for (var i = 0; i < functionEls.length; ++i) {
44 var visible = functionEls[i].id === divId;
45 if (functionEls[i].id === divId)
46 functionEls[i].removeAttribute('hidden');
48 functionEls[i].setAttribute('hidden', '');
52 function makeMessage(command, path) {
53 // Package a message using a simple protocol containing:
54 // command <path length> <path> <space-separated extra args>
60 // Maybe add extra args
61 for (var i = 2; i < arguments.length; ++i) {
62 msg += ' ' + arguments[i];
68 if (common.naclModule) {
69 var fileName = document.querySelector('#saveFile input').value;
70 var fileText = document.querySelector('#saveFile textarea').value;
71 common.naclModule.postMessage(makeMessage('sv', fileName, fileText));
78 if (common.naclModule) {
79 var fileName = document.querySelector('#loadFile input').value;
80 // clear the editor first (in case there is an error and there is no
82 document.querySelector('#loadFile textarea').value = '';
83 common.naclModule.postMessage(makeMessage('ld', fileName));
87 function deleteFileOrDirectory() {
88 if (common.naclModule) {
89 var fileName = document.querySelector('#delete input').value;
90 common.naclModule.postMessage(makeMessage('de', fileName));
95 if (common.naclModule) {
96 var dirName = document.querySelector('#listDir input').value;
97 common.naclModule.postMessage(makeMessage('ls', dirName));
102 if (common.naclModule) {
103 var dirName = document.querySelector('#makeDir input').value;
104 common.naclModule.postMessage(makeMessage('md', dirName));
108 // Called by the common.js module.
109 function handleMessage(message_event) {
110 var msg = message_event.data;
111 var parts = msg.split('|');
112 var command = parts[0];
113 var args = parts.slice(1);
115 if (command == 'ERR') {
116 common.logMessage('Error: ' + args[0]);
117 } else if (command == 'STAT') {
118 common.logMessage(args[0]);
119 } else if (command == 'READY') {
120 common.logMessage('Filesystem ready!');
121 } else if (command == 'DISP') {
122 // Find the file editor that is currently visible.
124 document.querySelector('.function:not([hidden]) > textarea');
125 // Rejoin args with pipe (|) -- there is only one argument, and it can
126 // contain the pipe character.
127 fileEditorEl.value = args.join('|');
128 } else if (command == 'LIST') {
129 var listDirOutputEl = document.getElementById('listDirOutput');
131 // NOTE: files with | in their names will be incorrectly split. Fixing this
132 // is left as an exercise for the reader.
134 // Remove all children of this element...
135 while (listDirOutputEl.firstChild) {
136 listDirOutputEl.removeChild(listDirOutputEl.firstChild);
140 // Add new <li> elements for each file.
141 for (var i = 0; i < args.length; ++i) {
142 var itemEl = document.createElement('li');
143 itemEl.textContent = args[i];
144 listDirOutputEl.appendChild(itemEl);
147 var itemEl = document.createElement('li');
148 itemEl.textContent = '<empty directory>';
149 listDirOutputEl.appendChild(itemEl);