Skip to content

Commit 5cbf916

Browse files
committed
Added createClipboard() method to Utils.js. Also added new alert() and confirm() methods from the javaxt-express project.
1 parent 12412f9 commit 5cbf916

File tree

1 file changed

+227
-2
lines changed

1 file changed

+227
-2
lines changed

src/utils/Utils.js

Lines changed: 227 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,6 @@ javaxt.dhtml.utils = {
281281
var d2 = diff(obj2, obj1);
282282

283283
return merge(d1,d2);
284-
285-
286284
},
287285

288286

@@ -647,6 +645,38 @@ javaxt.dhtml.utils = {
647645
},
648646

649647

648+
//**************************************************************************
649+
//** createClipboard
650+
//**************************************************************************
651+
/** Used to create a hidden clipboard in a given parent. Text and other data
652+
* can be inserted into the clipboard via the insert() method on the DOM
653+
* object returned by this method. Once data is inserted into the clipboard,
654+
* clients can retrieve the data via the browser "paste" event (e.g. ctrl+v
655+
* on windows).
656+
* @param parent DOM element used to hold the clipboard (required)
657+
* @returns DOM object (dov) with a custom insert() method
658+
*/
659+
createClipboard: function(parent){
660+
var createElement = javaxt.dhtml.utils.createElement;
661+
662+
var clipboard = createElement("textarea");
663+
clipboard.insert = function(str){
664+
clipboard.value = str;
665+
clipboard.select();
666+
clipboard.setSelectionRange(0, 99999);
667+
document.execCommand('copy');
668+
};
669+
var clipboardDiv = createElement("div", parent, {
670+
position: "absolute",
671+
left: "-9999px",
672+
width: "0px",
673+
height: "0px"
674+
});
675+
clipboardDiv.appendChild(clipboard);
676+
return clipboard;
677+
},
678+
679+
650680
//**************************************************************************
651681
//** getSuggestedColumnWidths
652682
//**************************************************************************
@@ -1471,6 +1501,201 @@ javaxt.dhtml.utils = {
14711501
else{
14721502
return Math.round(number);
14731503
}
1504+
},
1505+
1506+
1507+
//**************************************************************************
1508+
//** alert
1509+
//**************************************************************************
1510+
/** Used to render an alert dialog using the javaxt.dhtml.Window class.
1511+
* @param msg Message to display in the alert. Supports strings and XHR
1512+
* responses with "responseText".
1513+
* @param config Optional config used to instantiate the
1514+
* javaxt.dhtml.Window class.
1515+
*/
1516+
alert: function(msg, config){
1517+
var win = javaxt.dhtml.utils.Alert;
1518+
if (win && win.isOpen()) return;
1519+
1520+
1521+
if (msg==null) msg = "";
1522+
1523+
1524+
//Special case for ajax request
1525+
if (!(typeof(msg) === 'string' || msg instanceof String)){
1526+
if (typeof msg.responseText !== 'undefined'){
1527+
msg = (msg.responseText.length>0 ? msg.responseText : msg.statusText);
1528+
if (!msg) msg = "Unknown Server Error";
1529+
}
1530+
}
1531+
1532+
1533+
var win = javaxt.dhtml.utils.Alert;
1534+
if (!win){
1535+
var createElement = javaxt.dhtml.utils.createElement;
1536+
1537+
1538+
var outerDiv = createElement('div', {
1539+
position: "relative",
1540+
width: "100%",
1541+
height: "100%",
1542+
cursor: "inherit"
1543+
});
1544+
1545+
var innerDiv = createElement('div', outerDiv, {
1546+
position: "absolute",
1547+
width: "100%",
1548+
height: "100%",
1549+
overflowX: "hidden",
1550+
cursor: "inherit"
1551+
});
1552+
1553+
1554+
if (!config) config = {};
1555+
javaxt.dhtml.utils.merge(config, {
1556+
width: 450,
1557+
height: 200,
1558+
valign: "top",
1559+
modal: true,
1560+
title: "Alert",
1561+
body: outerDiv,
1562+
style: {
1563+
panel: "window",
1564+
header: "window-header alert-header",
1565+
title: "window-title",
1566+
buttonBar: "window-header-button-bar",
1567+
button: "window-header-button",
1568+
body: "window-body alert-body"
1569+
}
1570+
});
1571+
1572+
1573+
win = new javaxt.dhtml.Window(document.body, config);
1574+
win.div = innerDiv;
1575+
javaxt.dhtml.utils.Alert = win;
1576+
}
1577+
1578+
1579+
win.div.innerHTML = msg;
1580+
1581+
win.show();
1582+
return win;
1583+
},
1584+
1585+
1586+
//**************************************************************************
1587+
//** confirm
1588+
//**************************************************************************
1589+
/** Used to render a confirm dialog using the javaxt.dhtml.Window class.
1590+
* Example:
1591+
<pre>
1592+
javaxt.dhtml.utils.confirm({
1593+
title: "Quit Game?",
1594+
text: "Are you sure you want to quit the game?",
1595+
leftButton: {
1596+
label: "Yes",
1597+
value: true
1598+
},
1599+
rightButton: {
1600+
label: "No",
1601+
value: false
1602+
},
1603+
callback: function(answer){
1604+
if (answer===true) console.log("quit game");
1605+
else console.log("continue game");
1606+
}
1607+
});
1608+
</pre>
1609+
@param msg String with question/prompt or a JSON config like the example
1610+
above. If passing a string, a default config is used which can be
1611+
overridden using the optional "config" parameter.
1612+
@param config JSON config like the example above. This parameter is
1613+
optional.
1614+
*/
1615+
confirm: function(msg, config){
1616+
var win = javaxt.dhtml.utils.Confirm;
1617+
if (win && win.isOpen()) return;
1618+
1619+
1620+
if (!(typeof(msg) === 'string' || msg instanceof String)){
1621+
config = msg;
1622+
}
1623+
1624+
if (!config) config = {};
1625+
javaxt.dhtml.utils.merge(config, {
1626+
title: "Confirm",
1627+
text: msg
1628+
});
1629+
1630+
1631+
//Create new window as needed
1632+
if (!win){
1633+
var createElement = javaxt.dhtml.utils.createElement;
1634+
1635+
1636+
var buttonDiv = createElement("div", "button-div");
1637+
1638+
1639+
var createButton = function(label, result){
1640+
var input = createElement("input", buttonDiv, "form-button");
1641+
input.type = "button";
1642+
1643+
input.onclick = function(){
1644+
win.result = this.result;
1645+
win.close();
1646+
};
1647+
input.setLabel = function(label){
1648+
if (label) this.name = this.value = label;
1649+
};
1650+
input.setValue = function(b){
1651+
if (b===true || b===false) this.result = b;
1652+
};
1653+
input.update = function(config){
1654+
if (config){
1655+
this.setLabel(config.label);
1656+
this.setValue(config.value);
1657+
}
1658+
};
1659+
input.setLabel(label);
1660+
input.setValue(result);
1661+
return input;
1662+
};
1663+
1664+
1665+
win = new javaxt.dhtml.Window(document.body, {
1666+
width: 450,
1667+
height: 150,
1668+
valign: "top",
1669+
modal: true,
1670+
footer: buttonDiv,
1671+
style: {
1672+
panel: "window",
1673+
header: "window-header",
1674+
title: "window-title",
1675+
buttonBar: "window-header-button-bar",
1676+
button: "window-header-button",
1677+
body: "window-body confirm-body"
1678+
}
1679+
});
1680+
javaxt.dhtml.utils.Confirm = win;
1681+
1682+
1683+
win.leftButton = createButton("OK", true);
1684+
win.rightButton = createButton("Cancel", false);
1685+
}
1686+
1687+
1688+
win.setTitle(config.title);
1689+
win.setContent(config.text.replace("\n","<p></p>"));
1690+
win.leftButton.update(config.leftButton);
1691+
win.rightButton.update(config.rightButton);
1692+
win.result = false;
1693+
win.onClose = function(){
1694+
var callback = config.callback;
1695+
if (callback) callback.apply(win, [win.result]);
1696+
};
1697+
win.show();
1698+
return win;
14741699
}
14751700

14761701
};

0 commit comments

Comments
 (0)