| Index: extensions/renderer/resources/guest_view/guest_view_attributes.js
|
| diff --git a/extensions/renderer/resources/guest_view/guest_view_attributes.js b/extensions/renderer/resources/guest_view/guest_view_attributes.js
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..a4c42b69b32a67c1437ff94f78d6e93843a70451
|
| --- /dev/null
|
| +++ b/extensions/renderer/resources/guest_view/guest_view_attributes.js
|
| @@ -0,0 +1,131 @@
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +// This module implements the base attributes of the GuestView tags.
|
| +
|
| +// -----------------------------------------------------------------------------
|
| +// Attribute object.
|
| +
|
| +// Default implementation of a GuestView attribute.
|
| +function Attribute(name, view) {
|
| + this.name = name;
|
| + this.view = view;
|
| + this.ignoreMutation = false;
|
| +
|
| + this.defineProperty();
|
| +}
|
| +
|
| +// Retrieves and returns the attribute's value.
|
| +Attribute.prototype.getValue = function() {
|
| + return this.view.element.getAttribute(this.name) || '';
|
| +};
|
| +
|
| +// Sets the attribute's value.
|
| +Attribute.prototype.setValue = function(value) {
|
| + this.view.element.setAttribute(this.name, value || '');
|
| +};
|
| +
|
| +// Changes the attribute's value without triggering its mutation handler.
|
| +Attribute.prototype.setValueIgnoreMutation = function(value) {
|
| + this.ignoreMutation = true;
|
| + this.setValue(value);
|
| + this.ignoreMutation = false;
|
| +};
|
| +
|
| +// Defines this attribute as a property on the view's element.
|
| +Attribute.prototype.defineProperty = function() {
|
| + $Object.defineProperty(this.view.element, this.name, {
|
| + get: function() {
|
| + return this.getValue();
|
| + }.bind(this),
|
| + set: function(value) {
|
| + this.setValue(value);
|
| + }.bind(this),
|
| + enumerable: true
|
| + });
|
| +};
|
| +
|
| +// Called when the attribute's value changes.
|
| +Attribute.prototype.maybeHandleMutation = function(oldValue, newValue) {
|
| + if (this.ignoreMutation)
|
| + return;
|
| +
|
| + this.handleMutation(oldValue, newValue);
|
| +};
|
| +
|
| +// Called when a change that isn't ignored occurs to the attribute's value.
|
| +Attribute.prototype.handleMutation = function(oldValue, newValue) {};
|
| +
|
| +// Called when the view's element is attached to the DOM tree.
|
| +Attribute.prototype.attach = function() {};
|
| +
|
| +// Called when the view's element is detached from the DOM tree.
|
| +Attribute.prototype.detach = function() {};
|
| +
|
| +// -----------------------------------------------------------------------------
|
| +// BooleanAttribute object.
|
| +
|
| +// An attribute that is treated as a Boolean.
|
| +function BooleanAttribute(name, view) {
|
| + Attribute.call(this, name, view);
|
| +}
|
| +
|
| +BooleanAttribute.prototype.__proto__ = Attribute.prototype;
|
| +
|
| +BooleanAttribute.prototype.getValue = function() {
|
| + return this.view.element.hasAttribute(this.name);
|
| +};
|
| +
|
| +BooleanAttribute.prototype.setValue = function(value) {
|
| + if (!value) {
|
| + this.view.element.removeAttribute(this.name);
|
| + } else {
|
| + this.view.element.setAttribute(this.name, '');
|
| + }
|
| +};
|
| +
|
| +// -----------------------------------------------------------------------------
|
| +// IntegerAttribute object.
|
| +
|
| +// An attribute that is treated as an integer.
|
| +function IntegerAttribute(name, view) {
|
| + Attribute.call(this, name, view);
|
| +}
|
| +
|
| +IntegerAttribute.prototype.__proto__ = Attribute.prototype;
|
| +
|
| +IntegerAttribute.prototype.getValue = function() {
|
| + return parseInt(this.view.element.getAttribute(this.name)) || 0;
|
| +};
|
| +
|
| +IntegerAttribute.prototype.setValue = function(value) {
|
| + this.view.element.setAttribute(this.name, parseInt(value) || 0);
|
| +};
|
| +
|
| +// -----------------------------------------------------------------------------
|
| +// ReadOnlyAttribute object.
|
| +
|
| +// An attribute that cannot be changed (externally). The only way to set it
|
| +// internally is via |setValueIgnoreMutation|.
|
| +function ReadOnlyAttribute(name, view) {
|
| + Attribute.call(this, name, view);
|
| +}
|
| +
|
| +ReadOnlyAttribute.prototype.__proto__ = Attribute.prototype;
|
| +
|
| +ReadOnlyAttribute.prototype.handleMutation = function(oldValue, newValue) {
|
| + this.setValueIgnoreMutation(oldValue);
|
| +}
|
| +
|
| +// -----------------------------------------------------------------------------
|
| +
|
| +var GuestViewAttributes = {
|
| + Attribute: Attribute,
|
| + BooleanAttribute: BooleanAttribute,
|
| + IntegerAttribute: IntegerAttribute,
|
| + ReadOnlyAttribute: ReadOnlyAttribute
|
| +};
|
| +
|
| +// Exports.
|
| +exports.GuestViewAttributes = GuestViewAttributes;
|
|
|