Advertisement
renix1

modal

May 5th, 2020
959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ensureElement (node, tag) {
  2.     const el = document.createElement(tag);
  3.     node.appendChild(el);
  4.     return el;
  5. }
  6.  
  7. function ensureElementWithAttr(node, tag, attrs) {
  8.     const el = ensureElement(node, tag);
  9.     for (var attr in attrs) {
  10.         el.setAttribute(attr, attrs[attr]);
  11.     }
  12.     return el;
  13. }
  14.  
  15. function buildModalFooter(modal_content, config) {
  16.     var modal_footer = ensureElementWithAttr(modal_content, 'div', {class: "modal-footer"});
  17.     for (var button in config.buttons) {
  18.         var btn = config.buttons[button];
  19.         ensureElementWithAttr(modal_footer, 'button', btn.config).innerText = btn.text;
  20.     }
  21.  
  22.     return modal_footer;
  23. }
  24.  
  25. function buildModal (config) {
  26.     const modals = document.querySelector('.modals');
  27.     var modal = ensureElementWithAttr(modals, 'div', {class: "modal", role: "dialog", tabindex: "-1", id: config.id});
  28.     var modal_dialog = ensureElementWithAttr(modal, 'div', {class: "modal-dialog", role: "document"});
  29.     var modal_content = ensureElementWithAttr(modal_dialog, 'div', {class: "modal-content"});
  30.     var modal_header = ensureElementWithAttr(modal_content, 'div', {class: "modal-header"});
  31.     ensureElementWithAttr(modal_header, 'h5', {class: "modal-title"}).innerText = config.title;
  32.     ensureElementWithAttr(modal_content, 'div', {class: "modal-body"}).innerHTML = config.body;
  33.  
  34.     buildModalFooter(modal_content, config);
  35. }
  36.  
  37. function loadModal(config) {
  38.     if (!config) {
  39.         config = {}
  40.     }
  41.  
  42.     if (!config.message) {
  43.         config.message = "Houve um erro em nossos computadores";
  44.     }
  45.  
  46.     if (!config.title) {
  47.         config.title = "Aviso";
  48.     }
  49.  
  50.     if (!config.body) {
  51.         config.body = config.message;
  52.     }
  53.  
  54.     if (!config.id) {
  55.         config.id = config.title.toCamelCase().replace(" ", '');
  56.     }
  57.  
  58.     if (!config.buttons) {
  59.         config.buttons = [
  60.             {
  61.                 "text": "Fechar",
  62.                 "config": {
  63.                     "class": "btn btn-primary",
  64.                     "data-dismiss": "modal"
  65.                 }
  66.             }
  67.         ]
  68.     }
  69.  
  70.     if (config.id) {
  71.         var modal = document.querySelector(`.modal[id="${config.id}"]`);
  72.         if (modal) {
  73.             var title = modal.querySelector('[class="modal-title"]').innerText = config.title;
  74.             var body = modal.querySelector('[class="modal-body"]').innerHTML = config.body;
  75.         } else {
  76.             buildModal(config);
  77.         }
  78.     }
  79.  
  80.     $(`.modal[id="${config.id}"]`).modal('show');
  81. }
  82.  
  83. function loadWarningModal (config) {
  84.     if (!config.id) {
  85.         config.id = "aviso";
  86.     }
  87.  
  88.     loadModal(config);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement