Created
April 21, 2013 02:53
-
-
Save anonymous/5428273 to your computer and use it in GitHub Desktop.
Experimenting with using a file chooser in a Chrome App written in Dart. Got stuck again :(
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'dart:chrome'; | |
import 'dart:html'; | |
import 'package:js/js.dart' as js; | |
void main() { | |
var open = new ButtonElement()..text = 'Open'; | |
document.body.children.add(open); | |
write(s) => document.body.children.add(new DivElement()..text = s); | |
open.onClick.listen((_) { | |
FileEntry.choose() | |
.then((entry) { | |
entry.displayPath.then(write); | |
// Yikes | |
// Can't use dart's file reader as entry is a proxy not a Dart FileEntry/Blob. | |
// Not sure how to call the javascript ctor new FileReader() via js_interop. | |
//var reader = new FileReader(); | |
//reader.readAsText(entry); | |
}); | |
}); | |
} | |
class ChooseEntryType { | |
const ChooseEntryType(this._value); | |
final String _value; | |
toString() => _value; | |
} | |
const OPEN_FILE = const ChooseEntryType("openFile"); | |
const OPEN_FILE_WRITABLE = const ChooseEntryType("openWritableFile"); | |
const SAVE_FILE = const ChooseEntryType("saveFile"); | |
class AcceptOption { | |
AcceptOption(this.description, this.mimeTypes, this.extensions); | |
final String description; | |
final List<String> mimeTypes; | |
final List<String> extensions; | |
} | |
class FileEntry { | |
final _proxy; | |
FileEntry._internal(this._proxy); | |
void release() { | |
js.release(_proxy); | |
} | |
static Future<FileEntry> choose( | |
{ChooseEntryType type: OPEN_FILE, | |
String suggestedName : '', | |
List<AcceptOption> accepts : const [], | |
bool acceptsAllTypes: true}) { | |
var cpl = new Completer<String>(); | |
js.scoped(() { | |
var options = {'type': type.toString(), | |
'suggestedName': suggestedName, | |
'accepts': js.array(accepts), //TODO | |
'acceptsAllTypes': acceptsAllTypes}; | |
js.context.chrome.fileSystem.chooseEntry(js.map(options), | |
new js.Callback.once((fileEntry) { | |
cpl.complete(new FileEntry._internal(fileEntry)); | |
})); | |
}); | |
return cpl.future; | |
} | |
static FileEntry getById(String id) { | |
var fileEntryProxy; | |
js.scoped(() { | |
fileEntryProxy = js.context.chrome.fileSystem.getEntryById(id); | |
js.retain(fileEntryProxy); | |
}); | |
return new FileEntry._internal(fileEntryProxy); | |
} | |
String get id { | |
var entryId; | |
js.scoped(() { | |
entryId = js.context.chrome.fileSystem.getEntryId(_proxy); | |
}); | |
return entryId; | |
} | |
Future<String> get displayPath { | |
var cpl = new Completer<String>(); | |
js.scoped(() { | |
js.context.chrome.fileSystem.getDisplayPath(_proxy, | |
new js.Callback.once((path) { | |
cpl.complete(path); | |
})); | |
}); | |
return cpl.future; | |
} | |
Future<FileEntry> makeWritable() { | |
throw new UnimplementedError(); | |
} | |
Future<bool> get isWritable { | |
throw new UnimplementedError(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment