package com.akjava.gwt.html5.client.download;

import com.google.gwt.user.client.ui.Anchor;



public class HTML5Download {
	public static  native final DownloadURL URL()/*-{
	return   window.webkitURL || window.URL;
	}-*/;
	
	public static  native final DownloadBlobBuilder BlobBuilder()/*-{
	$wnd.BlobBuilder=   $wnd.BlobBuilder || $wnd.WebKitBlobBuilder ||
                       $wnd.MozBlobBuilder;
    return new $wnd.BlobBuilder();
	}-*/;
	
	public static Anchor generateTextDownloadLink(String text,String fileName,String anchorText){
		DownloadBlobBuilder bb=HTML5Download.BlobBuilder();
		String mimeType="text/plain";
		bb.append(text);
		
		final Anchor link=new Anchor(anchorText);
		String url=HTML5Download.URL().createObjectURL(bb.getBlob(mimeType));
		link.setHref(url);
		link.getElement().setAttribute("download", fileName);
		link.getElement().setAttribute("dataset.downloadurl", mimeType+":"+fileName+":"+url);
		return link;
	}
	
	public static Anchor generateDownloadLink(DownloadBlobBuilder blob,String mimeType,String fileName,String anchorText){
		final Anchor link=new Anchor(anchorText);
		String url=HTML5Download.URL().createObjectURL(blob.getBlob(mimeType));
		link.setHref(url);
		link.getElement().setAttribute("download", fileName);
		link.getElement().setAttribute("dataset.downloadurl", mimeType+":"+fileName+":"+url);
		return link;
	}
	

}

@@ -42,16 +42,122 @@
 oad;%0D%0A%0D%0A
+import com.google.gwt.event.dom.client.ClickEvent;%0D%0Aimport com.google.gwt.event.dom.client.ClickHandler;%0D%0A
 import c
@@ -194,16 +194,16 @@
 nchor;%0D%0A
-
 %0D%0A%0D%0A%0D%0Apu
@@ -660,16 +660,203 @@
 Text)%7B%0D%0A
+%09%09return generateTextDownloadLink(text,fileName,anchorText,false);%0D%0A%09%7D%0D%0A%09public static Anchor generateTextDownloadLink(String text,String fileName,String anchorText,boolean autoRemove)%7B%0D%0A
 %09%09Downlo
@@ -1148,32 +1148,32 @@
 d%22, fileName);%0D%0A
-
 %09%09link.getElemen
@@ -1237,32 +1237,214 @@
 Name+%22:%22+url);%0D%0A
+%09%09if(autoRemove)%7B%0D%0A%09%09%09link.addClickHandler(new ClickHandler() %7B%0D%0A%09%09%09%09%0D%0A%09%09%09%09@Override%0D%0A%09%09%09%09public void onClick(ClickEvent event) %7B%0D%0A%09%09%09%09%09link.removeFromParent();%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D);%0D%0A%09%09%7D%0D%0A
 %09%09return link;%0D%0A

improve download