Blame view

static/plugins/webshim-gh-pages/js-webshim/dev/shims/jme/alternate-media.js 4.29 KB
831eac332   zhuzhenchao   add file
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
  webshim.ready('jmebase es5', function(){
  	var webshim = window.webshim;
  	var $ = webshim.$;
  	var jme = $.jme;
  
  	jme.defineProp('currentAlternateSrc', {
  		readonly: true,
  		get: function(elem){
  			var ret, prop;
  			var data = jme.data(elem);
  			var alternateSrces = data.media.jmeFn('getAlternateSrcData');
  			var currentSrc = data.media.prop('currentSrc');
  
  			if(currentSrc){
  				$.each(alternateSrces.groups, function(groupName, group){
  					if(group.srces.indexOf(currentSrc) != -1){
  						ret = group;
  					}
  				});
  			} else {
  				for(prop in alternateSrces.groups){
  					ret = alternateSrces[prop];
  					break;
  				}
  			}
  			return ret;
  		}
  	});
  
  
  	jme.defineMethod('getAlternateSrcData', function(srces){
  		var ret = {groups: {}, groupLength: 0};
  		var data = jme.data(this);
  
  		if(!srces){
  			srces = data.media.jmeProp('srces');
  		}
  
  		srces.forEach(function(src){
  			var group = src['data-alternate'];
  
  			if(!ret.groups[group]){
  				ret.groupLength++;
  				ret.groups[group] = {
  					group: group,
  					label: group,
  					srces: []
  				};
  			}
  
  			if(src['data-label'] || src.title){
  				ret.groups[group].label = src['data-label'] || src.title;
  			}
  
  			ret.groups[group].srces.push(src.src);
  		});
  		ret.srces = srces;
  		return ret;
  
  	});
  
  	jme.defineMethod('orderAlternateByName', function(names, srces){
  		var data = jme.data(this);
  
  		if(!srces){
  			srces = data.media.jmeProp('srces');
  		}
  		if(!Array.isArray(names)){
  			names = [names];
  		}
  		names.reverse();
  		srces.sort(function(src1, src2){
  			return names.indexOf(src2['data-alternate']) - names.indexOf(src1['data-alternate']);
  		});
  		data.media.jmeProp('srces', srces);
  		return srces;
  	});
  
  	jme.defineMethod('switchAlternateByName', function(names, srces){
  		var data = jme.data(this);
  		var isPaused = data.media.prop('paused');
  		var currentTime = data.media.prop('currentTime');
  		$(this).jmeFn('orderAlternateByName', names, srces);
  
  		if(currentTime > 1){
  			data.media.play();
  			data.media.one('loadedmetadata', function(){
  				if(isPaused){
  					data.media.pause();
  				}
  				data.media.prop('currentTime', currentTime);
  			});
  		}
  		if(!isPaused){
  			data.media.play();
  		}
  	});
  
  
  	function flattenSrces(src){
  		return src.src || src;
  	}
  
  	function isSameSrces(newSrces, oldScres){
  		var ret = true;
  		if(oldScres && newSrces.length === oldScres.length){
  			newSrces = newSrces.map(flattenSrces);
  			$.each(oldScres, function(i, src){
  				if(newSrces.indexOf(src.src || src) == -1){
  					ret = false;
  					return false;
  				}
  			});
  		} else {
  			ret = false;
  		}
  		return ret;
  	}
  
  	var radioGroup = 0;
  
  	function createGroupList(group){
  		return '<li role="presentation"><label><input type="radio" name="alternate-'+ radioGroup +'" value="'+ group.group +'" /><span>'+ group.label +'</span></label></li>';
  	}
  
  	jme.addToConfigmenu('alternate-media', function($container, media, base, menu){
  		var $menu, timer, alternateSrcData;
  
  		var itemChange = function(e){
  			var value = $.prop(e.target, 'value');
  			if(value && alternateSrcData && alternateSrcData.groups[value]){
  				media.jmeFn('switchAlternateByName', value, alternateSrcData.srces);
  				menu.hide();
  			}
  		};
  		var updateMenuItem = function(){
  			var current;
  			if($menu){
  				current = media.jmeProp('currentAlternateSrc');
  				if(current && current.group){
  					$('[value="'+ current.group +'"]', $menu).prop('checked', true);
  				}
  
  			}
  		};
  		var updateMenu = function(data){
  			if(!$menu || !alternateSrcData || !isSameSrces(data.srces, alternateSrcData.srces)){
  				alternateSrcData = data;
  				radioGroup++;
  
  				var list = $.map(alternateSrcData.groups, createGroupList);
  				if($menu){
  					$menu.remove();
  				}
  				$menu = $('<div class="media-submenu"><ul>'+ list.join('') +'</ul></div>').appendTo($container);
  				$menu.on('change', itemChange);
  			}
  			updateMenuItem();
  		};
  
  		var addRemoveMenu = function(){
  			var data = media.jmeFn('getAlternateSrcData');
  			if(data.groupLength){
  				updateMenu(data);
  			} else if($menu) {
  				$menu.remove();
  				$menu = null;
  				alternateSrcData = null;
  			}
  		};
  		var timedAddRemove = function(){
  			clearTimeout(timer);
  			timer = setTimeout(addRemoveMenu);
  		};
  		addRemoveMenu();
  		media.on('loadstart emptied', timedAddRemove);
  		media.on('loadedmetadata', updateMenuItem);
  	});
  
  
  	webshim.ready('jme-base', function(){
  		webshim.isReady('alternate-media', true);
  	});
  });