summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/ui/editor/MediaViewer.js
blob: 4ae6f97ff2ad0bc9b11fd986dc42f41f333a42f6 (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
var MediaViewer = ModalView.extend({
	el: ".mediaDrawer.mediaViewer",
	destroyAction: "/api/media/destroy",
	
	events: {
		'click .foundToggle': "foundToggle",
		'click .yourMedia': "userToggle",
		'click #deleteMedia': "deleteArmed",
		'mousedown .mediaContainer': "pick",
	},

	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)		
	},

	deleteIsArmed: false,
	deleteArmed: function(e, state){
		if (typeof state != "boolean") {
			state = ! this.deleteIsArmed
		}
		console.log(state)
		this.deleteIsArmed = state
		$("body").toggleClass("deleteArmed", state)
	},
	
	pick: function(e){
		var target = e.currentTarget
		var $target = $(target)
		var media = $target.data('media')

		if (this.deleteIsArmed) {
			this.destroy(media._id)
			$target.remove()
		}
		else {
			// pick this image ...
		}
	},

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

})