Use let/const instead of var

This commit is contained in:
Kevin Sawicki 2016-11-03 10:39:40 -07:00
parent 3053be345b
commit 712b15286c
4 changed files with 85 additions and 100 deletions

View file

@ -8,19 +8,18 @@ const v8Util = process.atomBinding('v8_util')
const guestViewInternal = require('./guest-view-internal')
const webViewConstants = require('./web-view-constants')
var hasProp = {}.hasOwnProperty
const hasProp = {}.hasOwnProperty
// ID generator.
var nextId = 0
let nextId = 0
var getNextId = function () {
const getNextId = function () {
return ++nextId
}
// Represents the internal state of the WebView node.
var WebViewImpl = (function () {
const WebViewImpl = (function () {
function WebViewImpl (webviewNode) {
var shadowRoot
this.webviewNode = webviewNode
v8Util.setHiddenValue(this.webviewNode, 'internal', this)
this.attached = false
@ -30,7 +29,7 @@ var WebViewImpl = (function () {
// on* Event handlers.
this.on = {}
this.browserPluginNode = this.createBrowserPluginNode()
shadowRoot = this.webviewNode.createShadowRoot()
const shadowRoot = this.webviewNode.createShadowRoot()
shadowRoot.innerHTML = '<!DOCTYPE html><style type="text/css">:host { display: flex; }</style>'
this.setupWebViewAttributes()
this.setupFocusPropagation()
@ -52,7 +51,7 @@ var WebViewImpl = (function () {
WebViewImpl.prototype.createBrowserPluginNode = function () {
// We create BrowserPlugin as a custom element in order to observe changes
// to attributes synchronously.
var browserPluginNode = new WebViewImpl.BrowserPlugin()
const browserPluginNode = new WebViewImpl.BrowserPlugin()
v8Util.setHiddenValue(browserPluginNode, 'internal', this)
return browserPluginNode
}
@ -136,18 +135,16 @@ var WebViewImpl = (function () {
}
WebViewImpl.prototype.onSizeChanged = function (webViewEvent) {
var maxHeight, maxWidth, minHeight, minWidth, newHeight, newWidth, node, width
newWidth = webViewEvent.newWidth
newHeight = webViewEvent.newHeight
node = this.webviewNode
width = node.offsetWidth
const {newHeight, newWidth} = webViewEvent
const node = this.webviewNode
const width = node.offsetWidth
// Check the current bounds to make sure we do not resize <webview>
// outside of current constraints.
maxWidth = this.attributes[webViewConstants.ATTRIBUTE_MAXWIDTH].getValue() | width
maxHeight = this.attributes[webViewConstants.ATTRIBUTE_MAXHEIGHT].getValue() | width
minWidth = this.attributes[webViewConstants.ATTRIBUTE_MINWIDTH].getValue() | width
minHeight = this.attributes[webViewConstants.ATTRIBUTE_MINHEIGHT].getValue() | width
const maxWidth = this.attributes[webViewConstants.ATTRIBUTE_MAXWIDTH].getValue() | width
const maxHeight = this.attributes[webViewConstants.ATTRIBUTE_MAXHEIGHT].getValue() | width
let minWidth = this.attributes[webViewConstants.ATTRIBUTE_MINWIDTH].getValue() | width
let minHeight = this.attributes[webViewConstants.ATTRIBUTE_MINHEIGHT].getValue() | width
minWidth = Math.min(minWidth, maxWidth)
minHeight = Math.min(minHeight, maxHeight)
if (!this.attributes[webViewConstants.ATTRIBUTE_AUTOSIZE].getValue() || (newWidth >= minWidth && newWidth <= maxWidth && newHeight >= minHeight && newHeight <= maxHeight)) {
@ -162,8 +159,7 @@ var WebViewImpl = (function () {
WebViewImpl.prototype.onElementResize = function (newSize) {
// Dispatch the 'resize' event.
var resizeEvent
resizeEvent = new Event('resize', {
const resizeEvent = new Event('resize', {
bubbles: true
})
@ -195,8 +191,7 @@ var WebViewImpl = (function () {
// Adds an 'on<event>' property on the webview, which can be used to set/unset
// an event handler.
WebViewImpl.prototype.setupEventProperty = function (eventName) {
var propertyName
propertyName = 'on' + eventName.toLowerCase()
const propertyName = 'on' + eventName.toLowerCase()
return Object.defineProperty(this.webviewNode, propertyName, {
get: () => {
return this.on[propertyName]
@ -216,14 +211,13 @@ var WebViewImpl = (function () {
// Updates state upon loadcommit.
WebViewImpl.prototype.onLoadCommit = function (webViewEvent) {
var newValue, oldValue
oldValue = this.webviewNode.getAttribute(webViewConstants.ATTRIBUTE_SRC)
newValue = webViewEvent.url
const oldValue = this.webviewNode.getAttribute(webViewConstants.ATTRIBUTE_SRC)
const newValue = webViewEvent.url
if (webViewEvent.isMainFrame && (oldValue !== newValue)) {
// Touching the src attribute triggers a navigation. To avoid
// triggering a page reload on every guest-initiated navigation,
// we do not handle this mutation.
return this.attributes[webViewConstants.ATTRIBUTE_SRC].setValueIgnoreMutation(newValue)
this.attributes[webViewConstants.ATTRIBUTE_SRC].setValueIgnoreMutation(newValue)
}
}
@ -232,17 +226,15 @@ var WebViewImpl = (function () {
}
WebViewImpl.prototype.buildParams = function () {
var attribute, attributeName, css, elementRect, params, ref1
params = {
const params = {
instanceId: this.viewInstanceId,
userAgentOverride: this.userAgentOverride,
zoomFactor: webFrame.getZoomFactor()
}
ref1 = this.attributes
for (attributeName in ref1) {
if (!hasProp.call(ref1, attributeName)) continue
attribute = ref1[attributeName]
params[attributeName] = attribute.getValue()
for (const attributeName in this.attributes) {
if (hasProp.call(this.attributes, attributeName)) {
params[attributeName] = this.attributes[attributeName].getValue()
}
}
// When the WebView is not participating in layout (display:none)
@ -250,8 +242,8 @@ var WebViewImpl = (function () {
// However, in the case where the WebView has a fixed size we can
// use that value to initially size the guest so as to avoid a relayout of
// the on display:block.
css = window.getComputedStyle(this.webviewNode, null)
elementRect = this.webviewNode.getBoundingClientRect()
const css = window.getComputedStyle(this.webviewNode, null)
const elementRect = this.webviewNode.getBoundingClientRect()
params.elementWidth = parseInt(elementRect.width) || parseInt(css.getPropertyValue('width'))
params.elementHeight = parseInt(elementRect.height) || parseInt(css.getPropertyValue('height'))
return params
@ -271,8 +263,8 @@ var WebViewImpl = (function () {
})()
// Registers browser plugin <object> custom element.
var registerBrowserPluginElement = function () {
var proto = Object.create(HTMLObjectElement.prototype)
const registerBrowserPluginElement = function () {
const proto = Object.create(HTMLObjectElement.prototype)
proto.createdCallback = function () {
this.setAttribute('type', 'application/browser-plugin')
this.setAttribute('id', 'browser-plugin-' + getNextId())
@ -298,13 +290,12 @@ var registerBrowserPluginElement = function () {
delete proto.createdCallback
delete proto.attachedCallback
delete proto.detachedCallback
return delete proto.attributeChangedCallback
delete proto.attributeChangedCallback
}
// Registers <webview> custom element.
var registerWebViewElement = function () {
var createBlockHandler, createNonBlockHandler, i, j, len, len1, m, methods, nonblockMethods, proto
proto = Object.create(HTMLObjectElement.prototype)
const proto = Object.create(HTMLObjectElement.prototype)
proto.createdCallback = function () {
return new WebViewImpl(this)
}
@ -345,7 +336,7 @@ var registerWebViewElement = function () {
}
// Public-facing API methods.
methods = [
const methods = [
'getURL',
'loadURL',
'getTitle',
@ -394,7 +385,7 @@ var registerWebViewElement = function () {
'showDefinitionForSelection',
'capturePage'
]
nonblockMethods = [
const nonblockMethods = [
'insertCSS',
'insertText',
'send',
@ -405,7 +396,7 @@ var registerWebViewElement = function () {
]
// Forward proto.foo* method calls to WebViewImpl.foo*.
createBlockHandler = function (m) {
const createBlockHandler = function (m) {
return function (...args) {
const internal = v8Util.getHiddenValue(this, 'internal')
if (internal.webContents) {
@ -415,28 +406,28 @@ var registerWebViewElement = function () {
}
}
}
for (i = 0, len = methods.length; i < len; i++) {
m = methods[i]
proto[m] = createBlockHandler(m)
for (let i = 0, len = methods.length; i < len; i++) {
const method = methods[i]
proto[method] = createBlockHandler(method)
}
createNonBlockHandler = function (m) {
const createNonBlockHandler = function (m) {
return function (...args) {
const internal = v8Util.getHiddenValue(this, 'internal')
ipcRenderer.send.apply(ipcRenderer, ['ELECTRON_BROWSER_ASYNC_CALL_TO_GUEST_VIEW', null, internal.guestInstanceId, m].concat(args))
}
}
for (j = 0, len1 = nonblockMethods.length; j < len1; j++) {
m = nonblockMethods[j]
proto[m] = createNonBlockHandler(m)
for (let j = 0, len1 = nonblockMethods.length; j < len1; j++) {
const method = nonblockMethods[j]
proto[method] = createNonBlockHandler(method)
}
proto.executeJavaScript = function (code, hasUserGesture, callback) {
var internal = v8Util.getHiddenValue(this, 'internal')
const internal = v8Util.getHiddenValue(this, 'internal')
if (typeof hasUserGesture === 'function') {
callback = hasUserGesture
hasUserGesture = false
}
let requestId = getNextId()
const requestId = getNextId()
ipcRenderer.send('ELECTRON_BROWSER_ASYNC_CALL_TO_GUEST_VIEW', requestId, internal.guestInstanceId, 'executeJavaScript', code, hasUserGesture)
ipcRenderer.once(`ELECTRON_RENDERER_ASYNC_CALL_TO_GUEST_VIEW_RESPONSE_${requestId}`, function (event, result) {
if (callback) callback(result)
@ -445,7 +436,7 @@ var registerWebViewElement = function () {
// WebContents associated with this webview.
proto.getWebContents = function () {
var internal = v8Util.getHiddenValue(this, 'internal')
const internal = v8Util.getHiddenValue(this, 'internal')
return internal.webContents
}
@ -461,9 +452,9 @@ var registerWebViewElement = function () {
delete proto.attributeChangedCallback
}
var useCapture = true
const useCapture = true
var listener = function (event) {
const listener = function (event) {
if (document.readyState === 'loading') {
return
}