| 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 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 installClass('HTMLMarqueeElement', function(HTMLMarqueeElementPrototype) { | 7 installClass('HTMLMarqueeElement', function(HTMLMarqueeElementPrototype) { |
| 8 | 8 |
| 9 var kDefaultScrollAmount = 6; | 9 var kDefaultScrollAmount = 6; |
| 10 var kDefaultScrollDelayMS = 85; | 10 var kDefaultScrollDelayMS = 85; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 this.setAttribute(attributeName, value); | 63 this.setAttribute(attributeName, value); |
| 64 }, | 64 }, |
| 65 configurable: true, | 65 configurable: true, |
| 66 enumerable: true, | 66 enumerable: true, |
| 67 }); | 67 }); |
| 68 } | 68 } |
| 69 | 69 |
| 70 function reflectBooleanAttribute(prototype, attributeName, propertyName) { | 70 function reflectBooleanAttribute(prototype, attributeName, propertyName) { |
| 71 Object.defineProperty(prototype, propertyName, { | 71 Object.defineProperty(prototype, propertyName, { |
| 72 get: function() { | 72 get: function() { |
| 73 return this.hasAttribute(attributeName); | 73 return this.getAttribute(attributeName); |
| 74 }, | 74 }, |
| 75 set: function(value) { | 75 set: function(value) { |
| 76 if (value) | 76 if (value) |
| 77 this.setAttribute(attributeName, ''); | 77 this.setAttribute(attributeName, ''); |
| 78 else | 78 else |
| 79 this.removeAttribute(attributeName); | 79 this.removeAttribute(attributeName); |
| 80 }, | 80 }, |
| 81 }); | 81 }); |
| 82 } | 82 } |
| 83 | 83 |
| 84 function defineInlineEventHandler(prototype, eventName) { | |
| 85 var propertyName = 'on' + eventName; | |
| 86 // FIXME: We should use symbols here instead. | |
| 87 var functionPropertyName = propertyName + 'Function_'; | |
| 88 var eventHandlerPropertyName = propertyName + 'EventHandler_'; | |
| 89 Object.defineProperty(prototype, propertyName, { | |
| 90 get: function() { | |
| 91 var func = this[functionPropertyName]; | |
| 92 return func || null; | |
| 93 }, | |
| 94 set: function(value) { | |
| 95 var oldEventHandler = this[eventHandlerPropertyName]; | |
| 96 if (oldEventHandler) | |
| 97 this.removeEventListener(eventName, oldEventHandler); | |
| 98 // Notice that we wrap |value| in an anonymous function so that
the | |
| 99 // author can't call removeEventListener themselves to unregiste
r the | |
| 100 // inline event handler. | |
| 101 var newEventHandler = value ? function() { value.apply(this, arg
uments) } : null; | |
| 102 if (newEventHandler) | |
| 103 this.addEventListener(eventName, newEventHandler); | |
| 104 this[functionPropertyName] = value; | |
| 105 this[eventHandlerPropertyName] = newEventHandler; | |
| 106 }, | |
| 107 }); | |
| 108 } | |
| 109 | |
| 110 reflectAttribute(HTMLMarqueeElementPrototype, 'behavior', 'behavior'); | 84 reflectAttribute(HTMLMarqueeElementPrototype, 'behavior', 'behavior'); |
| 111 reflectAttribute(HTMLMarqueeElementPrototype, 'bgcolor', 'bgColor'); | 85 reflectAttribute(HTMLMarqueeElementPrototype, 'bgcolor', 'bgColor'); |
| 112 reflectAttribute(HTMLMarqueeElementPrototype, 'direction', 'direction'); | 86 reflectAttribute(HTMLMarqueeElementPrototype, 'direction', 'direction'); |
| 113 reflectAttribute(HTMLMarqueeElementPrototype, 'height', 'height'); | 87 reflectAttribute(HTMLMarqueeElementPrototype, 'height', 'height'); |
| 114 reflectAttribute(HTMLMarqueeElementPrototype, 'hspace', 'hspace'); | 88 reflectAttribute(HTMLMarqueeElementPrototype, 'hspace', 'hspace'); |
| 115 reflectAttribute(HTMLMarqueeElementPrototype, 'vspace', 'vspace'); | 89 reflectAttribute(HTMLMarqueeElementPrototype, 'vspace', 'vspace'); |
| 116 reflectAttribute(HTMLMarqueeElementPrototype, 'width', 'width'); | 90 reflectAttribute(HTMLMarqueeElementPrototype, 'width', 'width'); |
| 117 reflectBooleanAttribute(HTMLMarqueeElementPrototype, 'truespeed', 'trueSpeed
'); | 91 reflectBooleanAttribute(HTMLMarqueeElementPrototype, 'truespeed', 'trueSpeed
'); |
| 118 | 92 |
| 119 defineInlineEventHandler(HTMLMarqueeElementPrototype, 'start'); | |
| 120 defineInlineEventHandler(HTMLMarqueeElementPrototype, 'finish'); | |
| 121 defineInlineEventHandler(HTMLMarqueeElementPrototype, 'bounce'); | |
| 122 | |
| 123 HTMLMarqueeElementPrototype.createdCallback = function() { | 93 HTMLMarqueeElementPrototype.createdCallback = function() { |
| 124 var shadow = this.createShadowRoot(); | 94 var shadow = this.createShadowRoot(); |
| 125 var style = document.createElement('style'); | 95 var style = document.createElement('style'); |
| 126 style.textContent = ':host { display: inline-block; width: -webkit-fill-avai
lable; overflow: hidden; text-align: initial; }' + | 96 style.textContent = ':host { display: inline-block; width: -webkit-fill-
available; overflow: hidden; text-align: initial; }' |
| 127 ':host([direction="up"]), :host([direction="down"]) { height: 200px;
}'; | 97 + ':host([direction="up"]), :host([direction="down"]) { overflow: in
itial; overflow-y: hidden; }'; |
| 128 shadow.appendChild(style); | 98 shadow.appendChild(style); |
| 129 | 99 |
| 130 var mover = document.createElement('div'); | 100 var mover = document.createElement('div'); |
| 131 shadow.appendChild(mover); | 101 shadow.appendChild(mover); |
| 132 | 102 |
| 133 mover.appendChild(document.createElement('content')); | 103 mover.appendChild(document.createElement('content')); |
| 134 | 104 |
| 135 this.loopCount_ = 0; | 105 this.loopCount_ = 0; |
| 136 this.mover_ = mover; | 106 this.mover_ = mover; |
| 137 this.player_ = null; | 107 this.player_ = null; |
| 138 this.continueCallback_ = null; | 108 this.continueCallback_ = null; |
| 139 | |
| 140 for (var i = 0; i < kPresentationalAttributes.length; ++i) | |
| 141 this.initializeAttribute_(kPresentationalAttributes[i]); | |
| 142 }; | 109 }; |
| 143 | 110 |
| 144 HTMLMarqueeElementPrototype.attachedCallback = function() { | 111 HTMLMarqueeElementPrototype.attachedCallback = function() { |
| 112 for (var i = 0; i < kPresentationalAttributes.length; ++i) { |
| 113 this.initializeAttribute_(kPresentationalAttributes[i]); |
| 114 } |
| 115 |
| 145 this.start(); | 116 this.start(); |
| 146 }; | 117 }; |
| 147 | 118 |
| 148 HTMLMarqueeElementPrototype.detachedCallback = function() { | 119 HTMLMarqueeElementPrototype.detachedCallback = function() { |
| 149 this.stop(); | 120 this.stop(); |
| 150 }; | 121 }; |
| 151 | 122 |
| 152 HTMLMarqueeElementPrototype.attributeChangedCallback = function(name, oldVal
ue, newValue) { | 123 HTMLMarqueeElementPrototype.attributeChangedCallback = function(name, oldVal
ue, newValue) { |
| 153 switch (name) { | 124 switch (name) { |
| 154 case 'bgcolor': | 125 case 'bgcolor': |
| (...skipping 10 matching lines...) Expand all Loading... |
| 165 case 'vspace': | 136 case 'vspace': |
| 166 var margin = convertHTMLLengthToCSSLength(newValue); | 137 var margin = convertHTMLLengthToCSSLength(newValue); |
| 167 this.style.marginTop = margin; | 138 this.style.marginTop = margin; |
| 168 this.style.marginBottom = margin; | 139 this.style.marginBottom = margin; |
| 169 break; | 140 break; |
| 170 case 'width': | 141 case 'width': |
| 171 this.style.width = convertHTMLLengthToCSSLength(newValue); | 142 this.style.width = convertHTMLLengthToCSSLength(newValue); |
| 172 break; | 143 break; |
| 173 case 'behavior': | 144 case 'behavior': |
| 174 case 'direction': | 145 case 'direction': |
| 146 case 'loop': |
| 147 case 'scrollAmount': |
| 148 case 'scrollDelay': |
| 149 case 'trueSpeed': |
| 150 // FIXME: Not implemented. |
| 175 this.stop(); | 151 this.stop(); |
| 176 this.loopCount_ = 0; | |
| 177 this.start(); | 152 this.start(); |
| 178 break; | 153 break; |
| 179 } | 154 } |
| 180 }; | 155 }; |
| 181 | 156 |
| 182 HTMLMarqueeElementPrototype.initializeAttribute_ = function(name) { | 157 HTMLMarqueeElementPrototype.initializeAttribute_ = function(name) { |
| 183 var value = this.getAttribute(name); | 158 var value = this.getAttribute(name); |
| 184 if (value === null) | 159 if (value === null) |
| 185 return; | 160 return; |
| 186 this.attributeChangedCallback(name, null, value); | 161 this.attributeChangedCallback(name, null, value); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 return loop; | 200 return loop; |
| 226 }, | 201 }, |
| 227 set: function(value) { | 202 set: function(value) { |
| 228 if (value <= 0 && value != -1) | 203 if (value <= 0 && value != -1) |
| 229 throwException(PrivateScriptDOMException.IndexSizeError, "The pr
ovided value (" + value + ") is neither positive nor -1."); | 204 throwException(PrivateScriptDOMException.IndexSizeError, "The pr
ovided value (" + value + ") is neither positive nor -1."); |
| 230 this.setAttribute('loop', value); | 205 this.setAttribute('loop', value); |
| 231 }, | 206 }, |
| 232 }); | 207 }); |
| 233 | 208 |
| 234 HTMLMarqueeElementPrototype.getGetMetrics_ = function() { | 209 HTMLMarqueeElementPrototype.getGetMetrics_ = function() { |
| 235 this.mover_.style.width = '-webkit-max-content'; | 210 if (this.direction === 'up' || this.direction === 'down') |
| 211 this.mover_.style.height = '-webkit-max-content'; |
| 212 else |
| 213 this.mover_.style.width = '-webkit-max-content'; |
| 236 | 214 |
| 237 var moverStyle = getComputedStyle(this.mover_); | 215 var moverStyle = getComputedStyle(this.mover_); |
| 238 var marqueeStyle = getComputedStyle(this); | 216 var marqueeStyle = getComputedStyle(this); |
| 239 | 217 |
| 240 var metrics = {}; | 218 var metrics = {}; |
| 241 metrics.contentWidth = parseInt(moverStyle.width); | 219 metrics.contentWidth = parseInt(moverStyle.width); |
| 242 metrics.contentHeight = parseInt(moverStyle.height); | 220 metrics.contentHeight = parseInt(moverStyle.height); |
| 243 metrics.marqueeWidth = parseInt(marqueeStyle.width); | 221 metrics.marqueeWidth = parseInt(marqueeStyle.width); |
| 244 metrics.marqueeHeight = parseInt(marqueeStyle.height); | 222 metrics.marqueeHeight = parseInt(marqueeStyle.height); |
| 245 | 223 |
| 246 this.mover_.style.width = ''; | 224 if (this.direction === 'up' || this.direction === 'down') |
| 225 this.mover_.style.height = ''; |
| 226 else |
| 227 this.mover_.style.width = ''; |
| 247 return metrics; | 228 return metrics; |
| 248 }; | 229 }; |
| 249 | 230 |
| 250 HTMLMarqueeElementPrototype.getAnimationParameters_ = function() { | 231 HTMLMarqueeElementPrototype.getAnimationParameters_ = function() { |
| 251 var metrics = this.getGetMetrics_(); | 232 var metrics = this.getGetMetrics_(); |
| 252 | 233 |
| 253 var totalWidth = metrics.marqueeWidth + metrics.contentWidth; | 234 var totalWidth = metrics.marqueeWidth + metrics.contentWidth; |
| 254 var totalHeight = metrics.marqueeHeight + metrics.contentHeight; | 235 var totalHeight = metrics.marqueeHeight + metrics.contentHeight; |
| 255 | 236 |
| 256 var innerWidth = metrics.marqueeWidth - metrics.contentWidth; | 237 var innerWidth = metrics.marqueeWidth - metrics.contentWidth; |
| 257 var innerHeight = metrics.marqueeHeight - metrics.contentHeight; | 238 var innerHeight = metrics.marqueeHeight - metrics.contentHeight; |
| 258 | 239 |
| 259 var parameters = {}; | 240 var parameters = {}; |
| 260 | 241 |
| 261 switch (this.behavior) { | 242 switch (this.behavior) { |
| 262 case kBehaviorScroll: | 243 case kBehaviorScroll: |
| 263 default: | 244 default: |
| 264 switch (this.direction) { | 245 switch (this.direction) { |
| 265 case kDirectionLeft: | 246 case kDirectionLeft: |
| 266 default: | 247 default: |
| 267 parameters.transformBegin = 'translateX(' + metrics.marqueeWidth
+ 'px)'; | 248 parameters.transformBegin = 'translateX(' + metrics.marqueeWidth
+ 'px)'; |
| 268 parameters.transformEnd = 'translateX(-100%)'; | 249 parameters.transformEnd = 'translateX(-' + metrics.contentWidth
+ 'px)'; |
| 269 parameters.distance = totalWidth; | 250 parameters.distance = totalWidth; |
| 270 break; | 251 break; |
| 271 case kDirectionRight: | 252 case kDirectionRight: |
| 272 parameters.transformBegin = 'translateX(-' + metrics.contentWidt
h + 'px)'; | 253 parameters.transformBegin = 'translateX(-' + metrics.contentWidt
h + 'px)'; |
| 273 parameters.transformEnd = 'translateX(' + metrics.marqueeWidth +
'px)'; | 254 parameters.transformEnd = 'translateX(' + metrics.marqueeWidth +
'px)'; |
| 274 parameters.distance = totalWidth; | 255 parameters.distance = totalWidth; |
| 275 break; | 256 break; |
| 276 case kDirectionUp: | 257 case kDirectionUp: |
| 277 parameters.transformBegin = 'translateY(' + metrics.marqueeHeigh
t + 'px)'; | 258 parameters.transformBegin = 'translateY(' + metrics.marqueeHeigh
t + 'px)'; |
| 278 parameters.transformEnd = 'translateY(-' + metrics.contentHeight
+ 'px)'; | 259 parameters.transformEnd = 'translateY(-' + metrics.contentHeight
+ 'px)'; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 parameters.transformEnd = 'translateY(' + innerHeight + 'px)'; | 321 parameters.transformEnd = 'translateY(' + innerHeight + 'px)'; |
| 341 parameters.distance = metrics.marqueeHeight; | 322 parameters.distance = metrics.marqueeHeight; |
| 342 break; | 323 break; |
| 343 } | 324 } |
| 344 break; | 325 break; |
| 345 } | 326 } |
| 346 | 327 |
| 347 return parameters | 328 return parameters |
| 348 }; | 329 }; |
| 349 | 330 |
| 331 function animationFinished_(event) { |
| 332 var player = event.target; |
| 333 var marquee = player.marquee_; |
| 334 marquee.loopCount_++; |
| 335 marquee.start(); |
| 336 }; |
| 337 |
| 350 HTMLMarqueeElementPrototype.shouldContinue_ = function() { | 338 HTMLMarqueeElementPrototype.shouldContinue_ = function() { |
| 351 var loop = this.loop; | 339 var loop = this.loop; |
| 352 | 340 |
| 353 // By default, slide loops only once. | 341 // By default, slide loops only once. |
| 354 if (loop <= 0 && this.behavior === kBehaviorSlide) | 342 if (loop <= 0 && this.behavior === kBehaviorSlide) |
| 355 loop = 1; | 343 loop = 1; |
| 356 | 344 |
| 357 if (loop <= 0) | 345 if (loop <= 0) |
| 358 return true; | 346 return true; |
| 359 return this.loopCount_ < loop; | 347 return this.loopCount_ < loop; |
| 360 }; | 348 }; |
| 361 | 349 |
| 362 HTMLMarqueeElementPrototype.continue_ = function() { | 350 HTMLMarqueeElementPrototype.continue_ = function() { |
| 363 if (!this.shouldContinue_()) { | 351 if (!this.shouldContinue_()) { |
| 364 this.player_ = null; | 352 return; |
| 365 this.dispatchEvent(new Event('finish', false, true)); | 353 } |
| 354 |
| 355 if (this.player_ && this.player_.playState === 'paused') { |
| 356 this.player_.play(); |
| 366 return; | 357 return; |
| 367 } | 358 } |
| 368 | 359 |
| 369 var parameters = this.getAnimationParameters_(); | 360 var parameters = this.getAnimationParameters_(); |
| 370 | 361 var scrollDelay = this.scrollDelay; |
| 362 if (scrollDelay < kMinimumScrollDelayMS && !this.trueSpeed) |
| 363 scrollDelay = kDefaultScrollDelayMS; |
| 371 var player = this.mover_.animate([ | 364 var player = this.mover_.animate([ |
| 372 { transform: parameters.transformBegin }, | 365 { transform: parameters.transformBegin }, |
| 373 { transform: parameters.transformEnd }, | 366 { transform: parameters.transformEnd }, |
| 374 ], { | 367 ], { |
| 375 duration: parameters.distance * this.scrollDelay / this.scrollAmount
, | 368 duration: this.scrollAmount == 0 ? 0 : parameters.distance * scrollD
elay / this.scrollAmount, |
| 376 fill: 'forwards', | 369 fill: 'forwards', |
| 377 }); | 370 }); |
| 371 player.marquee_ = this; |
| 372 player.onfinish = animationFinished_; |
| 378 | 373 |
| 379 this.player_ = player; | 374 this.player_ = player; |
| 380 | |
| 381 player.addEventListener('finish', function() { | |
| 382 if (player != this.player_) | |
| 383 return; | |
| 384 ++this.loopCount_; | |
| 385 this.continue_(); | |
| 386 if (this.player_ && this.behavior === kBehaviorAlternate) | |
| 387 this.dispatchEvent(new Event('bounce', false, true)); | |
| 388 }.bind(this)); | |
| 389 }; | 375 }; |
| 390 | 376 |
| 391 HTMLMarqueeElementPrototype.start = function() { | 377 HTMLMarqueeElementPrototype.start = function() { |
| 392 if (this.continueCallback_ || this.player_) | 378 if (this.continueCallback_) |
| 393 return; | 379 return; |
| 394 this.continueCallback_ = requestAnimationFrame(function() { | 380 this.continueCallback_ = requestAnimationFrame(function() { |
| 395 this.continueCallback_ = null; | 381 this.continueCallback_ = null; |
| 396 this.continue_(); | 382 this.continue_(); |
| 397 }.bind(this)); | 383 }.bind(this)); |
| 398 this.dispatchEvent(new Event('start', false, true)); | |
| 399 }; | 384 }; |
| 400 | 385 |
| 401 HTMLMarqueeElementPrototype.stop = function() { | 386 HTMLMarqueeElementPrototype.stop = function() { |
| 402 if (!this.continueCallback_ && !this.player_) | |
| 403 return; | |
| 404 | |
| 405 if (this.continueCallback_) { | 387 if (this.continueCallback_) { |
| 406 cancelAnimationFrame(this.continueCallback_); | 388 cancelAnimationFrame(this.continueCallback_); |
| 407 this.continueCallback_ = null; | 389 this.continueCallback_ = null; |
| 408 return; | 390 return; |
| 409 } | 391 } |
| 410 | 392 |
| 411 // FIXME: Rather than canceling the animation, we really should just | |
| 412 // pause the animation, but the pause function is still flagged as | |
| 413 // experimental. | |
| 414 if (this.player_) { | 393 if (this.player_) { |
| 415 var player = this.player_; | 394 this.player_.pause(); |
| 416 this.player_ = null; | |
| 417 player.cancel(); | |
| 418 } | 395 } |
| 419 }; | 396 }; |
| 420 | |
| 421 // FIXME: We have to inject this HTMLMarqueeElement as a custom element in o
rder to make | |
| 422 // createdCallback, attachedCallback, detachedCallback and attributeChangedC
allback workable. | |
| 423 // document.registerElement('i-marquee', { | |
| 424 // prototype: HTMLMarqueeElementPrototype, | |
| 425 // }); | |
| 426 }); | 397 }); |
| OLD | NEW |