summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/ui/editor/MediaViewer.js
blob: 3911d3b9b52f653010484060ff148026847839f4 (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
var MediaViewer = ModalView.extend({
	el: ".mediaDrawer.mediaViewer",
	createAction: "/api/docs/new",
	updateAction: "/api/docs/edit",
	destroyAction: "/api/docs/destroy",
	
	events: {
		'click .foundToggle': "foundToggle",
		'click .yourMedia': "userToggle",
	},

	foundToggle: function(){
    $(".foundMedia").addClass("active");
    $(".myMedia").addClass("inactive");
    $('a').removeClass("active");
    $('.foundToggle').addClass("active");
	},
	
	userToggle: function(){
    this.$(".foundMedia").removeClass("active");
    this.$(".myMedia").removeClass("inactive");
    this.$('a').removeClass("active");
    this.$('.yourMedia').addClass("active");
	},

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

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

	populate: function(data){
		this.loaded = true
		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(){})
	},

})