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(5 * 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
);
38 addEventListenerToButton('rename', rename
);
41 function onRadioClicked(e
) {
42 var divId
= this.id
.slice(6); // skip "radio_"
43 var functionEls
= document
.querySelectorAll('.function');
44 for (var i
= 0; i
< functionEls
.length
; ++i
) {
45 var visible
= functionEls
[i
].id
=== divId
;
46 if (functionEls
[i
].id
=== divId
)
47 functionEls
[i
].removeAttribute('hidden');
49 functionEls
[i
].setAttribute('hidden', '');
53 function makeMessage(command
, path
) {
54 // Package a message using a simple protocol containing:
55 // [command, <path>, <extra args>...]
56 var msg
= [command
, path
];
57 for (var i
= 2; i
< arguments
.length
; ++i
) {
58 msg
.push(arguments
[i
]);
64 if (common
.naclModule
) {
65 var fileName
= document
.querySelector('#saveFile input').value
;
66 var fileText
= document
.querySelector('#saveFile textarea').value
;
67 common
.naclModule
.postMessage(makeMessage('save', fileName
, fileText
));
74 if (common
.naclModule
) {
75 var fileName
= document
.querySelector('#loadFile input').value
;
76 // clear the editor first (in case there is an error and there is no
78 document
.querySelector('#loadFile textarea').value
= '';
79 common
.naclModule
.postMessage(makeMessage('load', fileName
));
83 function deleteFileOrDirectory() {
84 if (common
.naclModule
) {
85 var fileName
= document
.querySelector('#delete input').value
;
86 common
.naclModule
.postMessage(makeMessage('delete', fileName
));
91 if (common
.naclModule
) {
92 var dirName
= document
.querySelector('#listDir input').value
;
93 common
.naclModule
.postMessage(makeMessage('list', dirName
));
98 if (common
.naclModule
) {
99 var dirName
= document
.querySelector('#makeDir input').value
;
100 common
.naclModule
.postMessage(makeMessage('makedir', dirName
));
105 if (common
.naclModule
) {
106 var oldName
= document
.querySelector('#renameOld').value
;
107 var newName
= document
.querySelector('#renameNew').value
;
108 common
.naclModule
.postMessage(makeMessage('rename', oldName
, newName
));
112 // Called by the common.js module.
113 function handleMessage(message_event
) {
114 var msg
= message_event
.data
;
115 var command
= msg
[0];
116 var args
= msg
.slice(1);
118 if (command
== 'ERR') {
119 common
.logMessage('Error: ' + args
[0]);
120 } else if (command
== 'STAT') {
121 common
.logMessage(args
[0]);
122 } else if (command
== 'READY') {
123 common
.logMessage('Filesystem ready!');
124 } else if (command
== 'DISP') {
125 // Find the file editor that is currently visible.
127 document
.querySelector('.function:not([hidden]) > textarea');
128 // Rejoin args with pipe (|) -- there is only one argument, and it can
129 // contain the pipe character.
130 fileEditorEl
.value
= args
.join('|');
131 } else if (command
== 'LIST') {
132 var listDirOutputEl
= document
.getElementById('listDirOutput');
134 // NOTE: files with | in their names will be incorrectly split. Fixing this
135 // is left as an exercise for the reader.
137 // Remove all children of this element...
138 while (listDirOutputEl
.firstChild
) {
139 listDirOutputEl
.removeChild(listDirOutputEl
.firstChild
);
143 // Add new <li> elements for each file.
144 for (var i
= 0; i
< args
.length
; ++i
) {
145 var itemEl
= document
.createElement('li');
146 itemEl
.textContent
= args
[i
];
147 listDirOutputEl
.appendChild(itemEl
);
150 var itemEl
= document
.createElement('li');
151 itemEl
.textContent
= '<empty directory>';
152 listDirOutputEl
.appendChild(itemEl
);