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