electron/atom/renderer/lib/inspector.coffee

61 lines
1.8 KiB
CoffeeScript
Raw Normal View History

2014-04-03 12:54:42 +00:00
window.onload = ->
# Use menu API to show context menu.
2015-09-02 10:28:40 +00:00
InspectorFrontendHost.showContextMenuAtPoint = createMenu
2014-04-03 12:54:42 +00:00
# Use dialog API to override file chooser dialog.
2015-09-02 10:28:40 +00:00
WebInspector.createFileSelectorElement = createFileSelectorElement
2014-04-03 12:54:42 +00:00
convertToMenuTemplate = (items) ->
template = []
for item in items
do (item) ->
transformed =
if item.type is 'subMenu'
type: 'submenu'
label: item.label
enabled: item.enabled
submenu: convertToMenuTemplate item.subItems
else if item.type is 'separator'
type: 'separator'
else if item.type is 'checkbox'
type: 'checkbox'
label: item.label
enabled: item.enabled
checked: item.checked
else
type: 'normal'
label: item.label
enabled: item.enabled
if item.id?
transformed.click = -> DevToolsAPI.contextMenuItemSelected item.id
template.push transformed
2014-04-03 12:54:42 +00:00
template
createMenu = (x, y, items, document) ->
2014-04-03 12:54:42 +00:00
remote = require 'remote'
Menu = remote.require 'menu'
menu = Menu.buildFromTemplate convertToMenuTemplate(items)
# The menu is expected to show asynchronously.
setImmediate ->
menu.popup remote.getCurrentWindow()
DevToolsAPI.contextMenuCleared()
showFileChooserDialog = (callback) ->
remote = require 'remote'
dialog = remote.require 'dialog'
files = dialog.showOpenDialog remote.getCurrentWindow(), null
callback pathToHtml5FileObject files[0] if files?
pathToHtml5FileObject = (path) ->
fs = require 'fs'
blob = new Blob([fs.readFileSync(path)])
blob.name = path
blob
createFileSelectorElement = (callback) ->
fileSelectorElement = document.createElement 'span'
fileSelectorElement.style.display = 'none'
fileSelectorElement.click = showFileChooserDialog.bind this, callback
return fileSelectorElement