| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 var DocumentNatives = requireNative('document_natives'); | |
| 6 var ExtensionOptionsEvents = | 5 var ExtensionOptionsEvents = |
| 7 require('extensionOptionsEvents').ExtensionOptionsEvents; | 6 require('extensionOptionsEvents').ExtensionOptionsEvents; |
| 8 var GuestView = require('guestView').GuestView; | 7 var GuestView = require('guestView').GuestView; |
| 9 var GuestViewContainer = require('guestViewContainer').GuestViewContainer; | 8 var GuestViewContainer = require('guestViewContainer').GuestViewContainer; |
| 10 var GuestViewInternal = | |
| 11 require('binding').Binding.create('guestViewInternal').generate(); | |
| 12 var IdGenerator = requireNative('id_generator'); | |
| 13 var utils = require('utils'); | |
| 14 | |
| 15 // Mapping of the autosize attribute names to default values | |
| 16 var AUTO_SIZE_ATTRIBUTES = { | |
| 17 'autosize': 'on', | |
| 18 'maxheight': window.innerHeight, | |
| 19 'maxwidth': window.innerWidth, | |
| 20 'minheight': 32, | |
| 21 'minwidth': 32 | |
| 22 }; | |
| 23 | 9 |
| 24 function ExtensionOptionsImpl(extensionoptionsElement) { | 10 function ExtensionOptionsImpl(extensionoptionsElement) { |
| 25 GuestViewContainer.call(this, extensionoptionsElement, 'extensionoptions'); | 11 GuestViewContainer.call(this, extensionoptionsElement, 'extensionoptions'); |
| 26 this.autosizeDeferred = false; | 12 this.autosizeDeferred = false; |
| 27 | 13 |
| 28 new ExtensionOptionsEvents(this); | 14 new ExtensionOptionsEvents(this); |
| 29 this.setupElementProperties(); | 15 this.setupElementProperties(); |
| 30 }; | 16 }; |
| 31 | 17 |
| 32 ExtensionOptionsImpl.prototype.__proto__ = GuestViewContainer.prototype; | 18 ExtensionOptionsImpl.prototype.__proto__ = GuestViewContainer.prototype; |
| 33 | 19 |
| 34 ExtensionOptionsImpl.VIEW_TYPE = 'ExtensionOptions'; | 20 ExtensionOptionsImpl.VIEW_TYPE = 'ExtensionOptions'; |
| 35 | 21 |
| 36 // Add extra functionality to |this.element|. | |
| 37 ExtensionOptionsImpl.setupElement = function(proto) { | |
| 38 var apiMethods = [ | |
| 39 'setDeferAutoSize', | |
| 40 'resumeDeferredAutoSize' | |
| 41 ]; | |
| 42 | |
| 43 // Forward proto.foo* method calls to ExtensionOptionsImpl.foo*. | |
| 44 GuestViewContainer.forwardApiMethods(proto, apiMethods); | |
| 45 } | |
| 46 | |
| 47 ExtensionOptionsImpl.prototype.onElementAttached = function() { | 22 ExtensionOptionsImpl.prototype.onElementAttached = function() { |
| 48 this.createGuest(); | 23 this.createGuest(); |
| 49 } | 24 } |
| 50 | 25 |
| 51 ExtensionOptionsImpl.prototype.buildContainerParams = function() { | 26 ExtensionOptionsImpl.prototype.buildContainerParams = function() { |
| 52 var params = { | 27 return { |
| 53 'autosize': this.element.hasAttribute('autosize'), | |
| 54 'maxheight': parseInt(this.maxheight || 0), | |
| 55 'maxwidth': parseInt(this.maxwidth || 0), | |
| 56 'minheight': parseInt(this.minheight || 0), | |
| 57 'minwidth': parseInt(this.minwidth || 0), | |
| 58 'extensionId': this.element.getAttribute('extension') | 28 'extensionId': this.element.getAttribute('extension') |
| 59 }; | 29 }; |
| 60 return params; | |
| 61 }; | 30 }; |
| 62 | 31 |
| 63 ExtensionOptionsImpl.prototype.createGuest = function() { | 32 ExtensionOptionsImpl.prototype.createGuest = function() { |
| 64 if (!this.elementAttached) { | 33 if (!this.elementAttached) { |
| 65 return; | 34 return; |
| 66 } | 35 } |
| 67 | 36 |
| 68 // Destroy the old guest if one exists. | 37 // Destroy the old guest if one exists. |
| 69 this.guest.destroy(); | 38 this.guest.destroy(); |
| 70 | 39 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 85 // We treat null attribute (attribute removed) and the empty string as | 54 // We treat null attribute (attribute removed) and the empty string as |
| 86 // one case. | 55 // one case. |
| 87 oldValue = oldValue || ''; | 56 oldValue = oldValue || ''; |
| 88 newValue = newValue || ''; | 57 newValue = newValue || ''; |
| 89 | 58 |
| 90 if (oldValue === newValue) | 59 if (oldValue === newValue) |
| 91 return; | 60 return; |
| 92 | 61 |
| 93 if (name == 'extension') { | 62 if (name == 'extension') { |
| 94 this.createGuest(); | 63 this.createGuest(); |
| 95 } else if (AUTO_SIZE_ATTRIBUTES.hasOwnProperty(name) > -1) { | |
| 96 this[name] = newValue; | |
| 97 this.resetSizeConstraintsIfInvalid(); | |
| 98 | |
| 99 if (!this.guest.getId()) | |
| 100 return; | |
| 101 | |
| 102 this.guest.setSize({ | |
| 103 'enableAutoSize': this.element.hasAttribute('autosize'), | |
| 104 'min': { | |
| 105 'width': parseInt(this.minwidth || 0), | |
| 106 'height': parseInt(this.minheight || 0) | |
| 107 }, | |
| 108 'max': { | |
| 109 'width': parseInt(this.maxwidth || 0), | |
| 110 'height': parseInt(this.maxheight || 0) | |
| 111 } | |
| 112 }); | |
| 113 } | 64 } |
| 114 }; | 65 }; |
| 115 | 66 |
| 116 ExtensionOptionsImpl.prototype.onSizeChanged = | |
| 117 function(newWidth, newHeight, oldWidth, oldHeight) { | |
| 118 if (this.autosizeDeferred) { | |
| 119 this.deferredAutoSizeState = { | |
| 120 newWidth: newWidth, | |
| 121 newHeight: newHeight, | |
| 122 oldWidth: oldWidth, | |
| 123 oldHeight: oldHeight | |
| 124 }; | |
| 125 } else { | |
| 126 this.resize(newWidth, newHeight, oldWidth, oldHeight); | |
| 127 } | |
| 128 }; | |
| 129 | |
| 130 ExtensionOptionsImpl.prototype.resize = | |
| 131 function(newWidth, newHeight, oldWidth, oldHeight) { | |
| 132 this.element.style.width = newWidth + 'px'; | |
| 133 this.element.style.height = newHeight + 'px'; | |
| 134 | |
| 135 // Do not allow the options page's dimensions to shrink. This ensures that the | |
| 136 // options page has a consistent UI. If the new size is larger than the | |
| 137 // minimum, make that the new minimum size. | |
| 138 if (newWidth > this.minwidth) | |
| 139 this.minwidth = newWidth; | |
| 140 if (newHeight > this.minheight) | |
| 141 this.minheight = newHeight; | |
| 142 | |
| 143 this.guest.setSize({ | |
| 144 'enableAutoSize': this.element.hasAttribute('autosize'), | |
| 145 'min': { | |
| 146 'width': parseInt(this.minwidth || 0), | |
| 147 'height': parseInt(this.minheight || 0) | |
| 148 }, | |
| 149 'max': { | |
| 150 'width': parseInt(this.maxwidth || 0), | |
| 151 'height': parseInt(this.maxheight || 0) | |
| 152 } | |
| 153 }); | |
| 154 }; | |
| 155 | |
| 156 ExtensionOptionsImpl.prototype.setupElementProperties = function() { | 67 ExtensionOptionsImpl.prototype.setupElementProperties = function() { |
| 157 utils.forEach(AUTO_SIZE_ATTRIBUTES, function(attributeName) { | 68 $Object.defineProperty(this.element, 'extension', { |
| 158 // Get the size constraints from the <extensionoptions> tag, or use the | |
| 159 // defaults if not specified | |
| 160 if (this.element.hasAttribute(attributeName)) { | |
| 161 this[attributeName] = | |
| 162 this.element.getAttribute(attributeName); | |
| 163 } else { | |
| 164 this[attributeName] = AUTO_SIZE_ATTRIBUTES[attributeName]; | |
| 165 } | |
| 166 | |
| 167 Object.defineProperty(this.element, attributeName, { | |
| 168 get: function() { | |
| 169 return this[attributeName]; | |
| 170 }.bind(this), | |
| 171 set: function(value) { | |
| 172 this.element.setAttribute(attributeName, value); | |
| 173 }.bind(this), | |
| 174 enumerable: true | |
| 175 }); | |
| 176 }, this); | |
| 177 | |
| 178 this.resetSizeConstraintsIfInvalid(); | |
| 179 | |
| 180 Object.defineProperty(this.element, 'extension', { | |
| 181 get: function() { | 69 get: function() { |
| 182 return this.element.getAttribute('extension'); | 70 return this.element.getAttribute('extension'); |
| 183 }.bind(this), | 71 }.bind(this), |
| 184 set: function(value) { | 72 set: function(value) { |
| 185 this.element.setAttribute('extension', value); | 73 this.element.setAttribute('extension', value); |
| 186 }.bind(this), | 74 }.bind(this), |
| 187 enumerable: true | 75 enumerable: true |
| 188 }); | 76 }); |
| 189 }; | 77 }; |
| 190 | 78 |
| 191 ExtensionOptionsImpl.prototype.resetSizeConstraintsIfInvalid = function () { | |
| 192 if (this.minheight > this.maxheight || this.minheight < 0) { | |
| 193 this.minheight = AUTO_SIZE_ATTRIBUTES.minheight; | |
| 194 this.maxheight = AUTO_SIZE_ATTRIBUTES.maxheight; | |
| 195 } | |
| 196 if (this.minwidth > this.maxwidth || this.minwidth < 0) { | |
| 197 this.minwidth = AUTO_SIZE_ATTRIBUTES.minwidth; | |
| 198 this.maxwidth = AUTO_SIZE_ATTRIBUTES.maxwidth; | |
| 199 } | |
| 200 }; | |
| 201 | |
| 202 /** | |
| 203 * Toggles whether the element should automatically resize to its preferred | |
| 204 * size. If set to true, when the element receives new autosize dimensions, | |
| 205 * it passes them to the embedder in a sizechanged event, but does not resize | |
| 206 * itself to those dimensions until the embedder calls resumeDeferredAutoSize. | |
| 207 * This allows the embedder to defer the resizing until it is ready. | |
| 208 * When set to false, the element resizes whenever it receives new autosize | |
| 209 * dimensions. | |
| 210 */ | |
| 211 ExtensionOptionsImpl.prototype.setDeferAutoSize = function(value) { | |
| 212 if (!value) | |
| 213 resumeDeferredAutoSize(); | |
| 214 this.autosizeDeferred = value; | |
| 215 }; | |
| 216 | |
| 217 /** | |
| 218 * Allows the element to resize to most recent set of autosize dimensions if | |
| 219 * autosizing is being deferred. | |
| 220 */ | |
| 221 ExtensionOptionsImpl.prototype.resumeDeferredAutoSize = function() { | |
| 222 if (this.autosizeDeferred) { | |
| 223 this.resize(this.deferredAutoSizeState.newWidth, | |
| 224 this.deferredAutoSizeState.newHeight, | |
| 225 this.deferredAutoSizeState.oldWidth, | |
| 226 this.deferredAutoSizeState.oldHeight); | |
| 227 } | |
| 228 }; | |
| 229 | |
| 230 GuestViewContainer.registerElement(ExtensionOptionsImpl); | 79 GuestViewContainer.registerElement(ExtensionOptionsImpl); |
| OLD | NEW |