Commit 2dba20aa authored by Penny Leach's avatar Penny Leach Committed by Nigel McNie
Browse files

added support for ajax adding and removing columns. I changed...

added support for ajax adding and removing columns. I changed view_process_changes so it works from either a normal post or an ajax request, view class methods now take a 'returndata' argument for required ajax return data. The javascript rewriting buttons has changed to match what the normal buttons are called so that view_process_changes can read them
parent 101b67b0
......@@ -63,8 +63,8 @@ function ViewManager() {
$('column_' + oldID).setAttribute('id', 'column_' + newID);
// Renumber the add/remove column buttons
getFirstElementByTagAndClassName('input', 'addcolumn', 'column_' + newID).setAttribute('name', 'action_add_column_before_' + (newID + 1));
getFirstElementByTagAndClassName('input', 'removecolumn', 'column_' + newID).setAttribute('name', 'action_remove_column_' + newID);
getFirstElementByTagAndClassName('input', 'addcolumn', 'column_' + newID).setAttribute('name', 'action_addcolumn_before_' + (newID + 1));
getFirstElementByTagAndClassName('input', 'removecolumn', 'column_' + newID).setAttribute('name', 'action_removecolumn_id_' + newID);
}
removeElementClass(column, 'columns' + numColumns);
addElementClass(column, 'columns' + (numColumns + 1));
......@@ -137,8 +137,8 @@ function ViewManager() {
$('column_' + oldID).setAttribute('id', 'column_' + newID);
// Renumber the add/remove column buttons
getFirstElementByTagAndClassName('input', 'addcolumn', 'column_' + newID).setAttribute('name', 'action_add_column_before_' + oldID);
getFirstElementByTagAndClassName('input', 'removecolumn', 'column_' + newID).setAttribute('name', 'action_remove_column_' + newID);
getFirstElementByTagAndClassName('input', 'addcolumn', 'column_' + newID).setAttribute('name', 'action_addcolumn_before_' + oldID);
getFirstElementByTagAndClassName('input', 'removecolumn', 'column_' + newID).setAttribute('name', 'action_removecolumn_id_' + newID);
}
}
......@@ -199,7 +199,9 @@ function ViewManager() {
this.rewriteDeleteButtons = function() {
forEach(getElementsByTagAndClassName('input', 'deletebutton', 'bottom-pane'), function(i) {
connect(i, 'onclick', function(e) {
sendjsonrequest('viewrework.json.php', {'view': $('viewid').value, 'action': 'delete_blockinstance', 'data': e.src().getAttribute('name')}, 'POST', function(data) {
var pd = {'view': $('viewid').value, 'change': 1};
pd['action_' + e.src().getAttribute('name')] = 1;
sendjsonrequest('viewrework.json.php', pd, 'POST', function(data) {
if (!data.error) {
// TODO: not happy with using .parentNode, it's fragile
removeElement(i.parentNode.parentNode);
......@@ -232,7 +234,9 @@ function ViewManager() {
connect(i, 'onclick', function(e) {
var name = e.src().getAttribute('name');
var id = parseInt(name.substr(-1));
sendjsonrequest('viewrework.json.php', {'view': $('viewid').value, 'action': 'add_column', 'column': id}, 'POST', function(data) {
var pd = {'view': $('viewid').value, 'change': 1}
pd['action_addcolumn_before_' + id] = 1;
sendjsonrequest('viewrework.json.php', pd, 'POST', function(data) {
if (!data.error) {
self.addColumn(id, data);
}
......@@ -264,7 +268,9 @@ function ViewManager() {
connect(i, 'onclick', function(e) {
var name = e.src().getAttribute('name');
var id = parseInt(name.substr(-1));
sendjsonrequest('viewrework.json.php', {'view': $('viewid').value, 'action': 'remove_column', 'column': id}, 'POST', function(data) {
var pd = {'view': $('viewid').value, 'change': 1}
pd['action_removecolumn_column_' + id] = 1;
sendjsonrequest('viewrework.json.php', pd, 'POST', function(data) {
if (!data.error) {
self.removeColumn(id);
}
......
......@@ -433,6 +433,9 @@ class View {
}
$this->columns[$this->get('numcolumns')] = null; // set the key
db_commit();
if ($values['returndata']) {
return view_build_column($this, $values['before'], true);
}
}
public function removecolumn($values) {
......
......@@ -239,7 +239,7 @@ function view_build_column(View $view, $column, $javascript=false) {
return $result;
}
function view_process_changes() {
function view_process_changes($ajax=false) {
global $SESSION;
if (!count($_POST)) {
......@@ -253,9 +253,6 @@ function view_process_changes() {
if (substr($key, 0, 7) == 'action_') {
$action = substr($key, 7);
}
else {
$data[$key] = $value;
}
}
$actionstring = $action;
......@@ -281,16 +278,32 @@ function view_process_changes() {
default:
throw new InvalidArgumentException(get_string('noviewcontrolaction', 'error', $action));
}
$message = '';
$success = false;
try {
$view->$action($values);
$SESSION->add_ok_msg($view->get_viewcontrol_ok_string($action));
$values['returndata'] = $ajax;
$returndata = $view->$action($values);
$message = $view->get_viewcontrol_ok_string($action);
$success = true;
}
catch (Exception $e) {
$SESSION->add_error_msg($view->get_viewcontrol_err_string($action) . ': ' . $e->getMessage());
// if we're in ajax land, just throw it
if ($ajax) {
throw $e;
}
$message = $view->get_viewcontrol_err_string($action) . ': ' . $e->getMessage();
}
if (empty($ajax)) {
$fun = 'add_ok_msg';
if (!$success) {
$fun = 'add_err_msg';
}
$SESSION->{$fun}($message);
// TODO fix this url
redirect('/viewrework.php?view=' . $view->get('id') . '&category=file');
}
// TODO fix this url
redirect('/viewrework.php?view=' . $view->get('id') . '&category=file');
return array('message' => $message, 'data' => $returndata);
}
......
......@@ -33,13 +33,21 @@ require('view.php');
$view = param_integer('view');
$view = new View($view);
$action = param_alphanumext('action');
$change = param_boolean('change', false);
$action = param_alphanumext('action', '');
// we actually ned to process stuff
if ($change) {
try {
$returndata = view_process_changes(true);
json_reply(false, $returndata);
}
catch (Exception $e) {
json_reply(true, $e->getMessage());
}
}
// else we're just reading data...
switch ($action) {
case 'delete_blockinstance':
// @todo
json_reply(false, false);
break;
case 'blocktype_list':
$category = param_alpha('category');
// TODO:
......@@ -65,26 +73,6 @@ case 'blocktype_list':
$data = view_build_blocktype_list($category, true);
json_reply(false, array('message' => false, 'data' => $data));
break;
case 'add_column':
$column = param_integer('column');
try {
$view->addcolumn(array('before' => $column));
json_reply(false, array('message' => false, 'data' => view_build_column($view, $column, true)));
}
catch (Exception $e) {
json_reply(true, 'Failed to add column: ' . $e->getMessage());
}
case 'remove_column':
$column = param_integer('column');
try {
$view->removecolumn(array('column' => $column));
// Just do it - no message
json_reply(false, false);
}
catch (Exception $e) {
json_reply(true, 'Failed to remove column: ' . $e->getMessage());
}
break;
}
json_reply(true, 'Unknown action "' . $action . '"');
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment