<?php
/**
* page callback for javascript
*
* Normally I'd just include a javascript file
* but I wanted to put all the code in one file for this demo
*
*/
function progresbardemo_javascript() {
$content = '
if (Drupal.jsEnabled) {
$(document).ready(function() {
$("#clickme").click(function() {
titlediv = $("#clickme").parent();
pb = new Drupal.progressBar(\'myProgressBar\');
// $("#progress").appendChild(pb.element);
document.getElementById("progress").appendChild(pb.element);
pb.setProgress("0", "processing...");
uri = \'/progresbardemo/work_ajax\';
$.get(uri , \'\', function(data) {
$("#fillme").empty();
$("#fillme").append(data);
pb.stopMonitoring();
pb.setProgress("100", "done");
});
pb.startMonitoring(\'/progresbardemo/monitor\', 1000);
return false;
})
})
}
';
header("Content-type: text/javascript");
}
/**
* Page callback for the demo start page
*
* @return html output
*/
function progresbardemo_page() {
$output .= '<h2>Progress Bar Demo</h2>';
$output .=
'<div><p> '. l
('Start',
'progresbardemo/work',
array('id' =>
'clickme')) .
'</p></div>';
$output .= '<div id="fillme"></div>';
$output .= '<div id="progress"></div>';
return $output;
}
/**
* Do the work
* This function can be called directly as a menu callback where it will provide the page content
* Or via the ajax callback function
*
* @return html content
*/
function progresbardemo_work() {
$progres = 0;
db_query("insert into {variable} (name, value) values ('progresbardemo_progres', '%s')",
$progres);
while ($progres < PERCENT) {
$progres += (PERCENT / STEPS);
db_query("update {variable} set value = '%s' where name = 'progresbardemo_progres'",
$progres);
}
db_query("delete from {variable} where name='progresbardemo_progres'");
return "<p>all done</p>";
}
/**
* Do the task - called in ajax context - return the result without the page wrapper
*/
function progresbardemo_work_ajax() {
print progresbardemo_work
();
}
/**
* called by the ajax monitor
*/
function progresbardemo_monitor() {
$progres =
(int
) db_result(db_query("select value from {variable} where name='progresbardemo_progres'"));
}
/**
* define the menu
* implementation of hook_menu
*
* @return menu array
*/
function progresbardemo_menu($may_cache) {
if (!$may_cache) {
'title' =>
t("Progress Bar Demo"),
'path' => 'progresbardemo',
'callback' => 'progresbardemo_page',
'access' => TRUE
);
'path' => 'progresbardemo/work',
'callback' => 'progresbardemo_work',
'access' => TRUE
);
'path' => 'progresbardemo/work_ajax',
'callback' => 'progresbardemo_work_ajax',
'access' => TRUE
);
'path' => 'progresbardemo/monitor',
'callback' => 'progresbardemo_monitor',
'access' => TRUE
);
'path' => 'progresbardemo/javascript',
'callback' => 'progresbardemo_javascript',
'access' => TRUE
);
}
return $items;
}