mirror of
https://github.com/friendica/friendica
synced 2024-11-14 05:42:53 +00:00
1 line
No EOL
2.2 KiB
JavaScript
1 line
No EOL
2.2 KiB
JavaScript
(function($){var ajax_old=$.ajax;var get_old=$.get;var post_old=$.post;var active=true;$.ajaxSetup({stream:false,pollInterval:500});$.enableAjaxStream=function(enable){if(typeof enable=="undefined")enable=!active;if(!enable){$.ajax=ajax_old;$.get=get_old;$.post=post_old;active=false}else{$.ajax=ajax_stream;$.get=ajax_get_stream;$.post=ajax_post_stream;active=true}};var ajax_stream=$.ajax=function(options){options=jQuery.extend(true,options,jQuery.extend(true,{},jQuery.ajaxSettings,options));if(options.stream){var timer=0;var offset=0;var xmlhttp=null;var lastlen=0;var done=false;var hook=function(xhr){xmlhttp=xhr;checkagain()};var fix=function(){check("stream")};var checkagain=function(){if(!done)timer=setTimeout(fix,options.pollInterval)};var check=function(status){if(typeof status=="undefined")status="stream";if(xmlhttp.status<3)return;var text=xmlhttp.responseText;if(status=="stream"){if(text.length<=lastlen){checkagain();return}lastlength=text.length;if(offset==text.length){checkagain();return}}var pkt=text.substr(offset);offset=text.length;if($.isFunction(options.OnDataRecieved)){options.OnDataRecieved(pkt,status,xmlhttp.responseText,xmlhttp)}if(xmlhttp.status!=4)checkagain()};var complete=function(xhr,s){clearTimeout(timer);done=true;check(s)};if($.isFunction(options.success)){var oc=options.success;options.success=function(xhr,s){complete(xhr,s);oc(xhr,s)}}else options.success=complete;if($.isFunction(options.beforeSend)){var obs=options.beforeSend;options.beforeSend=function(xhr){obs(xhr);hook(xhr)}}else options.beforeSend=hook}ajax_old(options)};var ajax_get_stream=$.get=function(url,data,callback,type,stream){if($.isFunction(data)){var orgcb=callback;callback=data;if($.isFunction(orgcb)){stream=orgcb}data=null}if($.isFunction(type)){stream=type;type=undefined}var dostream=$.isFunction(stream);return jQuery.ajax({type:"GET",url:url,data:data,success:callback,dataType:type,stream:dostream,OnDataRecieved:stream})};var ajax_post_stream=$.post=function(url,data,callback,type,stream){if($.isFunction(data)){var orgcb=callback;callback=data}if($.isFunction(type)){stream=type;type=undefined}var dostream=$.isFunction(stream);return jQuery.ajax({type:"POST",url:url,data:data,success:callback,dataType:type,stream:dostream,OnDataRecieved:stream})}})(jQuery); |