2016-07-21 19:11:15 +08:00
|
|
|
//= require handsontable.full.min
|
|
|
|
//= require Sortable.min
|
|
|
|
//= require canvas-to-blob.min
|
|
|
|
//= require assets
|
2016-08-25 16:26:40 +08:00
|
|
|
//= require comments
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
(function(global) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Sets callbacks for toggling checkboxes
|
|
|
|
function applyCheckboxCallBack() {
|
2018-07-17 17:41:42 +08:00
|
|
|
$("[data-action='check-item']").off().on('click', function(e){
|
2017-05-16 17:52:07 +08:00
|
|
|
var checkboxitem = $(this).find("input");
|
|
|
|
var checked = checkboxitem.is(":checked");
|
2017-05-04 18:41:22 +08:00
|
|
|
$.ajax({
|
2017-05-16 17:52:07 +08:00
|
|
|
url: checkboxitem.data("link-url"),
|
|
|
|
type: "POST",
|
|
|
|
dataType: "json",
|
|
|
|
data: {checklistitem_id: checkboxitem.data("id"), checked: checked},
|
|
|
|
success: function (data) {
|
|
|
|
checkboxitem.prop("checked", checked);
|
2017-05-04 18:41:22 +08:00
|
|
|
},
|
2017-05-16 17:52:07 +08:00
|
|
|
error: function (data) {
|
|
|
|
checkboxitem.prop("checked", !checked);
|
2017-05-04 18:41:22 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-05-16 17:52:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complete mymodule
|
|
|
|
function complete_my_module_actions() {
|
|
|
|
var modal = $('#completed-task-modal');
|
|
|
|
|
|
|
|
modal.find('[data-action="complete"]')
|
|
|
|
.off().on().click(function(event) {
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
$.ajax({
|
|
|
|
url: modal.data('url'),
|
|
|
|
type: 'GET',
|
|
|
|
success: function(data) {
|
|
|
|
var task_button = $("[data-action='complete-task']");
|
|
|
|
task_button.attr('data-action', 'uncomplete-task');
|
|
|
|
task_button.find('.btn')
|
2018-05-18 22:24:24 +08:00
|
|
|
.removeClass('btn-toggle').addClass('btn-default');
|
2017-05-16 17:52:07 +08:00
|
|
|
$('.task-due-date').html(data.module_header_due_date_label);
|
|
|
|
$('.task-state-label').html(data.module_state_label);
|
|
|
|
task_button
|
|
|
|
.find('button')
|
2018-07-09 19:13:44 +08:00
|
|
|
.html('<span class="fas fa-times"></span> ' +
|
2017-05-16 17:52:07 +08:00
|
|
|
data.task_button_title);
|
|
|
|
modal.modal('hide');
|
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
modal.modal('hide');
|
2017-02-10 21:27:20 +08:00
|
|
|
}
|
2017-05-16 17:52:07 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
// Sets callback for completing/uncompleting step
|
|
|
|
function applyStepCompletedCallBack() {
|
|
|
|
// First, remove old event handlers, as we use turbolinks
|
|
|
|
$("[data-action='complete-step'], [data-action='uncomplete-step']")
|
|
|
|
.off().on('click', function(e){
|
|
|
|
var button = $(this);
|
|
|
|
var step = $(this).parents(".step");
|
|
|
|
var completed = !step.hasClass("completed");
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
$.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");
|
2018-05-18 22:24:24 +08:00
|
|
|
button.find(".btn").removeClass("btn-toggle").addClass("btn-default");
|
2018-07-09 19:13:44 +08:00
|
|
|
button.find("button").html('<span class="fas fa-times"></span> ' + data.new_title);
|
2017-05-16 17:52:07 +08:00
|
|
|
|
|
|
|
if (data.task_ready_to_complete) {
|
|
|
|
$('#completed-task-modal').modal('show');
|
|
|
|
complete_my_module_actions();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
step.addClass("not-completed").removeClass("completed");
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
button = step.find("[data-action='uncomplete-step']");
|
|
|
|
button.attr("data-action", "complete-step");
|
2018-05-18 22:24:24 +08:00
|
|
|
button.find(".btn").removeClass("btn-default").addClass("btn-toggle");
|
2018-07-09 19:13:44 +08:00
|
|
|
button.find("button").html('<span class="fas fa-check"></span> ' + data.new_title);
|
2017-05-16 17:52:07 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function (data) {
|
|
|
|
console.log ("error");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
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);
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
initStepsComments();
|
|
|
|
initPreviewModal();
|
|
|
|
SmartAnnotation.preventPropagation('.atwho-user-popover');
|
|
|
|
TinyMCE.destroyAll();
|
2017-11-28 23:48:47 +08:00
|
|
|
DragNDropSteps.clearFiles();
|
2017-05-16 17:52:07 +08:00
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
})
|
|
|
|
.on("ajax:error", function(e, xhr, status, error) {
|
|
|
|
// TODO: error handling
|
|
|
|
});
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
// 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();
|
2016-11-22 21:39:08 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
formCallback($form);
|
|
|
|
initEditableHandsOnTable($form);
|
|
|
|
applyCancelCallBack();
|
|
|
|
formEditAjax($form);
|
|
|
|
toggleButtons(false);
|
|
|
|
initializeCheckboxSorting();
|
|
|
|
animateSpinner(null, false);
|
2017-01-06 22:58:23 +08:00
|
|
|
initPreviewModal();
|
2017-11-28 23:48:47 +08:00
|
|
|
DragNDropSteps.clearFiles();
|
|
|
|
TinyMCE.refresh();
|
2017-05-16 17:52:07 +08:00
|
|
|
$("#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']").off("ajax:success");
|
|
|
|
$("[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");
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
// 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 });
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
});
|
2017-05-16 17:52:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-07-09 19:13:44 +08:00
|
|
|
collapseIcon.toggleClass("fa-caret-square-up", !collapsed);
|
|
|
|
collapseIcon.toggleClass("fa-caret-square-down", collapsed);
|
2017-05-16 17:52:07 +08:00
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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("");
|
|
|
|
}
|
2016-07-21 19:11:15 +08:00
|
|
|
});
|
2017-05-16 17:52:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init ajax success/error for edit form
|
|
|
|
function formEditAjax($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);
|
|
|
|
toggleButtons(true);
|
|
|
|
initPreviewModal();
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
TinyMCE.destroyAll();
|
|
|
|
SmartAnnotation.preventPropagation('.atwho-user-popover');
|
|
|
|
// 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));
|
|
|
|
});
|
|
|
|
setupAssetsLoading();
|
|
|
|
})
|
|
|
|
.on("ajax:error", function(e, xhr, status, error) {
|
|
|
|
$form.renderFormErrors('step', xhr.responseJSON, true, e);
|
|
|
|
|
|
|
|
formCallback($form);
|
2016-02-12 23:52:43 +08:00
|
|
|
initEditableHandsOnTable($form);
|
2017-05-16 17:52:07 +08:00
|
|
|
applyCancelCallBack();
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
TinyMCE.refresh();
|
|
|
|
SmartAnnotation.preventPropagation('.atwho-user-popover');
|
|
|
|
|
|
|
|
//Rerender tables
|
|
|
|
$form.find("[data-role='step-hot-table']").each(function() {
|
|
|
|
renderTable($(this));
|
|
|
|
});
|
2016-02-12 23:52:43 +08:00
|
|
|
});
|
2017-05-16 17:52:07 +08:00
|
|
|
}
|
2016-11-22 21:39:08 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
function toggleButtons(show) {
|
|
|
|
if (show) {
|
|
|
|
$("[data-action='new-step']").show();
|
|
|
|
$("[data-action='edit-step']").show();
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
// 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();
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
} else {
|
|
|
|
$("[data-action='new-step']").hide();
|
|
|
|
$("[data-action='edit-step']").hide();
|
2016-07-22 19:29:01 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
// Also hide "no steps" label if no steps are present
|
|
|
|
$("[data-role='no-steps-text']").hide();
|
|
|
|
}
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
// 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');
|
2017-01-23 16:47:04 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
$container.handsontable({
|
|
|
|
startRows: <%= Constants::HANDSONTABLE_INIT_ROWS_CNT %>,
|
|
|
|
startCols: <%= Constants::HANDSONTABLE_INIT_COLS_CNT %>,
|
|
|
|
rowHeaders: true,
|
|
|
|
colHeaders: true,
|
|
|
|
fillHandle: false,
|
|
|
|
formulas: true,
|
|
|
|
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);
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
});
|
2017-05-16 17:52:07 +08:00
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
// Init handsontable which can be edited
|
|
|
|
function initEditableHandsOnTable(root) {
|
|
|
|
root.find("[data-role='editable-table']").each(function() {
|
|
|
|
var $container = $(this).find(".hot");
|
2017-01-23 16:47:04 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
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: <%= Constants::HANDSONTABLE_INIT_ROWS_CNT %>,
|
|
|
|
startCols: <%= Constants::HANDSONTABLE_INIT_COLS_CNT %>,
|
|
|
|
minRows: 1,
|
|
|
|
minCols: 1,
|
|
|
|
rowHeaders: true,
|
|
|
|
colHeaders: true,
|
|
|
|
contextMenu: true,
|
|
|
|
formulas: true,
|
|
|
|
preventOverflow: 'horizontal'
|
|
|
|
});
|
|
|
|
|
|
|
|
$container.attr("initialized", true);
|
|
|
|
renderTable($container);
|
2016-07-21 19:11:15 +08:00
|
|
|
});
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
$("#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));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2016-07-22 19:29:01 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
function applyCancelOnNew() {
|
|
|
|
$("[data-action='cancel-new']").click(function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
event.stopImmediatePropagation();
|
2017-01-25 21:22:06 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
var $form = $(this).closest("form");
|
|
|
|
$form.parent().remove().promise().done(function() {
|
|
|
|
newStepHandler();
|
|
|
|
});
|
|
|
|
toggleButtons(true);
|
2017-11-28 23:48:47 +08:00
|
|
|
DragNDropSteps.clearFiles();
|
2017-05-16 17:52:07 +08:00
|
|
|
});
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
function initDeleteStep(){
|
|
|
|
$("[data-action='delete-step']").on("confirm:complete", function (e, answer) {
|
|
|
|
if (answer) {
|
|
|
|
animateLoading();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
function initCallBacks() {
|
|
|
|
applyCheckboxCallBack();
|
|
|
|
applyStepCompletedCallBack();
|
|
|
|
applyEditCallBack();
|
|
|
|
applyMoveStepCallBack();
|
|
|
|
applyCollapseLinkCallBack();
|
|
|
|
initDeleteStep();
|
|
|
|
TinyMCE.highlight();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Correction for sorting with "Sortable.min" JS library to work correctly with
|
|
|
|
* "nested_form_fields" gem.
|
|
|
|
*/
|
|
|
|
function reorderCheckboxData(checkboxUl) {
|
|
|
|
// Make sure checkbox item insertion script is always at the bottom of "ul"
|
|
|
|
// tag, otherwise item will not be inserted at bottom
|
|
|
|
if(!$(checkboxUl).children().last().is('script')) {
|
|
|
|
$(checkboxUl).find("script").appendTo(checkboxUl);
|
2016-07-21 19:11:15 +08:00
|
|
|
}
|
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
var $checkboxes = $(checkboxUl).find(".nested_fields");
|
|
|
|
$checkboxes.each(function (itemPos) {
|
|
|
|
var $this = $(this);
|
|
|
|
|
|
|
|
var $formGroup = $this.find(".form-group");
|
|
|
|
var $label = $formGroup.find(".control-label");
|
|
|
|
var $textInput = $formGroup.find(".checklist-item-text");
|
|
|
|
var $posInput = $formGroup.parent().find(".checklist-item-pos");
|
|
|
|
var $destroyLink = $this.find(".remove_nested_fields_link");
|
|
|
|
|
|
|
|
var labelFor = $label.attr("for");
|
|
|
|
var textName = $textInput.attr("name");
|
|
|
|
var textId = $textInput.attr("id");
|
|
|
|
var posName = $posInput.attr("name");
|
|
|
|
var posId = $posInput.attr("id");
|
|
|
|
var destroyLink = $destroyLink.attr("data-delete-association-field-name");
|
|
|
|
|
|
|
|
labelFor = labelFor.replace(/\d+_text/, itemPos + "_text");
|
|
|
|
textName = textName.replace(/\[\d+\]\[text\]/, "[" + itemPos + "][text]");
|
|
|
|
textId = textId.replace(/\d+_text/, itemPos + "_text");
|
|
|
|
posName = posName.replace(/\[\d+\]\[position\]/, "[" + itemPos + "][position]");
|
|
|
|
posId = posId.replace(/\d+_position/, itemPos + "_position");
|
|
|
|
destroyLink = destroyLink.replace(/\[\d+\]\[_destroy\]/, "[" + itemPos + "][_destroy]");
|
|
|
|
|
|
|
|
$label.attr("for", labelFor);
|
|
|
|
$textInput.attr("name", textName); // Actually needed for sorting to work
|
|
|
|
$textInput.attr("id", textId);
|
|
|
|
$posInput.attr("name", posName);
|
|
|
|
$posInput.attr("id", posId);
|
|
|
|
$posInput.val(itemPos);
|
|
|
|
$destroyLink.attr("data-delete-association-field-name", destroyLink);
|
|
|
|
|
|
|
|
var $idInput = $this.find("> input");
|
|
|
|
if ($idInput.length) {
|
|
|
|
var idName = $idInput.attr("name");
|
|
|
|
var idId = $idInput.attr("id");
|
|
|
|
|
|
|
|
idName = idName.replace(/\[\d+\]\[id\]/, "[" + itemPos + "][id]");
|
|
|
|
idId = idId.replace(/\d+_id/, itemPos + "_id");
|
|
|
|
|
|
|
|
$idInput.attr("name", idName);
|
|
|
|
$idInput.attr("id", idId);
|
|
|
|
}
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
if ($this.css('display') == 'none') {
|
|
|
|
// Actually needed for deleting to work
|
|
|
|
var $destroyInput = $this.prev();
|
|
|
|
var destroyName = $destroyInput.attr("name");
|
|
|
|
destroyName = destroyName.replace(/\[\d+\]\[_destroy\]/, "[" + itemPos + "][_destroy]");
|
|
|
|
$destroyInput.attr("name", destroyName);
|
|
|
|
}
|
|
|
|
});
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
2017-05-16 17:52:07 +08:00
|
|
|
|
|
|
|
function enableCheckboxSorting(el) {
|
|
|
|
Sortable.create(el, {
|
|
|
|
draggable: 'fieldset',
|
2018-05-22 22:55:58 +08:00
|
|
|
handle: '.fa-circle',
|
2017-05-16 17:52:07 +08:00
|
|
|
onUpdate: function () {
|
|
|
|
reorderCheckboxData(el);
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
|
|
|
});
|
2017-05-16 17:52:07 +08:00
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
function initializeCheckboxSorting() {
|
|
|
|
var el = $("#new-step-checklists a[data-association-path=step_checklists]");
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
el.click(function () {
|
|
|
|
// calling code below must be defered because at this step HTML is not
|
|
|
|
// inserted into DOM.
|
|
|
|
setTimeout(function () {
|
|
|
|
var list = el.parent().find("fieldset.nested_step_checklists:last ul");
|
|
|
|
enableCheckboxSorting(list.get(0));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
// New step AJAX
|
|
|
|
function newStepHandler() {
|
|
|
|
$("[data-action='new-step']").off().on('click', function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
var $btn = $(this);
|
|
|
|
$btn.off();
|
|
|
|
animateSpinner(null, true);
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
$.ajax({
|
|
|
|
url: $btn.data('href'),
|
|
|
|
method: 'GET',
|
|
|
|
success: function(data) {
|
|
|
|
var $form = $(data.html);
|
|
|
|
$('#steps').append($form).promise().done(function() {
|
|
|
|
animateSpinner(null, false);
|
|
|
|
// Scroll to bottom
|
|
|
|
$('html, body').animate({
|
|
|
|
scrollTop: $(document).height() - $(window).height()
|
|
|
|
});
|
|
|
|
formCallback($form);
|
|
|
|
applyCancelOnNew();
|
|
|
|
toggleButtons(false);
|
|
|
|
initializeCheckboxSorting();
|
|
|
|
|
|
|
|
$('#step_name').focus();
|
|
|
|
$('#new-step-main-tab a').on('shown.bs.tab', function() {
|
|
|
|
$('#step_name').focus();
|
|
|
|
});
|
|
|
|
|
|
|
|
TinyMCE.refresh();
|
|
|
|
});
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
newStepHandler();
|
|
|
|
}
|
|
|
|
})
|
2016-02-12 23:52:43 +08:00
|
|
|
});
|
2017-05-16 17:52:07 +08:00
|
|
|
}
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
// Needed because server-side validation failure clears locations of
|
|
|
|
// files to be uploaded and checklist's items etc. Also user
|
|
|
|
// experience is improved
|
|
|
|
global.processStep = function processStep(ev, editMode) {
|
|
|
|
ev.stopPropagation();
|
2017-06-02 22:34:09 +08:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopImmediatePropagation();
|
2017-05-16 17:52:07 +08:00
|
|
|
|
|
|
|
var $form = $(ev.target.form);
|
|
|
|
$form.clearFormErrors();
|
|
|
|
$form.removeBlankFileForms();
|
|
|
|
|
|
|
|
var $checklists = $form.find(".nested_step_checklists");
|
|
|
|
var checklistsValid = checklistsValidator(ev, $checklists, editMode);
|
|
|
|
var $nameInput = $form.find("#step_name");
|
|
|
|
var nameValid = textValidator(ev, $nameInput, 1,
|
|
|
|
<%= Constants::NAME_MAX_LENGTH %>);
|
|
|
|
var $descrTextarea = $form.find("#step_description");
|
2017-09-07 20:54:12 +08:00
|
|
|
var $tinyMCEInput = TinyMCE.getContent();
|
2017-05-16 17:52:07 +08:00
|
|
|
var descriptionValid = textValidator(ev, $descrTextarea, 0,
|
2017-09-07 20:54:12 +08:00
|
|
|
<%= Constants::TEXT_MAX_LENGTH %>, false, $tinyMCEInput);
|
2017-05-16 17:52:07 +08:00
|
|
|
|
2017-06-07 19:29:39 +08:00
|
|
|
if (DragNDropSteps.filesStatus() &&
|
2017-06-02 22:34:09 +08:00
|
|
|
checklistsValid &&
|
|
|
|
nameValid &&
|
|
|
|
descriptionValid) {
|
|
|
|
|
|
|
|
$form.find("[data-role='editable-table']").each(function() {
|
|
|
|
var hot = $(this).find(".hot").handsontable('getInstance');
|
|
|
|
var contents = $(this).find('.hot-contents');
|
|
|
|
var data = JSON.stringify({data: hot.getData()});
|
|
|
|
contents.attr("value", data);
|
|
|
|
});
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
initStepsComments();
|
|
|
|
SmartAnnotation.preventPropagation('.atwho-user-popover');
|
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
animateSpinner(null, true);
|
2017-06-07 19:29:39 +08:00
|
|
|
var data = DragNDropSteps.appendFilesToForm(ev);
|
2017-06-02 22:34:09 +08:00
|
|
|
data.append('step[description]', TinyMCE.getContent());
|
|
|
|
$.ajax({
|
|
|
|
url: $form.attr('action'),
|
|
|
|
method: 'POST',
|
|
|
|
data: data,
|
|
|
|
contentType: false,
|
|
|
|
processData: false,
|
|
|
|
beforeSend: function() {
|
|
|
|
$(".nested_step_checklists ul").each(function () {
|
|
|
|
reorderCheckboxData(this);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
success: function(data) {
|
|
|
|
$($form.closest('.well')).after(data.html);
|
|
|
|
var $new_step = $($form.closest('.well')).next();
|
|
|
|
$($form.closest('.well')).remove();
|
|
|
|
|
|
|
|
initCallBacks();
|
|
|
|
initHandsOnTable($new_step);
|
|
|
|
expandStep($new_step);
|
|
|
|
toggleButtons(true);
|
|
|
|
SmartAnnotation.preventPropagation('.atwho-user-popover');
|
|
|
|
|
|
|
|
//Rerender tables
|
|
|
|
$new_step.find("div.step-result-hot-table").each(function() {
|
|
|
|
$(this).handsontable("render");
|
|
|
|
});
|
|
|
|
animateSpinner(null, false);
|
|
|
|
setupAssetsLoading();
|
2017-12-01 18:51:22 +08:00
|
|
|
DragNDropSteps.clearFiles();
|
2018-04-26 23:00:51 +08:00
|
|
|
initPreviewModal();
|
2017-06-02 22:34:09 +08:00
|
|
|
},
|
2017-11-21 20:48:06 +08:00
|
|
|
error: function(xhr) {
|
2017-11-28 18:34:57 +08:00
|
|
|
if (xhr.responseJSON['assets.file']) {
|
2017-11-21 20:48:06 +08:00
|
|
|
$('#new-step-assets-group').addClass('has-error');
|
|
|
|
$('#new-step-assets-tab').addClass('has-error');
|
|
|
|
$.each(xhr.responseJSON['assets.file'], function(_, value) {
|
|
|
|
$('#new-step-assets-group').prepend('<span class="help-block">' + value + '</span>');
|
|
|
|
});
|
|
|
|
}
|
2017-06-02 22:34:09 +08:00
|
|
|
animateSpinner(null, false);
|
|
|
|
SmartAnnotation.preventPropagation('.atwho-user-popover');
|
|
|
|
}
|
|
|
|
});
|
2017-05-16 17:52:07 +08:00
|
|
|
newStepHandler();
|
|
|
|
}
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
// Expand all steps
|
|
|
|
function expandAllSteps() {
|
|
|
|
$('.step .panel-collapse').collapse('show');
|
|
|
|
$(document).find("[data-role='step-hot-table']").each(function() {
|
2016-07-21 19:11:15 +08:00
|
|
|
renderTable($(this));
|
2016-02-12 23:52:43 +08:00
|
|
|
});
|
2017-05-16 17:52:07 +08:00
|
|
|
$(document).find("span.collapse-step-icon").each(function() {
|
2018-07-09 19:13:44 +08:00
|
|
|
$(this).addClass("fa-caret-square-up");
|
|
|
|
$(this).removeClass("fa-caret-square-down");
|
2017-05-16 17:52:07 +08:00
|
|
|
});
|
2016-09-12 22:52:51 +08:00
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
function expandStep(step) {
|
|
|
|
$('.panel-collapse', step).collapse('show');
|
|
|
|
$(step).find("span.collapse-step-icon")
|
2018-07-09 19:13:44 +08:00
|
|
|
.addClass("fa-caret-square-up")
|
|
|
|
.removeClass("fa-caret-square-down");
|
2017-05-16 17:52:07 +08:00
|
|
|
$(step).find("div.step-result-hot-table").each(function() {
|
|
|
|
renderTable($(this));
|
|
|
|
});
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-16 17:52:07 +08:00
|
|
|
function renderTable(table) {
|
|
|
|
$(table).handsontable("render");
|
|
|
|
// Yet another dirty hack to solve HandsOnTable problems
|
2017-06-01 21:52:02 +08:00
|
|
|
if (parseInt($(table).css('height'), 10) < parseInt($(table).css('max-height'), 10) - 30) {
|
|
|
|
$(table).find('.ht_master .wtHolder').css({ 'height': '100%',
|
|
|
|
'width': '100%' });
|
2016-09-12 22:52:51 +08:00
|
|
|
}
|
2016-07-14 00:47:06 +08:00
|
|
|
}
|
2017-05-16 17:52:07 +08:00
|
|
|
|
|
|
|
function initStepsComments() {
|
|
|
|
Comments.initialize();
|
|
|
|
Comments.initCommentOptions("ul.content-comments");
|
|
|
|
Comments.initEditComments("#steps");
|
|
|
|
Comments.initDeleteComments("#steps");
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
2017-05-16 17:52:07 +08:00
|
|
|
|
2018-07-19 23:56:42 +08:00
|
|
|
// On init
|
|
|
|
initCallBacks();
|
|
|
|
initHandsOnTable($(document));
|
|
|
|
expandAllSteps();
|
|
|
|
setupAssetsLoading();
|
|
|
|
initStepsComments();
|
|
|
|
initPreviewModal();
|
|
|
|
TinyMCE.highlight();
|
|
|
|
SmartAnnotation.preventPropagation('.atwho-user-popover');
|
|
|
|
newStepHandler();
|
|
|
|
|
|
|
|
$(function () {
|
|
|
|
$("[data-action='collapse-steps']").click(function () {
|
|
|
|
$('.step .panel-collapse').collapse('hide');
|
|
|
|
$(document).find("span.collapse-step-icon").each(function() {
|
|
|
|
$(this).addClass("fa-caret-square-down");
|
|
|
|
$(this).removeClass("fa-caret-square-up");
|
2016-09-05 21:06:48 +08:00
|
|
|
});
|
|
|
|
});
|
2018-07-19 23:56:42 +08:00
|
|
|
$("[data-action='expand-steps']").click(expandAllSteps);
|
|
|
|
});
|
2016-09-05 21:06:48 +08:00
|
|
|
|
2017-06-06 14:02:01 +08:00
|
|
|
global.initHandsOnTable = initHandsOnTable;
|
2017-05-16 17:52:07 +08:00
|
|
|
})(window);
|