cvs: pear /HTML_Progress2/examples/ajax Poly9UrlParser.js bluesand2.css default1.php progress2AjaxHandler.js

From: Date: Mon, 15 Jan 2007 17:38:22 +0000
Subject: cvs: pear /HTML_Progress2/examples/ajax Poly9UrlParser.js bluesand2.css default1.php progress2AjaxHandler.js
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-44233@lists.php.net to get a copy of this message
farell Mon Jan 15 17:38:22 2007 UTC Added files: /pear/HTML_Progress2/examples/ajax bluesand2.css default1.php Poly9UrlParser.js progress2AjaxHandler.js Log: initial commit of first Ajax progress bar example , using the default driver

http://cvs.php.net/viewvc.cgi/pear/HTML_Progress2/examples/ajax/bluesand2.css?view=markup&rev=1.1 Index: pear/HTML_Progress2/examples/ajax/bluesand2.css +++ pear/HTML_Progress2/examples/ajax/bluesand2.css #PB1 .cellI, #PB1 .cellA { width: 10px; height: 20px; font-family: Courier, Verdana; font-size: 8px; float: left; } #PB1 .progressBorder { width: 122px; height: 24px; border-width: 1px; border-style: solid; border-color: navy; background-color: #FFFFFF; } #PB1 .progressPercentLabel { width: 60px; text-align: center; background-color: transparent; font-size: 14px; font-family: Verdana, Tahoma, Arial; font-weight: normal; color: #000000; } #PB1 .cellI { background-color: #EEEECC; } #PB1 .cellA { background-color: #3874B4; } http://cvs.php.net/viewvc.cgi/pear/HTML_Progress2/examples/ajax/default1.php?view=markup&rev=1.1 Index: pear/HTML_Progress2/examples/ajax/default1.php +++ pear/HTML_Progress2/examples/ajax/default1.php <?php /** * Natural horizontal AJAX ProgressBar with blue skin, using default Ajax driver. * * @version $Id: default1.php,v 1.1 2007/01/15 17:38:22 farell Exp $ * @author Laurent Laville <pear@laurent-laville.org> * @package HTML_Progress2 * @subpackage Examples * @access public * @example examples/ajax/default1.php * AJAX bluesand source code * @link http://www.laurent-laville.org/img/progress/screenshot/bluesand.png * screenshot (Image PNG, 190x40 pixels) 352 bytes */ require_once 'PEAR.php'; PEAR::setErrorHandling(PEAR_ERROR_DIE); /** * Simulate a user-task that will take 9 cycles to complete (12% on each cycle) */ function processTask() { $_SESSION['progressPercentage'] = $_SESSION['progressPercentage'] + 12; $percent = min(100, $_SESSION['progressPercentage']); return $percent; } session_start(); if (isset($_REQUEST['registerTask'])) { // Negociate a new task identifier $_SESSION['taskId'] = mt_rand(); $_SESSION['progressId'] = $_REQUEST['registerTask']; $_SESSION['progressPercentage'] = 0; header("Content-Type: text/xml"); echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"; ?> <progress id="<?php echo $_SESSION['progressId']; ?>" taskId="<?php echo $_SESSION['taskId']; ?>" /> <?php } elseif (isset($_REQUEST['TaskId'])) { // Proceed user-task on each polling loop if ($_REQUEST['TaskId'] != $_SESSION['taskId']) { die('wrong task id'); } header("Content-Type: text/xml"); echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"; ?> <progress id="<?php echo $_SESSION['progressId']; ?>" taskId="<?php echo $_SESSION['taskId']; ?>"> <percentage><?php echo processTask(); ?></percentage> </progress> <?php } else { // Initialize progress bar render with initial page content include_once 'HTML/Progress2.php'; $pb = new HTML_Progress2(); $pb->setIdent('PB1'); $pb->setAnimSpeed(200); $pb->setIncrement(10); $pb->setBorderAttributes('class=progressBorder'); $pb->setCellAttributes('class=cell'); $pb->setLabelAttributes('pct1', array( 'class' => 'progressPercentLabel', 'align' => 'center' )); $pb->importStyle('bluesand2.css'); $ae = $pb->registerAjax(); $ae->setRequestUri(basename(__FILE__)); ?> <!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>BlueSand Ajax Progress2 example</title> <link rel="stylesheet" type="text/css" href="bluesand2.css" /> <style type="text/css"> <!-- body {background-color: #EEEEEE; } --> </style> <script type="text/javascript" src="progress2Handler.js"></script> <script type="text/javascript" src="Poly9UrlParser.js"></script> <script type="text/javascript"> //<![CDATA[ <?php echo $ae->getScript(true); ?> function complete() { // nothing do to at end, when server-side operation complete // JS function declaration avoid a JS error on javascript console } //]]> </script> </head> <body> <?php $pb->display(); ?> <script type="text/javascript"> //<![CDATA[ <?php $m = $ae->getRequestMethod(); if ($m != 'GET') { echo "reqMethod = '$m';\n"; } $t = $ae->getRequestTimeout(); if ($t != 2000) { echo "timeout = '$t';\n"; } echo "url1 = '". $ae->getRequestUri(1) ."';\n"; echo "url2 = '". $ae->getRequestUri(2) ."';\n"; ?> startTask(); //]]> </script> </body> </html> <?php } ?> http://cvs.php.net/viewvc.cgi/pear/HTML_Progress2/examples/ajax/Poly9UrlParser.js?view=markup&rev=1.1 Index: pear/HTML_Progress2/examples/ajax/Poly9UrlParser.js +++ pear/HTML_Progress2/examples/ajax/Poly9UrlParser.js /** * @projectDescription Poly9's polyvalent URLParser class * * @author Denis Laprise - denis@poly9.com - http://poly9.com * @version 0.1 * @namespace Poly9 * * Usage: var p = new Poly9.URLParser('http://user:password@poly9.com/pathname?arguments=1#fragment'); * p.getHost() == 'poly9.com'; * p.getProtocol() == 'http'; * p.getPathname() == '/pathname'; * p.getQuerystring() == 'arguments=1'; * p.getFragment() == 'fragment'; * p.getUsername() == 'user'; * p.getPassword() == 'password'; * * See the unit test file for more examples. * URLParser is freely distributable under the terms of an MIT-style license. */ if (typeof Poly9 == 'undefined') var Poly9 = {}; /** * Creates an URLParser instance * * @classDescription Creates an URLParser instance * @return {Object} return an URLParser object * @param {String} url The url to parse * @constructor * @exception {String} Throws an exception if the specified url is invalid */ Poly9.URLParser = function(url) { this._fields = {'Username' : 4, 'Password' : 5, 'Port' : 7, 'Protocol' : 2, 'Host' : 6, 'Pathname' : 8, 'URL' : 0, 'Querystring' : 9, 'Fragment' : 10}; this._values = {}; this._regex = null; this.version = 0.1; this._regex = /^((\w+):\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/; for(var f in this._fields) this['get' + f] = this._makeGetter(f); if (typeof url != 'undefined') this._parse(url); } /** * @method * @param {String} url The url to parse * @exception {String} Throws an exception if the specified url is invalid */ Poly9.URLParser.prototype.setURL = function(url) { this._parse(url); } Poly9.URLParser.prototype._initValues = function() { for(var f in this._fields) this._values[f] = ''; } Poly9.URLParser.prototype._parse = function(url) { this._initValues(); var r = this._regex.exec(url); if (!r) throw "DPURLParser::_parse -> Invalid URL" for(var f in this._fields) if (typeof r[this._fields[f]] != 'undefined') this._values[f] = r[this._fields[f]]; } Poly9.URLParser.prototype._makeGetter = function(field) { return function() { return this._values[field]; } } http://cvs.php.net/viewvc.cgi/pear/HTML_Progress2/examples/ajax/progress2AjaxHandler.js?view=markup&rev=1.1 Index: pear/HTML_Progress2/examples/ajax/progress2AjaxHandler.js +++ pear/HTML_Progress2/examples/ajax/progress2AjaxHandler.js /** * JavaScript functions to handle AJAX progress bar. * * @category HTML * @package HTML_Progress2 * @author Laurent Laville <pear@laurent-laville.org> * @license http://www.php.net/license/3_01.txt PHP License 3.01 * @version CVS: $Id: progress2AjaxHandler.js,v 1.1 2007/01/15 17:38:22 farell Exp $ * @since File available since Release 2.3.0a1 */ var req = null; var url1 = null; var url2 = null; var reqMethod = 'GET'; var progressId = null; var taskId = null; var timeout = 2000; initRequest(); /** * - initRequest - * * Instantiates XMLHttpRequest object. * * @return void * @public * @since 2.3.0a1 */ function initRequest() { var msxml = new Array('MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'); try { // Instantiates XMLHttpRequest in non-IE browsers req = new XMLHttpRequest(); } catch(e) { for (var i = 0; i < msxml.length; i++) { try { // Instantiates XMLHttpRequest for IE and req = new ActiveXObject(msxml[i]); break; } catch(e) { } } } if (req == null) { alert("Your browser does not support AJAX"); } } /** * - startTask - * * Negociate a task id: * This Id is used by the polling loop to track the progress of the task. * * @return void * @public * @since 2.3.0a1 */ function startTask() { var dataSend = null; var m = reqMethod.toUpperCase(); if (m == 'GET') { // avoid browser url cache problem url1 = url1 + '&dummy=' + new Date().getTime(); } else { var p = new Poly9.URLParser(url1); dataSend = p.getQuerystring(); } req.open(m, url1, true); req.onreadystatechange = processInitialRequest; if (m == 'POST') { req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); } req.send(dataSend); } /** * - processInitialRequest - * * Callback function for initial request to schedule a task. * Server script will return XML code like this: * * &lt;?xml version="1.0" encoding="iso-8859-1"?&gt; * &lt;progress id="PB1" taskId="375" /&gt; * * where "id" is the progress bar identifier; see HTML_Progress2::getIdent() * and "taskId" is the negociated task identifier. * * @return void * @public * @since 2.3.0a1 */ function processInitialRequest() { if (req.readyState == 4) { if (req.status == 200) { var message = req.responseXML.getElementsByTagName("progress")[0]; progressId = message.getAttribute("id"); taskId = message.getAttribute("taskId"); } // do the initial poll in 2 seconds (default timeout) setTimeout("pollTaskmaster()", timeout); } } /** * - pollTaskmaster - * * proceed polling loop until server side operation is completed (a response of 100 is returned) * * @return void * @public * @since 2.3.0a1 */ function pollTaskmaster() { var m = reqMethod.toUpperCase(); var dataSend = null; url2 = url2 + escape(taskId); if (m == 'GET') { // avoid browser url cache problem url2 = url2 + '&dummy=' + new Date().getTime(); } else { var p = new Poly9.URLParser(url2); dataSend = p.getQuerystring(); } req.open(m, url2, true); req.onreadystatechange = processPollRequest; if (m == 'POST') { req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); } req.send(dataSend); } /** * - processPollRequest - * * Callback function for polling loop request to handle a scheduled task. * Server script will return XML code like this: * * &lt;code&gt; * &lt;?xml version="1.0" encoding="iso-8859-1"?&gt; * &lt;progress id="PB1" taskId="375"&gt; * &lt;percentage&gt;12&lt;/percentage&gt; * &lt;/code&gt; * * @return void * @public * @since 2.3.0a1 */ function processPollRequest() { if (req.readyState == 4) { if (req.status == 200) { var message = req.responseXML.getElementsByTagName('progress')[0]; var percent = message.getElementsByTagName('percentage')[0]; var percentage = percent.firstChild.nodeValue; var cell = Math.floor(percentage / 10); setProgress(progressId, cell, 0); setLabelText(progressId, 'pct1', percentage + "%") } // continue on other poll in 2 seconds (default timeout) if (percentage < 100) { setTimeout("pollTaskmaster()", timeout); } else { setTimeout("complete()", timeout); } } }
« previous php.pear.cvs (#44233) next »