2014-12-09 00:14:12 +00:00
|
|
|
WebView = require './web-view'
|
|
|
|
webViewConstants = require './web-view-constants'
|
|
|
|
|
|
|
|
# Attribute objects.
|
|
|
|
# Default implementation of a WebView attribute.
|
|
|
|
class WebViewAttribute
|
|
|
|
constructor: (name, webViewImpl) ->
|
|
|
|
@name = name
|
|
|
|
@webViewImpl = webViewImpl
|
2014-12-09 00:56:14 +00:00
|
|
|
@ignoreNextMutation = false
|
2014-12-09 00:14:12 +00:00
|
|
|
|
2014-12-09 00:56:14 +00:00
|
|
|
# Retrieves and returns the attribute's value.
|
|
|
|
getValue: -> @webViewImpl.webviewNode.getAttribute(@name) || ''
|
2014-12-09 00:14:12 +00:00
|
|
|
|
2014-12-09 00:56:14 +00:00
|
|
|
# Sets the attribute's value.
|
|
|
|
setValue: (value) -> @webViewImpl.webviewNode.setAttribute(@name, value || '')
|
|
|
|
|
2014-12-09 01:05:57 +00:00
|
|
|
# Defines this attribute as a property on the webview node.
|
|
|
|
define: ->
|
|
|
|
Object.defineProperty @webViewImpl.webviewNode, @name,
|
|
|
|
get: => @getValue()
|
|
|
|
set: (value) => @setValue value
|
|
|
|
enumerable: true
|
|
|
|
|
2014-12-09 00:56:14 +00:00
|
|
|
# Called when the attribute's value changes.
|
|
|
|
handleMutation: ->
|
|
|
|
|
2014-12-09 01:05:57 +00:00
|
|
|
# An attribute that is treated as a Boolean.
|
2014-12-09 00:56:14 +00:00
|
|
|
class BooleanAttribute extends WebViewAttribute
|
|
|
|
constructor: (name, webViewImpl) ->
|
|
|
|
super name, webViewImpl
|
|
|
|
|
|
|
|
getValue: ->
|
|
|
|
@webViewImpl.webviewNode.hasAttribute @name
|
|
|
|
|
|
|
|
setValue: (value) ->
|
|
|
|
unless value
|
|
|
|
@webViewImpl.webviewNode.removeAttribute @name
|
|
|
|
else
|
|
|
|
@webViewImpl.webviewNode.setAttribute @name, ''
|
2014-12-09 00:14:12 +00:00
|
|
|
|
|
|
|
# Attribute representing the state of the storage partition.
|
|
|
|
class Partition extends WebViewAttribute
|
|
|
|
constructor: (webViewImpl) ->
|
|
|
|
super webViewConstants.ATTRIBUTE_PARTITION, webViewImpl
|
|
|
|
@validPartitionId = true
|
|
|
|
|
2014-12-09 00:56:14 +00:00
|
|
|
handleMutation: (oldValue, newValue) ->
|
|
|
|
newValue = newValue || ''
|
2014-12-09 00:14:12 +00:00
|
|
|
|
2014-12-09 00:56:14 +00:00
|
|
|
# The partition cannot change if the webview has already navigated.
|
|
|
|
unless @webViewImpl.beforeFirstNavigation
|
|
|
|
window.console.error webViewConstants.ERROR_MSG_ALREADY_NAVIGATED
|
|
|
|
@ignoreNextMutation = true
|
|
|
|
@webViewImpl.webviewNode.setAttribute @name, oldValue
|
|
|
|
return
|
|
|
|
|
|
|
|
if newValue is 'persist:'
|
|
|
|
@validPartitionId = false
|
|
|
|
window.console.error webViewConstants.ERROR_MSG_INVALID_PARTITION_ATTRIBUTE
|
2014-12-09 00:14:12 +00:00
|
|
|
|
|
|
|
# Sets up all of the webview attributes.
|
|
|
|
WebView::setupWebViewAttributes = ->
|
|
|
|
@attributes = {}
|
|
|
|
|
|
|
|
# Initialize the attributes with special behavior (and custom attribute
|
|
|
|
# objects).
|
2014-12-09 00:56:14 +00:00
|
|
|
@attributes[webViewConstants.ATTRIBUTE_ALLOWTRANSPARENCY] =
|
|
|
|
new BooleanAttribute(webViewConstants.ATTRIBUTE_ALLOWTRANSPARENCY, this)
|
|
|
|
@attributes[webViewConstants.ATTRIBUTE_AUTOSIZE] =
|
|
|
|
new BooleanAttribute(webViewConstants.ATTRIBUTE_AUTOSIZE, this)
|
2014-12-09 00:14:12 +00:00
|
|
|
@attributes[webViewConstants.ATTRIBUTE_PARTITION] = new Partition(this)
|
|
|
|
|
|
|
|
# Initialize the remaining attributes, which have default behavior.
|
|
|
|
defaultAttributes = [
|
|
|
|
webViewConstants.ATTRIBUTE_MAXHEIGHT
|
|
|
|
webViewConstants.ATTRIBUTE_MAXWIDTH
|
|
|
|
webViewConstants.ATTRIBUTE_MINHEIGHT
|
|
|
|
webViewConstants.ATTRIBUTE_MINWIDTH
|
|
|
|
webViewConstants.ATTRIBUTE_SRC
|
|
|
|
webViewConstants.ATTRIBUTE_HTTPREFERRER
|
|
|
|
]
|
|
|
|
|
|
|
|
for attribute in defaultAttributes
|
|
|
|
@attributes[attribute] = new WebViewAttribute(attribute, this)
|