Fixed demo support for loading BlobBuilder.js asynchronously

This commit is contained in:
Eli Grey
2011-08-02 02:56:56 -04:00
parent b04f9b118a
commit 5a8328d35b
2 changed files with 11 additions and 6 deletions
+10 -5
View File
@@ -1,5 +1,5 @@
/* FileSaver.js demo script
* 2011-07-14
* 2011-08-02
*
* By Eli Grey, http://eligrey.com
* License: X11/MIT
@@ -18,8 +18,11 @@ var
return document.getElementById(id);
}
, session = view.sessionStorage
, BlobBuilder = view.BlobBuilder || view.WebKitBlobBuilder || view.MozBlobBuilder
// only get URL when necessary in case BlobBuilder.js hasn't defined it yet
, get_blob_builder = function() {
return view.BlobBuilder || view.WebKitBlobBuilder || view.MozBlobBuilder;
}
, canvas = $("canvas")
, canvas_options_form = $("canvas-options")
, canvas_filename = $("canvas-filename")
@@ -168,7 +171,8 @@ canvas_options_form.addEventListener("submit", function(event) {
text_options_form.addEventListener("submit", function(event) {
event.preventDefault();
var bb = new BlobBuilder;
var BB = get_blob_builder();
var bb = new BB;
bb.append(text.value || text.placeholder);
saveAs(
bb.getBlob("text/plain;charset=" + document.characterSet)
@@ -179,7 +183,8 @@ text_options_form.addEventListener("submit", function(event) {
html_options_form.addEventListener("submit", function(event) {
event.preventDefault();
var
bb = new BlobBuilder
BB = get_blob_builder()
, bb = new BB
, xml_serializer = new XMLSerializer
, doc = create_html_doc(html)
;
+1 -1
View File
@@ -1,2 +1,2 @@
/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/demo/demo.js */
(function(o){"use strict";var t=o.document,g=function(A){return t.getElementById(A)},b=o.sessionStorage,j=o.BlobBuilder||o.WebKitBlobBuilder||o.MozBlobBuilder,f=g("canvas"),s=g("canvas-options"),y=g("canvas-filename"),q=g("canvas-clear"),r=g("text"),u=g("text-options"),h=g("text-filename"),n=g("html"),e=g("html-options"),i=g("html-filename"),v=f.getContext("2d"),z=false,a=b.x_points||[],p=b.y_points||[],d=b.drag_points||[],k=function(A,C,B){a.push(A);p.push(C);d.push(B)},m=function(){f.width=f.width;v.lineWidth=6;v.lineJoin="round";v.strokeStyle="#000000";var B=0,A=a.length;for(;B<A;B++){v.beginPath();if(B&&d[B]){v.moveTo(a[B-1],p[B-1])}else{v.moveTo(a[B]-1,p[B])}v.lineTo(a[B],p[B]);v.closePath();v.stroke()}},c=function(){z=false},x=function(E){var D="h6 h5 h4 h3 h2 h1".split(" "),C=D.length,F,G;while(C--){F=E.getElementsByTagName(D[C]);for(var B=0,A=F.length;B<A;B++){G=F[B].textContent.trim();if(G){return G}}}},w=t.implementation,l=function(D){var B=w.createDocumentType("html",null,null),J=w.createDocument("http://www.w3.org/1999/xhtml","html",B),A=J.documentElement,H=A.appendChild(J.createElement("head")),K=H.appendChild(J.createElement("meta")),I=H.appendChild(J.createElement("title")),E=A.appendChild(J.createElement("body")),C=0,G=D.childNodes.length;K.setAttribute("charset",D.ownerDocument.characterSet);for(;C<G;C++){E.appendChild(J.importNode(D.childNodes.item(C),true))}var F=x(J);if(F){I.appendChild(J.createTextNode(F))}return J};f.width=500;f.height=300;if(typeof a==="string"){a=JSON.parse(a)}if(typeof p==="string"){p=JSON.parse(p)}if(typeof d==="string"){d=JSON.parse(d)}if(b.canvas_filename){y.value=b.canvas_filename}if(b.text){r.value=b.text}if(b.text_filename){h.value=b.text_filename}if(b.html){n.innerHTML=b.html}if(b.html_filename){i.value=b.html_filename}z=true;m();z=false;q.addEventListener("click",function(){f.width=f.width;a.length=p.length=d.length=0},false);f.addEventListener("mousedown",function(A){z=true;k(A.pageX-f.offsetLeft,A.pageY-f.offsetTop,false);m()},false);f.addEventListener("mousemove",function(A){if(z){k(A.pageX-f.offsetLeft,A.pageY-f.offsetTop,true);m()}},false);f.addEventListener("mouseup",c,false);f.addEventListener("mouseout",c,false);s.addEventListener("submit",function(A){A.preventDefault();f.toBlob(function(B){saveAs(B,(y.value||y.placeholder)+".png")},"image/png")},false);u.addEventListener("submit",function(A){A.preventDefault();var B=new j;B.append(r.value||r.placeholder);saveAs(B.getBlob("text/plain;charset="+t.characterSet),(h.value||h.placeholder)+".txt")},false);e.addEventListener("submit",function(B){B.preventDefault();var D=new j,A=new XMLSerializer,C=l(n);D.append(A.serializeToString(C));saveAs(D.getBlob("application/xhtml+xml;charset="+t.characterSet),(i.value||i.placeholder)+".xhtml")},false);o.addEventListener("unload",function(){b.x_points=JSON.stringify(a);b.y_points=JSON.stringify(p);b.drag_points=JSON.stringify(d);b.canvas_filename=y.value;b.text=r.value;b.text_filename=h.value;b.html=n.innerHTML;b.html_filename=i.value},false)}(self));
(function(n){"use strict";var s=n.document,g=function(A){return s.getElementById(A)},b=n.sessionStorage,w=function(){return n.BlobBuilder||n.WebKitBlobBuilder||n.MozBlobBuilder},f=g("canvas"),r=g("canvas-options"),y=g("canvas-filename"),p=g("canvas-clear"),q=g("text"),t=g("text-options"),h=g("text-filename"),m=g("html"),e=g("html-options"),i=g("html-filename"),u=f.getContext("2d"),z=false,a=b.x_points||[],o=b.y_points||[],d=b.drag_points||[],j=function(A,C,B){a.push(A);o.push(C);d.push(B)},l=function(){f.width=f.width;u.lineWidth=6;u.lineJoin="round";u.strokeStyle="#000000";var B=0,A=a.length;for(;B<A;B++){u.beginPath();if(B&&d[B]){u.moveTo(a[B-1],o[B-1])}else{u.moveTo(a[B]-1,o[B])}u.lineTo(a[B],o[B]);u.closePath();u.stroke()}},c=function(){z=false},x=function(E){var D="h6 h5 h4 h3 h2 h1".split(" "),C=D.length,F,G;while(C--){F=E.getElementsByTagName(D[C]);for(var B=0,A=F.length;B<A;B++){G=F[B].textContent.trim();if(G){return G}}}},v=s.implementation,k=function(D){var B=v.createDocumentType("html",null,null),J=v.createDocument("http://www.w3.org/1999/xhtml","html",B),A=J.documentElement,H=A.appendChild(J.createElement("head")),K=H.appendChild(J.createElement("meta")),I=H.appendChild(J.createElement("title")),E=A.appendChild(J.createElement("body")),C=0,G=D.childNodes.length;K.setAttribute("charset",D.ownerDocument.characterSet);for(;C<G;C++){E.appendChild(J.importNode(D.childNodes.item(C),true))}var F=x(J);if(F){I.appendChild(J.createTextNode(F))}return J};f.width=500;f.height=300;if(typeof a==="string"){a=JSON.parse(a)}if(typeof o==="string"){o=JSON.parse(o)}if(typeof d==="string"){d=JSON.parse(d)}if(b.canvas_filename){y.value=b.canvas_filename}if(b.text){q.value=b.text}if(b.text_filename){h.value=b.text_filename}if(b.html){m.innerHTML=b.html}if(b.html_filename){i.value=b.html_filename}z=true;l();z=false;p.addEventListener("click",function(){f.width=f.width;a.length=o.length=d.length=0},false);f.addEventListener("mousedown",function(A){z=true;j(A.pageX-f.offsetLeft,A.pageY-f.offsetTop,false);l()},false);f.addEventListener("mousemove",function(A){if(z){j(A.pageX-f.offsetLeft,A.pageY-f.offsetTop,true);l()}},false);f.addEventListener("mouseup",c,false);f.addEventListener("mouseout",c,false);r.addEventListener("submit",function(A){A.preventDefault();f.toBlob(function(B){saveAs(B,(y.value||y.placeholder)+".png")},"image/png")},false);t.addEventListener("submit",function(A){A.preventDefault();var C=w();var B=new C;B.append(q.value||q.placeholder);saveAs(B.getBlob("text/plain;charset="+s.characterSet),(h.value||h.placeholder)+".txt")},false);e.addEventListener("submit",function(B){B.preventDefault();var E=w(),D=new E,A=new XMLSerializer,C=k(m);D.append(A.serializeToString(C));saveAs(D.getBlob("application/xhtml+xml;charset="+s.characterSet),(i.value||i.placeholder)+".xhtml")},false);n.addEventListener("unload",function(){b.x_points=JSON.stringify(a);b.y_points=JSON.stringify(o);b.drag_points=JSON.stringify(d);b.canvas_filename=y.value;b.text=q.value;b.text_filename=h.value;b.html=m.innerHTML;b.html_filename=i.value},false)}(self));