//= require handsontable.full.min //= require Sortable.min //= require canvas-to-blob.min //= require direct-upload //= require assets // Sets callbacks for toggling checkboxes function applyCheckboxCallBack() { $("[data-action='check-item']").on('click', function(e){ var checkboxitem = $(this).find("input"); var checked = checkboxitem.is(":checked"); $.ajax({ url: checkboxitem.data("link-url"), type: "POST", dataType: "json", data: {checklistitem_id: checkboxitem.data("id"), checked: checked}, success: function (data) { checkboxitem.prop("checked", checked); }, error: function (data) { checkboxitem.prop("checked", !checked); } }); }); } // Sets callback for completing/uncompleting step function applyStepCompletedCallBack() { $("[data-action='complete-step'], [data-action='uncomplete-step']").on('click', function(e){ var button = $(this); var step = $(this).parents(".step"); var completed = !step.hasClass("completed"); $.ajax({ url: button.data("link-url"), type: "POST", dataType: "json", data: {completed: completed}, success: function (data) { var button; if (completed) { step.addClass("completed").removeClass("not-completed"); button = step.find("[data-action='complete-step']"); button.attr("data-action", "uncomplete-step"); button.find(".btn").removeClass("btn-primary").addClass("btn-default"); } else { step.addClass("not-completed").removeClass("completed"); button = step.find("[data-action='uncomplete-step']"); button.attr("data-action", "complete-step"); button.find(".btn").removeClass("btn-default").addClass("btn-primary"); } button.find("button").html(data.new_title); }, error: function (data) { console.log ("error"); } }); }); } function applyCancelCallBack() { //Click on cancel button $("[data-action='cancel-edit']") .on("ajax:success", function(e, data) { var $form = $(this).closest("form"); $form.after(data.html); var $new_step = $(this).next(); $(this).remove(); initCallBacks(); initHandsOnTable($new_step); toggleButtons(true); }) .on("ajax:error", function(e, xhr, status, error) { // TODO: error handling }); } // Set callback for click on edit button function applyEditCallBack() { $("[data-action='edit-step']") .on("ajax:success", function(e, data) { var $step = $(this).closest(".step"); var $edit_step = $step.after(data.html); var $form = $step.next(); $step.remove(); formCallback($form); initEditableHandsOnTable($form); applyCancelCallBack(); formEditAjax($form); toggleButtons(false); initializeCheckboxSorting(); $("#new-step-checklists fieldset.nested_step_checklists ul").each(function () { enableCheckboxSorting(this); }); $("#step_name").focus(); $("#new-step-main-tab a").on("shown.bs.tab", function() { $("#step_name").focus(); }); }); } // Set callback for click on edit button function applyMoveStepCallBack() { $("[data-action='move-step']") .on("ajax:success", function(e, data) { var $step = $(this).closest(".step"); var stepUpPosition = data.step_up_position; var stepDownPosition = data.step_down_position; var $stepDown, $stepUp; switch (data.move_direction) { case "up": $stepDown = $step.prev(".step"); $stepUp = $step; break; case "down": $stepDown = $step; $stepUp = $step.next(".step"); } // Switch position of top and bottom steps if (!_.isUndefined($stepDown) && !_.isUndefined($stepUp)) { $stepDown.insertAfter($stepUp); $stepDown.find(".badge").html(stepDownPosition+1); $stepUp.find(".badge").html(stepUpPosition+1); $("html, body").animate({ scrollTop: $step.offset().top - window.innerHeight / 2 }); } }); } function applyCollapseLinkCallBack() { $(".step-panel-collapse-link") .on("ajax:success", function() { var collapseIcon = $(this).find(".collapse-step-icon"); var collapsed = $(this).hasClass("collapsed"); // Toggle collapse button collapseIcon.toggleClass("glyphicon-collapse-up", !collapsed); collapseIcon.toggleClass("glyphicon-collapse-down", collapsed); }); } function formCallback($form) { $form .on("fields_added.nested_form_fields", function(e, param) { if (param.object_class == "table") { initEditableHandsOnTable($form); } }) .on("fields_removed.nested_form_fields", function(e, param) { if (param.object_class == "asset") { // Clear file input $(e.target).find("input[type='file']").val(""); } }); // Add asset validation $form.add_upload_file_size_check(function() { tabsPropagateErrorClass($form); }); // Add hidden fields for tables $form.submit(function(){ $(this).find("[data-role='editable-table']").each(function() { var hot = $(this).find(".hot").handsontable('getInstance'); if (hot && hot.countEmptyRows() != hot.countRows()) { var contents = $(this).find('.hot-contents'); var data = JSON.stringify({data: hot.getData()}); contents.attr("value", data); } }); return true; }); } // Init ajax success/error for edit form function formEditAjax($form) { var selectedTabIndex; $form .on("ajax:beforeSend", function () { $(".nested_step_checklists ul").each(function () { reorderCheckboxData(this); }); }) .on("ajax:send", function(e, data) { selectedTabIndex = $form.find("li.active").index() + 1; }) .on("ajax:success", function(e, data) { $(this).after(data.html); var $new_step = $(this).next(); $(this).remove(); initCallBacks(); initHandsOnTable($new_step); toggleButtons(true); // Show the edited step $new_step.find(".panel-collapse:first").addClass("collapse in"); //Rerender tables $new_step.find("[data-role='step-hot-table']").each(function() { renderTable($(this)); }); animateSpinner(null, false); }) .on("ajax:error", function(e, xhr, status, error) { $(this).after(xhr.responseJSON.html); var $form = $(this).next(); $(this).remove(); formCallback($form); initEditableHandsOnTable($form); applyCancelCallBack(); formEditAjax($form); tabsPropagateErrorClass($form); //Rerender tables $form.find("[data-role='step-hot-table']").each(function() { renderTable($(this)); }); // Select the same tab pane as before $form.find("ul.nav-tabs li.active").removeClass("active"); $form.find(".tab-content div.active").removeClass("active"); $form.find("ul.nav-tabs li:nth-child(" + selectedTabIndex + ")").addClass("active"); $form.find(".tab-content div:nth-child(" + selectedTabIndex + ")").addClass("active"); animateSpinner(null, false); }); } function formNewAjax($form) { $form .on("ajax:beforeSend", function () { $(".nested_step_checklists ul").each(function () { reorderCheckboxData(this); }); }) .on("ajax:success", function(e, data) { $(this).after(data.html); var $new_step = $(this).next(); $(this).remove(); initCallBacks(); initHandsOnTable($new_step); expandStep($new_step); toggleButtons(true); //Rerender tables $new_step.find("div.step-result-hot-table").each(function() { $(this).handsontable("render"); }); animateSpinner(null, false); }) .on("ajax:error", function(e, xhr, status, error) { $(this).after(xhr.responseJSON.html); var $form = $(this).next(); $(this).remove(); formCallback($form); formNewAjax($form); applyCancelOnNew(); tabsPropagateErrorClass($form); animateSpinner(null, false); }); } function toggleButtons(show) { if (show) { $("[data-action='new-step']").show(); $("[data-action='edit-step']").show(); // Also show "no steps" label if no steps are present if (!$(".step").length) { $("[data-role='no-steps-text']").show(); } else { $("[data-role='no-steps-text']").hide(); } } else { $("[data-action='new-step']").hide(); $("[data-action='edit-step']").hide(); // Also hide "no steps" label if no steps are present $("[data-role='no-steps-text']").hide(); } } // Creates handsontable where needed function initHandsOnTable(root) { root.find("[data-role='hot-table']").each(function() { var $container = $(this).find("[data-role='step-hot-table']"); var contents = $(this).find('.hot-contents'); $container.handsontable({ startRows: 5, startCols: 5, rowHeaders: true, colHeaders: true, fillHandle: false, cells: function (row, col, prop) { var cellProperties = {}; if (col >= 0) cellProperties.readOnly = true; else cellProperties.readOnly = false; return cellProperties; } }); var hot = $container.handsontable('getInstance'); if (contents.attr("value")) { var data = JSON.parse(contents.attr("value")); hot.loadData(data.data); } }); } // Init handsontable which can be edited function initEditableHandsOnTable(root) { root.find("[data-role='editable-table']").each(function() { var $container = $(this).find(".hot"); if ($container.is("[initialized]")) { return true; } var contents = $(this).find('.hot-contents'); var data = null; if (contents.attr("value")) { data = JSON.parse(contents.attr("value")).data; } if ($container.is(":visible")) { $(this).css("width", $("#new-step-tables").css("width")); } $container.handsontable({ data: data, startRows: 5, startCols: 5, minRows: 1, minCols: 1, rowHeaders: true, colHeaders: true, contextMenu: true, preventOverflow: 'horizontal' }); $container.attr("initialized", true); renderTable($container); }); $("#new-step-tables-tab a") .on("shown.bs.tab", function() { $(this).parents("form").find("div.hot").each(function() { $(this).parent().css("width", $("#new-step-tables").css("width")); renderTable($(this)); }); }); } // Initialize comment form. function initStepCommentForm($el) { var $form = $el.find("ul form"); $(".help-block", $form).addClass("hide"); $form.on("ajax:send", function (data) { $("#comment_message", $form).attr("readonly", true); }) .on("ajax:success", function (e, data) { if (data.html) { var list = $form.parents("ul"); // Remove potential "no comments" element list.parent().find(".content-comments") .find("li.no-comments").remove(); list.parent().find(".content-comments") .prepend("