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 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) { | |
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"]) { height: 200px; }'; |
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 | |
145 this.start(); | 115 this.start(); |
146 }; | 116 }; |
147 | 117 |
148 HTMLMarqueeElementPrototype.detachedCallback = function() { | 118 HTMLMarqueeElementPrototype.detachedCallback = function() { |
149 this.stop(); | 119 this.stop(); |
150 }; | 120 }; |
151 | 121 |
152 HTMLMarqueeElementPrototype.attributeChangedCallback = function(name, oldVal ue, newValue) { | 122 HTMLMarqueeElementPrototype.attributeChangedCallback = function(name, oldVal ue, newValue) { |
153 switch (name) { | 123 switch (name) { |
154 case 'bgcolor': | 124 case 'bgcolor': |
(...skipping 10 matching lines...) Expand all Loading... | |
165 case 'vspace': | 135 case 'vspace': |
166 var margin = convertHTMLLengthToCSSLength(newValue); | 136 var margin = convertHTMLLengthToCSSLength(newValue); |
167 this.style.marginTop = margin; | 137 this.style.marginTop = margin; |
168 this.style.marginBottom = margin; | 138 this.style.marginBottom = margin; |
169 break; | 139 break; |
170 case 'width': | 140 case 'width': |
171 this.style.width = convertHTMLLengthToCSSLength(newValue); | 141 this.style.width = convertHTMLLengthToCSSLength(newValue); |
172 break; | 142 break; |
173 case 'behavior': | 143 case 'behavior': |
174 case 'direction': | 144 case 'direction': |
175 this.stop(); | 145 case 'loop': |
176 this.loopCount_ = 0; | 146 case 'scrollAmount': |
177 this.start(); | 147 case 'scrollDelay': |
148 case 'trueSpeed': | |
149 // FIXME: Not implemented. | |
haraken
2014/09/26 07:53:06
This is the only regression this CL may cause. Thi
| |
178 break; | 150 break; |
179 } | 151 } |
180 }; | 152 }; |
181 | 153 |
182 HTMLMarqueeElementPrototype.initializeAttribute_ = function(name) { | 154 HTMLMarqueeElementPrototype.initializeAttribute_ = function(name) { |
183 var value = this.getAttribute(name); | 155 var value = this.getAttribute(name); |
184 if (value === null) | 156 if (value === null) |
185 return; | 157 return; |
186 this.attributeChangedCallback(name, null, value); | 158 this.attributeChangedCallback(name, null, value); |
187 }; | 159 }; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
224 return kDefaultLoopLimit; | 196 return kDefaultLoopLimit; |
225 return loop; | 197 return loop; |
226 }, | 198 }, |
227 set: function(value) { | 199 set: function(value) { |
228 if (value <= 0 && value != -1) | 200 if (value <= 0 && value != -1) |
229 throwException(PrivateScriptDOMException.IndexSizeError, "The pr ovided value (" + value + ") is neither positive nor -1."); | 201 throwException(PrivateScriptDOMException.IndexSizeError, "The pr ovided value (" + value + ") is neither positive nor -1."); |
230 this.setAttribute('loop', value); | 202 this.setAttribute('loop', value); |
231 }, | 203 }, |
232 }); | 204 }); |
233 | 205 |
234 HTMLMarqueeElementPrototype.getGetMetrics_ = function() { | 206 HTMLMarqueeElementPrototype.getGetMetrics_ = function() { |
235 this.mover_.style.width = '-webkit-max-content'; | 207 this.mover_.style.width = '-webkit-max-content'; |
236 | 208 |
237 var moverStyle = getComputedStyle(this.mover_); | 209 var moverStyle = getComputedStyle(this.mover_); |
238 var marqueeStyle = getComputedStyle(this); | 210 var marqueeStyle = getComputedStyle(this); |
239 | 211 |
240 var metrics = {}; | 212 var metrics = {}; |
241 metrics.contentWidth = parseInt(moverStyle.width); | 213 metrics.contentWidth = parseInt(moverStyle.width); |
242 metrics.contentHeight = parseInt(moverStyle.height); | 214 metrics.contentHeight = parseInt(moverStyle.height); |
243 metrics.marqueeWidth = parseInt(marqueeStyle.width); | 215 metrics.marqueeWidth = parseInt(marqueeStyle.width); |
244 metrics.marqueeHeight = parseInt(marqueeStyle.height); | 216 metrics.marqueeHeight = parseInt(marqueeStyle.height); |
(...skipping 13 matching lines...) Expand all Loading... | |
258 | 230 |
259 var parameters = {}; | 231 var parameters = {}; |
260 | 232 |
261 switch (this.behavior) { | 233 switch (this.behavior) { |
262 case kBehaviorScroll: | 234 case kBehaviorScroll: |
263 default: | 235 default: |
264 switch (this.direction) { | 236 switch (this.direction) { |
265 case kDirectionLeft: | 237 case kDirectionLeft: |
266 default: | 238 default: |
267 parameters.transformBegin = 'translateX(' + metrics.marqueeWidth + 'px)'; | 239 parameters.transformBegin = 'translateX(' + metrics.marqueeWidth + 'px)'; |
268 parameters.transformEnd = 'translateX(-100%)'; | 240 parameters.transformEnd = 'translateX(-' + metrics.contentWidth + 'px)'; |
269 parameters.distance = totalWidth; | 241 parameters.distance = totalWidth; |
270 break; | 242 break; |
271 case kDirectionRight: | 243 case kDirectionRight: |
272 parameters.transformBegin = 'translateX(-' + metrics.contentWidt h + 'px)'; | 244 parameters.transformBegin = 'translateX(-' + metrics.contentWidt h + 'px)'; |
273 parameters.transformEnd = 'translateX(' + metrics.marqueeWidth + 'px)'; | 245 parameters.transformEnd = 'translateX(' + metrics.marqueeWidth + 'px)'; |
274 parameters.distance = totalWidth; | 246 parameters.distance = totalWidth; |
275 break; | 247 break; |
276 case kDirectionUp: | 248 case kDirectionUp: |
277 parameters.transformBegin = 'translateY(' + metrics.marqueeHeigh t + 'px)'; | 249 parameters.transformBegin = 'translateY(' + metrics.marqueeHeigh t + 'px)'; |
278 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... | |
340 parameters.transformEnd = 'translateY(' + innerHeight + 'px)'; | 312 parameters.transformEnd = 'translateY(' + innerHeight + 'px)'; |
341 parameters.distance = metrics.marqueeHeight; | 313 parameters.distance = metrics.marqueeHeight; |
342 break; | 314 break; |
343 } | 315 } |
344 break; | 316 break; |
345 } | 317 } |
346 | 318 |
347 return parameters | 319 return parameters |
348 }; | 320 }; |
349 | 321 |
322 function animationFinished_(event) { | |
323 var player = event.target; | |
324 var marquee = player.marquee_; | |
325 marquee.loopCount_++; | |
326 marquee.start(); | |
327 }; | |
328 | |
350 HTMLMarqueeElementPrototype.shouldContinue_ = function() { | 329 HTMLMarqueeElementPrototype.shouldContinue_ = function() { |
351 var loop = this.loop; | 330 var loop = this.loop; |
352 | 331 |
353 // By default, slide loops only once. | 332 // By default, slide loops only once. |
354 if (loop <= 0 && this.behavior === kBehaviorSlide) | 333 if (loop <= 0 && this.behavior === kBehaviorSlide) |
355 loop = 1; | 334 loop = 1; |
356 | 335 |
357 if (loop <= 0) | 336 if (loop <= 0) |
358 return true; | 337 return true; |
359 return this.loopCount_ < loop; | 338 return this.loopCount_ < loop; |
360 }; | 339 }; |
361 | 340 |
362 HTMLMarqueeElementPrototype.continue_ = function() { | 341 HTMLMarqueeElementPrototype.continue_ = function() { |
363 if (!this.shouldContinue_()) { | 342 if (!this.shouldContinue_()) { |
364 this.player_ = null; | 343 return; |
365 this.dispatchEvent(new Event('finish', false, true)); | 344 } |
345 | |
346 if (this.player_ && this.player_.playState === "paused") { | |
347 this.player_.play(); | |
366 return; | 348 return; |
367 } | 349 } |
368 | 350 |
369 var parameters = this.getAnimationParameters_(); | 351 var parameters = this.getAnimationParameters_(); |
370 | 352 var scrollDelay = this.scrollDelay; |
353 if (scrollDelay < kMinimumScrollDelayMS && !this.trueSpeed) | |
354 scrollDelay = kDefaultScrollDelayMS; | |
371 var player = this.mover_.animate([ | 355 var player = this.mover_.animate([ |
372 { transform: parameters.transformBegin }, | 356 { transform: parameters.transformBegin }, |
373 { transform: parameters.transformEnd }, | 357 { transform: parameters.transformEnd }, |
374 ], { | 358 ], { |
375 duration: parameters.distance * this.scrollDelay / this.scrollAmount , | 359 duration: parameters.distance * scrollDelay / this.scrollAmount, |
376 fill: 'forwards', | 360 fill: 'forwards', |
377 }); | 361 }); |
362 player.marquee_ = this; | |
363 player.onfinish = animationFinished_; | |
378 | 364 |
379 this.player_ = player; | 365 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 }; | 366 }; |
390 | 367 |
391 HTMLMarqueeElementPrototype.start = function() { | 368 HTMLMarqueeElementPrototype.start = function() { |
392 if (this.continueCallback_ || this.player_) | 369 if (this.continueCallback_) |
393 return; | 370 return; |
394 this.continueCallback_ = requestAnimationFrame(function() { | 371 this.continueCallback_ = requestAnimationFrame(function() { |
395 this.continueCallback_ = null; | 372 this.continueCallback_ = null; |
396 this.continue_(); | 373 this.continue_(); |
397 }.bind(this)); | 374 }.bind(this)); |
398 this.dispatchEvent(new Event('start', false, true)); | |
399 }; | 375 }; |
400 | 376 |
401 HTMLMarqueeElementPrototype.stop = function() { | 377 HTMLMarqueeElementPrototype.stop = function() { |
402 if (!this.continueCallback_ && !this.player_) | |
403 return; | |
404 | |
405 if (this.continueCallback_) { | 378 if (this.continueCallback_) { |
406 cancelAnimationFrame(this.continueCallback_); | 379 cancelAnimationFrame(this.continueCallback_); |
407 this.continueCallback_ = null; | 380 this.continueCallback_ = null; |
408 return; | 381 return; |
409 } | 382 } |
410 | 383 |
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_) { | 384 if (this.player_) { |
415 var player = this.player_; | 385 this.player_.pause(); |
416 this.player_ = null; | |
417 player.cancel(); | |
418 } | 386 } |
419 }; | 387 }; |
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 }); | 388 }); |
OLD | NEW |