scinote-web/app/assets/javascripts/results/result_assets.js

74 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-02-12 23:52:43 +08:00
// New result asset behaviour
$("#new-result-asset").on("ajax:success", function(e, data) {
2016-07-21 19:11:15 +08:00
var $form = $(data.html);
$("#results").prepend($form);
2016-02-12 23:52:43 +08:00
2016-07-21 19:11:15 +08:00
formAjaxResultAsset($form);
2016-02-12 23:52:43 +08:00
2016-07-21 19:11:15 +08:00
// Cancel button
$form.find(".cancel-new").click(function () {
$form.remove();
toggleResultEditButtons(true);
});
2016-02-12 23:52:43 +08:00
2016-07-21 19:11:15 +08:00
toggleResultEditButtons(false);
$("#result_name").focus();
2016-02-12 23:52:43 +08:00
});
$("#new-result-asset").on("ajax:error", function(e, xhr, status, error) {
// TODO
2016-02-12 23:52:43 +08:00
});
// Edit result asset button behaviour
function applyEditResultAssetCallback() {
2016-07-21 19:11:15 +08:00
$(".edit-result-asset").on("ajax:success", function(e, data) {
var $result = $(this).closest(".result");
var $form = $(data.html);
var $prevResult = $result;
$result.after($form);
$result.remove();
2016-02-12 23:52:43 +08:00
2016-07-21 19:11:15 +08:00
formAjaxResultAsset($form);
// Cancel button
$form.find(".cancel-edit").click(function () {
$form.after($prevResult);
$form.remove();
applyEditResultAssetCallback();
toggleResultEditButtons(true);
2016-02-12 23:52:43 +08:00
});
2016-07-21 19:11:15 +08:00
toggleResultEditButtons(false);
$("#result_name").focus();
});
$(".edit-result-asset").on("ajax:error", function(e, xhr, status, error) {
// TODO
2016-07-21 19:11:15 +08:00
});
2016-02-12 23:52:43 +08:00
}
// Apply ajax callback to form
function formAjaxResultAsset($form) {
2016-07-21 19:11:15 +08:00
$form
.on("ajax:success", function(e, data) {
$form.after(data.html);
var $newResult = $form.next();
initFormSubmitLinks($newResult);
$(this).remove();
applyEditResultAssetCallback();
applyCollapseLinkCallBack();
2016-07-21 19:11:15 +08:00
toggleResultEditButtons(true);
expandResult($newResult);
$imgs = $newResult.find("img");
reloadImages($imgs);
2016-07-21 19:11:15 +08:00
})
.on("ajax:error", function(e, data) {
$form.renderFormErrors("result", data.errors, true, e);
2016-07-21 19:11:15 +08:00
});
2016-02-12 23:52:43 +08:00
}
applyEditResultAssetCallback();