forked from h5p/h5p-twitter-user-feed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter-user-feed.js
84 lines (70 loc) · 2.2 KB
/
twitter-user-feed.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
var H5P = H5P || {};
H5P.TwitterUserFeed = (function ($) {
/**
* Constructor function.
*/
function C(options, id) {
H5P.EventDispatcher.call(this);
// Extend defaults with provided options
this.options = $.extend(true, {}, {
userName: 'H5PTechnology',
showReplies: false,
numTweets: 5
}, options);
// Keep provided id.
this.id = id;
}
// Inheritance
C.prototype = Object.create(H5P.EventDispatcher.prototype);
C.prototype.constructor = C;
/**
* Attach function called by H5P framework to insert H5P content into
* page
*
* @param {jQuery} $container
*/
C.prototype.attach = function ($container) {
var self = this;
this.setUpTwitter();
// notify that twitter feed has been loaded
twttr.ready(function (twttr) {
twttr.events.bind('loaded', function () { self.trigger('loaded'); });
}
);
// Set class on container to identify twitter user feed
$container.addClass("h5p-twitter-user-feed");
$container.append(
'<a class="twitter-timeline" href="https://twitter.com/twitterapi"' +
'data-widget-id="558756407995273216" data-screen-name="' + this.options.userName +
'" data-show-replies="' + this.options.showReplies +
'" data-tweet-limit="' + this.options.numTweets + '">Tweets by @' +
this.options.userName + '</a>');
if (window.twttr !== undefined && window.twttr.widgets !== undefined) {
window.twttr.widgets.load($container.get(0));
}
};
C.prototype.setUpTwitter = function() {
if (window.twttr) {
return; // Already set up
}
// Load widgets api if not already done
var id = 'twitter-wjs';
if (!document.getElementById(id)) {
// Create script tag
var js = document.createElement('script');
js.id = id;
js.src = "https://platform.twitter.com/widgets.js";
// Insert before first head JS
var firstJS = document.getElementsByTagName('script')[0];
firstJS.parentNode.insertBefore(js, firstJS);
}
// Create twttr object used by script
window.twttr = {
_e: [],
ready: function (callback) {
window.twttr._e.push(callback);
}
};
};
return C;
})(H5P.jQuery);