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. 4 5function moduleDidLoad() { 6 common.hideModule(); 7} 8 9// Called by the common.js module. 10function domContentLoaded(name, tc, config, width, height) { 11 navigator.webkitPersistentStorage.requestQuota(1024 * 1024, 12 function(bytes) { 13 common.updateStatus( 14 'Allocated ' + bytes + ' bytes of persistant storage.'); 15 common.attachDefaultListeners(); 16 common.createNaClModule(name, tc, config, width, height); 17 }, 18 function(e) { alert('Failed to allocate space') }); 19} 20 21// Called by the common.js module. 22function attachListeners() { 23 var radioEls = document.querySelectorAll('input[type="radio"]'); 24 for (var i = 0; i < radioEls.length; ++i) { 25 radioEls[i].addEventListener('click', onRadioClicked); 26 } 27 28 function addEventListenerToButton(parentId, func) { 29 document.querySelector('#' + parentId + ' button') 30 .addEventListener('click', func); 31 } 32 33 addEventListenerToButton('saveFile', saveFile); 34 addEventListenerToButton('loadFile', loadFile); 35 addEventListenerToButton('delete', deleteFileOrDirectory); 36 addEventListenerToButton('listDir', listDir); 37 addEventListenerToButton('makeDir', makeDir); 38} 39 40function 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'); 47 else 48 functionEls[i].setAttribute('hidden', ''); 49 } 50} 51 52function makeMessage(command, path) { 53 // Package a message using a simple protocol containing: 54 // command <path length> <path> <space-separated extra args> 55 var msg = command; 56 msg += ' '; 57 msg += path.length; 58 msg += ' '; 59 msg += path; 60 // Maybe add extra args 61 for (var i = 2; i < arguments.length; ++i) { 62 msg += ' ' + arguments[i]; 63 } 64 return msg; 65} 66 67function saveFile() { 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)); 72 // clear the editor. 73 fileText.value = ''; 74 } 75} 76 77function loadFile() { 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 81 // output). 82 document.querySelector('#loadFile textarea').value = ''; 83 common.naclModule.postMessage(makeMessage('ld', fileName)); 84 } 85} 86 87function deleteFileOrDirectory() { 88 if (common.naclModule) { 89 var fileName = document.querySelector('#delete input').value; 90 common.naclModule.postMessage(makeMessage('de', fileName)); 91 } 92} 93 94function listDir() { 95 if (common.naclModule) { 96 var dirName = document.querySelector('#listDir input').value; 97 common.naclModule.postMessage(makeMessage('ls', dirName)); 98 } 99} 100 101function makeDir() { 102 if (common.naclModule) { 103 var dirName = document.querySelector('#makeDir input').value; 104 common.naclModule.postMessage(makeMessage('md', dirName)); 105 } 106} 107 108// Called by the common.js module. 109function 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); 114 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. 123 var fileEditorEl = 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'); 130 131 // NOTE: files with | in their names will be incorrectly split. Fixing this 132 // is left as an exercise for the reader. 133 134 // Remove all children of this element... 135 while (listDirOutputEl.firstChild) { 136 listDirOutputEl.removeChild(listDirOutputEl.firstChild); 137 } 138 139 if (args.length) { 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); 145 } 146 } else { 147 var itemEl = document.createElement('li'); 148 itemEl.textContent = '<empty directory>'; 149 listDirOutputEl.appendChild(itemEl); 150 } 151 } 152} 153