source: aportesmurachi/tahel/Murachi-pre/js/ajaxSetup.js @ 12e6ddf

Last change on this file since 12e6ddf was 12e6ddf, checked in by Antonio Araujo <aaraujo@…>, 7 years ago

Agregados los directorio de aportes de Tahel al control de versiones.

  • Property mode set to 100644
File size: 7.9 KB
Line 
1/*!
2 Downloader Jquery Progress v1.0
3 http://uanesi.net/Downloader
4 requires jQuery v1.9.1 or later
5
6 Copyright 2017, Tahel Romero, UANESI, CA
7*/
8
9(function ($) {
10                $.ajaxTransport("+binary", function(options, originalOptions, jqXHR){
11                        if (window.FormData && ((options.dataType && (options.dataType == 'binary')) || (options.data && ((window.ArrayBuffer && options.data instanceof ArrayBuffer) || (window.Blob && options.data instanceof Blob)))))
12                        {
13                                return {
14                                        send: function(headers, callback){
15                                                // setup all variables
16                                                var xhr = new XMLHttpRequest(),
17                                                onFinal = options.funcionFinal,
18                                                url = options.url,
19                                                type = options.type,
20                                                async = options.async || true,
21                                                dataType = options.responseType || "blob",
22                                                data = options.data || null,
23                                                username = options.username || null,
24                                                password = options.password || null;
25                                                jqXHR.opciones = options;
26                                               
27                                                xhr.addEventListener("progress", function (evt) {
28                                                        if(jqXHR.opciones.onProgreso){
29                                                                jqXHR.opciones.onProgreso(evt,jqXHR);
30                                                        }
31                                                }, false);
32                                                xhr.addEventListener('error', function(jqXHR, textStatus, errorThrown){
33                                                        if(jqXHR.opciones.onError){
34                                                                jqXHR.opciones.onError(jqXHR, textStatus, errorThrown);
35                                                        }
36                                                });
37                                                xhr.addEventListener('load', function(){
38                                                        var data = {};
39                                                       
40                                                       
41                                                        data[options.dataType] = xhr.response;
42                                                        callback(xhr.status, xhr.statusText, data, xhr.getAllResponseHeaders());
43                                                });
44                                                xhr.open(type, url, async, username, password);
45                                                for (var i in headers ) {
46                                                        xhr.setRequestHeader(i, headers[i] );
47                                                }
48                                                if(data){
49                                                        if($.isArray(data)){
50                                                                var datosNuevos = new Array();
51                                                                for (var i in data) {
52                                                                        if(typeof data[i] == "string"){
53                                                                                 datosNuevos.push(i + '=' + encodeURI(data[i]) );
54                                                                        }
55                                                                }
56                                                                data = datosNuevos.join("&");
57                                                        }else if(typeof data == "object"){
58                                                                var datosNuevos = new Array();
59                                                                $.each( data, function( key, value ) {
60                                                                  datosNuevos.push(key + "=" + encodeURI(value));
61                                                                });
62                                                                data = datosNuevos.join("&");
63                                                        }
64                                                }
65                                                xhr.responseType = dataType;
66                                                xhr.send(data);
67                                        },
68                                        abort: function(){
69                                                jqXHR.abort();
70                                                //this.abort();
71                                                alert("Debe Abortar");
72                                                return true;
73                                        }
74                                };
75                        }
76                });
77                $.ajaxTransport("text", function(options, originalOptions, jqXHR){
78                        return {
79                                send: function(headers, callback){
80                                        // setup all variables
81                                        var xhr = new XMLHttpRequest(),
82                                        onFinal = options.funcionFinal,
83                                        url = options.url,
84                                        type = options.type,
85                                        async = options.async || true,
86                                        dataType = options.responseType || "text",
87                                        data = options.data || null,
88                                        username = options.username || null,
89                                        password = options.password || null;
90                                        jqXHR.opciones = options;
91                                       
92                                        xhr.addEventListener("progress", function (evt) {
93                                                if(jqXHR.opciones.onProgreso){
94                                                        jqXHR.opciones.onProgreso(evt,jqXHR);
95                                                }
96                                        }, false);
97                                        xhr.addEventListener('error', function(jqXHR, textStatus, errorThrown){
98                                                if(jqXHR.opciones.onError){
99                                                        jqXHR.opciones.onError(jqXHR, textStatus, errorThrown);
100                                                }
101                                        });
102                                        xhr.addEventListener('load', function(){
103                                                var data = {};
104                                                data[options.dataType] = xhr.response;
105                                                callback(xhr.status, xhr.statusText, data, xhr.getAllResponseHeaders());
106                                        });
107                                        xhr.open(type, url, async, username, password);
108                                        for (var i in headers ) {
109                                                xhr.setRequestHeader(i, headers[i] );
110                                        }
111                                        if(data){
112                                                if($.isArray(data)){
113                                                        var datosNuevos = new Array();
114                                                        for (var i in data) {
115                                                                if(typeof data[i] == "string"){
116                                                                         datosNuevos.push(i + '=' + encodeURI(data[i]) );
117                                                                }
118                                                        }
119                                                        data = datosNuevos.join("&");
120                                                }else if(typeof data == "object"){
121                                                        var datosNuevos = new Array();
122                                                        $.each( data, function( key, value ) {
123                                                          datosNuevos.push(key + "=" + encodeURI(value));
124                                                        });
125                                                        data = datosNuevos.join("&");
126                                                }
127                                        }
128                                        xhr.responseType = dataType;
129                                        xhr.send(data);
130                                },
131                                abort: function(){
132                                        jqXHR.abort();
133                                        //this.abort();
134                                        alert("Debe Abortar");
135                                        return true;
136                                }
137                        };
138                });
139   downQuery =  {
140           defaultOptions:{
141
142           },
143           formatSize:function(size) {
144                        if (!size) {
145                                return 'N/A';
146                        }
147                        function round(num, precision) {
148                                return Math.round(num * Math.pow(10, precision)) / Math.pow(10, precision);
149                        }
150                        var boundary = Math.pow(1024, 4);
151                        if (size > boundary) {
152                                return round(size / boundary, 2) + " " + 'TB';
153                        }
154                        if (size > (boundary/=1024)) {
155                                return round(size / boundary, 2) + " " + 'GB';
156                        }
157                        if (size > (boundary/=1024)) {
158                                return round(size / boundary, 2) + " " + 'MB';
159                        }
160                        if (size > 1024) {
161                                return Math.round(size / 1024) + " " + 'KB';
162                        }
163                        return size + " " + 'b';
164                },
165                obtieneNombre:function(xhr){
166                        $("#progresoTXT3").html(xhr.getAllResponseHeaders())
167                        //var encabezados = xhr.getAllResponseHeaders();
168                        if(xhr.getResponseHeader("Content-Disposition")){
169                                var nombreArchivo = xhr.getResponseHeader("Content-Disposition").replace('attachment','');
170                                var nombreArchivo = nombreArchivo.replace("Content-Disposition").replace('filename','');
171                                nombreArchivo = nombreArchivo.replace('-','');
172                                nombreArchivo = nombreArchivo.replace(';','');
173                                nombreArchivo = nombreArchivo.replace(';','');
174                                nombreArchivo = nombreArchivo.replace('=','');
175                                nombreArchivo = nombreArchivo.replace('"','');
176                                nombreArchivo = nombreArchivo.replace('"','');
177                        }
178                        if(!nombreArchivo){
179                                //var nombreArchivo = "archivo";
180                                var nombreArchivo = xhr.opciones.url.split("?");
181                                nombreArchivo = nombreArchivo[0].split("/");
182                                if(nombreArchivo.length > 1){
183                                        nombreArchivo = nombreArchivo[nombreArchivo.length-1];
184                                }else{
185                                        nombreArchivo = nombreArchivo[0];
186                                }
187                        }
188                        return  nombreArchivo;
189                },
190                dArchivo:function(objetivo,opciones){
191                        if(!opciones){
192                                opciones = new Object();
193                        }
194                        if(!opciones.metodo){
195                                opciones.metodo = "GET";       
196                        }
197                        if(!opciones.contenidos){
198                                opciones.contenidos=null;       
199                        }
200                        if(!opciones.tipoResultado){
201                                opciones.tipoResultado="binary";       
202                        }
203                        if(!opciones.onError){
204                                opciones.onError = this.errorSolicitud;
205                        }
206                        if(!opciones.onSend){
207                                opciones.onSend  = this.antesEnvioSolicitud;
208                        }
209                        if(!opciones.onCompletado){
210                                opciones.onCompletado = this.solicitudCompletada;
211                        }
212                        if(!opciones.onExito){
213                                opciones.onExito = this.exitoSolicitud;
214                        }
215                        if(!opciones.onProgreso){
216                                opciones.onProgreso = this.muestraProgreso;
217                        }
218                        if(!opciones.crossD){
219                                opciones.crossD = false;
220                        }
221                        var cargadorPagina = $.ajax({
222                                processData: false,
223                                url: objetivo,
224                                cache: false,
225                                data: opciones.contenidos,
226                                type: opciones.metodo,
227                                dataType: opciones.tipoResultado,
228                                error: opciones.onError,
229                                beforeSend: opciones.onSend,
230                                complete: opciones.onCompletado,
231                                success: opciones.onExito,
232                                crossDomain: opciones.crossD,
233                                onProgreso:opciones.onProgreso,
234                                id:opciones.id
235                        })
236                        return cargadorPagina;
237                },
238                muestraProgreso:function(evt){
239                        if (evt.lengthComputable) {
240                                var percentComplete = evt.loaded / evt.total;
241                                console.log(percentComplete);
242                        }else{
243                                console.log("Muestra progreso sin avance");
244                        }
245                },
246                errorSolicitud:function(xhr, opcionesAjax, errorArrojado) {
247                        console.log("Error al generar la vista previa");
248                        console.log(xhr.responseText);
249                        console.log(errorArrojado);
250                       
251                },
252                antesEnvioSolicitud:function(evt,opciones) {
253                        console.log("antesEnvioSolicitud");
254                },
255                solicitudCompletada:function(evt,estado) {
256                        console.log("solicitudCompletada");
257                },
258                exitoSolicitud:function(data, estado, xhr) { 
259                        /*
260                        if(data){
261                                var nombreArchivo = obtieneNombre(xhr);
262                                var downloadUrl = URL.createObjectURL(data);
263                                var a = document.createElement("a");
264                                a.href = downloadUrl;
265                                a.download = nombreArchivo;
266                                document.body.appendChild(a);
267                                a.click();
268                        }*/
269                        console.log("exitoSolicitud");
270                }
271   }
272})(jQuery);
Note: See TracBrowser for help on using the repository browser.