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 const searchconf = JSON.parse($agg.attr('data-searchconf')); 83 const withpid = searchconf['withpid']; 84 const isPageEditor = JSINFO.plugins.struct.isPageEditor; 85 86 if (withpid && !isPageEditor) return; 87 88 $form = jQuery('<form></form>'); 89 $form.html(data); 90 jQuery('<input>').attr({ 91 type: 'hidden', 92 name: 'searchconf', 93 value: $agg.attr('data-searchconf') 94 }).appendTo($form); // add the search config to the form 95 96 // if page id needs to be passed to backend, add pid 97 if (withpid) { 98 jQuery('<input>').attr({ 99 type: 'hidden', 100 name: 'pid', 101 value: JSINFO.id 102 }).appendTo($form); // add the page id to the form 103 } 104 $agg.append($form); 105 EntryEditor($form); 106 107 var $errors = $form.find('div.err').hide(); 108 109 $form.submit(function (e) { 110 e.preventDefault(); 111 $errors.hide(); 112 113 jQuery.post( 114 DOKU_BASE + 'lib/exe/ajax.php', 115 $form.serialize() 116 ) 117 .done(function (data) { 118 var $tbody = $table.find('tbody'); 119 if (!$tbody.length) { 120 $tbody = jQuery('<tbody>').appendTo($table); 121 } 122 $tbody.append(data); 123 addDeleteRowButtons(); // add the delete button to the new row 124 addForm(formdata); // reset the whole form 125 }) 126 .fail(function (xhr) { 127 $errors.text(xhr.responseText).show(); 128 }) 129 }); 130 } 131 132 /** 133 * Main 134 * 135 * Initializes the editor if the AJAX backend returns an editor, 136 * otherwise some (ACL) check did not check out and no editing 137 * capabilites are added. 138 */ 139 jQuery.post( 140 DOKU_BASE + 'lib/exe/ajax.php', 141 { 142 call: 'plugin_struct_aggregationeditor_new', 143 schema: schema 144 }, 145 function (data) { 146 if (!data) return; 147 formdata = data; 148 addDeleteRowButtons(); 149 addForm(data); 150 } 151 ); 152 153 154}; 155