Got rid of the internal copies of webview attributes

Imported from:
abb035a09b%5E%21/
This commit is contained in:
Cheng Zhao 2014-12-08 16:56:14 -08:00
parent d7eae69587
commit 2c27b953b5
2 changed files with 75 additions and 91 deletions

View file

@ -6,46 +6,52 @@ webViewConstants = require './web-view-constants'
class WebViewAttribute
constructor: (name, webViewImpl) ->
@name = name
@value = ''
@webViewImpl = webViewImpl
@ignoreNextMutation = false
getValue: -> @value || ''
# Retrieves and returns the attribute's value.
getValue: -> @webViewImpl.webviewNode.getAttribute(@name) || ''
setValue: (value) -> @value = value
# Sets the attribute's value.
setValue: (value) -> @webViewImpl.webviewNode.setAttribute(@name, value || '')
# Called when the attribute's value changes.
handleMutation: ->
# Attribute specifying whether transparency is allowed in the webview.
class BooleanAttribute extends WebViewAttribute
constructor: (name, webViewImpl) ->
super name, webViewImpl
getValue: ->
# This attribute is treated as a boolean, and is retrieved as such.
@webViewImpl.webviewNode.hasAttribute @name
setValue: (value) ->
unless value
@webViewImpl.webviewNode.removeAttribute @name
else
@webViewImpl.webviewNode.setAttribute @name, ''
# Attribute representing the state of the storage partition.
class Partition extends WebViewAttribute
constructor: (webViewImpl) ->
super webViewConstants.ATTRIBUTE_PARTITION, webViewImpl
@validPartitionId = true
@persistStorage = false
@storagePartitionId = ''
@webViewImpl = webViewImpl
getValue: ->
return '' unless @validPartitionId
(if @persistStorage then 'persist:' else '') + @storagePartitionId
handleMutation: (oldValue, newValue) ->
newValue = newValue || ''
setValue: (value) ->
result = {}
hasNavigated = !@webViewImpl.beforeFirstNavigation
if hasNavigated
result.error = webViewConstants.ERROR_MSG_ALREADY_NAVIGATED
return result
value = '' unless value
LEN = 'persist:'.length
if value.substr(0, LEN) == 'persist:'
value = value.substr LEN
unless value
@validPartitionId = false
result.error = webViewConstants.ERROR_MSG_INVALID_PARTITION_ATTRIBUTE
return result
@persistStorage = true
else
@persistStorage = false
@storagePartitionId = value
result
# 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
# Sets up all of the webview attributes.
WebView::setupWebViewAttributes = ->
@ -53,12 +59,14 @@ WebView::setupWebViewAttributes = ->
# Initialize the attributes with special behavior (and custom attribute
# objects).
@attributes[webViewConstants.ATTRIBUTE_ALLOWTRANSPARENCY] =
new BooleanAttribute(webViewConstants.ATTRIBUTE_ALLOWTRANSPARENCY, this)
@attributes[webViewConstants.ATTRIBUTE_AUTOSIZE] =
new BooleanAttribute(webViewConstants.ATTRIBUTE_AUTOSIZE, this)
@attributes[webViewConstants.ATTRIBUTE_PARTITION] = new Partition(this)
# Initialize the remaining attributes, which have default behavior.
defaultAttributes = [
webViewConstants.ATTRIBUTE_ALLOWTRANSPARENCY
webViewConstants.ATTRIBUTE_AUTOSIZE
webViewConstants.ATTRIBUTE_MAXHEIGHT
webViewConstants.ATTRIBUTE_MAXWIDTH
webViewConstants.ATTRIBUTE_MINHEIGHT