uglipop.min.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*|--uglipop.js--|
  2. |--(A Minimalistic Pure JavaScript Modal )--|
  3. |--Author : flouthoc (gunnerar7@gmail.com)(https://github.com/flouthoc)--|
  4. |--Contributers : Add Your Name Below--|
  5. |-- zhuharev (kirill at zhuharev.ru)(https://github.com/zhuharev)--|
  6. |--Nicolas Dietrich (https://github.com/nidico)--|*/
  7. (function(w, doc) {
  8. "use strict";
  9. var initted = false
  10. function on(el, eventName, handler) {
  11. if (el.addEventListener) {
  12. el.addEventListener(eventName, handler);
  13. } else {
  14. el.attachEvent('on' + eventName, function() {
  15. handler.call(el);
  16. });
  17. }
  18. }
  19. function init() {
  20. if (initted) return;
  21. initted = true
  22. var overlay = doc.createElement('div');
  23. var content_fixed = doc.createElement('div');
  24. var popbox = doc.createElement('div');
  25. var overlay_wrapper = doc.createElement('div');
  26. content_fixed.id = 'uglipop_content_fixed';
  27. content_fixed.setAttribute('style', 'position:fixed;top: 50%;left: 50%;transform: translate(-50%, -50%);-webkit-transform: translate(-50%, -50%);-ms-transform: translate(-50%, -50%);opacity:1;');
  28. popbox.id = 'uglipop_popbox';
  29. overlay_wrapper.id = "uglipop_overlay_wrapper";
  30. overlay_wrapper.setAttribute('style', 'position:absolute;top:0;bottom:0;left:0;right:0;');
  31. overlay.id = "uglipop_overlay";
  32. overlay.setAttribute('style', 'position:fixed;top:0;bottom:0;left:0;right:0;opacity:0.3;width:100%;height:100%;background-color:black;');
  33. overlay_wrapper.appendChild(overlay);
  34. content_fixed.appendChild(popbox);
  35. doc.body.appendChild(overlay_wrapper);
  36. doc.body.appendChild(content_fixed);
  37. doc.getElementById('uglipop_overlay_wrapper').style.display = 'none';
  38. doc.getElementById('uglipop_overlay').style.display = 'none';
  39. doc.getElementById('uglipop_content_fixed').style.display = 'none';
  40. //overlay_wrapper.addEventListener('click', remove);
  41. on(w, 'keydown', function(e) {
  42. //kill pop if button is ESC ;)
  43. if (e.keyCode == 27) {
  44. remove();
  45. }
  46. });
  47. //create global variables
  48. w.uglipop = uglipop
  49. w.removeuglipop = remove
  50. }
  51. function uglipop(config) {
  52. if (config) {
  53. if (typeof config.class == 'string' && config.class) {
  54. doc.getElementById('uglipop_popbox').setAttribute('class', config.class);
  55. }
  56. if (config.keepLayout && (!config.class)) {
  57. doc.getElementById('uglipop_popbox').setAttribute('style', 'position:relative;height:300px;width:300px;background-color:white;opacity:1;');
  58. }
  59. if (typeof config.content == 'string' && config.content && config.source == 'html') {
  60. doc.getElementById('uglipop_popbox').innerHTML = config.content;
  61. }
  62. if (typeof config.content == 'string' && config.content && config.source == 'div') {
  63. doc.getElementById('uglipop_popbox').innerHTML = doc.getElementById(config.content).innerHTML;
  64. }
  65. }
  66. doc.getElementById('uglipop_overlay_wrapper').style.display = '';
  67. doc.getElementById('uglipop_overlay').style.display = '';
  68. doc.getElementById('uglipop_content_fixed').style.display = '';
  69. }
  70. function remove() {
  71. doc.getElementById('uglipop_overlay_wrapper').style.display = 'none';
  72. doc.getElementById('uglipop_overlay').style.display = 'none';
  73. doc.getElementById('uglipop_content_fixed').style.display = 'none';
  74. }
  75. //init on window loaded
  76. on(doc, "DOMContentLoaded", init)
  77. on(doc, "load", init)
  78. })(window, document)