summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/ui/editor/MediaEditor.js
blob: 1ffe7b8a71656e6bac7bd74b7a03246dd43cffa5 (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
133
var MediaEditor = FormView.extend({
	el: "#mediaEditor",
		
	events: {
		"click [data-role=play-media]": "togglePaused",
		"mousedown [name=keyframe]": "stopPropagation",
		"mousedown": "stopPropagation",
		"change [name=keyframe]": "seek",
		"change [name=autoplay]": "setAutoplay",
		"change [name=loop]": "setLoop",
		"change [name=mute]": "setMute",
		"click [data-role=destroy-media]": "destroy",
	},
	
	initialize: function(opt){
		this.parent = opt.parent
		this.__super__.initialize.call(this)
		
		this.$name = this.$("[name=name]")
		this.$description = this.$("[name=description]")

		// image fields
		this.$widthDimension = this.$("[name=width]")
		this.$heightDimension = this.$("[name=height]")
		this.$units = this.$("[name=units]")
		
		// video fields
		this.$playButton = this.$("[data-role=play-media]")
		this.$autoplay = this.$("[name=autoplay]")
		this.$loop = this.$("[name=loop]")
		this.$mute = this.$("[name=mute]")
		this.$keyframe = this.$("[name=keyframe]")
	},
	
	toggle: function(state) {
    this.$el.toggleClass("active", state);
	},
	
	togglePaused: function(state){
		var state = this.scenery.toggle(state)
		this.$playButton.toggleClass("playing", ! state)
	},
	
	pick: function(scenery) {
		if (this.scenery) {
			this.unbind()
		}

		this.bind(scenery)
		this.$el.addClass("active")
		
		var media = scenery.media
		
		this.$name.val(media.title)
		this.$description.val(media.description)
		
		switch (media.type) {
			case "image":
				this.$(".image").show()
				this.$(".video").hide()

				this.$widthDimension.val( Number(media.widthDimension) || "" )
				this.$heightDimension.val( Number(media.heightDimension) || "" )
				this.$units.val( media.units || "cm" )
				
				break

			case "youtube":
			case "vimeo":
			case "video":
				this.$(".video").show()
				this.$(".image").hide()
				
				this.$playButton.toggleClass("paused", this.scenery.paused())
				this.$autoplay.prop('checked', !! media.autoplay)
				this.$loop.prop('checked', !! media.loop)
				this.$mute.prop('checked', !! media.mute)
				this.$keyframe.val( Number(media.keyframe || 0) )

				break
		}
	},

	hide: function(scenery){
		if (this.scenery) {
			this.unbind()
		}
		this.toggle(false)
	},
	
	seek: function(){
		var n = parseFloat( this.$keyframe.val() )
		this.scenery.seek(n)
		
		this.scenery.media.keyframe = n
	},
	setAutoplay: function(){
		var checked = this.$autoplay.prop('checked')
		this.scenery.media.autoplay = checked
		if (checked && this.scenery.paused()) {
			this.togglePaused()
		}
	},
	setLoop: function(){
		var checked = this.$loop.prop('checked')
		this.scenery.media.loop = checked
	},
	setMute: function(){
		var checked = this.$mute.prop('checked')
		this.scenery.media.mute = checked
		this.scenery.mute(checked)
	},

	bind: function(scenery){
		this.scenery = scenery
		this.scenery.mx.bound = true
	},
	
	unbind: function(){
		this.scenery.mx.bound = false
		this.scenery = null
	},
	
	destroy: function(){
		ConfirmModal.confirm("Are you sure you want to this media?", function(){
			var scenery = this.scenery
			this.hide()
			Scenery.remove(scenery.id)
		}.bind(this))
	},

})