Catalyst-Plugin-UploadProgress

 view release on metacpan or  search on metacpan

lib/Catalyst/Plugin/UploadProgress/Static.pm  view on Meta::CPAN


=head2 upload_progress_jmpl_js

=head1 AUTHOR

These files are taken from L<Apache2::UploadProgress>, by

    Christian Hansen <chansen@cpan.org>
    Cees Hek <ceeshek@cpan.org>

=head1 COPYRIGHT

This program is free software, you can redistribute it and/or modify it under
the same terms as Perl itself.

=cut

__DATA__

__upload_progress_css__
.progressmeter {
    font-family: Arial;
    font-size: 10pt;
    text-align: center;
}

.progressmeter table {
}

.progressmeter th {
    font-weight: bold;
    text-align: right;
}

.progressmeter td {
    text-align: left;
}

.finished {
    color: #667799;
}

.aborted {
    color: red;
}

.progressmeter .meter {
    position: relative;
    background-color: lightgrey;
    border: 2px solid #667799;
    height: 20px;
    width: 380px;
}

.progressmeter .meter .amount {
    background-color: #667799;
    width: 0px;
    height: 20px
}

.progressmeter .meter .percent {
    position: absolute;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 20px;
    z-index: 1000;
    text-align: center;
    font-family: Arial;
    font-size: 18px;
    font-weight: bold;
}

__upload_progress_js__
var progress;

function startPopupProgressBar(form, options) {
    var id = generateProgressID();
    if (form.action.match(/\?/))
        form.action += '&progress_id=' + id;
    else
        form.action += '?progress_id=' + id;

    var width  = options.width  || '480';
    var height = options.height || '150';
    window.open ('progress?progress_id='+id,'Apache2-UploadProgress','location=0,status=0,width='+width+',height='+height); return true;


}

function startEmbeddedProgressBar(form) {
    progress = {};
    progress.id         = generateProgressID();
    if (form.action.match(/\?/))
        form.action += '&progress_id=' + progress.id;
    else
        form.action += '?progress_id=' + progress.id;
    progress.starttime  = new Date();
    progress.lasttime   = new Date(progress.starttime);
    progress.lastamount = 0;
    window.setTimeout( reportUploadProgress, 100 );
    return true;
}

function updateHTMLProgressBar(progress) {
    if (progress.size > progress.received)
        window.setTimeout( function() { window.location.reload() }, 1000 );
    updateProgressBar( progress );
    return true;
}

function updateProgressBar(progress) {

    if (progress.received == progress.size)
        progress.finished = 1;

    // Only calculate rates and times is we were given a starttime
    if (progress.starttime) {
        var currenttime = new Date();
        var totalelapsedtime = ( currenttime.getTime() - progress.starttime.getTime() ) / 1000;
        var lastelapsedtime  = ( currenttime.getTime() - progress.lasttime.getTime() ) / 1000;

        progress.elapsedtime = totalelapsedtime;

        if (totalelapsedtime != 0)
            progress.rate = parseInt( progress.received / totalelapsedtime );
        else
            progress.rate = 0;

        if (lastelapsedtime != 0)
            progress.currentrate = parseInt( (progress.received - progress.lastamount) / lastelapsedtime );
        else
            progress.currentrate = 0

        if (progress.currentrate != 0)
            progress.remainingtime = parseInt( (progress.size - progress.received) / progress.rate );
        else
            progress.remainingtime = '';

        progress.currentrate   = formatBytes(progress.currentrate);
        progress.rate          = formatBytes(progress.rate);
        progress.elapsedtime   = formatTime(progress.elapsedtime);
        progress.remainingtime = formatTime(progress.remainingtime);
    }

    if (progress.size != 0)
        progress.percent = Math.round(progress.received / progress.size * 100);

    progress.size     = formatBytes(progress.size);
    progress.received = formatBytes(progress.received);
    
    document.getElementById('progress').innerHTML = Jemplate.process('progress.jmpl', progress);
}

function reportUploadProgress() {
    
    url = 'progress?progress_id=' + progress.id;

    var req = new XMLHttpRequest();
    req.open('GET', url, Boolean(handleUploadProgressResults));
    // We have to set the qvalue to 1.1 because Konqueror sends
    // it's standard Accept header with our header tacked on the end
    // which means that text/html gets picked first
    req.setRequestHeader(
        'Accept', 
        'text/x-json; q=1.1'
    );
    req.onreadystatechange = function() {
        if (req.readyState == 4)
            if (req.status == 200)
                handleUploadProgressResults(req.responseText);
            else
                // If there was an error, try again in 4 seconds
                window.setTimeout( reportUploadProgress, 4000 );
    };
    req.send(null);
}

