Make plugin handle all types of buttons

This commit is contained in:
Fedor A. Fetisov 2021-03-18 03:34:07 +03:00
parent d0f5b645bf
commit 9a4dd4e8f5

View File

@ -1,12 +1,28 @@
(function(Mibew) {
Mibew.APIFunctions.refreshButton = function(data) {
// Refresh the button image (if exists)
var button_object;
var button = document.getElementById("mibew-agent-button");
if (!button) { return; }
var img = button.getElementsByTagName("img")[0];
if (!img) { return; }
var originalSrc = img.src.replace(/&dummy=\d+/, '');
img.src = originalSrc + "&dummy=" + (new Date()).getTime();
if (!button) {
// The button is operator code field
button_object = document.getElementById("mibew-operator-code-field");
}
else {
button_object = button.getElementsByTagName("img")[0];
if (!button_object) {
// The button is text link
button_object = button;
}
else {
// The button is image, refresh it
var originalSrc = button_object.src.replace(/&dummy=\d+/, '');
button_object.src = originalSrc + "&dummy=" + (new Date()).getTime();
}
}
// Unable to find button of any type - nothing to do
if (!button_object) { return; }
// Hide the button if all popups are open or make it visible otherwise
var visible = false;
@ -18,20 +34,20 @@
// Check whether we actually need to hide the button
if (data.refreshButton.mode != 'none') {
if (data.refreshButton.mode == 'visibility') {
img.style.visibility = visible ? 'visible' : 'hidden';
button_object.style.visibility = visible ? 'visible' : 'hidden';
}
else if (data.refreshButton.mode == 'display') {
img.style.display = visible ? data.refreshButton.submode : 'none';
button_object.style.display = visible ? data.refreshButton.submode : 'none';
}
}
// Set appropriate class for the button depending on its alleged visibility
img.className = img.className.replace(/ mibew_(visible|hidden)/, '');
button_object.className = button_object.className.replace(/ mibew_(visible|hidden)/, '');
if (visible) {
img.className = img.className.concat(' mibew_visible');
button_object.className = button_object.className.concat(' mibew_visible');
}
else {
img.className = img.className.concat(' mibew_hidden');
button_object.className = button_object.className.concat(' mibew_hidden');
}
}
})(Mibew);