Skip to content

Commit

Permalink
AJAX population for milestone and version select options.
Browse files Browse the repository at this point in the history
  • Loading branch information
seanchen committed Feb 15, 2013
1 parent c9cd430 commit d0e9b0d
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
34 changes: 33 additions & 1 deletion wp-trac-client/ajax.php
Expand Up @@ -56,11 +56,43 @@ function wptc_valid_username_cb() {
exit;
}

/**
*
*/
add_action('wp_ajax_nopriv_wptc_toggle_select_opts',
'wptc_toggle_select_opts_cb');
add_action('wp_ajax_wptc_toggle_select_opts',
'wptc_toggle_select_opts_cb');
function wptc_toggle_select_opts_cb() {

$type = $_POST['type'];
$name = $_POST['name'];

switch($type) {
case "project":
// query milestones for the project.
$opts = wptc_widget_optgroups_html(
wptc_get_ticket_milestones($name), '');
break;
case "milestone":
// query version for the milestone
$project = wptc_get_project_name($name);
$opts = wptc_widget_optgroups_html(
wptc_get_ticket_versions($project, $name), '');
break;
default:
$opts = "";
break;
}

echo json_encode($opts);
exit;
}

/**
* the ajax wrappers for the trac functions
* The _cb suffix stands for Call Back
*/

add_action('wp_ajax_ticket_versions', 'wptc_get_versions_cb');
add_action('wp_ajax_nopriv_ticket_versions', 'wptc_get_versions_cb');
function wptc_get_versions_cb() {
Expand Down
4 changes: 4 additions & 0 deletions wp-trac-client/css/trac_ticket.css
Expand Up @@ -412,3 +412,7 @@ h1 span, h2 span { color: gray; font-size: 90%;}
position: relative;
display: inline-block;
}

select#field-project, select#field-milestone, select#field-version {
width: 250px;
}
67 changes: 65 additions & 2 deletions wp-trac-client/widgets.php
Expand Up @@ -231,7 +231,7 @@ function wptc_widget_ticket_finder($page_slug=null) {
name="ticketGo" src="{$goImageUrl}"
title="Go to Ticket">
<script>
<script type="text/javascript" charset="utf-8">
jQuery("#ticketGo").click(function(){
var ticket_id = jQuery("#ticketnumber").val();
if((ticket_id != "") && jQuery.isNumeric(ticket_id)) {
Expand All @@ -240,7 +240,7 @@ function wptc_widget_ticket_finder($page_slug=null) {
ticket_url = "{$blog_path}{$page_slug}?id=" + ticket_id;
window.location = ticket_url;
}
})
});
</script>
</div>
EOT;
Expand Down Expand Up @@ -971,6 +971,7 @@ function wptc_widget_ticket_form($ticket, $actions) {
<div id="modify" class="field">
EOT;
echo wptc_widget_ticket_fieldset($ticket);
echo wptc_widget_ticket_fieldset_js();
echo <<<EOT
</div>
<div class="buttons">
Expand Down Expand Up @@ -1009,6 +1010,68 @@ function wptc_widget_ticket_form($ticket, $actions) {
echo "</form>";
}

/**
* jQuery AJAX scripts to make following fields updating.
*/
function wptc_widget_ticket_fieldset_js() {

$ajax_url = admin_url('admin-ajax.php');

$js = <<<EOT
<script type="text/javascript" charset="utf-8">
<!--
jQuery("select#field-project").change(function() {
project = this.value;
//alert('change to [' + project + ']');
if(project == "") {
jQuery("select#field-milestone").html("");
jQuery("select#field-version").html("");
} else {
// ajax request data.
var data = {
"action" : "wptc_toggle_select_opts",
"type" : "project",
"name" : project,
};
jQuery.post("{$ajax_url}",
data,
function(response) {
res = JSON.parse(response);
// update milestone options.
jQuery("select#field-milestone").html(res);
jQuery("select#field-version").html("");
});
}
});
jQuery("select#field-milestone").change(function() {
milestone = this.value;
//alert('change to [' + milestone + ']');
if(milestone == "") {
jQuery("select#field-version").html("");
} else {
// ajax request data.
var data = {
"action" : "wptc_toggle_select_opts",
"type" : "milestone",
"name" : milestone,
};
jQuery.post("{$ajax_url}",
data,
function(response) {
res = JSON.parse(response);
// update milestone options.
jQuery("select#field-version").html(res);
});
}
});
-->
</script>
EOT;

return $js;
}

/**
* entry point for ticket details page.
*/
Expand Down

0 comments on commit d0e9b0d

Please sign in to comment.