Skip to content
This repository has been archived by the owner on Jan 20, 2021. It is now read-only.

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin committed Jun 2, 2020
1 parent 3643fcd commit a7d1bd1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "piedweb-cms-js-helpers",
"version": "0.0.10",
"version": "0.0.12",
"description": "PiedWeb/CMS (php)'s javascript functions for ui front end (native)",
"author": "Robin D. <[email protected]>",
"license": "MIT",
Expand Down
65 changes: 33 additions & 32 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
*
* @param {string} attribute
*/
export function liveBlock(attribute = "data-live") {
export function liveBlock(liveBlockAttribute = "data-live", liveFormSelector = ".live-form") {

var btnToBlock = function(event, btn) {
btn.setAttribute(attribute, btn.getAttribute("src-"+attribute));
getLiveBlock(btn);
btn.setAttribute(liveBlockAttribute, btn.getAttribute("src-"+liveBlockAttribute));
getLiveBlock(btn);
}

var getLiveBlock = function(item) {
fetch(item.getAttribute(attribute), {
fetch(item.getAttribute(liveBlockAttribute), {
//headers: { "Content-Type": "application/json", Accept: "text/plain" },
method: "POST",
credentials: "include"
Expand All @@ -35,44 +35,22 @@ export function liveBlock(attribute = "data-live") {
return response.text();
})
.then(function(body) {
item.removeAttribute(attribute);
item.removeAttribute(liveBlockAttribute);
item.outerHTML = body;

}).
then(function() {
document.dispatchEvent(new Event("DOMChanged"));
});
}

document.querySelectorAll("[" + attribute + "]").forEach(item => {
getLiveBlock(item);
});

document.querySelectorAll("[src-" + attribute + "]").forEach(item => {
item.addEventListener("click", event => {
btnToBlock(event, item);
});
});

}

/**
* ajaxify-form
*/
export function liveForm(selector = '.live-form') {
document.querySelectorAll(selector).forEach(item => {
if (item.querySelector("form") !== null) {
item.querySelector("form").addEventListener("submit", e => {
e.preventDefault();
sendForm(e, item);
});
}
});

var htmlLoader = '<div style="width:1em;height:1em;border: 2px solid #222;border-top-color: #fff;border-radius: 50%; animation: 1s spin linear infinite;"></div><style>@keyframes spin {from{transform:rotate(0deg)}to{transform:rotate(360deg)}}</style>';

var setLoader = function (form) {
var $submitButton = getSubmitButton(form);
if ($submitButton !== undefined) {
var initialButton = $submitButton.outerHTML;
$submitButton.innerHTML = '';
$submitButton.outerHTML = '<div style="width:1em;height:1em;border: 2px solid #222;border-top-color: #fff;border-radius: 50%; animation: 1s spin linear infinite;"></div><style>@keyframes spin {from{transform:rotate(0deg)}to{transform:rotate(360deg)}}</style>';
$submitButton.outerHTML = htmlLoader;
}
}

Expand Down Expand Up @@ -105,6 +83,29 @@ export function liveForm(selector = '.live-form') {
}
return null;
};


// Listen data-live
document.querySelectorAll("[" + liveBlockAttribute + "]").forEach(item => {
getLiveBlock(item);
});

// Listen button src-data-live
document.querySelectorAll("[src-" + liveBlockAttribute + "]").forEach(item => {
item.addEventListener("click", event => {
btnToBlock(event, item);
});
});

// Listen live-form
document.querySelectorAll(liveFormSelector).forEach(item => {
if (item.querySelector("form") !== null) {
item.querySelector("form").addEventListener("submit", e => {
e.preventDefault();
sendForm(e, item);
});
}
});
}

/**
Expand Down

0 comments on commit a7d1bd1

Please sign in to comment.