Commit | Line | Data |
---|---|---|
50ac472f D |
1 | _kiwi.view.MediaMessage = Backbone.View.extend({ |
2 | events: { | |
3 | 'click .media_close': 'close' | |
4 | }, | |
5 | ||
6 | initialize: function () { | |
7 | // Get the URL from the data | |
8 | this.url = this.$el.data('url'); | |
9 | }, | |
10 | ||
11 | // Close the media content and remove it from display | |
12 | close: function () { | |
13 | var that = this; | |
14 | this.$content.slideUp('fast', function () { | |
15 | that.$content.remove(); | |
16 | }); | |
17 | }, | |
18 | ||
19 | // Open the media content within its wrapper | |
20 | open: function () { | |
21 | // Create the content div if we haven't already | |
22 | if (!this.$content) { | |
23 | this.$content = $('<div class="media_content"><a class="media_close"><i class="icon-chevron-up"></i> Close media</a><br /><div class="content"></div></div>'); | |
24 | this.$content.find('.content').append(this.mediaTypes[this.$el.data('type')].apply(this, []) || 'Not found :('); | |
25 | } | |
26 | ||
27 | // Now show the content if not already | |
28 | if (!this.$content.is(':visible')) { | |
29 | // Hide it first so the slideDown always plays | |
30 | this.$content.hide(); | |
31 | ||
32 | // Add the media content and slide it into view | |
33 | this.$el.append(this.$content); | |
34 | this.$content.slideDown(); | |
35 | } | |
36 | }, | |
37 | ||
38 | ||
39 | ||
40 | // Generate the media content for each recognised type | |
41 | mediaTypes: { | |
42 | twitter: function () { | |
43 | var tweet_id = this.$el.data('tweetid'); | |
44 | var that = this; | |
45 | ||
46 | $.getJSON('https://api.twitter.com/1/statuses/oembed.json?id=' + tweet_id + '&callback=?', function (data) { | |
47 | that.$content.find('.content').html(data.html); | |
48 | }); | |
49 | ||
50 | return $('<div>Loading tweet..</div>'); | |
51 | }, | |
52 | ||
53 | ||
54 | image: function () { | |
55 | return $('<a href="' + this.url + '" target="_blank"><img height="100" src="' + this.url + '" /></a>'); | |
56 | }, | |
57 | ||
390de62e | 58 | imgur: function () { |
59 | var that = this; | |
60 | ||
61 | $.getJSON('http://api.imgur.com/oembed?url=' + this.url, function (data) { | |
8ffcb297 | 62 | var img_html = '<a href="' + data.url + '" target="_blank"><img height="100" src="' + data.url + '" /></a>'; |
390de62e | 63 | that.$content.find('.content').html(img_html); |
64 | }); | |
50ac472f | 65 | |
390de62e | 66 | return $('<div>Loading image..</div>'); |
67 | }, | |
68 | ||
69 | ||
50ac472f D |
70 | reddit: function () { |
71 | var that = this; | |
72 | var matches = (/reddit\.com\/r\/([a-zA-Z0-9_\-]+)\/comments\/([a-z0-9]+)\/([^\/]+)?/gi).exec(this.url); | |
73 | ||
74 | $.getJSON('http://www.' + matches[0] + '.json?jsonp=?', function (data) { | |
75 | console.log('Loaded reddit data', data); | |
76 | var post = data[0].data.children[0].data; | |
77 | var thumb = ''; | |
78 | ||
79 | // Show a thumbnail if there is one | |
80 | if (post.thumbnail) { | |
81 | //post.thumbnail = 'http://www.eurotunnel.com/uploadedImages/commercial/back-steps-icon-arrow.png'; | |
82 | ||
83 | // Hide the thumbnail if an over_18 image | |
84 | if (post.over_18) { | |
85 | thumb = '<span class="thumbnail_nsfw" onclick="$(this).find(\'p\').remove(); $(this).find(\'img\').css(\'visibility\', \'visible\');">'; | |
86 | thumb += '<p style="font-size:0.9em;line-height:1.2em;cursor:pointer;">Show<br />NSFW</p>'; | |
87 | thumb += '<img src="' + post.thumbnail + '" class="thumbnail" style="visibility:hidden;" />'; | |
88 | thumb += '</span>'; | |
89 | } else { | |
90 | thumb = '<img src="' + post.thumbnail + '" class="thumbnail" />'; | |
91 | } | |
92 | } | |
93 | ||
94 | // Build the template string up | |
95 | var tmpl = '<div>' + thumb + '<b><%- title %></b><br />Posted by <%- author %>. '; | |
96 | tmpl += '<i class="icon-arrow-up"></i> <%- ups %> <i class="icon-arrow-down"></i> <%- downs %><br />'; | |
97 | tmpl += '<%- num_comments %> comments made. <a href="http://www.reddit.com<%- permalink %>">View post</a></div>'; | |
98 | ||
99 | that.$content.find('.content').html(_.template(tmpl, post)); | |
100 | }); | |
101 | ||
102 | return $('<div>Loading Reddit thread..</div>'); | |
9ff10506 | 103 | }, |
1cee8867 | 104 | youtube: function () { |
105 | var ytid = this.$el.data('ytid'); | |
9ff10506 | 106 | var that = this; |
b99b615e | 107 | var yt_html = '<iframe width="480" height="270" src="https://www.youtube.com/embed/'+ ytid +'?feature=oembed" frameborder="0" allowfullscreen=""></iframe>'; |
1cee8867 | 108 | that.$content.find('.content').html(yt_html); |
390de62e | 109 | |
110 | return $(''); | |
50ac472f D |
111 | } |
112 | } | |
390de62e | 113 | }, { |
50ac472f D |
114 | |
115 | // Build the closed media HTML from a URL | |
116 | buildHtml: function (url) { | |
117 | var html = '', matches; | |
118 | ||
119 | // Is it an image? | |
120 | if (url.match(/(\.jpe?g|\.gif|\.bmp|\.png)\??$/i)) { | |
121 | html += '<span class="media image" data-type="image" data-url="' + url + '" title="Open Image"><a class="open"><i class="icon-chevron-right"></i></a></span>'; | |
122 | } | |
390de62e | 123 | |
124 | // is this an imgur link? | |
125 | matches = (/imgur\.com\/[^/]*(?!=\.[^!.]+($|\?))/ig).exec(url); | |
126 | if (matches) { | |
127 | if (url.match(/(\.jpe?g|\.gif|\.bmp|\.png)\??$/i)) { | |
128 | } else { | |
129 | html += '<span class="media imgur" data-type="imgur" data-url="' + url + '" title="Open Image"><a class="open"><i class="icon-chevron-right"></i></a></span>'; | |
130 | } | |
131 | } | |
50ac472f D |
132 | |
133 | // Is it a tweet? | |
134 | matches = (/https?:\/\/twitter.com\/([a-zA-Z0-9_]+)\/status\/([0-9]+)/ig).exec(url); | |
135 | if (matches) { | |
136 | html += '<span class="media twitter" data-type="twitter" data-url="' + url + '" data-tweetid="' + matches[2] + '" title="Show tweet information"><a class="open"><i class="icon-chevron-right"></i></a></span>'; | |
137 | } | |
138 | ||
139 | // Is reddit? | |
140 | matches = (/reddit\.com\/r\/([a-zA-Z0-9_\-]+)\/comments\/([a-z0-9]+)\/([^\/]+)?/gi).exec(url); | |
141 | if (matches) { | |
142 | html += '<span class="media reddit" data-type="reddit" data-url="' + url + '" title="Reddit thread"><a class="open"><i class="icon-chevron-right"></i></a></span>'; | |
143 | } | |
9ff10506 | 144 | |
145 | // Is youtube? | |
1cee8867 | 146 | matches = (/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/gi).exec(url); |
9ff10506 | 147 | if (matches) { |
1cee8867 | 148 | html += '<span class="media youtube" data-type="youtube" data-url="' + url + '" data-ytid="' + matches[1] + '" title="YouTube Video"><a class="open"><i class="icon-chevron-right"></i></a></span>'; |
9ff10506 | 149 | } |
390de62e | 150 | |
50ac472f D |
151 | return html; |
152 | } | |
9ff10506 | 153 | }); |