function handleUploadProgressResults(results) {

    var state = JSON.parse(results);

    if ( state != undefined ) {

        state.starttime       = progress.starttime;
        state.lasttime        = progress.lasttime;
        state.lastamount      = progress.lastamount;

        progress.lasttime     = new Date();
        progress.lastamount   = state.received;
        progress.size         = state.size;
        progress.received     = state.received;

        if ( progress.received != progress.size && !state.aborted ) {
            window.setTimeout( reportUploadProgress, 1000 );
        }

        updateProgressBar(state);
    }
}

function formatTime(time) {
    var seconds = Math.round(time);
    var minutes = 0;
    if (time >= 60) {
        minutes = Math.round(seconds / 60);
        seconds %= 60;

lib/Catalyst/Plugin/UploadProgress/Static.pm  view on Meta::CPAN

                        }
                        a[a.length] = '}';
                    } else {
                        return;
                    }
                    return a.join('');
                }
                return 'null';
            }
        };
    return {
        copyright: '(c)2005 JSON.org',
        license: 'http://www.crockford.com/JSON/license.html',
        stringify: function (v) {
            var f = s[typeof v];
            if (f) {
                v = f(v);
                if (typeof v == 'string') {
                    return v;
                }
            }
            return null;
        },
        parse: function (text) {
            try {
                return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
                        text.replace(/"(\\.|[^"\\])*"/g, ''))) &&
                    eval('(' + text + ')');
            } catch (e) {
                return false;
            }
        }
    };
}();


/*------------------------------------------------------------------------------
End Source for Jemplate - Template Toolkit for Javascript
------------------------------------------------------------------------------*/

__upload_progress_jmpl_js__
/*
   This JavaScript code was generated by Jemplate, the JavaScript
   Template Toolkit. Any changes made to this file will be lost the next
   time the templates are compiled.

   Copyright 2006 - Ingy döt Net - All rights reserved.
*/

if (typeof(Jemplate) == 'undefined')
    throw('Jemplate.js must be loaded before any Jemplate template files');

Jemplate.templateMap['progress.jmpl'] = function(context) {
    if (! context) throw('Jemplate function called without context\n');
    var stash = context.stash;
    var output = '';

    try {
output += '<span class="progressmeter">\n  <div class="meter">\n    <div class="amount"  style="width: ';
//line 3 "progress.jmpl"
output += stash.get('percent');
output += '%;"></div>\n    <div class="percent" style="width: 100%">';
//line 4 "progress.jmpl"
output += stash.get('percent');
output += '%</div>\n  </div>\n  <table>\n    <tbody>\n      <tr>\n        <th>Status:</th>\n        <td>\n';
//line 17 "progress.jmpl"
if (stash.get('finished')) {
output += '          <span class="finished">Transfer complete  (';
//line 12 "progress.jmpl"
output += stash.get('size');
output += ')</span>\n';
}
else if (stash.get('aborted')) {
output += '          <span class="aborted">Transfer aborted</span>\n';
}
else {
output += '          ';
//line 16 "progress.jmpl"
output += stash.get('received');
output += ' of ';
//line 16 "progress.jmpl"
output += stash.get('size');
output += '\n';
}

output += '        </td>\n      </tr>\n';
//line 29 "progress.jmpl"
if (stash.get('starttime')) {
output += '      <tr>\n        <th>Time:</th>\n        <td>';
//line 23 "progress.jmpl"
output += stash.get('elapsedtime');
output += ' (~ ';
//line 23 "progress.jmpl"
output += stash.get('remainingtime');
output += ' to go)</td>\n      </tr>\n      <tr>\n        <th>Rate:</th>\n        <td>';
//line 27 "progress.jmpl"
output += stash.get('currentrate');
output += ' (avg ';
//line 27 "progress.jmpl"
output += stash.get('rate');
output += ')</td>\n      </tr>\n';
}

output += '    </tbody>\n  </table>\n</span>\n';
    }
    catch(e) {
        var error = context.set_error(e, output);
        throw(error);
    }

    return output;
}





( run in 2.239 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )