1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This module implements the base attributes of the GuestView tags.
7 // -----------------------------------------------------------------------------
10 // Default implementation of a GuestView attribute.
11 function Attribute(name
, view
) {
13 this.ignoreMutation
= false;
17 this.defineProperty();
20 // Retrieves and returns the attribute's value.
21 Attribute
.prototype.getValue = function() {
22 return this.view
.element
.getAttribute(this.name
) || '';
25 // Retrieves and returns the attribute's value if it has been dirtied since
26 // the last time this method was called. Returns null otherwise.
27 Attribute
.prototype.getValueIfDirty = function() {
31 return this.getValue();
34 // Sets the attribute's value.
35 Attribute
.prototype.setValue = function(value
) {
36 this.view
.element
.setAttribute(this.name
, value
|| '');
39 // Changes the attribute's value without triggering its mutation handler.
40 Attribute
.prototype.setValueIgnoreMutation = function(value
) {
41 this.ignoreMutation
= true;
43 this.ignoreMutation
= false;
46 // Defines this attribute as a property on the view's element.
47 Attribute
.prototype.defineProperty = function() {
48 $Object
.defineProperty(this.view
.element
, this.name
, {
50 return this.getValue();
52 set: function(value
) {
59 // Called when the attribute's value changes.
60 Attribute
.prototype.maybeHandleMutation = function(oldValue
, newValue
) {
61 if (this.ignoreMutation
)
65 this.handleMutation(oldValue
, newValue
);
68 // Called when a change that isn't ignored occurs to the attribute's value.
69 Attribute
.prototype.handleMutation = function(oldValue
, newValue
) {};
71 // Called when the view's element is attached to the DOM tree.
72 Attribute
.prototype.attach = function() {};
74 // Called when the view's element is detached from the DOM tree.
75 Attribute
.prototype.detach = function() {};
77 // -----------------------------------------------------------------------------
78 // BooleanAttribute object.
80 // An attribute that is treated as a Boolean.
81 function BooleanAttribute(name
, view
) {
82 Attribute
.call(this, name
, view
);
85 BooleanAttribute
.prototype.__proto__
= Attribute
.prototype;
87 BooleanAttribute
.prototype.getValue = function() {
88 return this.view
.element
.hasAttribute(this.name
);
91 BooleanAttribute
.prototype.setValue = function(value
) {
93 this.view
.element
.removeAttribute(this.name
);
95 this.view
.element
.setAttribute(this.name
, '');
99 // -----------------------------------------------------------------------------
100 // IntegerAttribute object.
102 // An attribute that is treated as an integer.
103 function IntegerAttribute(name
, view
) {
104 Attribute
.call(this, name
, view
);
107 IntegerAttribute
.prototype.__proto__
= Attribute
.prototype;
109 IntegerAttribute
.prototype.getValue = function() {
110 return parseInt(this.view
.element
.getAttribute(this.name
)) || 0;
113 IntegerAttribute
.prototype.setValue = function(value
) {
114 this.view
.element
.setAttribute(this.name
, parseInt(value
) || 0);
117 // -----------------------------------------------------------------------------
118 // ReadOnlyAttribute object.
120 // An attribute that cannot be changed (externally). The only way to set it
121 // internally is via |setValueIgnoreMutation|.
122 function ReadOnlyAttribute(name
, view
) {
123 Attribute
.call(this, name
, view
);
126 ReadOnlyAttribute
.prototype.__proto__
= Attribute
.prototype;
128 ReadOnlyAttribute
.prototype.handleMutation = function(oldValue
, newValue
) {
129 this.setValueIgnoreMutation(oldValue
);
132 // -----------------------------------------------------------------------------
134 var GuestViewAttributes
= {
135 Attribute
: Attribute
,
136 BooleanAttribute
: BooleanAttribute
,
137 IntegerAttribute
: IntegerAttribute
,
138 ReadOnlyAttribute
: ReadOnlyAttribute
142 exports
.GuestViewAttributes
= GuestViewAttributes
;