summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/ui/editor/MediaViewer.js
blob: 96d7cf7c5494ed9b8e6ec813c50e5ac906869621 (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
var MediaViewer = ModalView.extend({
	el: ".mediaDrawer.mediaViewer",
	createAction: "/api/docs/new",
	updateAction: "/api/docs/edit",
	destroyAction: "/api/docs/destroy",

	show: function(){
		if (! this.loaded) {
			this.load()
		}
		else {
			this.__super__.show.call(this)
		}
	},

	load: function(){
		$.get("/api/media/user", $.proxy(this.populate, this))
	},

	populate: function(data){
		data.forEach($.proxy(this.add, this))

		this.__super__.show.call(this)
	},
	
	add: function(media){
		var image = new Image ()
		var $span = $("<span>")
		$span.addClass("mediaContainer")
		switch (media.type) {
			case 'image':
				image.src = media.url
				break

			case 'youtube':
			case 'vimeo':
				image.src = media.thumbnail
				break
		}
		
		$span.data("media", media)
		$span.append(image)

		this.$(".myMedia").prepend($span)		
	},

	destroy: function(name, cb){
		$.ajax({
			type: "delete",
			url: this.destroyAction,
			data: { name: name, _csrf: $("[name=_csrf]").val() }
		}).complete(cb || function(){})
	},

})