FileDialog = binding.FileDialog
FileDialog.prototype.__proto__ = EventEmitter.prototype
-callbacksInfo = {}
+class CallbacksRegistry
+ @nextId = 0
+ @callbacks = {}
-fileDialog = new FileDialog
+ @add: (callback) ->
+ @callbacks[++@nextId] = callback
+ @nextId
+
+ @get: (id) ->
+ @callbacks[id]
+
+ @call: (id, args...) ->
+ @callbacks[id].call global, args...
-onSelected = (event, id, paths...) ->
- {processId, routingId} = callbacksInfo[id]
- delete callbacksInfo[id]
+ @remove: (id) ->
+ delete @callbacks[id]
- ipc.sendChannel processId, routingId, 'ATOM_RENDERER_DIALOG', id, paths...
+fileDialog = new FileDialog
+
+fileDialog.on 'selected', (event, callbackId, paths...) ->
+ CallbacksRegistry.call callbackId, 'selected', paths...
+ CallbacksRegistry.remove callbackId
-fileDialog.on 'selected', onSelected
-fileDialog.on 'cancelled', onSelected
+fileDialog.on 'cancelled', (event, callbackId) ->
+ CallbacksRegistry.call callbackId, 'cancelled'
+ CallbacksRegistry.remove callbackId
validateOptions = (options) ->
return false unless typeof options is 'object'
options.defaultPath = '' unless options.defaultPath?
options.fileTypeIndex = 0 unless options.fileTypeIndex?
options.defaultExtension = '' unless options.defaultExtension?
-
true
-ipc.on 'ATOM_BROWSER_FILE_DIALOG', (processId, routingId, callbackId, type, title, options) ->
+selectFileWrap = (window, options, callback, type, title) ->
+ throw new TypeError('Need Window object') unless window.constructor?.name is 'Window'
+
options = {} unless options?
options.type = type
options.title = title unless options.title?
throw new TypeError('Bad arguments') unless validateOptions options
- callbacksInfo[callbackId] = processId: processId, routingId: routingId
+ callbackId = CallbacksRegistry.add callback
- fileDialog.selectFile processId, routingId,
+ fileDialog.selectFile window.getProcessId(), window.getRoutingId(),
options.type,
options.title,
options.defaultPath,
options.fileTypeIndex,
options.defaultExtension,
callbackId
+
+module.exports =
+ openFolder: (args...) ->
+ selectFileWrap args..., 1, 'Open Folder'
+
+ saveAs: (args...) ->
+ selectFileWrap args..., 2, 'Save As'
+
+ openFile: (args...) ->
+ selectFileWrap args..., 3, 'Open File'
+
+ openMultiFiles: (args...) ->
+ selectFileWrap args..., 4, 'Open Files'
-ipc = require 'ipc'
+remote = require 'remote'
+dialog = remote.require 'dialog'
-callbackId = 0
-callbacks = {}
-
-storeCallback = (callback) ->
- throw new TypeError('Bad argument') unless typeof callback is 'function'
-
- ++callbackId
- callbacks[callbackId] = callback
- callbackId
-
-makeCallback = (id, args...) ->
- callbacks[id].call global, args...
- delete callbacks[id]
-
-# Force loading dialog code in browser.
-remote.require 'dialog'
-
-ipc.on 'ATOM_RENDERER_DIALOG', (id, args...) ->
- makeCallback(id, args...)
-
-callFileDialogs = (options, callback, args...) ->
- if typeof options is 'function'
- callback = options
- options = {}
-
- ipc.sendChannel 'ATOM_BROWSER_FILE_DIALOG', storeCallback(callback), args..., options
+win = remote.getCurrentWindow()
module.exports =
- openFolder: (options, callback) ->
- callFileDialogs options, callback, 1, 'Open Folder'
-
- saveAs: (options, callback) ->
- callFileDialogs options, callback, 2, 'Save As'
-
- openFile: (options, callback) ->
- callFileDialogs options, callback, 3, 'Open File'
-
- openMultiFiles: (options, callback) ->
- callFileDialogs options, callback, 4, 'Open Files'
+ openFolder: (args...) -> dialog.openFolder win, args...
+ saveAs: (args...) -> dialog.saveAs win, args...
+ openFile: (args...) -> dialog.openFile win, args...
+ openMultiFiles: (args...) -> dialog.openMultiFiles win, args...