summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/mx/primitives/mx.vimeo.js
blob: 5b22821a504ea6b276ca45caaa7fad1e1d8e5961 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
MX.Vimeo = MX.Object3D.extend({

  init: function (ops) {
  
  	this.type = "Vimeo"

		this.media = ops.media
		this.width = ops.media.width
		this.height = ops.media.height
		this.x = ops.x || 0
		this.y = ops.y || 0
		this.z = ops.z || 0
		this.rotationX = ops.rotationX || 0
		this.rotationY = ops.rotationY || 0
		this.rotationZ = ops.rotationZ || 0
		this.scale = ops.scale || 1
		this.backface = ops.backface || false
		
		ops.className && this.el.classList.add(ops.className)
		this.backface && this.el.classList.add("backface-visible")		
		this.el.classList.add("video")
		this.el.classList.add("mx-scenery")
  	this.paused = !! this.media.autoplay
		this.muted = app.muted || !! this.media.mute
		this.started = false
		
		this.load()
  },

  load: function (ops) {
  	var uid = 'player-' + Uid ()
  	var loop = this.media.loop ? 'loop=1' : ""
  	var preload = document.createElement("iframe")
  	preload.id = uid
  	preload.setAttribute("src", "//player.vimeo.com/video/" + this.media.token + "?api=1&badge=0&controls=0branding=0&byline=0&portrait=0&title=0&" + loop + "&player_id=" + uid)
  	preload.style.backgroundImage = "url(" + this.media.thumbnail + ")"
  	preload.style.width = this.media.width + "px"
  	preload.style.height = this.media.height + "px"
  	preload.style.border = "0"
  	preload.style.pointerEvents = "none"
  	preload.className = "preload"
  	this.el.appendChild(preload)
  	this.player = $f(preload)

  	this.player.addEvent('ready', this.ready.bind(this))
  },
  
  ready: function(){
  	console.log("vimeo ready")
		this.started = true

  	// wait until ready before binding events. other events: play, pause
  	this.player.addEvent('play', this.onPlay.bind(this))
  	this.player.addEvent('pause', this.onPause.bind(this))
  	this.player.addEvent('finish', this.finished.bind(this))

  	// this is async on vimeo so call it asap
  	this.player.api('getDuration', function(n){
  		console.log("vimeo duration", n)
  		this.player.duration = n
  	}.bind(this))

		if (this.media.mute) {
			this.mute()
		}
		
		this.seek( this.media.keyframe || 0 )

		if (this.media.autoplay) {
			this.play()
		}
		else {
			this.pause()
		}
  },
  
  error: function(err){
  	console.log("vimeo error", err)
  },
  
  play: function(){
  	this.paused = false
  	this.player.api('play')
  },
  
  pause: function(){
  	this.paused = true
  	this.player.api('pause')
  },
  
  seek: function(n){
  	// defer seek until we have duration
  	if (! this.duration()) {
  		setTimeout(function(){
  			this.seek(n)
  		}.bind(this), 300)
  		return
  	}
  	
  	if (! this.started || n === 0) {
	  	return
  	}
  	
		if (n < 1) {
			n = n * this.duration()
		}
		this.player.api('seekTo', max(0, n-1))
		if (this.paused) {
			this.paused = false
			this.play()
			this.pause()
			setTimeout(function(){
				this.pause()
			}.bind(this), 100)
		}
  },
  
  duration: function(){
		return this.player.duration
  },

  mute: function(){
		this.player.api('setVolume', 0.0)
  	this.muted = true
  },
  
  unmute: function(){
		this.player.api('setVolume', 0.8)
  	this.muted = false
  },
  
  setLoop: function(state){
		this.media.loop = state
		this.player.api('setLoop', state)
  },
  
  onPlay: function(){
  	if (this.paused) {
  		this.pause()
  	}
  },
  
  onPause: function(){
  	if (! this.paused) {
  		this.play()
  	}
  },
  
  finished: function(){
//   	if (this.media.loop) {
//   		this.seek(0)
//   		this.play()
//   	}
//   	else if (this.bound) {
  	if (! this.media.loop && this.bound) {
  		$(".playButton").removeClass('playing')
  	}
  }
  
})