Sha256: 184cacb94b7c36fa139540ed60a7f734fe259e99daff23c1dff12614b4eb832a

Contents?: true

Size: 1.73 KB

Versions: 22

Compression:

Stored size: 1.73 KB

Contents

function ModalDialog(message, inputs, callback){
  let html = `<form id="dialog_confirm" title="${message}"><ul>`;
  for (let name in inputs) {
    var opts, wrapper;
    let type = inputs[name];
    if (/^(datepicker|checkbox|text|number)$/.test(type)) {
      wrapper = 'input';
    } else if (type === 'textarea') {
      wrapper = 'textarea';
    } else if ($.isArray(type)) {
      [wrapper, opts, type] = ['select', type, ''];
    } else {
      throw new Error(`Unsupported input type: {${name}: ${type}}`);
    }

    let klass = type === 'datepicker' ? type : '';
    html += `<li>
      <label>${name.charAt(0).toUpperCase() + name.slice(1)}</label>
      <${wrapper} name="${name}" class="${klass}" type="${type}">` +
        (opts ? ((() => {
          const result = [];

          opts.forEach(v => {
            const $elem = $('<option></option>');
            if ($.isArray(v)) {
              $elem.text(v[0]).val(v[1]);
            } else {
              $elem.text(v);
            }
            result.push($elem.wrap('<div></div>').parent().html());
          });

          return result;
        })()).join('') : '') +
      `</${wrapper}>` +
    "</li>";
    [wrapper, opts, type, klass] = []; // unset any temporary variables
  }

  html += "</ul></form>";

  const form = $(html).appendTo('body');
  $('body').trigger('modal_dialog:before_open', [form]);

  form.dialog({
    modal: true,
    open(_event, _ui) {
      $('body').trigger('modal_dialog:after_open', [form]);
    },
    dialogClass: 'active_admin_dialog',
    buttons: {
      OK() {
        callback($(this).serializeObject());
        $(this).dialog('close');
      },
      Cancel() {
        $(this).dialog('close').remove();
      }
    }
  });
}

export default ModalDialog;

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
activeadmin-3.2.5 app/javascript/active_admin/lib/modal-dialog.js
activeadmin-3.2.4 app/javascript/active_admin/lib/modal-dialog.js
activeadmin-3.2.3 app/javascript/active_admin/lib/modal-dialog.js
activeadmin-3.2.2 app/javascript/active_admin/lib/modal-dialog.js
activeadmin-3.2.1 app/javascript/active_admin/lib/modal-dialog.js
activeadmin-3.2.0 app/javascript/active_admin/lib/modal-dialog.js
activeadmin-3.1.0 app/javascript/active_admin/lib/modal-dialog.js
activeadmin-3.0.0 app/javascript/active_admin/lib/modal-dialog.js
activeadmin-2.14.0 app/javascript/active_admin/lib/modal-dialog.js
activeadmin-2.13.1 app/javascript/active_admin/lib/modal-dialog.js
activeadmin-2.13.0 app/javascript/active_admin/lib/modal-dialog.js
activeadmin-2.12.0 app/javascript/active_admin/lib/modal-dialog.js
activeadmin-2.11.2 app/javascript/active_admin/lib/modal-dialog.js
activeadmin-2.11.1 app/javascript/active_admin/lib/modal-dialog.js
activeadmin-2.11.0 app/javascript/active_admin/lib/modal-dialog.js
activeadmin-2.10.1 app/javascript/active_admin/lib/modal-dialog.js
activeadmin-2.10.0 app/javascript/active_admin/lib/modal-dialog.js
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/activeadmin-2.9.0/app/javascript/active_admin/lib/modal-dialog.js
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/activeadmin-2.9.0/app/javascript/active_admin/lib/modal-dialog.js
activeadmin-2.9.0 app/javascript/active_admin/lib/modal-dialog.js