create chunked module and add iajax. Update pkg to handled chunked responses

git-svn-id: file:///root/webif/svn/humax/pkg/src/webif/trunk@273 2a923420-c742-0410-a762-8d5b09965624
This commit is contained in:
hummypkg 2011-07-14 22:22:54 +00:00
parent dbc3d21807
commit 64b73ee14b
4 changed files with 101 additions and 29 deletions

View File

@ -2,7 +2,7 @@
package require cgi
source /mod/var/mongoose/lib/setup
require pkg.class
require pkg.class chunked
puts "Content-Type: text/html"
puts "Transfer-Encoding: chunked"
@ -11,22 +11,8 @@ puts ""
cgi_input
#cgi_dump
#set _cgi(cmd) {remove binutils}
set cmd [cgi_get cmd update]
if {![dict exists $_cgi cmd]} { set _cgi(cmd) update }
set cmd [dict get $_cgi cmd]
proc chunk {chk} {
puts -nonewline [format "%x\r\n" [string length $chk]]
puts -nonewline "$chk\r\n"
flush stdout
}
# Kick the browser into life...
chunk [string range [format "%301d" 0] 1 end-1]
#puts [exec /mod/var/mongoose/lib/opkg {*}$cmd]
set bcmd "|/mod/var/mongoose/lib/opkg $cmd"
set fd [open $bcmd r]
while {[gets $fd line] >= 0} {

View File

@ -0,0 +1,50 @@
jQuery.ajaxPrefilter(function(options, _, jqXHR) {
if (jQuery.isFunction(options.progress)) {
var xhrFactory = options.xhr,
interval;
options.xhr = function() {
var xhr = xhrFactory.apply(this, arguments),
partial = "",
prevcount = 1;
interval = setInterval(function() {
var responseText,
jQueryPartial;
try {
responseText = xhr.responseText;
if (responseText && (responseText.length > partial.length))
{
partial = responseText;
jQueryPartial = $(partial).filter("*")
if (jQueryPartial.length > prevcount) {
prevcount = jQueryPartial.length;
options.progress(jQueryPartial.filter("*:not(:last)"));
}
}
} catch(e) {
alert(e);
}
}, options.progressInterval);
return xhr;
};
function stop()
{
if (interval)
clearInterval(interval);
}
jqXHR.then(stop, stop);
}
});

View File

@ -42,12 +42,17 @@ Update package list from Internet
<div id=dspinner><img border=0 src=/img/loading.gif>Processing request...</div>
</div>
<script type=text/javascript src=/js/iajax.js></script>
<script type=text/javascript>
var opkg = '/cgi-bin/opkg.jim?cmd=';
$(document).ready(function() {
$('#opkgupdate').button().show('slow');
$('#opkgupdate')
.button()
.click(function() { execopkg('update'); })
.fadeIn('slow');
$('#pkgtabs').tabs({
load: setup_buttons,
@ -66,6 +71,37 @@ $(document).ready(function() {
close: function(e,u) { window.location.reload(true); }
});
jQuery.ajaxSetup({progressInterval: 1});
function loaddata(data, isfinal)
{
console.log('loaddata called, final=' + isfinal);
console.log('Data: ' + data);
$('#dresults').append(data);
if (isfinal)
$('#dspinner').hide('slow');
}
function execopkg(arg)
{
$('#dspinner').show();
$dialog.dialog('open');
$('#dresults').empty();
$.ajax({
type: "GET",
url: opkg + arg,
progress: loaddata,
success: function(data) {
console.log("ajax success");
loaddata(data, true);
},
error: function(_, _, e) {
console.log("ajax error");
alert(e);
}
});
}
function setup_buttons()
{
$('button.remove, button.install, button.upgrade')
@ -75,20 +111,10 @@ $(document).ready(function() {
!confirm('Please confirm removal of the ' +
$(this).attr('id') + ' package.'))
return;
$('#dspinner').show();
$('#dresults').load(opkg + encodeURIComponent(
$(this).attr('action') + ' ' + $(this).attr('id')),
function() { $('#dspinner').hide('slow') });
$dialog.dialog('open');
execopkg(encodeURIComponent($(this).attr('action') +
' ' + $(this).attr('id')));
}).fadeIn('slow');
}
$('#opkgupdate').click(function() {
$('#result_txt').load(opkg + 'update', function() {
$('#results').delay(3000).slideUp('slow');
});
$('#results').slideDown('slow');
});
});

10
var/mongoose/lib/chunked Executable file
View File

@ -0,0 +1,10 @@
proc chunk {chk} {
puts -nonewline [format "%x\r\n" [string length $chk]]
puts -nonewline "$chk\r\n"
flush stdout
}
# Kick the browser into life...
chunk [string range [format "%301d" 0] 1 end-1]