-
Notifications
You must be signed in to change notification settings - Fork 0
/
polymerday-twitter-realtimeline.js
179 lines (153 loc) · 4.85 KB
/
polymerday-twitter-realtimeline.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
(function(){
Polymer({
is: 'polymerday-twitter-realtimeline',
properties: {
/**
* host:port where your api server code is (no twitter api)
*/
apiUrlBase: {
type: String
},
/**
* Float the Tweet *left*, *right*, or *center* relative to its container.
*/
align: {
type: String
},
/**
* If *false*, links in a Tweet are not expanded to photo, video, or link previews.
*/
expand: {
type: Boolean,
value: false
},
/**
* When set to *none*, only the cited Tweet will be displayed even if it is in reply to another Tweet.
*/
conversation: {
type: String
},
/**
* Loads text components in the specified language. [Twitter language code](https://dev.twitter.com/overview/general/adding-international-support-to-your-apps).
* **Note**: does not affect the text of the cited Tweet.
*/
language: {
type: String,
value: 'es'
},
/**
* When set to *dark*, displays Tweet with light text over a darkbackground.
*/
theme: {
type: String,
value: 'light'
},
/**
* The maximum width of the rendered Tweet in whole pixels. This valueshould be between 250 and 550 pixels.
*/
width: {
type: Number
},
/**
* Minimum period of time (ms) that has to be spent to show a new tweet. By default every tweet is shown as soon as possible
*/
elapse: {
type: Number,
value: 0
},
/**
************************************
******** Private Properties ********
*/
/**
* computed property
*/
_cards: {
type: String,
computed: '_isExpanded(expand)'
}
},
/**
* Polymer ready event
*/
ready: function() {
twttr.ready(function() {
if (!this.apiUrlBase) {
this._log(this._logf('apiUrlBase', 'apiUrlBase must not be undefined'));
return;
}
var twSocket = io.connect(this.apiUrlBase);
var _tweetHandler = this._createTweetHandler();
// twSocket.on('connect', function () {console.log('Connection success');});
// twSocket.on('connect_error', function () {console.log('Connection failed!');});
// twSocket.on('connect_timeout', function () {console.log('Connection timeout!');});
// twSocket.on('reconnect_attempt', function () {console.log('Reconnection attemp!');});
// twSocket.on('reconnect', function () {console.log('Reconnection success!');});
// twSocket.on('reconnect_failed', function () {console.log('Reconnection failed!');});
twSocket.on('tweet', function (data) {
_tweetHandler(data);
}.bind(this));
}.bind(this));
},
/**
* Tweet handler.
* @returns {Function} Function that process the tweet and when it must be drawn depending on elapse property
*/
_createTweetHandler: function() {
var currentTweet, mustBeDrawn, arrTimeoutId, hashtag;
currentTweet = 0;
mustBeDrawn = 0;
arrTimeoutId = [];
return this.elapse === 0 ? (function(data) {
//this function will be run as at microtask timing
this.async(function() {
this._drawTweet(data.tweet);
});
}.bind(this)) : (function(data) {
var diff, time, timeoutId;
//check if necessary to cancel async functions and reninitialize values due to a new hashtag track
if (data.hashtag !== hashtag) {
arrTimeoutId.forEach(function(timeoutId) {
this.cancelAsync(timeoutId);
}, this);
hashtag = data.hashtag;
arrTimeoutId = [];
mustBeDrawn = 0;
}
currentTweet = performance.now();
diff = currentTweet - mustBeDrawn;
time = diff < 0 ? Math.abs(diff) : 0;
mustBeDrawn = (diff < 0 ? mustBeDrawn : currentTweet) + this.elapse;
//this function will be run with a calculated delay
timeoutId = this.async(function() {
this._drawTweet(data.tweet);
}, time);
arrTimeoutId.push(timeoutId);
}.bind(this));
},
/**
* @return {String} Returns *hidden* if expand is false.
*/
_isExpanded: function(expand) {
return expand ? '': 'hidden';
},
/**
* Calls Twitter for Websits widgets to get the look and feel of a tweet
* @param {Object} tweet object from Twitter stream API
*/
_drawTweet: function(tweet) {
twttr.widgets.createTweet(
tweet.id_str,
this.$.timeline,
{
align: this.align,
cards: this._cards,
conversation: this.conversation,
lang: this.language,
theme: this.theme,
width: this.width
}
);
}
});
}());