|
| 1 | +ActiveAdmin.dialogMassFieldsUpdate = function(message, inputs, callback){ |
| 2 | + let html = `<form id="dialog_confirm" title="${message}"><div stype="padding-right:4px;padding-left:1px;margin-right:2px"><ul>`; |
| 3 | + for (let name in inputs) { |
| 4 | + var elem, opts, wrapper; |
| 5 | + let type = inputs[name]; |
| 6 | + if (/^(datepicker|checkbox|text)$/.test(type)) { |
| 7 | + wrapper = 'input'; |
| 8 | + } else if ($.isArray(type)) { |
| 9 | + [wrapper, elem, opts, type] = Array.from(['select', 'option', type, '']); |
| 10 | + } else { |
| 11 | + throw new Error(`Unsupported input type: {${name}: ${type}}`); |
| 12 | + } |
| 13 | + |
| 14 | + let klass = type === 'datepicker' ? type : ''; |
| 15 | + html += `<li> |
| 16 | +<input type='checkbox' class='mass_update_protect_fild_flag' value='Y' id="mass_update_dialog_${name}" /> |
| 17 | +<label for="mass_update_dialog_${name}"> ${name.charAt(0).toUpperCase() + name.slice(1)}</label> |
| 18 | +<${wrapper} name="${name}" class="${klass}" type="${type}" disabled="disabled">` + |
| 19 | + (opts ? ((() => { |
| 20 | + const result = []; |
| 21 | + |
| 22 | + for (let v of Array.from(opts)) { |
| 23 | + const $elem = $(`<${elem}/>`); |
| 24 | + if ($.isArray(v)) { |
| 25 | + $elem.text(v[0]).val(v[1]); |
| 26 | + } else { |
| 27 | + $elem.text(v); |
| 28 | + } |
| 29 | + result.push($elem.wrap('<div>').parent().html()); |
| 30 | + } |
| 31 | + |
| 32 | + return result; |
| 33 | + })()).join('') : ''); |
| 34 | + if (wrapper === 'select') { |
| 35 | + html += `</${wrapper}>`; |
| 36 | + } |
| 37 | + html += "</li>"; |
| 38 | + |
| 39 | + [wrapper, elem, opts, type, klass] = Array.from([]); |
| 40 | + } // unset any temporary variables |
| 41 | + |
| 42 | + html += "</ul></div></form>"; |
| 43 | + |
| 44 | + const form = $(html).appendTo('body'); |
| 45 | + |
| 46 | + $('body').trigger('mass_update_modal_dialog:before_open', [form]); |
| 47 | + |
| 48 | + return form.dialog({ |
| 49 | + modal: true, |
| 50 | + dialogClass: 'active_admin_dialog active_admin_dialog_mass_update_by_filter', |
| 51 | + maxHeight: window.innerHeight - (window.innerHeight * 0.1), |
| 52 | + open() { |
| 53 | + $('body').trigger('mass_update_modal_dialog:after_open', [form]); |
| 54 | + return $('.mass_update_protect_fild_flag').on('change', function(e) { |
| 55 | + if (this.checked) { |
| 56 | + return $(e.target).next().next().removeAttr('disabled').trigger("chosen:updated"); |
| 57 | + } else { |
| 58 | + return $(e.target).next().next().attr('disabled', 'disabled').trigger("chosen:updated"); |
| 59 | + } |
| 60 | + }); |
| 61 | + }, |
| 62 | + buttons: { |
| 63 | + OK(e){ |
| 64 | + $(e.target).closest('.ui-dialog-buttonset').html('<span>Processing. Please wait...</span>'); |
| 65 | + return callback($(this).serializeObject()); |
| 66 | + }, |
| 67 | + Cancel() { |
| 68 | + $('.mass_update_protect_fild_flag').off('change'); |
| 69 | + return $(this).dialog('close').remove(); |
| 70 | + } |
| 71 | + } |
| 72 | + }); |
| 73 | +}; |
0 commit comments