author | ymh <ymh.work@gmail.com> |
Tue, 22 Nov 2016 14:04:03 +0100 | |
changeset 431 | 3e0a4a322f9e |
child 447 | 38d5789e30d0 |
permissions | -rw-r--r-- |
431
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1 |
import Ember from 'ember'; |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2 |
|
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
3 |
export default Ember.Component.extend({ |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
4 |
constants: Ember.inject.service(), |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
5 |
|
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
6 |
classNames: ['autoscroll-component'], |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
7 |
tagName: "span", |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
8 |
attributeBindings: ['title'], |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
9 |
|
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
10 |
title: Ember.computed.oneWay('text'), |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
11 |
|
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
12 |
text: null, |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
13 |
textWidth: null, |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
14 |
doScroll: false, |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
15 |
keyFrameStyleDef: null, |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
16 |
scrollRate: null, |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
17 |
|
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
18 |
willDestroyElement() { |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
19 |
const keyFrameStyleDef = this.get('keyFrameStyleDef'); |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
20 |
if(keyFrameStyleDef !== null) { |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
21 |
Ember.run(() => { |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
22 |
Ember.$(keyFrameStyleDef).remove(); |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
23 |
}); |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
24 |
this.set('keyFrameStyleDef', null); |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
25 |
} |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
26 |
}, |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
27 |
|
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
28 |
didRender() { |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
29 |
const textWidth = this.$('span').width(); |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
30 |
if(textWidth === this.get('textWidth')) { |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
31 |
return; |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
32 |
} |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
33 |
this.set('textWidth', textWidth); |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
34 |
const outerWidth = this.$().width(); |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
35 |
const doScroll = (textWidth > outerWidth); |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
36 |
const id = this.elementId; |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
37 |
|
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
38 |
Ember.run(() => { |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
39 |
if(this.get('keyFrameStyleDef') !== null) { |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
40 |
Ember.$(this.get('keyFrameStyleDef')).remove(); |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
41 |
} |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
42 |
if(doScroll) { |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
43 |
const scrollRate = this.get('scrollRate') || this.get('constants').AUTOSCROLL_RATE; |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
44 |
const duration = Math.ceil((textWidth + 30) / scrollRate) + 1; |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
45 |
const firstFramePercent = Math.ceil((1/duration)*100); |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
46 |
|
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
47 |
var keyframesDef = ''; |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
48 |
['-webkit-', '-moz-', ''].forEach(function(p) { |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
49 |
keyframesDef += `@${p}keyframes marquee-${id} { 0% { left: 0; } ${firstFramePercent}% { left: 0; } 100% { left: -${textWidth + 30 }px; } }\n`; |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
50 |
}); |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
51 |
this.set( |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
52 |
'keyFrameStyleDef', |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
53 |
Ember.$("<style>") |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
54 |
.prop('id', 'scroll-style-'+id) |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
55 |
.prop("type", "text/css") |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
56 |
.text(keyframesDef) |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
57 |
.appendTo("head") |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
58 |
); |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
59 |
this.$('div').css('animation', `marquee-${id} ${duration}s linear infinite`); |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
60 |
this.$('span:last-child').removeClass('hidden-double'); |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
61 |
} else { |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
62 |
this.$('span:last-child').addClass('hidden-double'); |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
63 |
this.$('div').css('animation', ''); |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
64 |
} |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
65 |
}); |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
66 |
|
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
67 |
} |
3e0a4a322f9e
Improve scrolling title, uses css animations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
68 |
}); |