|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var nwdialog = { |
| 4 | + |
| 5 | + _context: (typeof global === 'undefined' || typeof global.DOMDocument === 'undefined') ? document : global.DOMDocument, |
| 6 | + |
| 7 | + setContext: function(context) { |
| 8 | + this._context = context; |
| 9 | + }, |
| 10 | + |
| 11 | + openFileDialog: function(filter, multiple, workdir, callback) { |
| 12 | + |
| 13 | + var fn = callback; |
| 14 | + var node = this._context.createElement('input'); |
| 15 | + node.type = 'file'; |
| 16 | + node.id = 'open-file-dialog'; |
| 17 | + node.style = 'display: none'; |
| 18 | + |
| 19 | + if (typeof filter === 'function') { |
| 20 | + fn = filter; |
| 21 | + } else if (typeof filter === 'string') { |
| 22 | + node.setAttribute('accept', filter); |
| 23 | + } else if (typeof filter === 'boolean' && filter === true) { |
| 24 | + node.setAttribute('multiple', ''); |
| 25 | + } else if (this.isArray(filter)) { |
| 26 | + node.setAttribute('accept', filter.join(',')); |
| 27 | + } |
| 28 | + |
| 29 | + if (typeof multiple === 'function') { |
| 30 | + fn = multiple; |
| 31 | + } else if (typeof multiple === 'string') { |
| 32 | + node.setAttribute('nwworkingdir', multiple); |
| 33 | + } else if (typeof multiple === 'boolean' && multiple === true) { |
| 34 | + node.setAttribute('multiple', ''); |
| 35 | + } |
| 36 | + |
| 37 | + if (typeof workdir === 'function') { |
| 38 | + fn = workdir; |
| 39 | + } else if (typeof workdir === 'string') { |
| 40 | + node.setAttribute('nwworkingdir', workdir); |
| 41 | + } |
| 42 | + |
| 43 | + this._context.body.appendChild(node); |
| 44 | + node.addEventListener('change', function(e) { |
| 45 | + fn(node.value); |
| 46 | + node.remove(); |
| 47 | + }); |
| 48 | + node.click(); |
| 49 | + |
| 50 | + }, |
| 51 | + |
| 52 | + saveFileDialog: function(name, accept, directory, callback) { |
| 53 | + |
| 54 | + var fn = callback; |
| 55 | + var node = this._context.createElement('input'); |
| 56 | + node.type = 'file'; |
| 57 | + node.id = 'save-file-dialog'; |
| 58 | + node.style = 'display: none'; |
| 59 | + node.setAttribute('nwsaveas', ''); |
| 60 | + |
| 61 | + if (typeof name === 'function') { |
| 62 | + fn = name; |
| 63 | + } else if (typeof name === 'string') { |
| 64 | + node.setAttribute('nwsaveas', name); |
| 65 | + } |
| 66 | + |
| 67 | + if (typeof accept === 'function') { |
| 68 | + fn = accept; |
| 69 | + } else if (typeof accept === 'string') { |
| 70 | + node.setAttribute('accept', accept); |
| 71 | + } else if (this.isArray(accept)) { |
| 72 | + node.setAttribute('accept', accept.join(',')); |
| 73 | + } |
| 74 | + |
| 75 | + if (typeof directory === 'function') { |
| 76 | + fn = directory; |
| 77 | + } else if (typeof directory === 'string') { |
| 78 | + node.setAttribute('nwworkingdir', directory); |
| 79 | + } |
| 80 | + |
| 81 | + this._context.body.appendChild(node); |
| 82 | + node.addEventListener('change', function() { |
| 83 | + fn(node.value); |
| 84 | + node.remove(); |
| 85 | + }); |
| 86 | + node.click(); |
| 87 | + |
| 88 | + }, |
| 89 | + |
| 90 | + folderBrowserDialog: function(workdir, callback) { |
| 91 | + var fn = callback; |
| 92 | + var node = this._context.createElement('input'); |
| 93 | + node.type = 'file'; |
| 94 | + node.id = 'folder-browser-dialog'; |
| 95 | + node.style = 'display: none'; |
| 96 | + node.nwdirectory= true; |
| 97 | + |
| 98 | + if (typeof workdir === 'function') { |
| 99 | + fn = workdir |
| 100 | + } else if (typeof workdir === 'string') { |
| 101 | + node.setAttribute('nwworkingdir', workdir); |
| 102 | + } |
| 103 | + |
| 104 | + this._context.body.appendChild(node); |
| 105 | + node.addEventListener('change', function() { |
| 106 | + fn(node.value); |
| 107 | + node.remove(); |
| 108 | + }); |
| 109 | + node.click(); |
| 110 | + }, |
| 111 | + |
| 112 | + isArray: function(value) { |
| 113 | + return Object.prototype.toString.call(value) === '[object Array]'; |
| 114 | + } |
| 115 | + |
| 116 | +} |
| 117 | + |
| 118 | +if (typeof nw !== 'undefined') { |
| 119 | + if (typeof exports == 'undefined') { |
| 120 | + nw.Dialog = nwdialog; |
| 121 | + window.dialog = nwdialog; |
| 122 | + } else { |
| 123 | + module.exports = nwdialog; |
| 124 | + } |
| 125 | +} |
0 commit comments