| 220 | } |
| 221 | |
| 222 | void EmscriptenFilePicker::FetchFilesAsync(Containers::StringView fileFilter, bool multiple, Containers::Function<void(Containers::ArrayView<EmscriptenFileStream>)>&& callback) |
| 223 | { |
| 224 | if (_activeCallback) { |
| 225 | // Another callback is already active, cancel this one |
| 226 | callback({}); |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | _activeCallback = Death::move(callback); |
| 231 | |
| 232 | // Create file input element which will display a native file dialog |
| 233 | auto document = emscripten::val::global("document"); |
| 234 | auto input = document.call<emscripten::val>("createElement", std::string("input")); |
| 235 | input.set("type", "file"); |
| 236 | input.set("style", "display:none"); |
| 237 | if (!fileFilter.empty()) { |
| 238 | input.set("accept", emscripten::val(fileFilter.data())); |
| 239 | } |
| 240 | if (multiple) { |
| 241 | input.set("multiple", "multiple"); |
| 242 | } |
| 243 | |
| 244 | // http://perfectionkills.com/detecting-event-support-without-browser-sniffing |
| 245 | _supportsCancel = false; |
| 246 | if (emscripten::val("oncancel").in(input)) { |
| 247 | _supportsCancel = true; |
| 248 | } else { |
| 249 | input.set("oncancel", emscripten::val("return;")); |
| 250 | if (input["oncancel"].typeOf().as<std::string>() == "function") { |
| 251 | _supportsCancel = true; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | input.set("onchange", emscripten::val::module_property("jsReadFiles")); |
| 256 | input.set("oncancel", emscripten::val::module_property("jsCancelReadFiles")); |
| 257 | input.set("data-callbackContext", emscripten::val(std::size_t(this))); |
| 258 | |
| 259 | auto body = document["body"]; |
| 260 | body.call<void>("appendChild", input); |
| 261 | input.call<void>("click"); |
| 262 | body.call<void>("removeChild", input); |
| 263 | } |
| 264 | |
| 265 | void EmscriptenFilePicker::FetchDirectoryAsync(Containers::Function<void(Containers::ArrayView<EmscriptenFileStream>)>&& callback) |
| 266 | { |
no test coverage detected