Chromium Code Reviews| 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(global) { | 7 installClass('HTMLMarqueeElement', function(global) { |
| 8 | 8 |
| 9 var kDefaultScrollAmount = 6; | 9 var kDefaultScrollAmount = 6; |
| 10 var kDefaultScrollDelayMS = 85; | 10 var kDefaultScrollDelayMS = 85; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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) { | 84 var HTMLMarqueeElementPrototype = {}; |
| 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 var HTMLMarqueeElementPrototype = Object.create(HTMLElement.prototype); | |
| 111 | 85 |
| 112 reflectAttribute(HTMLMarqueeElementPrototype, 'behavior', 'behavior'); | 86 reflectAttribute(HTMLMarqueeElementPrototype, 'behavior', 'behavior'); |
| 113 reflectAttribute(HTMLMarqueeElementPrototype, 'bgcolor', 'bgColor'); | 87 reflectAttribute(HTMLMarqueeElementPrototype, 'bgcolor', 'bgColor'); |
| 114 reflectAttribute(HTMLMarqueeElementPrototype, 'direction', 'direction'); | 88 reflectAttribute(HTMLMarqueeElementPrototype, 'direction', 'direction'); |
| 115 reflectAttribute(HTMLMarqueeElementPrototype, 'height', 'height'); | 89 reflectAttribute(HTMLMarqueeElementPrototype, 'height', 'height'); |
| 116 reflectAttribute(HTMLMarqueeElementPrototype, 'hspace', 'hspace'); | 90 reflectAttribute(HTMLMarqueeElementPrototype, 'hspace', 'hspace'); |
| 117 reflectAttribute(HTMLMarqueeElementPrototype, 'vspace', 'vspace'); | 91 reflectAttribute(HTMLMarqueeElementPrototype, 'vspace', 'vspace'); |
| 118 reflectAttribute(HTMLMarqueeElementPrototype, 'width', 'width'); | 92 reflectAttribute(HTMLMarqueeElementPrototype, 'width', 'width'); |
| 119 reflectBooleanAttribute(HTMLMarqueeElementPrototype, 'truespeed', 'trueSpeed '); | 93 reflectBooleanAttribute(HTMLMarqueeElementPrototype, 'truespeed', 'trueSpeed '); |
| 120 | 94 |
| 121 defineInlineEventHandler(HTMLMarqueeElementPrototype, 'start'); | |
| 122 defineInlineEventHandler(HTMLMarqueeElementPrototype, 'finish'); | |
| 123 defineInlineEventHandler(HTMLMarqueeElementPrototype, 'bounce'); | |
| 124 | |
| 125 HTMLMarqueeElementPrototype.createdCallback = function() { | 95 HTMLMarqueeElementPrototype.createdCallback = function() { |
| 126 var shadow = this.createShadowRoot(); | 96 var shadow = this.createShadowRoot(); |
| 127 var style = global.document.createElement('style'); | 97 var style = global.document.createElement('style'); |
| 128 style.textContent = ':host { display: inline-block; width: -webkit-fill-avai lable; overflow: hidden; text-align: initial; }' + | 98 style.textContent = ':host { display: inline-block; width: -webkit-fill- available; overflow: hidden; text-align: initial; }' + |
| 129 ':host([direction="up"]), :host([direction="down"]) { height: 200px; }'; | 99 ':host([direction="up"]), :host([direction="down"]) { height: 200px; }'; |
| 130 shadow.appendChild(style); | 100 shadow.appendChild(style); |
| 131 | 101 |
| 132 var mover = global.document.createElement('div'); | 102 var mover = global.document.createElement('div'); |
| 133 shadow.appendChild(mover); | 103 shadow.appendChild(mover); |
| 134 | 104 |
| 135 mover.appendChild(global.document.createElement('content')); | 105 mover.appendChild(global.document.createElement('content')); |
| 136 | 106 |
| 137 this.loopCount_ = 0; | 107 this.loopCount_ = 0; |
| 138 this.mover_ = mover; | 108 this.mover_ = mover; |
| 139 this.player_ = null; | 109 this.player_ = null; |
| 140 this.continueCallback_ = null; | 110 this.continueCallback_ = null; |
| 141 | |
| 142 for (var i = 0; i < kPresentationalAttributes.length; ++i) | |
| 143 this.initializeAttribute_(kPresentationalAttributes[i]); | |
| 144 }; | 111 }; |
| 145 | 112 |
| 146 HTMLMarqueeElementPrototype.attachedCallback = function() { | 113 HTMLMarqueeElementPrototype.attachedCallback = function() { |
| 114 for (var i = 0; i < kPresentationalAttributes.length; ++i) | |
| 115 initializeAttribute_.call(this, kPresentationalAttributes[i]); | |
|
haraken
2014/07/23 13:46:40
FIXME1: We cannot write this code as:
this.init
arv (Not doing code reviews)
2014/07/23 16:51:15
`this` should be the element instance which should
haraken
2014/07/24 01:56:44
It's not related to custom elements. It's related
haraken
2014/07/24 08:58:36
I tried this in the patch set 6. Now |this| object
| |
| 116 | |
| 147 this.start(); | 117 this.start(); |
| 148 }; | 118 }; |
| 149 | 119 |
| 150 HTMLMarqueeElementPrototype.detachedCallback = function() { | 120 HTMLMarqueeElementPrototype.detachedCallback = function() { |
| 151 this.stop(); | 121 this.stop(); |
| 152 }; | 122 }; |
| 153 | 123 |
| 154 HTMLMarqueeElementPrototype.attributeChangedCallback = function(name, oldVal ue, newValue) { | 124 HTMLMarqueeElementPrototype.attributeChangedCallback = function(name, oldVal ue, newValue) { |
| 155 switch (name) { | 125 switch (name) { |
| 156 case 'bgcolor': | 126 case 'bgcolor': |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 167 case 'vspace': | 137 case 'vspace': |
| 168 var margin = convertHTMLLengthToCSSLength(newValue); | 138 var margin = convertHTMLLengthToCSSLength(newValue); |
| 169 this.style.marginTop = margin; | 139 this.style.marginTop = margin; |
| 170 this.style.marginBottom = margin; | 140 this.style.marginBottom = margin; |
| 171 break; | 141 break; |
| 172 case 'width': | 142 case 'width': |
| 173 this.style.width = convertHTMLLengthToCSSLength(newValue); | 143 this.style.width = convertHTMLLengthToCSSLength(newValue); |
| 174 break; | 144 break; |
| 175 case 'behavior': | 145 case 'behavior': |
| 176 case 'direction': | 146 case 'direction': |
| 177 this.stop(); | 147 case 'loop': |
| 178 this.loopCount_ = 0; | 148 case 'scrollAmount': |
| 179 this.start(); | 149 case 'scrollDelay': |
| 150 case 'trueSpeed': | |
| 151 // FIXME: Not implemented. | |
|
haraken
2014/07/23 13:46:40
FIXME2: Here we need to pause the animation, recon
| |
| 180 break; | 152 break; |
| 181 } | 153 } |
| 182 }; | 154 }; |
| 183 | 155 |
| 184 HTMLMarqueeElementPrototype.initializeAttribute_ = function(name) { | 156 function initializeAttribute_(name) { |
| 185 var value = this.getAttribute(name); | 157 var value = this.getAttribute(name); |
| 186 if (value === null) | 158 if (value === null) |
| 187 return; | 159 return; |
| 188 this.attributeChangedCallback(name, null, value); | 160 HTMLMarqueeElementPrototype.attributeChangedCallback.call(this, name, nu ll, value); |
| 189 }; | 161 }; |
| 190 | 162 |
| 191 Object.defineProperty(HTMLMarqueeElementPrototype, 'scrollAmount', { | 163 Object.defineProperty(HTMLMarqueeElementPrototype, 'scrollAmount', { |
| 192 get: function() { | 164 get: function() { |
| 193 var value = this.getAttribute('scrollamount'); | 165 var value = this.getAttribute('scrollamount'); |
| 194 var scrollAmount = convertToLong(value); | 166 var scrollAmount = convertToLong(value); |
| 195 if (isNaN(scrollAmount) || scrollAmount < 0) | 167 if (isNaN(scrollAmount) || scrollAmount < 0) |
| 196 return kDefaultScrollAmount; | 168 return kDefaultScrollAmount; |
| 197 return scrollAmount; | 169 return scrollAmount; |
| 198 }, | 170 }, |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 226 return kDefaultLoopLimit; | 198 return kDefaultLoopLimit; |
| 227 return loop; | 199 return loop; |
| 228 }, | 200 }, |
| 229 set: function(value) { | 201 set: function(value) { |
| 230 if (value <= 0 && value != -1) | 202 if (value <= 0 && value != -1) |
| 231 throw new DOMExceptionInPrivateScript("IndexSizeError", "The pro vided value (" + value + ") is neither positive nor -1."); | 203 throw new DOMExceptionInPrivateScript("IndexSizeError", "The pro vided value (" + value + ") is neither positive nor -1."); |
| 232 this.setAttribute('loop', value); | 204 this.setAttribute('loop', value); |
| 233 }, | 205 }, |
| 234 }); | 206 }); |
| 235 | 207 |
| 236 HTMLMarqueeElementPrototype.getGetMetrics_ = function() { | 208 function getGetMetrics_() { |
|
arv (Not doing code reviews)
2014/07/23 16:51:15
no trailing underscore for local binding names. On
| |
| 237 this.mover_.style.width = '-webkit-max-content'; | 209 this.mover_.style.width = '-webkit-max-content'; |
| 238 | 210 |
| 239 var moverStyle = global.getComputedStyle(this.mover_); | 211 var moverStyle = global.getComputedStyle(this.mover_); |
| 240 var marqueeStyle = global.getComputedStyle(this); | 212 var marqueeStyle = global.getComputedStyle(this); |
| 241 | 213 |
| 242 var metrics = {}; | 214 var metrics = {}; |
| 243 metrics.contentWidth = parseInt(moverStyle.width); | 215 metrics.contentWidth = parseInt(moverStyle.width); |
| 244 metrics.contentHeight = parseInt(moverStyle.height); | 216 metrics.contentHeight = parseInt(moverStyle.height); |
| 245 metrics.marqueeWidth = parseInt(marqueeStyle.width); | 217 metrics.marqueeWidth = parseInt(marqueeStyle.width); |
| 246 metrics.marqueeHeight = parseInt(marqueeStyle.height); | 218 metrics.marqueeHeight = parseInt(marqueeStyle.height); |
| 247 | 219 |
| 248 this.mover_.style.width = ''; | 220 this.mover_.style.width = ''; |
| 249 return metrics; | 221 return metrics; |
| 250 }; | 222 }; |
| 251 | 223 |
| 252 HTMLMarqueeElementPrototype.getAnimationParameters_ = function() { | 224 function getAnimationParameters_() { |
| 253 var metrics = this.getGetMetrics_(); | 225 var metrics = getGetMetrics_.call(this); |
|
arv (Not doing code reviews)
2014/07/23 16:51:15
Same, I would change these away from using `this`.
| |
| 254 | 226 |
| 255 var totalWidth = metrics.marqueeWidth + metrics.contentWidth; | 227 var totalWidth = metrics.marqueeWidth + metrics.contentWidth; |
| 256 var totalHeight = metrics.marqueeHeight + metrics.contentHeight; | 228 var totalHeight = metrics.marqueeHeight + metrics.contentHeight; |
| 257 | 229 |
| 258 var innerWidth = metrics.marqueeWidth - metrics.contentWidth; | 230 var innerWidth = metrics.marqueeWidth - metrics.contentWidth; |
| 259 var innerHeight = metrics.marqueeHeight - metrics.contentHeight; | 231 var innerHeight = metrics.marqueeHeight - metrics.contentHeight; |
| 260 | 232 |
| 261 var parameters = {}; | 233 var parameters = {}; |
| 262 | 234 |
| 263 switch (this.behavior) { | 235 switch (this.behavior) { |
| 264 case kBehaviorScroll: | 236 case kBehaviorScroll: |
| 265 default: | 237 default: |
| 266 switch (this.direction) { | 238 switch (this.direction) { |
| 267 case kDirectionLeft: | 239 case kDirectionLeft: |
| 268 default: | 240 default: |
| 269 parameters.transformBegin = 'translateX(' + metrics.marqueeWidth + 'px)'; | 241 parameters.transformBegin = 'translateX(' + metrics.marqueeWidth + 'px)'; |
| 270 parameters.transformEnd = 'translateX(-100%)'; | 242 parameters.transformEnd = 'translateX(-' + metrics.contentWidth + 'px)'; |
| 271 parameters.distance = totalWidth; | 243 parameters.distance = totalWidth; |
| 272 break; | 244 break; |
| 273 case kDirectionRight: | 245 case kDirectionRight: |
| 274 parameters.transformBegin = 'translateX(-' + metrics.contentWidt h + 'px)'; | 246 parameters.transformBegin = 'translateX(-' + metrics.contentWidt h + 'px)'; |
| 275 parameters.transformEnd = 'translateX(' + metrics.marqueeWidth + 'px)'; | 247 parameters.transformEnd = 'translateX(' + metrics.marqueeWidth + 'px)'; |
| 276 parameters.distance = totalWidth; | 248 parameters.distance = totalWidth; |
| 277 break; | 249 break; |
| 278 case kDirectionUp: | 250 case kDirectionUp: |
| 279 parameters.transformBegin = 'translateY(' + metrics.marqueeHeigh t + 'px)'; | 251 parameters.transformBegin = 'translateY(' + metrics.marqueeHeigh t + 'px)'; |
| 280 parameters.transformEnd = 'translateY(-' + metrics.contentHeight + 'px)'; | 252 parameters.transformEnd = 'translateY(-' + metrics.contentHeight + 'px)'; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 342 parameters.transformEnd = 'translateY(' + innerHeight + 'px)'; | 314 parameters.transformEnd = 'translateY(' + innerHeight + 'px)'; |
| 343 parameters.distance = metrics.marqueeHeight; | 315 parameters.distance = metrics.marqueeHeight; |
| 344 break; | 316 break; |
| 345 } | 317 } |
| 346 break; | 318 break; |
| 347 } | 319 } |
| 348 | 320 |
| 349 return parameters | 321 return parameters |
| 350 }; | 322 }; |
| 351 | 323 |
| 352 HTMLMarqueeElementPrototype.shouldContinue_ = function() { | 324 function animationFinished_(event) { |
| 325 var player = event.target; | |
| 326 var marquee = player.marquee_; | |
| 327 marquee.loopCount_++; | |
| 328 marquee.start(); | |
| 329 }; | |
| 330 | |
| 331 function shouldContinue_() { | |
| 353 var loop = this.loop; | 332 var loop = this.loop; |
| 354 | 333 |
| 355 // By default, slide loops only once. | 334 // By default, slide loops only once. |
| 356 if (loop <= 0 && this.behavior === kBehaviorSlide) | 335 if (loop <= 0 && this.behavior === kBehaviorSlide) |
| 357 loop = 1; | 336 loop = 1; |
| 358 | 337 |
| 359 if (loop <= 0) | 338 if (loop <= 0) |
| 360 return true; | 339 return true; |
| 361 return this.loopCount_ < loop; | 340 return this.loopCount_ < loop; |
| 362 }; | 341 }; |
| 363 | 342 |
| 364 HTMLMarqueeElementPrototype.continue_ = function() { | 343 function continue_() { |
| 365 if (!this.shouldContinue_()) { | 344 if (!shouldContinue_.call(this)) { |
| 366 this.player_ = null; | |
| 367 this.dispatchEvent(new Event('finish', false, true)); | |
| 368 return; | 345 return; |
| 369 } | 346 } |
| 370 | 347 |
| 371 var parameters = this.getAnimationParameters_(); | 348 if (this.player_ && this.player_.paused) { |
| 349 // FIXME: This needs the WebAnimationsAPI flag enabled. | |
| 350 this.player_.play(); | |
|
haraken
2014/07/23 13:46:40
FIXME3: .play() is behind the WebAnimationAPI flag
| |
| 351 return; | |
| 352 } | |
| 372 | 353 |
| 354 var parameters = getAnimationParameters_.call(this); | |
| 355 var scrollDelay = this.scrollDelay; | |
| 356 if (scrollDelay < kMinimumScrollDelayMS && !this.trueSpeed) | |
| 357 scrollDelay = kDefaultScrollDelayMS; | |
| 373 var player = this.mover_.animate([ | 358 var player = this.mover_.animate([ |
| 374 { transform: parameters.transformBegin }, | 359 { transform: parameters.transformBegin }, |
| 375 { transform: parameters.transformEnd }, | 360 { transform: parameters.transformEnd }, |
| 376 ], { | 361 ], { |
| 377 duration: parameters.distance * this.scrollDelay / this.scrollAmount , | 362 duration: parameters.distance * scrollDelay / this.scrollAmount, |
| 378 fill: 'forwards', | 363 fill: 'forwards', |
| 379 }); | 364 }); |
| 365 player.marquee_ = this; | |
| 366 player.onfinish = animationFinished_; | |
| 380 | 367 |
| 381 this.player_ = player; | 368 this.player_ = player; |
| 369 }; | |
| 382 | 370 |
| 383 player.addEventListener('finish', function() { | 371 HTMLMarqueeElementPrototype.start = function() { |
| 384 if (player != this.player_) | 372 if (this.continueCallback_) |
| 385 return; | 373 return; |
| 386 ++this.loopCount_; | 374 this.continueCallback_ = global.requestAnimationFrame(function() { |
| 387 this.continue_(); | 375 this.continueCallback_ = null; |
| 388 if (this.player_ && this.behavior === kBehaviorAlternate) | 376 continue_.call(this); |
| 389 this.dispatchEvent(new Event('bounce', false, true)); | |
| 390 }.bind(this)); | 377 }.bind(this)); |
| 391 }; | 378 }; |
| 392 | 379 |
| 393 HTMLMarqueeElementPrototype.start = function() { | |
| 394 if (this.continueCallback_ || this.player_) | |
| 395 return; | |
| 396 this.continueCallback_ = global.requestAnimationFrame(function() { | |
| 397 this.continueCallback_ = null; | |
| 398 this.continue_(); | |
| 399 }.bind(this)); | |
| 400 this.dispatchEvent(new Event('start', false, true)); | |
| 401 }; | |
| 402 | |
| 403 HTMLMarqueeElementPrototype.stop = function() { | 380 HTMLMarqueeElementPrototype.stop = function() { |
| 404 if (!this.continueCallback_ && !this.player_) | |
| 405 return; | |
| 406 | |
| 407 if (this.continueCallback_) { | 381 if (this.continueCallback_) { |
| 408 global.cancelAnimationFrame(this.continueCallback_); | 382 global.cancelAnimationFrame(this.continueCallback_); |
| 409 this.continueCallback_ = null; | 383 this.continueCallback_ = null; |
| 410 return; | 384 return; |
| 411 } | 385 } |
| 412 | 386 |
| 413 // FIXME: Rather than canceling the animation, we really should just | |
| 414 // pause the animation, but the pause function is still flagged as | |
| 415 // experimental. | |
| 416 if (this.player_) { | 387 if (this.player_) { |
| 417 var player = this.player_; | 388 // FIXME: This needs the WebAnimationsAPI flag enabled. |
| 418 this.player_ = null; | 389 this.player_.pause(); |
|
haraken
2014/07/23 13:46:40
FIXME4: .pause() is behind the WebAnimationAPI fla
| |
| 419 player.cancel(); | |
| 420 } | 390 } |
| 421 }; | 391 }; |
| 422 | 392 |
| 423 // FIXME: We have to inject this HTMLMarqueeElement as a custom element in o rder to make | |
| 424 // createdCallback, attachedCallback, detachedCallback and attributeChangedC allback workable. | |
| 425 // global.document.registerElement('i-marquee', { | |
| 426 // prototype: HTMLMarqueeElementPrototype, | |
| 427 // }); | |
| 428 | |
| 429 return HTMLMarqueeElementPrototype; | 393 return HTMLMarqueeElementPrototype; |
| 430 }); | 394 }); |
| OLD | NEW |