2014-04-25 08:23:40 +00:00
|
|
|
EventEmitter = require('events').EventEmitter
|
2015-04-26 13:28:30 +00:00
|
|
|
NavigationController = require './navigation-controller'
|
2014-10-23 06:04:13 +00:00
|
|
|
binding = process.atomBinding 'web_contents'
|
2014-04-25 08:23:40 +00:00
|
|
|
ipc = require 'ipc'
|
|
|
|
|
2015-06-10 03:34:16 +00:00
|
|
|
nextId = 0
|
|
|
|
getNextId = -> ++nextId
|
|
|
|
|
2015-07-26 03:43:23 +00:00
|
|
|
PDFPageSize =
|
|
|
|
A4:
|
|
|
|
custom_display_name: "A4"
|
|
|
|
height_microns: 297000
|
|
|
|
name: "ISO_A4"
|
|
|
|
is_default: "true"
|
|
|
|
width_microns: 210000
|
|
|
|
A3:
|
|
|
|
custom_display_name: "A3"
|
|
|
|
height_microns: 420000
|
|
|
|
name: "ISO_A3"
|
|
|
|
width_microns: 297000
|
|
|
|
Legal:
|
|
|
|
custom_display_name: "Legal"
|
|
|
|
height_microns: 355600
|
|
|
|
name: "NA_LEGAL"
|
|
|
|
width_microns: 215900
|
|
|
|
Letter:
|
|
|
|
custom_display_name: "Letter"
|
|
|
|
height_microns: 279400
|
|
|
|
name: "NA_LETTER"
|
|
|
|
width_microns: 215900
|
|
|
|
Tabloid:
|
|
|
|
height_microns: 431800
|
|
|
|
name: "NA_LEDGER"
|
|
|
|
width_microns: 279400
|
|
|
|
custom_display_name: "Tabloid"
|
|
|
|
|
2015-06-05 09:01:17 +00:00
|
|
|
wrapWebContents = (webContents) ->
|
2014-04-25 08:23:40 +00:00
|
|
|
# webContents is an EventEmitter.
|
|
|
|
webContents.__proto__ = EventEmitter.prototype
|
|
|
|
|
2014-04-25 08:45:14 +00:00
|
|
|
# WebContents::send(channel, args..)
|
2015-01-27 18:47:11 +00:00
|
|
|
webContents.send = (channel, args...) ->
|
|
|
|
@_send channel, [args...]
|
2014-04-25 08:23:40 +00:00
|
|
|
|
2014-06-16 01:10:41 +00:00
|
|
|
# Make sure webContents.executeJavaScript would run the code only when the
|
|
|
|
# web contents has been loaded.
|
|
|
|
webContents.loaded = false
|
|
|
|
webContents.once 'did-finish-load', -> @loaded = true
|
2015-07-30 09:12:03 +00:00
|
|
|
webContents.executeJavaScript = (code, hasUserGesture=false) ->
|
2014-06-16 01:10:41 +00:00
|
|
|
if @loaded
|
2015-07-30 09:12:03 +00:00
|
|
|
@_executeJavaScript code, hasUserGesture
|
2014-06-16 01:10:41 +00:00
|
|
|
else
|
2015-07-30 09:12:03 +00:00
|
|
|
webContents.once 'did-finish-load', @_executeJavaScript.bind(this, code, hasUserGesture)
|
2014-06-16 01:10:41 +00:00
|
|
|
|
2015-04-26 13:28:30 +00:00
|
|
|
# The navigation controller.
|
|
|
|
controller = new NavigationController(webContents)
|
|
|
|
for name, method of NavigationController.prototype when method instanceof Function
|
|
|
|
do (name, method) ->
|
|
|
|
webContents[name] = -> method.apply controller, arguments
|
2014-11-11 18:33:15 +00:00
|
|
|
|
2014-04-25 08:23:40 +00:00
|
|
|
# Dispatch IPC messages to the ipc module.
|
2015-01-15 03:33:24 +00:00
|
|
|
webContents.on 'ipc-message', (event, packed) ->
|
|
|
|
[channel, args...] = packed
|
2014-04-25 08:23:40 +00:00
|
|
|
ipc.emit channel, event, args...
|
2015-01-15 03:33:24 +00:00
|
|
|
webContents.on 'ipc-message-sync', (event, packed) ->
|
|
|
|
[channel, args...] = packed
|
2014-04-25 08:23:40 +00:00
|
|
|
Object.defineProperty event, 'returnValue', set: (value) -> event.sendReply JSON.stringify(value)
|
|
|
|
ipc.emit channel, event, args...
|
|
|
|
|
2015-06-10 03:34:16 +00:00
|
|
|
webContents.printToPDF = (options, callback) ->
|
|
|
|
printingSetting =
|
2015-07-26 03:43:23 +00:00
|
|
|
pageRage: []
|
|
|
|
mediaSize: {}
|
|
|
|
landscape: false
|
|
|
|
color: 2
|
|
|
|
headerFooterEnabled: false
|
|
|
|
marginsType: 0
|
|
|
|
isFirstRequest: false
|
|
|
|
requestID: getNextId()
|
|
|
|
previewModifiable: true
|
|
|
|
printToPDF: true
|
|
|
|
printWithCloudPrint: false
|
|
|
|
printWithPrivet: false
|
|
|
|
printWithExtension: false
|
|
|
|
deviceName: "Save as PDF"
|
|
|
|
generateDraftData: true
|
|
|
|
fitToPageEnabled: false
|
|
|
|
duplex: 0
|
|
|
|
copies: 1
|
|
|
|
collate: true
|
|
|
|
shouldPrintBackgrounds: false
|
|
|
|
shouldPrintSelectionOnly: false
|
2015-06-10 03:34:16 +00:00
|
|
|
|
|
|
|
if options.landscape
|
|
|
|
printingSetting.landscape = options.landscape
|
|
|
|
if options.marginsType
|
|
|
|
printingSetting.marginsType = options.marginsType
|
|
|
|
if options.printSelectionOnly
|
|
|
|
printingSetting.shouldPrintSelectionOnly = options.printSelectionOnly
|
2015-06-10 07:43:33 +00:00
|
|
|
if options.printBackgrounds
|
2015-06-11 06:43:45 +00:00
|
|
|
printingSetting.shouldPrintBackgrounds = options.printBackground
|
2015-06-10 03:34:16 +00:00
|
|
|
|
2015-07-26 03:43:23 +00:00
|
|
|
if options.pageSize and PDFPageSize[options.pageSize]
|
|
|
|
printingSetting.mediaSize = PDFPageSize[options.pageSize]
|
|
|
|
else
|
|
|
|
printingSetting.mediaSize = PDFPageSize['A4']
|
|
|
|
|
2015-06-23 14:11:59 +00:00
|
|
|
@_printToPDF printingSetting, callback
|
2014-10-23 06:04:13 +00:00
|
|
|
|
2015-06-05 09:01:17 +00:00
|
|
|
binding._setWrapWebContents wrapWebContents
|
|
|
|
process.once 'exit', binding._clearWrapWebContents
|
|
|
|
|
2014-10-23 06:04:13 +00:00
|
|
|
module.exports.create = (options={}) ->
|
2015-06-05 09:01:17 +00:00
|
|
|
binding.create(options)
|