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, HTMLMarqueeElementPrototype)
{ |
8 | 8 |
9 var kDefaultScrollAmount = 6; | 9 var kDefaultScrollAmount = 6; |
10 var kDefaultScrollDelayMS = 85; | 10 var kDefaultScrollDelayMS = 85; |
11 var kMinimumScrollDelayMS = 60; | 11 var kMinimumScrollDelayMS = 60; |
12 | 12 |
13 var kDefaultLoopLimit = -1; | 13 var kDefaultLoopLimit = -1; |
14 | 14 |
15 var kBehaviorScroll = 'scroll'; | 15 var kBehaviorScroll = 'scroll'; |
16 var kBehaviorSlide = 'slide'; | 16 var kBehaviorSlide = 'slide'; |
17 var kBehaviorAlternate = 'alternate'; | 17 var kBehaviorAlternate = 'alternate'; |
(...skipping 56 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) { | |
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 | |
112 reflectAttribute(HTMLMarqueeElementPrototype, 'behavior', 'behavior'); | 84 reflectAttribute(HTMLMarqueeElementPrototype, 'behavior', 'behavior'); |
113 reflectAttribute(HTMLMarqueeElementPrototype, 'bgcolor', 'bgColor'); | 85 reflectAttribute(HTMLMarqueeElementPrototype, 'bgcolor', 'bgColor'); |
114 reflectAttribute(HTMLMarqueeElementPrototype, 'direction', 'direction'); | 86 reflectAttribute(HTMLMarqueeElementPrototype, 'direction', 'direction'); |
115 reflectAttribute(HTMLMarqueeElementPrototype, 'height', 'height'); | 87 reflectAttribute(HTMLMarqueeElementPrototype, 'height', 'height'); |
116 reflectAttribute(HTMLMarqueeElementPrototype, 'hspace', 'hspace'); | 88 reflectAttribute(HTMLMarqueeElementPrototype, 'hspace', 'hspace'); |
117 reflectAttribute(HTMLMarqueeElementPrototype, 'vspace', 'vspace'); | 89 reflectAttribute(HTMLMarqueeElementPrototype, 'vspace', 'vspace'); |
118 reflectAttribute(HTMLMarqueeElementPrototype, 'width', 'width'); | 90 reflectAttribute(HTMLMarqueeElementPrototype, 'width', 'width'); |
119 reflectBooleanAttribute(HTMLMarqueeElementPrototype, 'truespeed', 'trueSpeed
'); | 91 reflectBooleanAttribute(HTMLMarqueeElementPrototype, 'truespeed', 'trueSpeed
'); |
120 | 92 |
121 defineInlineEventHandler(HTMLMarqueeElementPrototype, 'start'); | |
122 defineInlineEventHandler(HTMLMarqueeElementPrototype, 'finish'); | |
123 defineInlineEventHandler(HTMLMarqueeElementPrototype, 'bounce'); | |
124 | |
125 HTMLMarqueeElementPrototype.createdCallback = function() { | 93 HTMLMarqueeElementPrototype.createdCallback = function() { |
126 var shadow = this.createShadowRoot(); | 94 var shadow = this.createShadowRoot(); |
127 var style = global.document.createElement('style'); | 95 var style = global.document.createElement('style'); |
128 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; }' + |
129 ':host([direction="up"]), :host([direction="down"]) { height: 200px;
}'; | 97 ':host([direction="up"]), :host([direction="down"]) { height: 200px;
}'; |
130 shadow.appendChild(style); | 98 shadow.appendChild(style); |
131 | 99 |
132 var mover = global.document.createElement('div'); | 100 var mover = global.document.createElement('div'); |
133 shadow.appendChild(mover); | 101 shadow.appendChild(mover); |
134 | 102 |
135 mover.appendChild(global.document.createElement('content')); | 103 mover.appendChild(global.document.createElement('content')); |
136 | 104 |
137 this.loopCount_ = 0; | 105 this.loopCount_ = 0; |
138 this.mover_ = mover; | 106 this.mover_ = mover; |
139 this.player_ = null; | 107 this.player_ = null; |
140 this.continueCallback_ = null; | 108 this.continueCallback_ = null; |
141 | |
142 for (var i = 0; i < kPresentationalAttributes.length; ++i) | |
143 this.initializeAttribute_(kPresentationalAttributes[i]); | |
144 }; | 109 }; |
145 | 110 |
146 HTMLMarqueeElementPrototype.attachedCallback = function() { | 111 HTMLMarqueeElementPrototype.attachedCallback = function() { |
| 112 for (var i = 0; i < kPresentationalAttributes.length; ++i) |
| 113 this.initializeAttribute_(kPresentationalAttributes[i]); |
| 114 |
147 this.start(); | 115 this.start(); |
148 }; | 116 }; |
149 | 117 |
150 HTMLMarqueeElementPrototype.detachedCallback = function() { | 118 HTMLMarqueeElementPrototype.detachedCallback = function() { |
151 this.stop(); | 119 this.stop(); |
152 }; | 120 }; |
153 | 121 |
154 HTMLMarqueeElementPrototype.attributeChangedCallback = function(name, oldVal
ue, newValue) { | 122 HTMLMarqueeElementPrototype.attributeChangedCallback = function(name, oldVal
ue, newValue) { |
155 switch (name) { | 123 switch (name) { |
156 case 'bgcolor': | 124 case 'bgcolor': |
(...skipping 10 matching lines...) Expand all Loading... |
167 case 'vspace': | 135 case 'vspace': |
168 var margin = convertHTMLLengthToCSSLength(newValue); | 136 var margin = convertHTMLLengthToCSSLength(newValue); |
169 this.style.marginTop = margin; | 137 this.style.marginTop = margin; |
170 this.style.marginBottom = margin; | 138 this.style.marginBottom = margin; |
171 break; | 139 break; |
172 case 'width': | 140 case 'width': |
173 this.style.width = convertHTMLLengthToCSSLength(newValue); | 141 this.style.width = convertHTMLLengthToCSSLength(newValue); |
174 break; | 142 break; |
175 case 'behavior': | 143 case 'behavior': |
176 case 'direction': | 144 case 'direction': |
177 this.stop(); | 145 case 'loop': |
178 this.loopCount_ = 0; | 146 case 'scrollAmount': |
179 this.start(); | 147 case 'scrollDelay': |
| 148 case 'trueSpeed': |
| 149 // FIXME: Not implemented. |
180 break; | 150 break; |
181 } | 151 } |
182 }; | 152 }; |
183 | 153 |
184 HTMLMarqueeElementPrototype.initializeAttribute_ = function(name) { | 154 HTMLMarqueeElementPrototype.initializeAttribute_ = function(name) { |
185 var value = this.getAttribute(name); | 155 var value = this.getAttribute(name); |
186 if (value === null) | 156 if (value === null) |
187 return; | 157 return; |
188 this.attributeChangedCallback(name, null, value); | 158 this.attributeChangedCallback(name, null, value); |
189 }; | 159 }; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 return kDefaultLoopLimit; | 196 return kDefaultLoopLimit; |
227 return loop; | 197 return loop; |
228 }, | 198 }, |
229 set: function(value) { | 199 set: function(value) { |
230 if (value <= 0 && value != -1) | 200 if (value <= 0 && value != -1) |
231 throw new DOMExceptionInPrivateScript("IndexSizeError", "The pro
vided value (" + value + ") is neither positive nor -1."); | 201 throw new DOMExceptionInPrivateScript("IndexSizeError", "The pro
vided value (" + value + ") is neither positive nor -1."); |
232 this.setAttribute('loop', value); | 202 this.setAttribute('loop', value); |
233 }, | 203 }, |
234 }); | 204 }); |
235 | 205 |
236 HTMLMarqueeElementPrototype.getGetMetrics_ = function() { | 206 HTMLMarqueeElementPrototype.getGetMetrics_ = function() { |
237 this.mover_.style.width = '-webkit-max-content'; | 207 this.mover_.style.width = '-webkit-max-content'; |
238 | 208 |
239 var moverStyle = global.getComputedStyle(this.mover_); | 209 var moverStyle = global.getComputedStyle(this.mover_); |
240 var marqueeStyle = global.getComputedStyle(this); | 210 var marqueeStyle = global.getComputedStyle(this); |
241 | 211 |
242 var metrics = {}; | 212 var metrics = {}; |
243 metrics.contentWidth = parseInt(moverStyle.width); | 213 metrics.contentWidth = parseInt(moverStyle.width); |
244 metrics.contentHeight = parseInt(moverStyle.height); | 214 metrics.contentHeight = parseInt(moverStyle.height); |
245 metrics.marqueeWidth = parseInt(marqueeStyle.width); | 215 metrics.marqueeWidth = parseInt(marqueeStyle.width); |
246 metrics.marqueeHeight = parseInt(marqueeStyle.height); | 216 metrics.marqueeHeight = parseInt(marqueeStyle.height); |
(...skipping 13 matching lines...) Expand all Loading... |
260 | 230 |
261 var parameters = {}; | 231 var parameters = {}; |
262 | 232 |
263 switch (this.behavior) { | 233 switch (this.behavior) { |
264 case kBehaviorScroll: | 234 case kBehaviorScroll: |
265 default: | 235 default: |
266 switch (this.direction) { | 236 switch (this.direction) { |
267 case kDirectionLeft: | 237 case kDirectionLeft: |
268 default: | 238 default: |
269 parameters.transformBegin = 'translateX(' + metrics.marqueeWidth
+ 'px)'; | 239 parameters.transformBegin = 'translateX(' + metrics.marqueeWidth
+ 'px)'; |
270 parameters.transformEnd = 'translateX(-100%)'; | 240 parameters.transformEnd = 'translateX(-' + metrics.contentWidth
+ 'px)'; |
271 parameters.distance = totalWidth; | 241 parameters.distance = totalWidth; |
272 break; | 242 break; |
273 case kDirectionRight: | 243 case kDirectionRight: |
274 parameters.transformBegin = 'translateX(-' + metrics.contentWidt
h + 'px)'; | 244 parameters.transformBegin = 'translateX(-' + metrics.contentWidt
h + 'px)'; |
275 parameters.transformEnd = 'translateX(' + metrics.marqueeWidth +
'px)'; | 245 parameters.transformEnd = 'translateX(' + metrics.marqueeWidth +
'px)'; |
276 parameters.distance = totalWidth; | 246 parameters.distance = totalWidth; |
277 break; | 247 break; |
278 case kDirectionUp: | 248 case kDirectionUp: |
279 parameters.transformBegin = 'translateY(' + metrics.marqueeHeigh
t + 'px)'; | 249 parameters.transformBegin = 'translateY(' + metrics.marqueeHeigh
t + 'px)'; |
280 parameters.transformEnd = 'translateY(-' + metrics.contentHeight
+ 'px)'; | 250 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)'; | 312 parameters.transformEnd = 'translateY(' + innerHeight + 'px)'; |
343 parameters.distance = metrics.marqueeHeight; | 313 parameters.distance = metrics.marqueeHeight; |
344 break; | 314 break; |
345 } | 315 } |
346 break; | 316 break; |
347 } | 317 } |
348 | 318 |
349 return parameters | 319 return parameters |
350 }; | 320 }; |
351 | 321 |
| 322 function animationFinished_(event) { |
| 323 var player = event.target; |
| 324 var marquee = player.marquee_; |
| 325 marquee.loopCount_++; |
| 326 marquee.start(); |
| 327 }; |
| 328 |
352 HTMLMarqueeElementPrototype.shouldContinue_ = function() { | 329 HTMLMarqueeElementPrototype.shouldContinue_ = function() { |
353 var loop = this.loop; | 330 var loop = this.loop; |
354 | 331 |
355 // By default, slide loops only once. | 332 // By default, slide loops only once. |
356 if (loop <= 0 && this.behavior === kBehaviorSlide) | 333 if (loop <= 0 && this.behavior === kBehaviorSlide) |
357 loop = 1; | 334 loop = 1; |
358 | 335 |
359 if (loop <= 0) | 336 if (loop <= 0) |
360 return true; | 337 return true; |
361 return this.loopCount_ < loop; | 338 return this.loopCount_ < loop; |
362 }; | 339 }; |
363 | 340 |
364 HTMLMarqueeElementPrototype.continue_ = function() { | 341 HTMLMarqueeElementPrototype.continue_ = function() { |
365 if (!this.shouldContinue_()) { | 342 if (!this.shouldContinue_()) { |
366 this.player_ = null; | 343 return; |
367 this.dispatchEvent(new Event('finish', false, true)); | 344 } |
| 345 |
| 346 if (this.player_ && this.player_.paused) { |
| 347 // FIXME: This needs the WebAnimationsAPI flag enabled. |
| 348 this.player_.play(); |
368 return; | 349 return; |
369 } | 350 } |
370 | 351 |
371 var parameters = this.getAnimationParameters_(); | 352 var parameters = this.getAnimationParameters_(); |
372 | 353 var scrollDelay = this.scrollDelay; |
| 354 if (scrollDelay < kMinimumScrollDelayMS && !this.trueSpeed) |
| 355 scrollDelay = kDefaultScrollDelayMS; |
373 var player = this.mover_.animate([ | 356 var player = this.mover_.animate([ |
374 { transform: parameters.transformBegin }, | 357 { transform: parameters.transformBegin }, |
375 { transform: parameters.transformEnd }, | 358 { transform: parameters.transformEnd }, |
376 ], { | 359 ], { |
377 duration: parameters.distance * this.scrollDelay / this.scrollAmount
, | 360 duration: parameters.distance * scrollDelay / this.scrollAmount, |
378 fill: 'forwards', | 361 fill: 'forwards', |
379 }); | 362 }); |
| 363 player.marquee_ = this; |
| 364 player.onfinish = animationFinished_; |
380 | 365 |
381 this.player_ = player; | 366 this.player_ = player; |
382 | |
383 player.addEventListener('finish', function() { | |
384 if (player != this.player_) | |
385 return; | |
386 ++this.loopCount_; | |
387 this.continue_(); | |
388 if (this.player_ && this.behavior === kBehaviorAlternate) | |
389 this.dispatchEvent(new Event('bounce', false, true)); | |
390 }.bind(this)); | |
391 }; | 367 }; |
392 | 368 |
393 HTMLMarqueeElementPrototype.start = function() { | 369 HTMLMarqueeElementPrototype.start = function() { |
394 if (this.continueCallback_ || this.player_) | 370 if (this.continueCallback_) |
395 return; | 371 return; |
396 this.continueCallback_ = global.requestAnimationFrame(function() { | 372 this.continueCallback_ = global.requestAnimationFrame(function() { |
397 this.continueCallback_ = null; | 373 this.continueCallback_ = null; |
398 this.continue_(); | 374 this.continue_(); |
399 }.bind(this)); | 375 }.bind(this)); |
400 this.dispatchEvent(new Event('start', false, true)); | |
401 }; | 376 }; |
402 | 377 |
403 HTMLMarqueeElementPrototype.stop = function() { | 378 HTMLMarqueeElementPrototype.stop = function() { |
404 if (!this.continueCallback_ && !this.player_) | |
405 return; | |
406 | |
407 if (this.continueCallback_) { | 379 if (this.continueCallback_) { |
408 global.cancelAnimationFrame(this.continueCallback_); | 380 global.cancelAnimationFrame(this.continueCallback_); |
409 this.continueCallback_ = null; | 381 this.continueCallback_ = null; |
410 return; | 382 return; |
411 } | 383 } |
412 | 384 |
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_) { | 385 if (this.player_) { |
417 var player = this.player_; | 386 // FIXME: This needs the WebAnimationsAPI flag enabled. |
418 this.player_ = null; | 387 this.player_.pause(); |
419 player.cancel(); | |
420 } | 388 } |
421 }; | 389 }; |
422 | |
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; | |
430 }); | 390 }); |
OLD | NEW |