Commit 292540fa authored by Rafael Ostertag's avatar Rafael Ostertag
Browse files

QfqForm.js:

 * use anonymous functions for ajax error displays
 * expect response from server to file upload to have `status` and `message` attributes
 * replaced occurrences of `QfqNS` by `n` in order to support different namespace names.
parent 076db686
......@@ -11,6 +11,7 @@ if (!QfqNS) {
(function (n) {
'use strict';
// TODO: This object is getting too big. Start refactoring.
n.QfqForm = function (formId, submitTo, deleteUrl, dataRefreshUrl, fileUploadTo) {
this.formId = formId;
this.submitTo = submitTo;
......@@ -28,7 +29,10 @@ if (!QfqNS) {
this.form.addChangeHandler(this.changeHandler.bind(this));
this.form.addResetHandler(this.resetHandler.bind(this));
this.form.addSubmitSuccessHandler(this.submitSuccessDispatcher.bind(this));
this.form.addSubmitFailureHandler(this.submitFailureHandler.bind(this));
this.form.addSubmitFailureHandler(
function (form, textStatus, jqXHR, errorThrown) {
n.Helper.showAjaxError(jqXHR, textStatus, errorThrown);
});
this.getSaveButton().click(this.handleSaveClick.bind(this));
this.getCloseButton().click(this.handleCloseClick.bind(this));
......@@ -37,15 +41,38 @@ if (!QfqNS) {
this.setupFormUpdateHandler();
this.fileUploader = new QfqNS.FileUpload('#' + this.formId, this.fileUploadTo, this.getSip());
this.fileUploader = new n.FileUpload('#' + this.formId, this.fileUploadTo, this.getSip());
this.fileUploader.addFileUploadStartHandler(this.startUploadHandler);
this.fileUploader.addFileUploadSuccessHandler(this.fileUploadSuccessHandler);
this.fileUploader.addFileUploadErrorHandler(
function (uploadTriggeredBy, textStatus, errorThrown) {
n.Helper.showAjaxError(null, textStatus, errorThrown);
});
this.fileUploader.addFileUploadEndHandler(this.endUploadHandler);
};
/**
* @public
* @param bsTabs
*/
n.QfqForm.prototype.setBsTabs = function (bsTabs) {
this.bsTabs = bsTabs;
};
/**
* @private
*/
n.QfqForm.prototype.fileUploadSuccessHandler = function (uploadTriggeredBy, data, textStatus) {
if (!data.status) {
throw Error("Response on file upload missing status");
}
if (data.status === "error") {
var alert = new n.Alert(data.message, "error");
alert.show();
}
};
/**
*
* @param $button
......@@ -87,7 +114,7 @@ if (!QfqNS) {
}
if (data.status === "error") {
var alert = new QfqNS.Alert("Error while updating form:<br>" + (data.message ? data.message : "No reason" +
var alert = new n.Alert("Error while updating form:<br>" + (data.message ? data.message : "No reason" +
" given"), "error");
alert.show();
return;
......@@ -120,7 +147,7 @@ if (!QfqNS) {
*/
n.QfqForm.prototype.handleSaveClick = function () {
this.lastButtonPress = "save";
QfqNS.Log.debug("save click");
n.Log.debug("save click");
// First, remove all validation states, in case a previous submit has set a validation state, thus we're not
// stockpiling them.
this.clearAllValidationStates();
......@@ -133,7 +160,7 @@ if (!QfqNS) {
n.QfqForm.prototype.handleCloseClick = function () {
this.lastButtonPress = "close";
if (this.form.getFormChanged()) {
var alert = new QfqNS.Alert("You have unsaved changes. Do you want to close?", "warning", "yesnosave");
var alert = new n.Alert("You have unsaved changes. Do you want to close?", "warning", "yesnosave");
var that = this;
alert.addSaveButtonHandler(function () {
that.form.submitTo(that.submitTo);
......@@ -152,7 +179,7 @@ if (!QfqNS) {
*/
n.QfqForm.prototype.handleNewClick = function () {
this.lastButtonPress = "new";
QfqNS.Log.debug("new click");
n.Log.debug("new click");
};
/**
......@@ -160,8 +187,8 @@ if (!QfqNS) {
*/
n.QfqForm.prototype.handleDeleteClick = function () {
this.lastButtonPress = "delete";
QfqNS.Log.debug("delete click");
var alert = new QfqNS.Alert("Do you really want to delete the record?", "warning", "yesno");
n.Log.debug("delete click");
var alert = new n.Alert("Do you really want to delete the record?", "warning", "yesno");
var that = this;
alert.addOkButtonHandler(function () {
$.post(that.deleteUrl)
......@@ -215,7 +242,7 @@ if (!QfqNS) {
}
if (data.redirect === "no") {
var alert = new QfqNS.Alert("redirect=='no' not allowed", "error");
var alert = new n.Alert("redirect=='no' not allowed", "error");
alert.show();
return;
}
......@@ -236,13 +263,13 @@ if (!QfqNS) {
if (!data.message) {
throw Error("Status is 'error' but required 'message' attribute is missing.");
}
var alert = new QfqNS.Alert(data.message, "error");
var alert = new n.Alert(data.message, "error");
alert.show();
};
/**
*
* @param form {QfqNS.QfqForm}
* @param form {n.QfqForm}
*
* @private
*/
......@@ -254,7 +281,7 @@ if (!QfqNS) {
/**
*
* @param form {QfqNS.QfqForm}
* @param form {n.QfqForm}
*
* @private
*/
......@@ -304,18 +331,6 @@ if (!QfqNS) {
return $("#new-button");
};
/**
*
* @param jqXHR
* @param textStatus
* @param errorThrown
*
* @private
*/
n.QfqForm.prototype.submitFailureHandler = function (form, textStatus, jqXHR, errorThrown) {
n.Helper.showAjaxError(errorThrown);
};
/**
* @private
......@@ -349,7 +364,7 @@ if (!QfqNS) {
if (!data.message) {
throw Error("Status is 'error' but required 'message' attribute is missing.");
}
var alert = new QfqNS.Alert(data.message, "error");
var alert = new n.Alert(data.message, "error");
alert.show();
if (data["field-name"] && this.bsTabs) {
......@@ -371,12 +386,12 @@ if (!QfqNS) {
* @private
*/
n.QfqForm.prototype.handleSubmitSuccess = function (form, data) {
QfqNS.Log.debug('Reset form state');
n.Log.debug('Reset form state');
form.resetFormChanged();
if (this.lastButtonPress === 'save' || !data.redirect || data.redirect === "no") {
if (data.message) {
var alert = new QfqNS.Alert(data.message);
var alert = new n.Alert(data.message);
alert.timeout = 1500;
alert.show();
}
......@@ -404,7 +419,7 @@ if (!QfqNS) {
n.QfqForm.prototype.getFormGroupByControlName = function (formControlName) {
var $formControl = $("[name='" + formControlName + "']");
if ($formControl.length === 0) {
QfqNS.Log.debug("QfqForm.setValidationState(): unable to find form control with name '" + formControlName + "'");
n.Log.debug("QfqForm.setValidationState(): unable to find form control with name '" + formControlName + "'");
return null;
}
......@@ -484,7 +499,7 @@ if (!QfqNS) {
var configurationItem = configuration[i];
var formElementName = configurationItem["form-element"];
if (formElementName === undefined) {
QfqNS.Log.error("configuration lacks 'form-element' attribute. Skipping.");
n.Log.error("configuration lacks 'form-element' attribute. Skipping.");
continue;
}
try {
......@@ -503,7 +518,7 @@ if (!QfqNS) {
}
} catch (e) {
QfqNS.Log.error(e.message);
n.Log.error(e.message);
}
}
};
......
......@@ -3,10 +3,15 @@
* @author Rafael Ostertag <rafael.ostertag@math.uzh.ch>
*/
var_dump($_POST);
header("Content-Type: text/json");
foreach ($_FILES as $key => $value) {
echo "$key";
echo file_get_contents($value['tmp_name']);
foreach ($_FILES as $key => &$value) {
$value['file_content'] = file_get_contents($value['tmp_name']);
}
echo json_encode([
'status' => "ok",
'files_received' => $_FILES,
'request_variables' => $_REQUEST
]);
<?php
/**
* @author Rafael Ostertag <rafael.ostertag@math.uzh.ch>
*/
header("Content-Type: text/json");
echo json_encode([
'status' => "error",
'message'
]);
......@@ -37,6 +37,14 @@
</select>
</label>
<label>Upload to
<select name="uploadTo" id="uploadTo">
<option>404 error</option>
<option>uploadhandler.php</option>
<option>uploadhandler_error.php</option>
</select>
</label>
<div class="container-fluid">
<div class="row hidden-xs">
......@@ -973,7 +981,7 @@
formId: 'myForm',
submitTo: 'api/' + $("#submitTo").val(),
deleteUrl: 'api/' + $("#deleteUrl").val(),
fileUploadTo: 'api/uploadhandler.php'
fileUploadTo: 'api/' + $("#uploadTo").val()
});
$("#submitTo").on("change", function (evt) {
......@@ -986,6 +994,11 @@
qfqPage.qfqForm.deleteUrl = 'api/' + $(evt.target).val();
});
$("#uploadTo").on("change", function (evt) {
qfqPage.settings.fileUploadTo = 'api/' + $(evt.target).val();
qfqPage.qfqForm.fileUploader.targetUrl = 'api/' + $(evt.target).val();
});
QfqNS.Log.level = 0;
});
</script>
......
Supports Markdown
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