summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/ui/editor/MediaViewer.js
blob: e48c5b6327b86dd52684ce97da4263acdac18ffb (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
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
		}
		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(){})
	},

})

//   function placeMedia(evt, img) {
//   	// JULES DO YO THANG
//   	alert('Place media at (' + evt.pageX + ', ' + evt.pageY + ')');
//   }
// 
// 	$('.mediaContainer img').mousedown(function(e){
// 		e.preventDefault()
// 		e.stopPropagation()
// 	})
//   $('.mediaContainer img').click(function (e) {
// 		e.stopPropagation()
//   	$(".mediaDrawer, .fileUpload, .addMedia").removeClass("active icon-close");
//   	$floatingImg.attr('src', $(this).attr('src'));
//   	function _followCursor(e) {
//   		$floatingImg.parent().css({
//   			top: (e.pageY - ($floatingImg.height() / 2)) + 'px',
//   			left: (e.pageX - ($floatingImg.width() / 2)) + 'px'
//   		});
//   	}
//   	$(window).on('mousemove', _followCursor);
//   	$(window, this).one('click', function () {
//   		$floatingImg.attr('src', '');
//   		$(window).off('mousemove', _followCursor);
//   		$floatingImg.parent().removeClass('edit');
//   	});
//   	$floatingImg.parent().addClass('edit');
//   	_followCursor(e);
//   });