Skip to content

Commit 3b03b41

Browse files
committed
refactor: single source of truth for phoenix internal attribute
1 parent 815e48a commit 3b03b41

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
* exported functions.
3030
*/
3131
function RemoteFunctions(config = {}) {
32+
33+
const GLOBALS = {
34+
// given to internal elements like info box, options box, image gallery and all other phcode internal elements
35+
// to distinguish between phoenix internal vs user created elements
36+
PHCODE_INTERNAL_ATTR: "data-phcode-internal-c15r5a9"
37+
};
38+
3239
// this will store the element that was clicked previously (before the new click)
3340
// we need this so that we can remove click styling from the previous element when a new element is clicked
3441
let previouslyClickedElement = null;
@@ -132,7 +139,7 @@ function RemoteFunctions(config = {}) {
132139
if(element && // element should exist
133140
element.tagName.toLowerCase() !== "body" && // shouldn't be the body tag
134141
element.tagName.toLowerCase() !== "html" && // shouldn't be the HTML tag
135-
!element.closest("[data-phcode-internal-c15r5a9]") && // this attribute is used by phoenix internal elements
142+
!element.closest(`[${GLOBALS.PHCODE_INTERNAL_ATTR}]`) && // this attribute is used by phoenix internal elements
136143
!_isInsideHeadTag(element)) { // shouldn't be inside the head tag like meta tags and all
137144
return true;
138145
}
@@ -1583,7 +1590,7 @@ function RemoteFunctions(config = {}) {
15831590

15841591
_style: function() {
15851592
this.body = window.document.createElement("div");
1586-
this.body.setAttribute("data-phcode-internal-c15r5a9", "true");
1593+
this.body.setAttribute(GLOBALS.PHCODE_INTERNAL_ATTR, "true");
15871594

15881595
// this is shadow DOM.
15891596
// we need it because if we add the box directly to the DOM then users style might override it.
@@ -1789,7 +1796,7 @@ function RemoteFunctions(config = {}) {
17891796

17901797
// Create shadow DOM container
17911798
this.body = document.createElement('div');
1792-
this.body.setAttribute('data-phcode-internal-c15r5a9', '1');
1799+
this.body.setAttribute(GLOBALS.PHCODE_INTERNAL_ATTR, "true");
17931800
document.body.appendChild(this.body);
17941801

17951802
const shadow = this.body.attachShadow({ mode: 'open' });
@@ -2053,7 +2060,7 @@ function RemoteFunctions(config = {}) {
20532060

20542061
_style: function() {
20552062
this.body = window.document.createElement("div");
2056-
this.body.setAttribute("data-phcode-internal-c15r5a9", "true");
2063+
this.body.setAttribute(GLOBALS.PHCODE_INTERNAL_ATTR, "true");
20572064

20582065
const shadow = this.body.attachShadow({ mode: "open" });
20592066

@@ -2327,7 +2334,7 @@ function RemoteFunctions(config = {}) {
23272334

23282335
_style: function() {
23292336
this.body = window.document.createElement("div");
2330-
this.body.setAttribute("data-phcode-internal-c15r5a9", "true");
2337+
this.body.setAttribute(GLOBALS.PHCODE_INTERNAL_ATTR, "true");
23312338

23322339
// this is shadow DOM.
23332340
// we need it because if we add the box directly to the DOM then users style might override it.
@@ -2541,7 +2548,7 @@ function RemoteFunctions(config = {}) {
25412548

25422549
_style: function() {
25432550
this.body = window.document.createElement("div");
2544-
this.body.setAttribute("data-phcode-internal-c15r5a9", "true");
2551+
this.body.setAttribute(GLOBALS.PHCODE_INTERNAL_ATTR, "true");
25452552
// using shadow dom so that user styles doesn't override it
25462553
const shadow = this.body.attachShadow({ mode: "open" });
25472554

@@ -2911,7 +2918,7 @@ function RemoteFunctions(config = {}) {
29112918
ImageRibbonGallery.prototype = {
29122919
_style: function () {
29132920
this.body = window.document.createElement("div");
2914-
this.body.setAttribute("data-phcode-internal-c15r5a9", "true");
2921+
this.body.setAttribute(GLOBALS.PHCODE_INTERNAL_ATTR, "true");
29152922
this._shadow = this.body.attachShadow({ mode: 'open' });
29162923

29172924
this._shadow.innerHTML = `
@@ -4303,7 +4310,7 @@ function RemoteFunctions(config = {}) {
43034310

43044311
const makeDiv = () => {
43054312
const el = document.createElement("div");
4306-
el.setAttribute("data-phcode-internal-c15r5a9", "true");
4313+
el.setAttribute(GLOBALS.PHCODE_INTERNAL_ATTR, "true");
43074314
return el;
43084315
};
43094316

@@ -4449,7 +4456,7 @@ function RemoteFunctions(config = {}) {
44494456

44504457
// create overlay container
44514458
const overlay = window.document.createElement('div');
4452-
overlay.setAttribute('data-phcode-internal-c15r5a9', 'true');
4459+
overlay.setAttribute(GLOBALS.PHCODE_INTERNAL_ATTR, 'true');
44534460

44544461
const styles = `
44554462
<style>
@@ -4806,7 +4813,7 @@ function RemoteFunctions(config = {}) {
48064813
const element = event.target;
48074814

48084815
// WHITELIST: Allow Phoenix internal UI elements to work normally
4809-
if (element.closest("[data-phcode-internal-c15r5a9]")) {
4816+
if (element.closest(`[${GLOBALS.PHCODE_INTERNAL_ATTR}]`)) {
48104817
return;
48114818
}
48124819

@@ -5520,7 +5527,7 @@ function RemoteFunctions(config = {}) {
55205527
// create a new fresh toast container
55215528
const toast = window.document.createElement('div');
55225529
toast.id = 'phoenix-toast-notification';
5523-
toast.setAttribute("data-phcode-internal-c15r5a9", "true");
5530+
toast.setAttribute(GLOBALS.PHCODE_INTERNAL_ATTR, "true");
55245531
const shadow = toast.attachShadow({ mode: 'open' });
55255532

55265533
const styles = `

0 commit comments

Comments
 (0)