summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/ui/lib/Parser.js
blob: 46fe09cd1ae5894a663114186056b489a1c875b0 (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
var Parser = {
	integrations: [{
		type: 'image',
		regex: /\.(jpeg|jpg|gif|png|svg)(\?.*)?$/i,
		async: false,
		fetch: function(url, done) {
			done("", "")
		},
		tag: function (media) {
			return '<img src="' + media.url + '" onerror="imgError(this);">';
		}
	}, {
		type: 'youtube',
		regex:  /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i,
		async: false,
		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"
			done(id, thumb);
		},
		tag: function (media) {
			return '<img class="video" type="youtube" vid="'+media.token+'" src="'+media.thumbnail+'"><span class="playvid">&#9654;</span>';
		}
	}, {
		type: 'vimeo',
		regex: /vimeo.com\/\d+$/i,
		async: true,
		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, "") }
					done(id, result[0].thumbnail_large)
				}
			})
		},
		tag: function (media) {
			return '<img class="video" type="vimeo" vid="'+media.token+'" src="'+media.thumbnail+'"><span class="playvid">&#9654;</span>';
		}
	},
	/*
	{
		type: 'soundcloud',
		regex: /soundcloud.com\/[-a-zA-Z0-9]+\/[-a-zA-Z0-9]+\/?$/i,
		async: true,
		fetch: function (url, done) {
			$.ajax({
				type: 'GET',
				url: 'http://api.soundcloud.com/resolve.json?url=' 
					+ url
					+ '&client_id=' 
					+ '0673fbe6fc794a7750f680747e863b10',
				success: function(result) {
					done(result.id, "");
				}
			});
		},
		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,
		async: false,
		fetch: function(url, done) {
			done("", "")
		},
		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(token, thumbnail){
					cb({
						token: token,
						thumbnail: thumbnail,
						type: integration.type,
						url: url,
					})
				})
				return true
			}
			return false
		})
		if (! matched) {
			cb(null)
		}
	}
}