$(function() { abracadabraSubmissionInProgress = false; abracadabraButtonMousedown = false; abracadabraEscapeKeydown = false; function closeAbracadabra(element, destroy, valueChanged) { $element = $(element); if($element.hasClass("abracadabra-container")) { container = $element; } else { container = $element.parents(".abracadabra-container"); } if(destroy) { container.siblings(".abracadabra").remove(); } else { if(valueChanged == true) { value = container.find(".abracadabra-input").val(); } else { value = container.find(".abracadabra-input").data("original-value"); } container.siblings(".abracadabra").text(value).show(); } try { container.remove(); } catch (error) { if(error.name != "NotFoundError") { throw error; } } } function tabToNextAbracadabra(element, selector) { if(selector != undefined) { nextAbracadabra = $(selector); /* If selector isn't an ID, find the element with the class AFTER the current element */ if (selector.indexOf("#") == -1) { abracadabra = $(element).parents(".abracadabra-container").siblings(".abracadabra"); indexOfAbracadabra = $(selector).index(abracadabra); nextAbracadabra = $($(selector)[indexOfAbracadabra + 1]); } /* /If selector isn't an ID, find the element with the class AFTER the current element */ closeAbracadabra(element, false, true); nextAbracadabra.click(); } else { closeAbracadabra(element, false, true); } } $("body").on("submit", ".abracadabra-form", function(e) { if(abracadabraSubmissionInProgress == true || abracadabraEscapeKeydown == true) { console.log(abracadabraEscapeKeydown) e.preventDefault(); abracadabraEscapeKeydown = false; return false; } abracadabraSubmissionInProgress = true; }); $("body").on("ajax:before", ".abracadabra-delete", function() { if(abracadabraSubmissionInProgress == true) { e.preventDefault(); return false; } abracadabraSubmissionInProgress = true; }); $("body").on("mousedown", ".abracadabra-delete, .abracadabra-submit, .abracadabra-cancel", function() { abracadabraButtonMousedown = true; }); $("body").on("ajax:success", ".abracadabra-form", function(e) { target = $(e.target); abracadabraButtonMousedown = false; /* If form is a DELETE, remove abracadabra instance, if not, call tabToNextSelector */ if(target.hasClass("abracadabra-delete")) { closeAbracadabra(target, true, true); } else { input = $(target).find(".abracadabra-input"); tabToNextSelector = input.data("tab-to-next-selector"); tabToNextAbracadabra(target, tabToNextSelector); } /* /If form is a DELETE, remove abracadabra instance, if not, call tabToNextSelector */ abracadabraSubmissionInProgress = false; }); $("body").on("click", ".abracadabra-cancel", function() { if(abracadabraSubmissionInProgress == false) { closeAbracadabra(this, false, false); } }); $("body").on("blur", ".abracadabra-input", function() { if(abracadabraSubmissionInProgress == false && abracadabraButtonMousedown == false) { if($(this).data("submit-on-blur") == true) { $(this.form).submit(); } else { closeAbracadabra(this, false, false); } } }); $("body").on("keydown", ".abracadabra-input", function(e) { /* Press Tab to submit (same function as Enter key) */ if (e.keyCode == 9) { e.preventDefault(); if(abracadabraSubmissionInProgress == false) { $(this.form).submit(); } } /* /Press Tab to submit (same function as Enter key) */ /* Press Escape to cancel */ if (e.keyCode == 27) { abracadabraEscapeKeydown = true; e.preventDefault(); if(abracadabraSubmissionInProgress == false) { closeAbracadabra(this, false, false); } } /* /Press Escape to cancel */ }); $("body").on("confirm:complete", ".abracadabra-delete", function(e, response) { /* If cancel is clicked in the deletable_confirm dialog, focus on the input */ if(response == false) { input = $(this).parents(".abracadabra-delete-container").siblings(); inputValue = input.val(); input.focus().val("").val(inputValue); } /* /If cancel is clicked in the deletable_confirm dialog, focus on the input */ }); $(".abracadabra").on("click", function() { if($(".abracadabra-container:visible").length) { return false; } link = $(this); link.hide(); path = link.data("path"); attribute = link.data("attribute"); formMethod = link.data("method"); remote = ((link.data("remote") == true) ? " data-remote=\"true\"" : ""); /* Check if button classes have been manually overridden elsewhere */ if(typeof abracadabraSubmitIcon == "undefined") { abracadabraSubmitIcon = "fa fa-check"; } if(typeof abracadabraCancelIcon == "undefined") { abracadabraCancelIcon = "fa fa-times"; } if(typeof abracadabraDeleteIcon == "undefined") { abracadabraDeleteIcon = "fa fa-times-circle-o"; } /* /Check if button classes have been manually overridden elsewhere */ /* AJAX? */ if(remote == "") { authToken = ""; type = ""; deletableType = ""; } else { authToken = ""; type = " data-type=\"" + link.data("type") + "\""; deletableType = " data-type=\"" + link.data("deletable-type") + "\""; } /* /AJAX? */ /* Deletable? */ if(link.data("deletable") !== false) { deletablePath = link.data("deletable-path"); deletableConfirm = link.data("deletable"); if(deletableConfirm === true) { deletableConfirm = ""; } else { deletableConfirm = " data-confirm=\"" + deletableConfirm + "\""; } deletable = ""; } else { deletable = ""; } /* /Deletable? */ /* Tab to next? */ tabToNextSelector = link.data("tab-to-next"); if(tabToNextSelector !== false) { tabToNextSelector = link.data("tab-to-next"); tabToNextSelector = " data-tab-to-next-selector=\"" + tabToNextSelector + "\""; } else { tabToNextSelector = ""; } /* /Tab to next? */ /* Submit on blur? */ submitOnBlur = link.data("submit-on-blur"); if(submitOnBlur == true) { submitOnBlur = " data-submit-on-blur=\"true\""; } else { submitOnBlur = ""; } /* /Submit on blur? */ /* Show buttons? */ if(link.data("buttonless") == true) { buttons = ""; } else { buttons = ""; } /* /Show buttons? */ instanceClass = link.data("class"); inputValue = link.text().replace(/"|\\"/g, """); inputId = instanceClass + "_" + attribute; inputName = instanceClass + "[" + attribute + "]"; openFormTag = "
"; hiddenMethodTags = "
" + authToken + "
"; input = ""; html = "" + openFormTag + hiddenMethodTags; html += "
" + input + deletable + "
"; html += "
" + buttons + "
"; link.after(html); link.siblings(".abracadabra-container").find(".abracadabra-input").focus().val("").val(inputValue); }); });