import Ember from "ember";

export default Ember.Component.extend({
    session: Ember.inject.service(),
    tagName: "",
    classNames: [],
    didInsertElement: function() {
        this._super();
        this.set("active", true);
        Ember.run.scheduleOnce("afterRender", this, function(){
            
        }); 
    },
    actions: {
        del: function() {
            if (!this.get("active")) {
                return;
            }
            this.set("active", false);
            var self = this;
            this.get('session').authorize('authorizer:oauth2', function(header, content) {
                console.log(header);
                console.log(content);
                Ember.$.ajax({
                    url: self.get("file.filepath"),
                    data: JSON.stringify({}),
                    contentType: "application/json",
                    beforeSend: function(xhr) {
                        xhr.setRequestHeader(header, content);
                    },
                    type: 'DELETE',
                    success: function(data) {
                        console.log(data);
                        if(data['status'] === "ok") {
                            self.sendAction("del", self.get("file.id"));
                            console.log("Delete action!");
                        }
                    },
                    error: function(j, s, a) {
                        console.log(j);
                        console.log(s);
                        console.log(a);
                        self.set("active", true);
                    }
                });
            });
        },
        down: function() {
            var self = this;
            this.get('session').authorize('authorizer:oauth2', function(header, content) {
                Ember.$.ajax({
                    url: self.get("file.filepath"),
                    type: "GET",
                    beforeSend: function(xhr) {
                        xhr.setRequestHeader(header, content);
                    },
                    processData: false,
                    success: function (result, a, xhr) {
                        var v = new Int8Array(result.length);
                        for(var i = 0; i != result.length; i++) {
                            v[i] = result.charCodeAt(i);
                        }
                        var blob = new Blob([v], {type: xhr.getResponseHeader("content-type") || ""});
                        saveAs(blob, self.get("file.filename"));
                        //saveAs(blob, self.get("file.filename"));
                        /*var anchor = Ember.$("#sub_file" + self.get("file.id"));
                        console.log(anchor);
                        var windowUrl = window.URL || window.webkitURL;
                        var blob = new Blob([result.response], { type: 'text/bin' });
                        var url = windowUrl.createObjectURL(blob);
                        anchor.prop('href', url);
                        anchor.prop('download', self.get("file.filename"));
                        anchor.get(0).click();
                        windowUrl.revokeObjectURL(url);*/
                        /*console.log(result);
                        $("#sub_file" + self.get("file.id"))
                            .attr({
                            "href": result,
                            "download": "file.txt"
                        }).html($("#sub_file" + self.get("file.id")).attr("download"))
                            .get(0).click();*/
                    },
                    error: function (jqxhr, textStatus, errorThrown) {
                        console.log(textStatus, errorThrown);
                    }
                });
            });
        }
    }
});
@@ -1843,32 +1843,34 @@
                 
+/*
 Ember.$.ajax(%7B%0D%0A
@@ -2196,223 +2196,8 @@
  %7B%0D%0A
-                        var v = new Int8Array(result.length);%0D%0A                        for(var i = 0; i != result.length; i++) %7B%0D%0A                            v%5Bi%5D = result.charCodeAt(i);%0D%0A                        %7D%0D%0A
     
@@ -2237,17 +2237,22 @@
 w Blob(%5B
-v
+result
 %5D, %7Btype
@@ -2391,138 +2391,82 @@
     
-    //saveAs(blob, self.get(%22file.filename%22));%0D%0A                        /*var anchor = Ember.$(%22#sub_file%22 + self.get(%22file.id%22));
+%7D,%0D%0A                    error: function (jqxhr, textStatus, errorThrown) %7B
 %0D%0A  
@@ -2499,22 +2499,39 @@
 ole.log(
-anchor
+textStatus, errorThrown
 );%0D%0A    
@@ -2550,213 +2550,203 @@
     
+%7D%0D%0A
     
-var windowUrl = window.URL %7C%7C window.webkitURL;%0D%0A                        var blob = new Blob(%5Bresult.response%5D, %7B type: 'text/bin' %7D);%0D%0A                        var url = windowUrl.createObjectURL(
+            %7D);*/%0D%0A                var request = new XMLHttpRequest();%0D%0A                request.open(%22GET%22, self.get(%22file.filepath%22), true);%0D%0A                request.responseType = %22
 blob
-)
+%22
 ;%0D%0A 
@@ -2764,39 +2764,48 @@
     
-        anchor.prop('href', url
+request.setRequestHeader(header, content
 );%0D%0A
@@ -2824,174 +2824,127 @@
     
- 
-       anchor.prop('download', self.get(%22file.filename%22));%0D%0A                        anchor.get(0).click();%0D%0A                        windowUrl.revokeObjectURL(url);*/
+request.onload = function() %7B%0D%0A                    console.log(%22Here%22);%0D%0A                    if (this.status === 200) %7B
 %0D%0A  
@@ -2965,18 +2965,16 @@
         
-/*
 console.
@@ -2977,22 +2977,20 @@
 ole.log(
-result
+%22in%22
 );%0D%0A    
@@ -3013,87 +3013,125 @@
     
-$(%22#sub_file%22 + self.get(%22file.id%22))%0D%0A                            .attr(%7B%0D%0A    
+var file = window.URL.createObjectURL(this.response);%0D%0A                        var a = document.createElement(%22a%22);%0D%0A
     
@@ -3154,23 +3154,22 @@
     
-%22
+a.
 href
-%22: result,
+ = file;
 %0D%0A  
@@ -3194,21 +3194,18 @@
     
-    %22
+a.
 download
 %22: %22
@@ -3204,109 +3204,114 @@
 load
-%22: %22file.txt%22%0D%0A                        %7D).html($(%22#sub_file%22 + self.get(%22file.id%22)).attr(%22download%22))
+ = this.response.name %7C%7C self.get(%22file.filename%22);%0D%0A                        document.body.appendChild(a);
 %0D%0A  
@@ -3336,19 +3336,9 @@
     
-    .get(0)
+a
 .cli
@@ -3342,18 +3342,16 @@
 click();
-*/
 %0D%0A      
@@ -3368,144 +3368,127 @@
     
-%7D,%0D%0A                    error: function (jqxhr, textStatus, errorThrown) %7B%0D%0A                        console.log(textStatus, errorThrown)
+    window.onfocus = function() %7B%0D%0A                            document.body.removeChild(a);%0D%0A                        %7D
 ;%0D%0A 
@@ -3518,32 +3518,64 @@
                %7D
+;%0D%0A                request.send(
 );%0D%0A            

Fix file downloading