2013-05-03 13:03:26 +00:00
|
|
|
binding = process.atomBinding 'dialog'
|
2013-05-05 12:30:38 +00:00
|
|
|
CallbacksRegistry = require 'callbacks_registry'
|
2013-05-03 11:31:24 +00:00
|
|
|
EventEmitter = require('events').EventEmitter
|
|
|
|
ipc = require 'ipc'
|
|
|
|
|
2013-05-03 13:03:26 +00:00
|
|
|
FileDialog = binding.FileDialog
|
2013-05-03 11:31:24 +00:00
|
|
|
FileDialog.prototype.__proto__ = EventEmitter.prototype
|
|
|
|
|
2013-05-05 12:30:38 +00:00
|
|
|
callbacksRegistry = new CallbacksRegistry
|
2013-05-03 11:31:24 +00:00
|
|
|
|
2013-05-05 12:24:20 +00:00
|
|
|
fileDialog = new FileDialog
|
|
|
|
|
|
|
|
fileDialog.on 'selected', (event, callbackId, paths...) ->
|
2013-05-05 12:30:38 +00:00
|
|
|
callbacksRegistry.call callbackId, 'selected', paths...
|
|
|
|
callbacksRegistry.remove callbackId
|
2013-05-03 11:31:24 +00:00
|
|
|
|
2013-05-05 12:24:20 +00:00
|
|
|
fileDialog.on 'cancelled', (event, callbackId) ->
|
2013-05-05 12:30:38 +00:00
|
|
|
callbacksRegistry.call callbackId, 'cancelled'
|
|
|
|
callbacksRegistry.remove callbackId
|
2013-05-03 11:31:24 +00:00
|
|
|
|
|
|
|
validateOptions = (options) ->
|
|
|
|
return false unless typeof options is 'object'
|
|
|
|
|
|
|
|
options.fileTypes = [] unless Array.isArray options.fileTypes
|
|
|
|
for type in options.fileTypes
|
|
|
|
return false unless typeof type is 'object' and
|
|
|
|
typeof type.description is 'string'
|
|
|
|
Array.isArray type.extensions
|
|
|
|
|
|
|
|
options.defaultPath = '' unless options.defaultPath?
|
|
|
|
options.fileTypeIndex = 0 unless options.fileTypeIndex?
|
|
|
|
options.defaultExtension = '' unless options.defaultExtension?
|
|
|
|
true
|
|
|
|
|
2013-05-05 12:24:20 +00:00
|
|
|
selectFileWrap = (window, options, callback, type, title) ->
|
|
|
|
throw new TypeError('Need Window object') unless window.constructor?.name is 'Window'
|
|
|
|
|
2013-05-03 11:31:24 +00:00
|
|
|
options = {} unless options?
|
|
|
|
options.type = type
|
|
|
|
options.title = title unless options.title?
|
|
|
|
|
|
|
|
throw new TypeError('Bad arguments') unless validateOptions options
|
|
|
|
|
2013-05-05 12:30:38 +00:00
|
|
|
callbackId = callbacksRegistry.add callback
|
2013-05-03 11:31:24 +00:00
|
|
|
|
2013-05-05 12:24:20 +00:00
|
|
|
fileDialog.selectFile window.getProcessId(), window.getRoutingId(),
|
2013-05-03 11:31:24 +00:00
|
|
|
options.type,
|
|
|
|
options.title,
|
|
|
|
options.defaultPath,
|
|
|
|
options.fileTypes,
|
|
|
|
options.fileTypeIndex,
|
|
|
|
options.defaultExtension,
|
|
|
|
callbackId
|
2013-05-05 12:24:20 +00:00
|
|
|
|
|
|
|
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'
|