Blame view

static/plugins/webshim-gh-pages/js-webshim/dev/shims/mediacapture.js 4.36 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
  webshim.register('mediacapture', function($, webshim, window, document, undefined, featureOptions){
  	"use strict";
  	var hasCamera = -1;
  	var checkCameras = $.noop;
  	var sel = 'input[type="file"].ws-filereader, input[type="file"].ws-capture';
  	var cameraListPromise = $.Deferred();
  
  	//
  
  	(function(){
  		var tmp;
  
  		var hasNativeUserMedia = !!(navigator.getUserMedia && !navigator.wsGetUserMedia);
  		var hasFlash = swfmini.hasFlashPlayerVersion('11.3');
  		var writeToStorage = function(){
  			try{
  				sessionStorage.setItem('wsCameras', JSON.stringify(hasCamera));
  			} catch (e){}
  		};
  		var reject = function(){
  			hasCamera = 0;
  			writeToStorage();
  			cameraListPromise.reject(hasCamera);
  		};
  		var resolve = function(){
  			writeToStorage();
  			cameraListPromise.resolve(hasCamera);
  		};
  		try {
  			tmp = JSON.parse(sessionStorage.getItem('wsCameras'));
  			if(tmp == null){
  				hasCamera = -1;
  			}
  		} catch(e){}
  
  		if(hasCamera === 0 || (hasCamera == -1 && !hasNativeUserMedia && !hasFlash)){
  			reject();
  		} else if(hasFlash){
  			checkCameras = function(){
  				var mediaOptions = webshim.cfg.mediaelement;
  				var playerSwfPath = mediaOptions.playerPath || (webshim.cfg.basePath + "swf/" + (mediaOptions.playerName || 'JarisFLVPlayer.swf'));
  				var id = 'wscameralistdetection';
  				var vars = {
  					controltype: '1',
  					jsapi: '1',
  					source: '',
  					id: id,
  					evtId: id
  				};
  				var attrs = {
  					id: id,
  					name: id
  				};
  				var params = {
  					allowscriptaccess: 'always',
  					allowNetworking: 'all'
  				};
  				var $dom = $('<div><div id="'+ id +'"></div></div>')
  					.css({position: 'absolute', left: -999, width: 5, height: 5, overflow: 'hidden'})
  					.appendTo('body')
  				;
  
  				webshim.mediaelement.jarisEvent = webshim.mediaelement.jarisEvent || {};
  				webshim.mediaelement.jarisEvent[id] = function(jaris){
  					hasCamera = jaris.cameras;
  					$dom.remove();
  					if(hasCamera){
  						resolve();
  					} else {
  						reject();
  					}
  				};
  
  
  				checkCameras = $.noop;
  				swfmini.embedSWF(playerSwfPath, id, "100%", "100%", "11.3", false, vars, params, attrs);
  			};
  
  		} else {
  			hasCamera = -1;
  			resolve();
  		}
  	})();
  	var regImage = /image\/\*|image\/jp/i;
  	var loadPicker = function(){
  		webshim.ready('WINDOWLOAD', function(){
  			webshim.loader.loadList(['mediacapture-picker']);
  		});
  		loadPicker = $.noop;
  	};
  
  
  	var _createPhotoPicker = function(){
  		if($(this).is('[capture].ws-filereader, .ws-capture') && webshim.implement(this, 'capture')){
  			var $wrapper, $customFile, $button, popover;
  			var $fileinput = $(this);
  			var accept = $fileinput.prop('accept') || 'image/*';
  
  			if(!regImage.test(accept)){return;}
  
  			$button = $('<button type="button" class="ws-capture-button" />');
  			popover = webshim.objectCreate(webshim.wsPopover, {}, $.extend({prepareFor: $button}));
  
  			popover.element.addClass('capture-popover input-picker');
  
  			if($fileinput.is('.ws-custom-file > *')){
  				$customFile = $fileinput.closest('.ws-custom-file');
  				$wrapper = $('<div class="ws-capture-file" />').insertAfter($customFile);
  				$wrapper.append($button).append($customFile);
  			} else {
  				$fileinput.before($button);
  			}
  			popover.element.insertAfter($button);
  
  			$button.on('click', function(){
  				webshim.mediacapture.showContent($fileinput, $button, popover);
  				popover.show();
  			});
  			loadPicker();
  		}
  	};
  	var createPhotoPicker = function (){
  		var elem = this;
  		checkCameras();
  
  		cameraListPromise.done(function(){
  			_createPhotoPicker.call(elem);
  		});
  	};
  
  	webshim.mediacapture = {
  		showContent: function($fileinput, $button, popover){
  			webshim.loader.loadList(['mediacapture-picker']);
  			webshim.ready('mediacapture-picker', function(){
  				webshim.mediacapture.showContent($fileinput, $button, popover);
  			});
  		}
  	};
  	webshim.defineNodeNamesBooleanProperty('input', 'capture');
  
  	if(hasCamera){
  
  		cameraListPromise.done(function(){
  			createPhotoPicker = _createPhotoPicker;
  			webshim.loader.addModule('mediacapture-picker', {
  				noAutoCallback: true,
  				css: 'styles/forms-picker.css',
  				options: featureOptions
  			});
  
  		});
  
  		webshim.addReady(function(context, insertedElement){
  			$(sel, context).add(insertedElement.filter(sel))
  				.filter('[accept*="image"], :not([accept]), [accept=""]')
  				.each(createPhotoPicker)
  			;
  		});
  
  		webshim.ready('WINDOWLOAD', checkCameras);
  	}
  	if(document.readyState == 'complete'){
  		webshim.isReady('WINDOWLOAD', true);
  	}
  });