summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/ui/lib/Parser.js
blob: d68f58b770611dd8ce2709703370b41ef6000f1d (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
var Parser = {
	integrations: [{
		type: 'image',
		regex: /\.(jpeg|jpg|gif|png|svg)(\?.*)?$/i,
		fetch: function(url, done) {
			var img = new Image ()
			img.onload = function(){
				if (!img) return
				var width = img.naturalWidth, height = img.naturalHeight
				img = null
				done({
					url: url,
					type: "image",
					token: "",
					thumbnail: "",
					title: "",
					width: width,
					height: height,
				})
			}
			img.src = url
			if (img.complete) {
				img.onload()
			}
		},
		tag: function (media) {
			return '<img src="' + media.url + '">';
		}
	}, {
		type: 'video',
		regex: /\.(mp4|webm)(\?.*)?$/i,
		fetch: function(url, done) {
			var video = document.createElement("video")
			video.addEventListener("loadedmetadata", function(){
				var width = video.videoWidth, height = video.videoHeight
				video = null
				done({
					url: url,
					type: "video",
					token: "",
					thumbnail: "",
					title: "",
					width: width,
					height: height,
				})
			})
			video.src = url
			video.load()
		},
		tag: function (media) {
			return '<video src="' + media.url + '">';
		}
	}, {
		type: 'youtube',
		regex:  /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i,
		fetch: function(url, done) {
			var id = (url.match(/v=([-_a-zA-Z0-9]{11})/i) || url.match(/youtu.be\/([-_a-zA-Z0-9]{11})/i) || url.match(/embed\/([-_a-zA-Z0-9]{11})/i))[1].split('&')[0];
			var thumb = "http://i.ytimg.com/vi/" + id + "/hqdefault.jpg"
			$.ajax({
				type: 'GET',
				url: 'https://www.googleapis.com/youtube/v3/videos',
				dataType: "jsonp",
				data: {
					id: id,
					key: "AIzaSyDYPKGD0-_VRBWpUYRmX8Qg6BtWmcPU_cM",
					part: "id,contentDetails,snippet,status",
				},
				success: function(result){
					var res = result.items[0]
					done({
						url: url,
						type: "youtube",
						token: id,
						thumbnail: thumb,
						title: res.snippet.title,
						width: 640,
						height: 360,
					})
				}
			})
		},
		tag: function (media) {
			// return '<img class="video" type="youtube" vid="'+media.token+'" src="'+media.thumbnail+'"><span class="playvid">&#9654;</span>';
			return '<div class="video" style="width: ' + media.width + 'px; height: ' + media.height + 'px; overflow: hidden; position: relative;"><iframe frameborder="0" scrolling="no" seamless="seamless" webkitallowfullscreen="webkitAllowFullScreen" mozallowfullscreen="mozallowfullscreen" allowfullscreen="allowfullscreen" id="okplayer" width="' + media.width + '" height="' + media.height + '" src="http://youtube.com/embed/' + media.token + '?showinfo=0" style="position: absolute; top: 0px; left: 0px; width: ' + media.width + 'px; height: ' + media.height + 'px;"></iframe></div>'
		}
	}, {
		type: 'vimeo',
		regex: /vimeo.com\/\d+$/i,
		fetch: function(url, done) {
			var id = url.match(/\d+$/i)[0];
			$.ajax({
				type: 'GET',
				url: 'http://vimeo.com/api/v2/video/' + id + '.json',
				success: function(result){
					if (result.length == 0) { return done(id, "", 640, 360) }
					var res = result[0]
					if (res.embed_privacy != "anywhere") {
					  AlertModal.alert("Sorry, the author of this video has marked it private, preventing it from being embedded.", function(){})
					  return
					}
					done({
						url: url,
						type: "vimeo",
						token: id,
						thumbnail: res.thumbnail_large,
						title: res.title,
						width: res.width,
						height: res.height,
					})
				}
			})
		},
		tag: function (media) {
			// return '<img class="video" type="vimeo" vid="'+media.token+'" src="'+media.thumbnail+'"><span class="playvid">&#9654;</span>';
			return '<div class="video" style="width: ' + media.width + 'px; height: ' + media.height + 'px; overflow: hidden; position: relative;"><iframe frameborder="0" scrolling="no" seamless="seamless" webkitallowfullscreen="webkitAllowFullScreen" mozallowfullscreen="mozallowfullscreen" allowfullscreen="allowfullscreen" id="okplayer" src="http://player.vimeo.com/video/' + media.token + '?api=1&title=0&byline=0&portrait=0&playbar=0&player_id=okplayer&loop=0&autoplay=0" width="' + media.width + '" height="' + media.height + '" style="position: absolute; top: 0px; left: 0px; width: ' + media.width + 'px; height: ' + media.height + 'px;"></iframe></div>'
		}
	},
	/*
	{
		type: 'soundcloud',
		regex: /soundcloud.com\/[-a-zA-Z0-9]+\/[-a-zA-Z0-9]+\/?$/i,
		fetch: function (url, done) {
			$.ajax({
				type: 'GET',
				url: 'http://api.soundcloud.com/resolve.json?url=' 
					+ url
					+ '&client_id=' 
					+ '0673fbe6fc794a7750f680747e863b10',
				success: function(result) {
					done({
						url: url,
						type: "soundcloud",
						token: result.id,
						thumbnail: "",
						title: "",
						width: 100,
						height: 100,
					})
				}
			});
		},
		tag: function (media) {
			return '<iframe width="400" height="166" scrolling="no" frameborder="no"' +
					'src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/' + media.token +
					'&amp;color=ff6600&amp;auto_play=false&amp;show_artwork=true"></iframe>'
		}
	}, {
		type: 'link',
		regex: /^http.+/i,
		fetch: function(url, done) {
			done({
				url: url,
				type: "link",
				token: "",
				thumbnail: "",
				title: "",
				width: 100,
				height: 100,
			})
		},
		tag: function (media) {
			return '<a href="' + media.url + '" target="_blank">' + media.url + '</a>'
		}
	}
	*/
	],

	parse: function (url, cb) {
		var matched = Parser.integrations.some(function(integration){
			if (integration.regex.test(url)) {
				integration.fetch(url, function(res){
					cb(res)
				})
				return true
			}
			return false
		})
		if (! matched) {
			cb(null)
		}
	},
	
	tag: function (media){
	  if (media.type in Parser.lookup) {
	    return Parser.lookup[media.type].tag(media)
	  }
	  return ""
	},

	loadImage: function(url, cb, error){
		if (Parser.lookup.image.regex.test(url)) {
			Parser.lookup.image.fetch(url, function(media){
				cb(media)
			})
		}
		else error && error()
	},
	
  thumbnail: function (media) {
    return '<img src="' + (media.thumbnail || media.url) + '" class="thumb">';
  },

};
Parser.lookup = _.indexBy(Parser.integrations, 'type');