Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix(phoenix_html.js): Move code into a global module, so that even… #286

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions priv/static/phoenix_html.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"use strict";

(function() {
var PolyfillEvent = eventConstructor();

function eventConstructor() {
window._PhxForms = window._PhxForms || Object.freeze({
eventConstructor: function () {
if (typeof window.CustomEvent === "function") return window.CustomEvent;
// IE<=9 Support
function CustomEvent(event, params) {
Expand All @@ -14,20 +12,18 @@
}
CustomEvent.prototype = window.Event.prototype;
return CustomEvent;
}

function buildHiddenInput(name, value) {
},
buildHiddenInput: function (name, value) {
var input = document.createElement("input");
input.type = "hidden";
input.name = name;
input.value = value;
return input;
}

function handleClick(element) {
},
handleClick: function (element) {
var to = element.getAttribute("data-to"),
method = buildHiddenInput("_method", element.getAttribute("data-method")),
csrf = buildHiddenInput("_csrf_token", element.getAttribute("data-csrf")),
method = window._PhxForms.buildHiddenInput("_method", element.getAttribute("data-method")),
csrf = window._PhxForms.buildHiddenInput("_csrf_token", element.getAttribute("data-csrf")),
form = document.createElement("form"),
target = element.getAttribute("target");

Expand All @@ -41,36 +37,40 @@
form.appendChild(method);
document.body.appendChild(form);
form.submit();
}

window.addEventListener("click", function(e) {
},
clickHandler: function (e) {
var element = e.target;
var PolyfillEvent = window._PhxForms.eventConstructor();
var phoenixLinkEvent = new PolyfillEvent('phoenix.link.click', {
"bubbles": true,
"cancelable": true
});

while (element && element.getAttribute) {
var phoenixLinkEvent = new PolyfillEvent('phoenix.link.click', {
"bubbles": true, "cancelable": true
});

if (!element.dispatchEvent(phoenixLinkEvent)) {
e.preventDefault();
e.stopImmediatePropagation();
return false;
}

if (element.getAttribute("data-method")) {
handleClick(element);
window._PhxForms.handleClick(element);
e.preventDefault();
return false;
} else {
element = element.parentNode;
}
}
}, false);

window.addEventListener('phoenix.link.click', function (e) {
},
confirmHandler: function (e) {
var message = e.target.getAttribute("data-confirm");
if(message && !window.confirm(message)) {
if (message && !window.confirm(message)) {
e.preventDefault();
}
}, false);
}
});

(function() {
window.addEventListener("click", window._PhxForms.clickHandler, false);
window.addEventListener('phoenix.link.click', window._PhxForms.confirmHandler, false);
})();