antoniop
committed
on 29 Oct 24
Apply logics to delete the editor also when the field is the last
openacs-4/.../www/resources/repeat.js (+23 -16)
200 200             // Replace the ID in the name component inside the json
201 201             // structure.
202 202             //
203 203             var data = JSON.parse(data_repeat_attribute);
204 204
205 205             var compareLength = from.length;
206 206             if (data.name.substring(0,compareLength) == from) {
207 207                 var new_name = to + data.name.substring(compareLength,data.name.length);
208 208                 data.name = new_name;
209 209                 e.setAttribute('data-repeat',JSON.stringify(data));
210 210             }
211 211         }
212 212         for (var j = 0; j < items.length; j++) {
213 213             this.renameItem(top, items[j], from, to);
214 214         }
215 215     } else {
216 216         console.log('rename ignores ' + e);
217 217     }
218 218 }
219 219
  220 if (typeof window.tinyMCE !== 'undefined') {
  221     //
  222     // TinyMCE logics for editor deletion: for all fields we are going
  223     // to delete/move, persist their value in the form field and
  224     // remove the richtext editor from them. Will be added again by
  225     // the logics in render_input.
  226     //
  227     xowiki.tinyMCE = {};
  228     xowiki.tinyMCE.removeEditor = function (selector) {
  229         for (const el of document.querySelectorAll(selector)) {
  230             const editor = window.tinyMCE.get(el.id);
  231             // console.log(el, editor);
  232             if (!editor) {continue;}
  233             el.innerHTML = editor.getContent();
  234             editor.remove();
  235         }
  236     };
  237 }
  238
220 239 /*
221 240  * delItem
222 241  *
223 242  * Delete the current item. Actually, this implementation overwrites the
224 243  * current item with the template item, moves it to the end and renames
225 244  * the fields.
226 245  */
227 246 xowiki.repeat.delItem = function(e, json) {
228 247
229 248     var data = JSON.parse(json);
230 249     var item = e.parentNode;
231 250     var statsthis.itemStats(item);
232 251     //console.info(item);
233 252     console.info(stats);
234 253     //console.info(data);
235 254
236 255     var current = stats['current'];
237 256     var last    = stats['visible'];
238 257     var items   = item.parentNode.children;
239 258     var divs    = stats['divs'];
240 259     //console.info(divs);
241 260     var display = 'none';
242 261
243 262     if (stats['visible'] < data['min']+1) {
244 263         // we have reached the minimum
245 264         // so we simulate that the current item is the last one -> it is reset by the template
246 265         // the only difference is that we shouldn't hide it
247 266         last = current;
248 267         display = 'block';
249 268     }
250 269
251 270     console.log('delete ' + current);
252 271
253 272     if (current == last) {
254 273         //console.log('delete the last item');
  274         xowiki.tinyMCE?.removeEditor(`[id^='${item.parentNode.id}.${current}']`);
255 275     } else {
256 276         for (var j = current; j < last; j++) {
257 277             var k = j + 1;
258 278
259 279             //console.log('work on ' + j + ': ' + divs[j].innerHTML);
260 280
261 281             // before moving, we are storing the input values --> so that the values are being moved
262 282             // normal input fields
263 283             $(divs[k]).find(':input[type=text]').each(function() {
264 284                 $(this).attr('value',$(this).val());
265 285             });
266 286
267 287             // radio and checkbox input fields
268 288             // THIS DOES NOT SEEM TO WORK
269 289             $(divs[k]).find(':input[type=radio]').each(function() {
270 290                 //console.info($(this));
271 291                 if($(this).prop('checked')) {
272 292                     $(this).attr('checked', 'checked');
273 293                 }
274 294             });
 
296 316             // additionally we have to set the content of the ckeditor in the textarea
297 317             if (typeof CKEDITOR != "undefined") {
298 318                 // we are selecting all ckeditor instances which are at the same level and below of the current item
299 319                 for (var l in CKEDITOR.instances) {
300 320                     // console.log('instance name: ' + CKEDITOR.instances[l].name);
301 321                     var searchString = item.parentNode.id + k;
302 322                     // the instance names of the ckeditor are without '. : -' --> see also initialize of ckeditor
303 323                     searchString = searchString.replace(/[.:-]/g,'');
304 324
305 325                     // console.log('searchString: '+searchString);
306 326                     if (CKEDITOR.instances[l].name.search(searchString) == 0) {
307 327                         // console.log('data to copy: '+CKEDITOR.instances[l].getData());
308 328                         // should update the textarea but it doesn't -> so we have to do that manually
309 329                         CKEDITOR.instances[l].updateElement();
310 330                         document.getElementById(CKEDITOR.instances[l].name).innerHTML=CKEDITOR.instances[l].getData();
311 331
312 332                         CKEDITOR.instances[l].destroy(true);
313 333                     }
314 334                 }
315 335             }
316               //
317               // TinyMCE logics, similar to those for CKEditor above:
318               // for all fields we are going to delete/move, persist
319               // their value in the form field and remove the richtext
320               // editor from them. Will be added again by the logics in
321               // render_input.
322               //
323               if (typeof window.tinyMCE != "undefined") {
324                   for (const el of document.querySelectorAll(`[id^='${oldid}'], [id^='${newid}']`)) {
325                       const editor = window.tinyMCE.get(el.id);
326                       // console.log(el.id, editor);
327                       if (!editor) {continue;}
328                       el.innerHTML = editor.getContent();
329                       editor.remove();
330                   }
331               }
  336
  337             xowiki.tinyMCE?.removeEditor(`[id^='${oldid}'], [id^='${newid}']`);
  338
332 339             //console.log(j + ' becomes ' + k + ': ' + divs[k].innerHTML);
333 340             divs[j].innerHTML = divs[k].innerHTML;
334 341
335 342
336 343             // due to the fact that the ckeditor are using the ids for reloading
337 344             // we have to recycle them (and for the other cases it doesn't hurt)
338 345             this.renameIds(divs[j], oldid, newid);
339 346             //console.log("RENAME INNER");
340 347             this.renameItem(divs[j], divs[j],
341 348                             data['name'] + '.' + (k), data['name'] + '.' + (j));
342 349
343 350             this.registerAddDeleteAction(divs[j]);
344 351         }
345 352     };
346 353
347 354     // Make sure add item link is visible
348 355     $(item.parentNode).children(".repeat-add-link").show();
349 356
350 357     // Delete the node until we are reaching the minimum
351 358     if (stats['visible'] > data['min']) {