Files
Disco/Disco.Web/ClientSource/Scripts/Modules/Disco-AttachmentUploader.min.js
T
Gary Sharp c846fa053a Dialog height reduced & remove js minify maps
Provide better support for lower resolution devices (buttons became
hidden in tall dialogs). Remove references to JavaScript minification
source maps which aren't deployed and caused confusion.
2014-09-08 14:51:51 +10:00

1 line
11 KiB
JavaScript

var Webcam={version:"1.0.0",protocol:location.protocol.match(/https/i)?"https":"http",swfURL:"",loaded:!1,live:!1,userMedia:!0,params:{width:0,height:0,dest_width:0,dest_height:0,image_format:"jpeg",jpeg_quality:90,force_flash:!1},hooks:{load:null,live:null,uploadcomplete:null,uploadprogress:null,error:function(n){alert("Webcam.js Error: "+n)}},init:function(){navigator.getUserMedia=navigator.getUserMedia||navigator.webkitGetUserMedia||navigator.mozGetUserMedia||navigator.msGetUserMedia;window.URL=window.URL||window.webkitURL||window.mozURL||window.msURL;this.userMedia=this.userMedia&&!!navigator.getUserMedia&&!!window.URL;navigator.userAgent.match(/Firefox\D+(\d+)/)&&parseInt(RegExp.$1,10)<21&&(this.userMedia=null)},attach:function(n){var t,i,r,u,f,e;if(typeof n=="string"&&(n=document.getElementById(n)||document.querySelector(n)),!n)return this.dispatch("error","Could not locate DOM element to attach to.");this.container=n;this.params.width||(this.params.width=n.offsetWidth);this.params.height||(this.params.height=n.offsetHeight);this.params.dest_width||(this.params.dest_width=this.params.width);this.params.dest_height||(this.params.dest_height=this.params.height);this.params.force_flash&&(this.userMedia=null);this.userMedia?(t=document.createElement("video"),t.setAttribute("autoplay","autoplay"),t.style.width=""+this.params.dest_width+"px",t.style.height=""+this.params.dest_height+"px",i=this.params.width/this.params.dest_width,r=this.params.height/this.params.dest_height,(i!=1||r!=1)&&(n.style.overflow="visible",t.style.webkitTransformOrigin="0px 0px",t.style.mozTransformOrigin="0px 0px",t.style.msTransformOrigin="0px 0px",t.style.oTransformOrigin="0px 0px",t.style.transformOrigin="0px 0px",t.style.webkitTransform="scaleX("+i+") scaleY("+r+")",t.style.mozTransform="scaleX("+i+") scaleY("+r+")",t.style.msTransform="scaleX("+i+") scaleY("+r+")",t.style.oTransform="scaleX("+i+") scaleY("+r+")",t.style.transform="scaleX("+i+") scaleY("+r+")"),n.appendChild(t),this.video=t,u=document.createElement("canvas"),u.width=this.params.dest_width,u.height=this.params.dest_height,f=u.getContext("2d"),this.context=f,this.canvas=u,e=this,navigator.getUserMedia({audio:!1,video:!0},function(n){t.src=window.URL.createObjectURL(n)||n;Webcam.stream=n;Webcam.loaded=!0;Webcam.live=!0;Webcam.dispatch("load");Webcam.dispatch("live")},function(){return e.dispatch("error","Could not access webcam.")})):n.innerHTML=this.getSWFHTML()},reset:function(){if(this.userMedia){try{this.stream.stop()}catch(n){}delete this.stream;delete this.canvas;delete this.context;delete this.video}this.container.innerHTML="";delete this.container;this.loaded=!1;this.live=!1},set:function(){if(arguments.length==1)for(var n in arguments[0])this.params[n]=arguments[0][n];else this.params[arguments[0]]=arguments[1]},on:function(n,t){if(n=n.replace(/^on/i,"").toLowerCase(),typeof this.hooks[n]=="undefined")throw"Event type not supported: "+n;this.hooks[n]=t},dispatch:function(){var n=arguments[0].replace(/^on/i,"").toLowerCase(),t=Array.prototype.slice.call(arguments,1);return this.hooks[n]?(typeof this.hooks[n]=="function"?this.hooks[n].apply(this,t):typeof this.hooks[n]=="array"?this.hooks[n][0][this.hooks[n][1]].apply(this.hooks[n][0],t):window[this.hooks[n]]&&window[this.hooks[n]].apply(window,t),!0):!1},setSWFLocation:function(n){this.swfURL=n},getSWFHTML:function(){var i,u,t,f,r,n,e;if(location.protocol.match(/file/))return'<h1 style="color:red">Sorry, the Webcam.js Flash fallback does not work from local disk. Please upload it to a web server first.<\/h1>';if(!this.swfURL){for(i="",u=document.getElementsByTagName("script"),t=0,f=u.length;t<f;t++)r=u[t].getAttribute("src"),r&&r.match(/\/webcam(\.min)?\.js/)&&(i=r.replace(/\/webcam(\.min)?\.js.*$/,""),t=f);this.swfURL=i?i+"/webcam.swf":"webcam.swf"}window.localStorage&&!localStorage.getItem("visited")&&(this.params.new_user=1,localStorage.setItem("visited",1));n="";for(e in this.params)n&&(n+="&"),n+=e+"="+escape(this.params[e]);return""+('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="'+this.protocol+'://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+this.params.width+'" height="'+this.params.height+'" id="webcam_movie_obj" align="middle"><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="false" /><param name="movie" value="'+this.swfURL+'" /><param name="loop" value="false" /><param name="menu" value="false" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><param name="flashvars" value="'+n+'"/><embed id="webcam_movie_embed" src="'+this.swfURL+'" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="'+this.params.width+'" height="'+this.params.height+'" name="webcam_movie_embed" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="'+n+'"><\/embed><\/object>')},getMovie:function(){if(!this.loaded)return this.dispatch("error","Flash Movie is not loaded yet");var n=document.getElementById("webcam_movie_obj");return n&&n._snap||(n=document.getElementById("webcam_movie_embed")),n||this.dispatch("error","Cannot locate Flash movie in DOM"),n},snap:function(){if(!this.loaded)return this.dispatch("error","Webcam is not loaded yet");if(!this.live)return this.dispatch("error","Webcam is not live yet");if(this.userMedia)return this.context.drawImage(this.video,0,0,this.params.dest_width,this.params.dest_height),this.canvas.toDataURL("image/"+this.params.image_format,this.params.jpeg_quality/100);var n=this.getMovie()._snap();return"data:image/"+this.params.image_format+";base64,"+n},configure:function(n){n||(n="camera");this.getMovie()._configure(n)},flashNotify:function(n,t){switch(n){case"flashLoadComplete":this.loaded=!0;this.dispatch("load");break;case"cameraLive":this.live=!0;this.dispatch("live");break;case"error":this.dispatch("error",t)}},b64ToUint6:function(n){return n>64&&n<91?n-65:n>96&&n<123?n-71:n>47&&n<58?n+4:n===43?62:n===47?63:0},base64DecToArr:function(n,t){for(var s=n.replace(/[^A-Za-z0-9\+\/]/g,""),r=s.length,h=t?Math.ceil((r*3+1>>2)/t)*t:r*3+1>>2,c=new Uint8Array(h),u,f,e=0,o=0,i=0;i<r;i++)if(f=i&3,e|=this.b64ToUint6(s.charCodeAt(i))<<18-6*f,f===3||r-i==1){for(u=0;u<3&&o<h;u++,o++)c[o]=e>>>(16>>>u&24)&255;e=0}return c},upload:function(n,t,i){var f,u,o,r,s,e;if(i)Webcam.on("uploadComplete",i);if(f="webcam",u="",n.match(/^data\:image\/(\w+)/))u=RegExp.$1;else throw"Cannot locate image format in Data URI";o=n.replace(/^data\:image\/\w+\;base64\,/,"");r=new XMLHttpRequest;r.open("POST",t,!0);r.upload&&r.upload.addEventListener&&r.upload.addEventListener("progress",function(n){if(n.lengthComputable){var t=n.loaded/n.total;Webcam.dispatch("uploadProgress",t,n)}},!1);r.onload=function(){Webcam.dispatch("uploadComplete",r.status,r.responseText,r.statusText)};s=new Blob([this.base64DecToArr(o)],{type:"image/"+u});e=new FormData;e.append(f,s,f+"."+u.replace(/e/,""));r.send(e)}};Webcam.init(),function(n,t,i,r){"use strict";var u=function(u,f,e){var o=this;if(o.uploadUrl=u,o.dropTarget=f,o.uploadProgressContainer=e,o._uploadFilesInput=null,o.uploadFiles=function(){!o._uploadFilesInput||o._uploadFilesInput.remove();o._uploadFilesInput=i("<input>");o._uploadFilesInput.attr({type:"file",multiple:"multiple",title:"Disco File Uploading"}).hide().change(function(n){var t=n.target.files;!!t&&t.length>0&&o._uploadFiles(t);o._uploadFilesInput.remove()}).appendTo(o.uploadProgressContainer).click()},!!o.dropTarget){var c=i(t),s=!1,h=null;c.on("dragover",function(){o.dropTarget.addClass("dragHighlight");o.dropTarget.removeClass("dragHover");s=!1});c.on("dragleave",function(){!h||n.clearInterval(h);s=!0;n.setTimeout(function(){s&&o.dropTarget.removeClass("dragHighlight");h=null},200)});o.dropTarget.on("dragover",function(n){n.stopPropagation();n.preventDefault();o.dropTarget.addClass("dragHover");s=!1;n.originalEvent.dataTransfer.dropEffect="copy"});o.dropTarget.on("drop",function(n){n.stopPropagation();n.preventDefault();s=!0;o.dropTarget.removeClass("dragHighlight");var t=n.originalEvent.dataTransfer.files;o._uploadFiles(t)})}return o.uploadImage=function(){function s(){var n=r.snap();o._uploadImage(n)}var u=720,f=540,t=i("<div>").attr({id:"Disco_AttachmentUpload_ImageDialog",title:"Upload Image","class":"dialog Disco-AttachmentUpload-ImageDialog"}),e;t.dialog({autoOpen:!0,draggable:!1,modal:!0,resizable:!1,width:u,height:f,close:function(){r.reset();n.setTimeout(function(){t.dialog("destroy")},1)}}).closest(".ui-dialog").children(".ui-dialog-titlebar").css("border-bottom","none");e=[{text:"Capture",click:s}];r.set({width:u,height:f,dest_width:u*1.5,dest_height:f*1.5,jpeg_quality:95});r.setSWFLocation("/ClientSource/Scripts/Modules/Disco-AttachmentUploader/webcam.swf");r.on("error",function(n){alert(n);t.dialog("close")});r.on("live",function(){t.dialog("option","buttons",e);t.closest(".ui-dialog").children(".ui-dialog-buttonpane").css("margin-top",0).find(".ui-button:first").focus()});r.attach(t.attr("id"))},o.getFileComments=function(n,t,r){var f=!1,u=i("<div>").attr({title:"Upload File","class":"dialog Disco-AttachmentUpload-CommentDialog"}),e,o,s;u.html('<table><tr><th>File Name:<\/th><td class="filename"><\/td><\/tr><tr><th>Comments:<\/th><td><input class="comments" type="text"><\/input><\/td><\/tr><tr><td class="thumbnail" colspan="2"><img /><\/td><\/tr><\/table>');!t||(e=u.find("td.thumbnail"),o=e.find("img"),t(o)&&e.show());u.find("td.filename").text(n).attr("title",n);s=u.find("input.comments").keypress(function(n){n.which===13&&(f=!0,u.dialog("close"))});u.dialog({resizable:!1,width:400,modal:!0,autoOpen:!0,buttons:{Upload:function(){f=!0;u.dialog("close")},Cancel:function(){u.dialog("close")}},close:function(){var n=s.val();u.dialog("destroy").remove();r(f,n)}})},o._uploadImage=function(n){o._hideFlashVideoOverlay()&&i("#webcam_movie_obj, #webcam_movie_embed").css("display","none");var u=n.replace(/^data\:image\/\w+\;base64\,/,""),f=new Blob([r.base64DecToArr(u)],{type:"image/jpeg"}),t="CapturedImage-"+moment().format("YYYYMMDD-HHmmss")+".jpg";o.getFileComments(t,function(t){return t.attr("src",n),!0},function(n,r){(o._hideFlashVideoOverlay()&&i("#webcam_movie_obj, #webcam_movie_embed").css("display",""),n)&&o._uploadFile(f,t,r)})},o._uploadFiles=function(n){var t=i.makeArray(n),r=function(){if(t&&t.length!==0){var n=t.shift();o.getFileComments(n.name,function(t){if(!!n.type&&n.type.indexOf("image/")===0){var i=new FileReader;return i.onload=function(n){t.attr("src",n.target.result)},i.readAsDataURL(n),!0}return!1},function(t,i){t&&(o._uploadFile(n,n.name,i),r())})}};r()},o._uploadFile=function(n,t,r){var f=new FormData,u=new XMLHttpRequest,e=i("<div>").append(i("<i>").addClass("fa fa-cog fa-spin")).append(i("<span>").text("Uploading: "+t)).appendTo(o.uploadProgressContainer);f.append("Comments",r);f.append("File",n,t);u.open("POST",o.uploadUrl,!0);u.onreadystatechange=function(){u.readyState===4&&(u.status!==200&&alert("Error Uploading ["+t+"]: "+u.statusText),e.slideUp(400,function(){e.remove()}))};u.send(f)},o.__hideFlashVideoOverlay=null,o._hideFlashVideoOverlay=function(){if(o.__hideFlashVideoOverlay===null){o.__hideFlashVideoOverlay=!1;try{var n=/(windows nt) ([\w.]+)/.exec(navigator.userAgent.toLowerCase());!!n&&parseFloat(n[2])<=6.2&&(o.__hideFlashVideoOverlay=!0)}catch(t){}}return o.__hideFlashVideoOverlay},o};t.Disco||(t.Disco={});t.Disco.AttachmentUploader=u}(this,document,$,Webcam);