]> git.mxchange.org Git - friendica.git/blob - include/jquery.htmlstream.js
Merge pull request #163 from fabrixxm/master
[friendica.git] / include / jquery.htmlstream.js
1 /* jQuery ajax stream plugin
2 * Version 0.1
3 * Copyright (C) 2009 Chris Tarquini
4 * Licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License (http://creativecommons.org/licenses/by-sa/3.0/)
5 * Permissions beyond the scope of this license may be available by contacting petros000[at]hotmail.com.
6 */
7
8 (function($) {
9
10 // Save the original AJAX function
11 var ajax_old = $.ajax;
12 var get_old = $.get;
13 var post_old =  $.post;
14 var active = true;
15 // Add our settings
16 $.ajaxSetup({stream: false,pollInterval: 500/*, onDataRecieved: function(){}*/ });
17 $.enableAjaxStream = function(enable)
18 {
19 if(typeof enable == 'undefined') enable = !active;
20 if(!enable)
21 {
22 $.ajax = ajax_old;
23 $.get = get_old;
24 $.post = post_old;
25 active = false;
26 }
27 else
28 {
29 $.ajax = ajax_stream;
30 $.get = ajax_get_stream;
31 $.post = ajax_post_stream;
32 active = true;
33 }
34
35 }
36 var ajax_stream = $.ajax = function(options)
37 {
38 //copied from original ajax function
39         options  = jQuery.extend(true, options, jQuery.extend(true, {}, jQuery.ajaxSettings, options));
40 if(options.stream)
41 {
42 var timer = 0;
43 var offset = 0;
44 var xmlhttp = null;
45 var lastlen = 0;
46 var done = false;
47 var hook = function(xhr)
48 {
49 xmlhttp = xhr;
50 checkagain();
51 }
52 var fix = function(){ check('stream'); };// fixes weird bug with random numbers as arg
53 var checkagain = function(){if(!done) timer = setTimeout(fix,options.pollInterval);}
54 var check = function(status)
55 {
56 if(typeof status == 'undefined') status = "stream";
57 if(xmlhttp.status < 3) return; //only get the latest packet if data has been sent
58 var text = xmlhttp.responseText;
59 if(status == 'stream') //if we arent streaming then just flush the buffer
60 {
61 if(text.length <= lastlen) { checkagain(); return;}
62 lastlength = text.length;
63 if(offset == text.length) { checkagain(); return;}
64 }
65 var pkt = text.substr(offset);
66 offset =  text.length;
67 if($.isFunction(options.OnDataRecieved))
68 {
69 options.OnDataRecieved(pkt, status, xmlhttp.responseText, xmlhttp);
70 }
71 if(xmlhttp.status != 4)
72 checkagain();
73 }
74 var complete = function(xhr,s)
75 {
76 clearTimeout(timer);//done..stop polling
77 done = true;
78 // send final call
79 check(s);
80 }
81 // If the complete callback is set create a new callback that calls the users and outs
82 if($.isFunction(options.success))
83 {
84 var oc = options.success;
85 options.success = function(xhr,s){ complete(xhr,s); oc(xhr,s);};
86
87 }
88 else options.success = complete;
89 // Set up our hook on the beforeSend
90 if($.isFunction(options.beforeSend))
91 {
92 var obs = options.beforeSend;
93 options.beforeSend = function(xhr){ obs(xhr); hook(xhr);};
94 }
95 else options.beforeSend = hook;
96
97 }
98 ajax_old(options);
99 }
100
101 var ajax_get_stream = $.get = function(url,data,callback,type,stream)
102 {
103         if($.isFunction(data))
104         {
105                 var orgcb = callback;
106                 callback = data;
107                 if($.isFunction(orgcb))
108                 {
109                         stream = orgcb;
110                 }
111                 data = null;
112         }
113         if($.isFunction(type))
114         {
115                 stream = type;
116                 type = undefined;
117         }
118         var dostream = $.isFunction(stream);
119         return jQuery.ajax({
120                                         type: "GET",
121                                         url: url,
122                                         data: data,
123                                         success: callback,
124                                         dataType: type,
125                                         stream: dostream,
126                                         OnDataRecieved: stream
127                         });
128
129 }
130
131 var ajax_post_stream = $.post = function(url,data,callback,type,stream)
132 {
133         if($.isFunction(data))
134         {
135                                 var orgcb = callback;
136                 callback = data;
137         }
138                 if($.isFunction(type))
139                 {
140                         stream = type;
141                         type = undefined;
142                 }
143                 var dostream = $.isFunction(stream);
144                 return jQuery.ajax({
145                                 type: "POST",
146                                 url: url,
147                                 data: data,
148                                 success: callback,
149                                 dataType: type,
150                                 stream: dostream,
151                                 OnDataRecieved: stream
152                 });
153
154 }
155
156 })(jQuery);     
157