2014-04-03 12:54:42 +00:00
|
|
|
window.onload = ->
|
2014-04-05 02:24:46 +00:00
|
|
|
# Use menu API to show context menu.
|
2014-04-03 12:54:42 +00:00
|
|
|
WebInspector.ContextMenu.prototype.show = ->
|
|
|
|
menuObject = @_buildDescriptor()
|
|
|
|
if menuObject.length
|
|
|
|
WebInspector._contextMenu = this
|
|
|
|
createMenu(menuObject, @_event)
|
|
|
|
@_event.consume()
|
|
|
|
|
2014-04-05 02:24:46 +00:00
|
|
|
# Use dialog API to override file chooser dialog.
|
|
|
|
WebInspector.createFileSelectorElement = (callback) ->
|
|
|
|
fileSelectorElement = document.createElement 'span'
|
|
|
|
fileSelectorElement.style.display = 'none'
|
|
|
|
fileSelectorElement.click = showFileChooserDialog.bind this, callback
|
|
|
|
return fileSelectorElement
|
|
|
|
|
2014-04-03 12:54:42 +00:00
|
|
|
convertToMenuTemplate = (items) ->
|
|
|
|
template = []
|
|
|
|
for item in items
|
2014-04-04 14:37:34 +00:00
|
|
|
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 = -> WebInspector.contextMenuItemSelected item.id
|
|
|
|
template.push transformed
|
2014-04-03 12:54:42 +00:00
|
|
|
template
|
|
|
|
|
|
|
|
createMenu = (items, event) ->
|
|
|
|
remote = require 'remote'
|
|
|
|
Menu = remote.require 'menu'
|
|
|
|
|
2014-04-04 14:37:34 +00:00
|
|
|
menu = Menu.buildFromTemplate convertToMenuTemplate(items)
|
|
|
|
menu.popup remote.getCurrentWindow()
|
2014-04-03 12:54:42 +00:00
|
|
|
event.consume true
|
2014-04-05 02:24:46 +00:00
|
|
|
|
|
|
|
showFileChooserDialog = (callback) ->
|
|
|
|
remote = require 'remote'
|
|
|
|
dialog = remote.require 'dialog'
|
|
|
|
dialog.showOpenDialog remote.getCurrentWindow(), null, (files) ->
|
|
|
|
callback pathToHtml5FileObject(files[0]) if files?
|
|
|
|
|
|
|
|
pathToHtml5FileObject = (path) ->
|
|
|
|
fs = require 'fs'
|
|
|
|
blob = new Blob([fs.readFileSync(path)])
|
|
|
|
blob.name = path
|
|
|
|
blob
|