Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/public/js/modal.js

115 lignes
4,3 Kio
JavaScript
Brut Vue normale Historique

2017-06-04 07:43:41 +02:00
// @source https://github.com/NyaaPantsu/nyaa/tree/dev/public/js
// @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt Expat
// Get the modal
var Modal = {
2017-07-16 06:43:09 +02:00
active: 0,
// Initialise a modal or multiple ones
// takes as parameter an object params:
// @param params Object{ elements: NodeList|Node, button: ID(eg. #something)|Class(eg. .something), before: callback, after: callback, close: callback }
2017-07-16 06:43:09 +02:00
Init: function (params) {
var elements = params.elements
var button = (params.button != undefined) ? params.button : false
if (elements.innerHTML == undefined) {
2017-07-16 06:43:09 +02:00
var nbEl = elements.length
2017-07-23 04:50:36 +02:00
for (var i = 0; i < nbEl; i++) {
2017-07-16 06:43:09 +02:00
var modal = elements[i];
this.addModal(modal, button, i, params.before, params.after, params.close)
}
} else {
this.addModal(modal, button, i, params.before, params.after, params.close)
}
2017-07-16 06:43:09 +02:00
},
// addModal prepare a modal (called by Init so you don't have use it)
// @param modal Node
// @param btn ID(eg. #something)|Class(eg. .something)
// @param i If multiple btn, points out to which btn in the array apply the event
// @param before_callback callback called before opening a modal
// @param after_callback callback called after opening a modal
// @param close_callback callback called after closing a modal
2017-07-23 04:50:36 +02:00
addModal: function (modal, btn, i, before_callback, after_callback, close_callback) {
2017-07-16 06:43:09 +02:00
var isBtnArray = false;
// Get the button that opens the modal
if (!btn) {
2017-07-23 04:50:36 +02:00
btn = document.getElementById("modal_btn_" + modal.id)
} else if (typeof(btn) == "string" && btn.match(/^#/)) {
2017-07-16 06:43:09 +02:00
btn = document.getElementById(btn.substr(1));
} else if (typeof(btn) == "string" && btn.match(/^\./)) {
2017-07-16 06:43:09 +02:00
btn = document.getElementsByClassName(btn.substr(1));
isBtnArray = true;
} else if (btn instanceof Array) {
btn = btn.map(function(val, index) {
if (val.match(/^#/)) {
return document.getElementById(val.substr(1));
} else if (val.match(/^\./)) {
return document.getElementsByClassName(val.substr(1))[index];
}
return document.querySelector(val)
})
isBtnArray = true;
2017-07-16 06:43:09 +02:00
} else {
console.error("Couldn't find the button, please provide either a #id, a .classname or an array of #id")
2017-07-16 06:43:09 +02:00
return
}
if ((isBtnArray) && (i > 0) && (btn.length > 0) && (btn.length > i)) {
2017-07-23 04:50:36 +02:00
btn[i].addEventListener("click", function (e) {
2017-07-16 06:43:09 +02:00
if (before_callback != undefined) before_callback()
modal.style.display = "block";
Modal.active = modal;
if (after_callback != undefined) after_callback()
e.preventDefault();
});
} else {
btn = (isBtnArray) ? btn[0] : btn;
// When the user clicks on the button, open the modal
2017-07-23 04:50:36 +02:00
btn.addEventListener("click", function (e) {
2017-07-16 06:43:09 +02:00
if (before_callback != undefined) before_callback()
modal.style.display = "block";
Modal.active = modal;
if (after_callback != undefined) after_callback()
e.preventDefault();
});
}
// Get the <span> element that closes the modal
2017-07-23 04:50:36 +02:00
var span = document.querySelectorAll("#" + modal.id + " .close")[0]
2017-07-16 06:43:09 +02:00
// When the user clicks on <span> (x), close the modal
2017-07-23 04:50:36 +02:00
span.addEventListener("click", function (e) {
2017-07-16 06:43:09 +02:00
modal.style.display = "none";
Modal.active = 0;
if (close_callback != undefined) close_callback()
e.preventDefault();
});
// When the user clicks anywhere outside of the modal, close it
2017-07-23 04:50:36 +02:00
window.addEventListener("click", function (event) {
2017-07-16 06:43:09 +02:00
if (event.target == modal) {
modal.style.display = "none";
Modal.active = 0;
if (close_callback != undefined) close_callback()
}
});
},
// CloseActive closes the opened modal, if any
2017-07-23 04:50:36 +02:00
CloseActive: function () {
2017-07-16 06:43:09 +02:00
if (this.active != 0) {
2017-07-23 04:50:36 +02:00
this.active.style.display = "none";
2017-07-16 06:43:09 +02:00
this.active = 0;
}
},
// GetActive return the opened modal div
2017-07-23 04:50:36 +02:00
GetActive: function () {
2017-07-16 06:43:09 +02:00
return this.active;
},
// Open opens a modal and closes the active one if any
2017-07-23 04:50:36 +02:00
Open: function (q) {
var activeModal = this.GetActive()
if (activeModal != 0) {
this.CloseActive()
}
2017-07-16 06:43:09 +02:00
var modal = document.querySelector(q);
if (modal != undefined) {
2017-07-23 04:50:36 +02:00
modal.style.display = "none";
2017-07-16 06:43:09 +02:00
this.active = modal;
}
}
2017-06-04 07:43:41 +02:00
};
// @license-end