| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2013 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 /* globals NaClTerm, lib */ |
| 8 |
| 7 'use strict'; | 9 'use strict'; |
| 8 | 10 |
| 9 NaClTerm.nmf = 'vim.nmf' | 11 NaClTerm.nmf = 'vim.nmf'; |
| 10 | 12 |
| 11 function log(message) { | 13 function log(message) { |
| 12 console.log(message) | 14 console.log(message); |
| 13 } | 15 } |
| 14 | 16 |
| 15 function fsErrorHandler(error) { | 17 function fsErrorHandler(error) { |
| 16 // TODO(sbc): Add popup that tells the user there was an error. | 18 // TODO(sbc): Add popup that tells the user there was an error. |
| 17 log('Filesystem error: ' + error); | 19 log('Filesystem error: ' + error); |
| 18 } | 20 } |
| 19 | 21 |
| 20 function runVim() { | 22 function runVim() { |
| 21 NaClTerm.init(); | 23 NaClTerm.init(); |
| 22 } | 24 } |
| 23 | 25 |
| 24 function runVimWithFile(file) { | 26 function runVimWithFile(file) { |
| 25 log('runVimWithFile: ' + file.name); | 27 log('runVimWithFile: ' + file.name); |
| 26 tempFS.root.getFile(file.name, {create: true}, | 28 window.tempFS.root.getFile(file.name, {create: true}, |
| 27 function(fileEntry) { | 29 function(fileEntry) { |
| 28 window.tmpFileEntry = fileEntry | 30 window.tmpFileEntry = fileEntry; |
| 29 fileEntry.createWriter(function(fileWriter) { | 31 fileEntry.createWriter(function(fileWriter) { |
| 30 // Note: write() can take a File or Blob object. | 32 // Note: write() can take a File or Blob object. |
| 31 fileWriter.write(file); | 33 fileWriter.write(file); |
| 32 log('File written to temporary filesystem\n'); | 34 log('File written to temporary filesystem\n'); |
| 33 NaClTerm.argv = ['/tmp/' + file.name]; | 35 NaClTerm.argv = ['/tmp/' + file.name]; |
| 34 runVim(); | 36 runVim(); |
| 35 }, fsErrorHandler); | 37 }, fsErrorHandler); |
| 36 }, fsErrorHandler); | 38 }, fsErrorHandler); |
| 37 } | 39 } |
| 38 | 40 |
| 39 function runVimWithFileEntry(fileEntry) { | 41 function runVimWithFileEntry(fileEntry) { |
| 40 window.fileEntryToSave = fileEntry; | 42 window.fileEntryToSave = fileEntry; |
| 41 fileEntry.file(function(file) { | 43 fileEntry.file(function(file) { |
| 42 runVimWithFile(file); | 44 runVimWithFile(file); |
| 43 }); | 45 }); |
| 44 } | 46 } |
| 45 | 47 |
| 46 function uploadDidChange(event) { | 48 function uploadDidChange(event) { |
| 47 var file = event.target.files[0]; | 49 var file = event.target.files[0]; |
| 48 runVimWithFile(file); | 50 runVimWithFile(file); |
| 49 } | 51 } |
| 50 | 52 |
| 51 function onInitFS(fs) { | 53 function onInitFS(fs) { |
| 52 log('onInitFS'); | 54 log('onInitFS'); |
| 53 window.tempFS = fs | 55 window.tempFS = fs; |
| 54 | 56 |
| 55 // Once the temp filesystem is initialised we launch vim. | 57 // Once the temp filesystem is initialised we launch vim. |
| 56 // For packaged apps the fileEntryToLoad attribute will be set if the | 58 // For packaged apps the fileEntryToLoad attribute will be set if the |
| 57 // user launched us with a fileEntry. Otherwise we fallback to asking | 59 // user launched us with a fileEntry. Otherwise we fallback to asking |
| 58 // them using chooseEntry. | 60 // them using chooseEntry. |
| 59 if (window.fileEntryToLoad !== undefined) { | 61 if (window.fileEntryToLoad !== undefined) { |
| 60 // We have fileEntry already. | 62 // We have fileEntry already. |
| 61 runVimWithFileEntry(window.fileEntryToLoad); | 63 runVimWithFileEntry(window.fileEntryToLoad); |
| 62 } else if (chrome.fileSystem) { | 64 } else if (chrome.fileSystem) { |
| 63 // We have access the fileSystem API, so ask the user | 65 // We have access the fileSystem API, so ask the user |
| (...skipping 12 matching lines...) Expand all Loading... |
| 76 upload.addEventListener('change', uploadDidChange, false); | 78 upload.addEventListener('change', uploadDidChange, false); |
| 77 } else { | 79 } else { |
| 78 runVim(); | 80 runVim(); |
| 79 } | 81 } |
| 80 } | 82 } |
| 81 } | 83 } |
| 82 | 84 |
| 83 function onInit() { | 85 function onInit() { |
| 84 navigator.webkitPersistentStorage.requestQuota(1024 * 1024, | 86 navigator.webkitPersistentStorage.requestQuota(1024 * 1024, |
| 85 function(bytes) { | 87 function(bytes) { |
| 86 window.webkitRequestFileSystem(window.TEMPORARAY, bytes, onInitFS) | 88 window.webkitRequestFileSystem(window.TEMPORARAY, bytes, onInitFS); |
| 87 }, | 89 }, |
| 88 function() { | 90 function() { |
| 89 log('Failed to allocate space!\n'); | 91 log('Failed to allocate space!\n'); |
| 90 // Start the terminal even if FS failed to init. | 92 // Start the terminal even if FS failed to init. |
| 91 runVim(); | 93 runVim(); |
| 92 } | 94 } |
| 93 ); | 95 ); |
| 94 } | 96 } |
| 95 | 97 |
| 96 window.onload = function() { | 98 window.onload = function() { |
| 97 lib.init(function() { | 99 lib.init(function() { |
| 98 onInit(); | 100 onInit(); |
| 99 }); | 101 }); |
| 100 }; | 102 }; |
| OLD | NEW |