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

Commit 3643fcd

Browse files
author
Robin
committed
add btn live block
1 parent 693395b commit 3643fcd

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "piedweb-cms-js-helpers",
3-
"version": "0.0.9",
3+
"version": "0.0.10",
44
"description": "PiedWeb/CMS (php)'s javascript functions for ui front end (native)",
55
"author": "Robin D. <[email protected]>",
66
"license": "MIT",

src/helpers.js

+28-10
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,46 @@
1212
*/
1313

1414
/**
15+
* Live Block Watcher (and button)
16+
*
1517
* Fetch (ajax) function permitting to get block via a POST request
16-
* (prevent from spam)
1718
*
1819
* @param {string} attribute
1920
*/
2021
export function liveBlock(attribute = "data-live") {
21-
document.querySelectorAll("[" + attribute + "]").forEach(item => {
22+
23+
var btnToBlock = function(event, btn) {
24+
btn.setAttribute(attribute, btn.getAttribute("src-"+attribute));
25+
getLiveBlock(btn);
26+
}
27+
28+
var getLiveBlock = function(item) {
2229
fetch(item.getAttribute(attribute), {
2330
//headers: { "Content-Type": "application/json", Accept: "text/plain" },
2431
method: "POST",
2532
credentials: "include"
2633
})
27-
.then(function(response) {
28-
return response.text();
29-
})
30-
.then(function(body) {
31-
item.removeAttribute(attribute);
32-
item.innerHTML = body;
34+
.then(function(response) {
35+
return response.text();
36+
})
37+
.then(function(body) {
38+
item.removeAttribute(attribute);
39+
item.outerHTML = body;
3340

34-
document.dispatchEvent(new Event("DOMChanged"));
35-
});
41+
document.dispatchEvent(new Event("DOMChanged"));
42+
});
43+
}
44+
45+
document.querySelectorAll("[" + attribute + "]").forEach(item => {
46+
getLiveBlock(item);
3647
});
48+
49+
document.querySelectorAll("[src-" + attribute + "]").forEach(item => {
50+
item.addEventListener("click", event => {
51+
btnToBlock(event, item);
52+
});
53+
});
54+
3755
}
3856

3957
/**

0 commit comments

Comments
 (0)