1/** 2 * Aggregation table editor 3 */ 4const AggregationEditor = function (idx, table) { 5 const $table = jQuery(table); 6 let $form = null; 7 let formdata; 8 9 const schema = $table.parents('.structaggregation').data('schema'); 10 if (!schema) return; 11 12 /** 13 * Adds delete row buttons to each row 14 */ 15 function addDeleteRowButtons() { 16 const disableDeleteSerial = JSINFO.plugins.struct.disableDeleteSerial; 17 18 $table.find('tr').each(function () { 19 const $me = jQuery(this); 20 21 // already added here? 22 if ($me.find('th.action, td.action').length) { 23 return; 24 } 25 26 const rid = $me.data('rid'); 27 const pid = $me.data('pid'); 28 let isDisabled = ''; 29 30 // empty header cells 31 if (!rid) { 32 $me.append('<th class="action">' + LANG.plugins.struct.actions + '</th>'); 33 return; 34 } 35 36 // delete buttons for rows 37 const $td = jQuery('<td class="action"></td>'); 38 if (rid === '') return; // skip button addition for page data 39 // disable button for serial data if so configured 40 if (rid && pid && disableDeleteSerial) { 41 isDisabled = ' disabled'; 42 } 43 44 const $btn = jQuery('<button' + isDisabled + '><i class="ui-icon ui-icon-trash"></i></button>') 45 .addClass('delete') 46 .attr('title', LANG.plugins.struct.lookup_delete) 47 .click(function (e) { 48 e.preventDefault(); 49 if (!window.confirm(LANG.del_confirm)) return; 50 51 jQuery.post( 52 DOKU_BASE + 'lib/exe/ajax.php', 53 { 54 call: 'plugin_struct_aggregationeditor_delete', 55 schema: schema, 56 rid: rid, 57 sectok: $me.parents('.structaggregationeditor').find('.struct_entry_form input[name=sectok]').val() 58 } 59 ) 60 .done(function () { 61 $me.remove(); 62 }) 63 .fail(function (xhr) { 64 alert(xhr.responseText) 65 }) 66 }); 67 68 $td.append($btn); 69 $me.append($td); 70 71 }); 72 } 73 74 /** 75 * Initializes the form for the editor and attaches handlers 76 * 77 * @param {string} data The HTML for the form 78 */ 79 function addForm(data) { 80 if ($form) $form.remove(); 81 var $agg = $table.parents('.structaggregation'); 82 83 $form = jQuery('<form></form>'); 84 $form.html(data); 85 jQuery('<input>').attr({ 86 type: 'hidden', 87 name: 'searchconf', 88 value: $agg.attr('data-searchconf') 89 }).appendTo($form); // add the search config to the form 90 91 // if page id needs to be passed to backend, add pid 92 const searchconf = JSON.parse($agg.attr('data-searchconf')); 93 if (searchconf['withpid']) { 94 jQuery('<input>').attr({ 95 type: 'hidden', 96 name: 'pid', 97 value: JSINFO.id 98 }).appendTo($form); // add the page id to the form 99 } 100 $agg.append($form); 101 EntryEditor($form); 102 103 var $errors = $form.find('div.err').hide(); 104 105 $form.submit(function (e) { 106 e.preventDefault(); 107 $errors.hide(); 108 109 jQuery.post( 110 DOKU_BASE + 'lib/exe/ajax.php', 111 $form.serialize() 112 ) 113 .done(function (data) { 114 var $tbody = $table.find('tbody'); 115 if (!$tbody.length) { 116 $tbody = jQuery('<tbody>').appendTo($table); 117 } 118 $tbody.append(data); 119 addDeleteRowButtons(); // add the delete button to the new row 120 addForm(formdata); // reset the whole form 121 }) 122 .fail(function (xhr) { 123 $errors.text(xhr.responseText).show(); 124 }) 125 }); 126 127 // focus first input 128 $form.find('input, textarea').first().focus(); 129 } 130 131 /** 132 * Main 133 * 134 * Initializes the editor if the AJAX backend returns an editor, 135 * otherwise some (ACL) check did not check out and no editing 136 * capabilites are added. 137 */ 138 jQuery.post( 139 DOKU_BASE + 'lib/exe/ajax.php', 140 { 141 call: 'plugin_struct_aggregationeditor_new', 142 schema: schema 143 }, 144 function (data) { 145 if (!data) return; 146 formdata = data; 147 addDeleteRowButtons(); 148 addForm(data); 149 } 150 ); 151 152 153}; 154