BgImages labels with AJAX
Overview
This example will run a natural horizontal progress bar with background pictures (example 1.2),
in asynchronous mode.
Use an auto (HTML_)AJAX server with PHP class and methods.
Screenshot
Demonstration
Give it a try
Dependencies
And also but optional :
Explains step by step
Lines 12-17:
clean-up session data when user-task is over (progress meter at 100%). The
default behavior of AJAX progress bar is to reload the same page when task is complete.
Use QueryString "reload=true". To change this behavior, you have to modify content of
HTML_Progress2.onComplete JavaScript variable (See example 15.4 with usage of Scriptaculous AJAX framework).
Lines 21-40:
Level design with all API to define look and feel of your progress bar.
Line 41:
function HTML_Progress2::registerAJAX, allow to identify components of (HTML_)AJAX
server. Here we don't use the default server name ('server.php')
but script 'auto_server.php' that will handle through PHP class RequestFullStatus (see line 61) and
one of its methods updateTask (see line 62) the simulation of a user-task that
take many cycles to complete, and return new status of the progress bar
(percentage and labels key-value pairs).
Line 57:
Do all job to initialize references to AJAX server and JS client librairies used.
Will print these lines:
<script type='text/javascript' src='auto_server.php?client=all'></script>
<script type='text/javascript' src='auto_server.php?stub=RequestFullStatus'></script>
<script type="text/javascript">HTML_AJAX.defaultServerUrl = 'auto_server.php'</script>
Lines 61-62:
Identify the PHP class/method that will act as responder to handle user-task
(RequestStatus.class.php lines 73-77)
and update the progress bar status (lines 43-62).
Source Code
default3.php
<?php
/**
* Basic AJAX example that used an auto server to handle progress bar responses
* Same as example default2.php, but its also multiple-labels system compatible.
*
* @author Laurent Laville <pear@laurent-laville.org>
* @package HTML_Progress2
* @subpackage Examples
* @access public
*/
if (isset($_GET['reload'])) {
session_start();
unset($_SESSION['progressPercentage']);
session_destroy();
die('Job is over !');
}
require_once 'HTML/Progress2.php';
$pb = new HTML_Progress2();
$pb->addLabel(HTML_PROGRESS2_LABEL_TEXT, 'txt1', 'Basic AJAX Progress2 example 3');
$pb->addLabel(HTML_PROGRESS2_LABEL_TEXT, 'txt2', 'caption ...');
$pb->setLabelAttributes('txt2', array('valign' => 'bottom'));
$pb->setCellCount(20);
$pb->setCellAttributes(array(
'active-color' => '#000084',
'inactive-color' => '#3A6EA5',
'width' => 25,
'spacing' => 0,
'background-image' => 'download.gif'
));
$pb->setBorderPainted(true);
$pb->setBorderAttributes('width=1 style=inset color=white');
$pb->setLabelAttributes('pct1', array(
'width' => 60,
'font-size' => 10,
'align' => 'center',
'valign' => 'left'
));
$pb->registerAJAX('auto_server.php', array('RequestFullStatus'));
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Basic Ajax Progress2 example 3</title>
<style type="text/css">
<!--
<?php echo $pb->getStyle(); ?>
body {background-color: #EEEEEE; }
-->
</style>
<script type="text/javascript" src="HTML_Progress2.js"></script>
<?php
echo $pb->setupAJAX();
?>
<script type="text/javascript">
//<![CDATA[
HTML_Progress2.serverClassName = 'RequestFullStatus';
HTML_Progress2.serverMethodName = 'updateTask';
//]]>
</script>
</head>
<body>
<a href="javascript:HTML_Progress2.start('<?php echo $pb->getIdent(); ?>');">Start</a>
<?php $pb->display(); ?>
</body>
</html>
auto_server.php
<?php
/**
* Auto server that will serve user task script responder
*
* @version $Id: auto_server.php,v 1.1 2007/01/22 17:58:54 farell Exp $
* @author Laurent Laville <pear@laurent-laville.org>
* @package HTML_Progress2
* @subpackage Examples
* @access public
*/
session_start();
require_once 'HTML/AJAX/Server.php';
class AutoServer extends HTML_AJAX_Server
{
// this flag must be set for your init methods to be used
var $initMethods = true;
function initRequestStatus()
{
include_once 'RequestStatus.class.php';
$status = new RequestStatus();
$this->registerClass($status, 'RequestStatus', array('updatePercentage'));
}
function initRequestFullStatus()
{
include_once 'RequestStatus.class.php';
$status = new RequestStatus();
$this->registerClass($status, 'RequestFullStatus', array('updateTask'));
}
function initRequestStatusAndFx()
{
include_once 'RequestStatus.class.php';
$status = new RequestStatus();
$this->registerClass($status, 'RequestStatusAndFx', array('updateTask'));
$this->registerJsLibrary('scriptaculous',
array('prototype.js', 'scriptaculous.js', 'effects.js'),
dirname(__FILE__) . DIRECTORY_SEPARATOR. 'scriptaculous-js-1.7.0' . DIRECTORY_SEPARATOR);
}
}
$server = new AutoServer();
$server->handleRequest();
?>
RequestStatus.class.php
<?php
/**
* Sample of user-task script, that also handle response for progress meter
*
* @version $Id: RequestStatus.class.php,v 1.1 2007/01/22 17:57:26 farell Exp $
* @author Laurent Laville <pear@laurent-laville.org>
* @package HTML_Progress2
* @subpackage Examples
* @access public
*/
class RequestStatus
{
var $status = array('labels' => array(), 'percentage' => 0);
function RequestStatus()
{
if (!isset($_SESSION['progressPercentage'])) {
$_SESSION['progressPercentage'] = 0;
}
}
/**
* Returns only new percentage value
*
* @return array
* @access public
*/
function getStatus()
{
return $this->status;
}
/**
* Returns new percentage value with associate new labels values.
*
* You can define label value even, if its not declared in progress bar;
* no error will be raised.
* See example default3.php and label id 'txt4' usage
*
* @return array
* @access public
*/
function getFullStatus()
{
$status = $this->getStatus();
if ($status['percentage'] < 25) {
$this->status['labels']['txt1'] = '1st quarter';
$this->status['labels']['txt2'] = 'Q1';
} elseif ($status['percentage'] < 50) {
$this->status['labels']['txt1'] = '2nd quarter';
$this->status['labels']['txt2'] = 'Q2';
} elseif ($status['percentage'] < 75) {
$this->status['labels']['txt1'] = '3rd quarter';
$this->status['labels']['txt2'] = 'Q3';
} else {
$this->status['labels']['txt1'] = '4th quarter';
$this->status['labels']['txt4'] = 'Q4';
}
return $this->status;
}
/**
* Move on current user-task and returns full status of progress meter.
*
* Here we simulate a user-task that take many cycles to complete, and we
* don't know how many.
*
* @return array
* @access public
*/
function updateTask()
{
$this->_updateStatus();
return $this->getFullStatus();
}
/**
* Move on current user-task and returns only new percentage of progress meter.
*
* Here we simulate a user-task that take many cycles to complete, and we
* don't know how many.
*
* @return array
* @access public
*/
function updatePercentage()
{
$this->_updateStatus();
return $this->getStatus();
}
/**
* Updates only progress meter status
*
* @return void
* @access private
*/
function _updateStatus()
{
$newVal = $_SESSION['progressPercentage'] + mt_rand(1, 25);
$_SESSION['progressPercentage'] = min(100, $newVal);
$this->status['percentage'] = $_SESSION['progressPercentage'];
}
}
?>