Skip to content

Commit 7ce5683

Browse files
refactor: rename and enhance dynamia-tools.js with additional utility functions and improved clipboard handling
1 parent 0e9c959 commit 7ce5683

4 files changed

Lines changed: 157 additions & 92 deletions

File tree

zk/src/main/resources/metainfo/zk/lang-addon.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<stylesheet href="/static/dynamia-tools/css/zk-listbox-colors.css?v=${project.version}" type="text/css"/>
4141

4242

43-
<javascript src="/static/dynamia-tools/js/dynamia-tools.js?v=${project.version}"/>
43+
<javascript src="/static/dynamia-tools/js/zk/dynamia-tools.js?v=${project.version}"/>
4444

4545

4646

zk/src/main/resources/static/dynamia-tools/js/dynamia-tools.js

Lines changed: 0 additions & 91 deletions
This file was deleted.

zk/src/main/resources/static/dynamia-tools/js/dynamia-tools-ws.js renamed to zk/src/main/resources/static/dynamia-tools/js/zk/dynamia-tools-ws.js

File renamed without changes.
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* Copyright (C) 2023 Dynamia Soluciones IT S.A.S - NIT 900302344-1
3+
* Colombia / South America
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* Changes the browser URL hash or history state.
20+
* @param {string} value - The new hash value or page identifier.
21+
*/
22+
function changeHash(value) {
23+
if (window.history && window.history.pushState) {
24+
history.pushState({}, "Page " + value, getContextPath() + "/page/" + value);
25+
} else {
26+
window.location.hash = value;
27+
}
28+
}
29+
30+
/**
31+
* Sends the current URL hash to a ZK component.
32+
* @param {string} uuid - The UUID of the ZK component.
33+
*/
34+
function sendMeHash(uuid) {
35+
var hash = window.location.hash;
36+
if (hash) {
37+
hash = hash.substring(1, hash.length);
38+
if (typeof zAu !== 'undefined' && typeof zk !== 'undefined') {
39+
zAu.send(new zk.Event(zk.Widget.$(uuid), 'onHash', hash));
40+
}
41+
}
42+
}
43+
44+
/**
45+
* Gets the context path of the application.
46+
* @returns {string} The context path.
47+
*/
48+
function getContextPath() {
49+
var path = window.location.pathname.substring(0, window.location.pathname.indexOf("/", 2));
50+
if (path === '/page') {
51+
path = '';
52+
}
53+
return path;
54+
}
55+
56+
/**
57+
* Gets the server path (protocol + domain + port).
58+
* @returns {string} The server path.
59+
*/
60+
function getServerPath() {
61+
if (window.location.origin) {
62+
return window.location.origin;
63+
}
64+
return window.location.href.substring(0, window.location.href.indexOf(window.location.pathname));
65+
}
66+
67+
/**
68+
* Gets the full context path (server path + context path).
69+
* @returns {string} The full context path.
70+
*/
71+
function getFullContextPath() {
72+
return getServerPath() + getContextPath();
73+
}
74+
75+
/**
76+
* Opens a URL in a new tab.
77+
* @param {string} url - The URL to open.
78+
*/
79+
function openURL(url) {
80+
window.open(url, "_blank");
81+
}
82+
83+
/**
84+
* Copies text to the clipboard.
85+
* Tries to use the modern Clipboard API, falls back to execCommand, and finally to prompt.
86+
* @param {string} txt - The text to copy.
87+
*/
88+
function copyToClipboard(txt) {
89+
if (navigator.clipboard && window.isSecureContext) {
90+
navigator.clipboard.writeText(txt).catch(function(err) {
91+
fallbackCopyToClipboard(txt);
92+
});
93+
} else {
94+
fallbackCopyToClipboard(txt);
95+
}
96+
}
97+
98+
function fallbackCopyToClipboard(txt) {
99+
var textArea = document.createElement("textarea");
100+
textArea.value = txt;
101+
102+
// Avoid scrolling to bottom
103+
textArea.style.top = "0";
104+
textArea.style.left = "0";
105+
textArea.style.position = "fixed";
106+
107+
document.body.appendChild(textArea);
108+
textArea.focus();
109+
textArea.select();
110+
111+
try {
112+
var successful = document.execCommand('copy');
113+
if (!successful) {
114+
window.prompt("Press Ctrl+C and then Enter", txt);
115+
}
116+
} catch (err) {
117+
window.prompt("Press Ctrl+C and then Enter", txt);
118+
}
119+
120+
document.body.removeChild(textArea);
121+
}
122+
123+
/**
124+
* Toggles full screen mode.
125+
*/
126+
function toggleFullScreen() {
127+
if (document.fullScreenElement ||
128+
(!document.mozFullScreen && !document.webkitIsFullScreen)) {
129+
if (document.documentElement.requestFullScreen) {
130+
document.documentElement.requestFullScreen();
131+
} else if (document.documentElement.mozRequestFullScreen) {
132+
document.documentElement.mozRequestFullScreen();
133+
} else if (document.documentElement.webkitRequestFullScreen) {
134+
document.documentElement.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
135+
}
136+
} else {
137+
if (document.cancelFullScreen) {
138+
document.cancelFullScreen();
139+
} else if (document.mozCancelFullScreen) {
140+
document.mozCancelFullScreen();
141+
} else if (document.webkitCancelFullScreen) {
142+
document.webkitCancelFullScreen();
143+
}
144+
}
145+
}
146+
147+
/**
148+
* Changes the browser URI using history.pushState.
149+
* @param {string} title - The title of the new state.
150+
* @param {string} uri - The new URI.
151+
*/
152+
function changeURI(title, uri) {
153+
if (window.history && window.history.pushState) {
154+
history.pushState({}, title, uri);
155+
}
156+
}

0 commit comments

Comments
 (0)