1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
// 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.dirty = false;
this.ignoreMutation = false;
this.name = name;
this.view = view;
this.defineProperty();
}
// Retrieves and returns the attribute's value.
Attribute.prototype.getValue = function() {
return this.view.element.getAttribute(this.name) || '';
};
// Retrieves and returns the attribute's value if it has been dirtied since
// the last time this method was called. Returns null otherwise.
Attribute.prototype.getValueIfDirty = function() {
if (!this.dirty)
return null;
this.dirty = false;
return this.getValue();
};
// 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.dirty = true;
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.$set('GuestViewAttributes', GuestViewAttributes);
|