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

112 lines
2.8 KiB
JavaScript
Raw Normal View History

2016-02-12 23:52:43 +08:00
// Init handsontable which can be edited
function initEditableHandsOnTable(root) {
root.find(".editable-table").each(function() {
var $container = $(this).find(".hot");
2016-07-21 19:11:15 +08:00
var data = null;
var contents = $(this).find('.hot-contents');
if (contents.attr("value")) {
data = JSON.parse(contents.attr("value")).data;
}
2016-02-12 23:52:43 +08:00
$container.handsontable({
2016-07-21 19:11:15 +08:00
data: data,
2016-02-12 23:52:43 +08:00
startRows: 5,
startCols: 5,
2016-07-21 19:11:15 +08:00
minRows: 1,
minCols: 1,
2016-02-12 23:52:43 +08:00
rowHeaders: true,
colHeaders: true,
2016-07-21 19:11:15 +08:00
contextMenu: true,
2016-09-05 19:54:20 +08:00
formulas: true,
2016-07-21 19:11:15 +08:00
preventOverflow: 'horizontal'
2016-02-12 23:52:43 +08:00
});
});
}
function onSubmitExtractTable($form) {
$form.submit(function(){
var hot = $(".hot").handsontable('getInstance');
var contents = $('.hot-contents');
var data = JSON.stringify({data: hot.getData()});
2016-07-21 19:11:15 +08:00
contents.attr("value", data);
2016-02-12 23:52:43 +08:00
return true;
});
}
// Edit result table button behaviour
function applyEditResultTableCallback() {
$(".edit-result-table").on("ajax:success", function(e, data) {
var $result = $(this).closest(".result");
var $form = $(data.html);
var $prevResult = $result;
$result.after($form);
$result.remove();
formAjaxResultTable($form);
initEditableHandsOnTable($form);
onSubmitExtractTable($form);
// Cancel button
$form.find(".cancel-edit").click(function () {
$form.after($prevResult);
$form.remove();
applyEditResultTableCallback();
toggleResultEditButtons(true);
});
toggleResultEditButtons(false);
2016-07-21 19:11:15 +08:00
$("#result_name").focus();
2016-02-12 23:52:43 +08:00
});
$(".edit-result-table").on("ajax:error", function(e, xhr, status, error) {
//TODO: Add error handling
});
}
// New result text behaviour
$("#new-result-table").on("ajax:success", function(e, data) {
var $form = $(data.html);
$("#results").prepend($form);
formAjaxResultTable($form);
initEditableHandsOnTable($form);
onSubmitExtractTable($form);
// Cancel button
$form.find(".cancel-new").click(function () {
$form.remove();
toggleResultEditButtons(true);
});
toggleResultEditButtons(false);
2016-07-21 19:11:15 +08:00
$("#result_name").focus();
2016-02-12 23:52:43 +08:00
});
$("#new-result-table").on("ajax:error", function(e, xhr, status, error) {
//TODO: Add error handling
});
// Apply ajax callback to form
function formAjaxResultTable($form) {
$form.on("ajax:success", function(e, data) {
$form.after(data.html);
$result = $(this).next();
initFormSubmitLinks($result);
$(this).remove();
applyEditResultTableCallback();
2016-07-21 19:11:15 +08:00
applyCollapseLinkCallBack();
2016-02-12 23:52:43 +08:00
initHandsOnTables($result);
toggleResultEditButtons(true);
initResultCommentTabAjax();
expandResult($result);
});
$form.on("ajax:error", function(e, xhr, status, error) {
var data = xhr.responseJSON;
$form.renderFormErrors("result", data);
2016-02-12 23:52:43 +08:00
});
}
applyEditResultTableCallback();