|
335 | 335 | }
|
336 | 336 | },
|
337 | 337 |
|
| 338 | + /** |
| 339 | + * Finalizes a process of widget creation. This includes: |
| 340 | + * |
| 341 | + * * inserting widget element into editor, |
| 342 | + * * marking widget instance as ready (see {@link CKEDITOR.plugins.widget#event-ready}), |
| 343 | + * * focusing widget instance. |
| 344 | + * |
| 345 | + * This method is used by the default widget's command and is called |
| 346 | + * after widget's dialog (if set) is closed. It may also be used in a |
| 347 | + * customized process of widget creation and insertion. |
| 348 | + * |
| 349 | + * widget.once( 'edit', function() { |
| 350 | + * // Finalize creation only of not ready widgets. |
| 351 | + * if ( widget.isReady() ) |
| 352 | + * return; |
| 353 | + * |
| 354 | + * // Cancel edit event to prevent automatic widget insertion. |
| 355 | + * evt.cancel(); |
| 356 | + * |
| 357 | + * CustomDialog.open( widget.data, function saveCallback( savedData ) { |
| 358 | + * // Cache the container, because widget may be destroyed while saving data, |
| 359 | + * // if this process will require some deep transformations. |
| 360 | + * var container = widget.wrapper.getParent(); |
| 361 | + * |
| 362 | + * widget.setData( savedData ); |
| 363 | + * |
| 364 | + * // Widget will be retrieved from container and inserted into editor. |
| 365 | + * editor.widgets.finalizeCreation( container ); |
| 366 | + * } ); |
| 367 | + * } ); |
| 368 | + * |
| 369 | + * @param {CKEDITOR.dom.element/CKEDITOR.dom.documentFragment} container The element |
| 370 | + * or document fragment which contains widget wrapper. The container is used, so before |
| 371 | + * finalizing creation the widget can be freely transformed (even destroyed and reinitialized). |
| 372 | + */ |
| 373 | + finalizeCreation: function( container ) { |
| 374 | + var wrapper = container.getFirst(); |
| 375 | + if ( wrapper && isWidgetWrapper2( wrapper ) ) { |
| 376 | + this.editor.insertElement( wrapper ); |
| 377 | + |
| 378 | + var widget = this.getByElement( wrapper ); |
| 379 | + // Fire postponed #ready event. |
| 380 | + widget.ready = true; |
| 381 | + widget.fire( 'ready' ); |
| 382 | + widget.focus(); |
| 383 | + } |
| 384 | + }, |
| 385 | + |
338 | 386 | /**
|
339 | 387 | * Finds a widget instance which contains a given element. The element will be the {@link CKEDITOR.plugins.widget#wrapper wrapper}
|
340 | 388 | * of the returned widget or a descendant of this {@link CKEDITOR.plugins.widget#wrapper wrapper}.
|
|
1103 | 1151 | */
|
1104 | 1152 |
|
1105 | 1153 | /**
|
1106 |
| - * An event fired by the {@link #method-edit} method. It can be cancelled |
1107 |
| - * in order to stop the default action (opening a dialog window). |
| 1154 | + * An event fired by the {@link #method-edit} method. It can be canceled |
| 1155 | + * in order to stop the default action (opening a dialog window and/or |
| 1156 | + * {@link CKEDITOR.plugins.widget.repository#finalizeCreation finalizing widget creation}). |
1108 | 1157 | *
|
1109 | 1158 | * @event edit
|
1110 | 1159 | * @param data
|
|
1291 | 1340 | element = CKEDITOR.dom.element.createFromHtml( widgetDef.template.output( defaults ) ),
|
1292 | 1341 | instance,
|
1293 | 1342 | wrapper = editor.widgets.wrapElement( element, widgetDef.name ),
|
1294 |
| - temp = new CKEDITOR.dom.documentFragment( wrapper.getDocument() ), |
1295 |
| - editWasCanceled = true; |
| 1343 | + temp = new CKEDITOR.dom.documentFragment( wrapper.getDocument() ); |
1296 | 1344 |
|
1297 | 1345 | // Append wrapper to a temporary document. This will unify the environment
|
1298 | 1346 | // in which #data listeners work when creating and editing widget.
|
|
1313 | 1361 | // temporary instance.
|
1314 | 1362 | // * If dialog wasn't set and edit wasn't canceled, insert widget.
|
1315 | 1363 | var editListener = instance.once( 'edit', function( evt ) {
|
1316 |
| - editWasCanceled = false; |
1317 |
| - |
1318 | 1364 | if ( evt.data.dialog ) {
|
1319 | 1365 | instance.once( 'dialog', function( evt ) {
|
1320 | 1366 | var dialog = evt.data,
|
|
1344 | 1390 | // Remove listener in case someone canceled it before this
|
1345 | 1391 | // listener was executed.
|
1346 | 1392 | editListener.removeListener();
|
1347 |
| - |
1348 |
| - // In case edit was canceled - finalize creation here which should happen anyway (just without |
1349 |
| - // initial edit). |
1350 |
| - if ( editWasCanceled ) |
1351 |
| - finalizeCreation(); |
1352 | 1393 | }
|
1353 | 1394 |
|
1354 | 1395 | function finalizeCreation() {
|
1355 |
| - var wrapper = temp.getFirst(); |
1356 |
| - if ( wrapper && isWidgetWrapper2( wrapper ) ) { |
1357 |
| - editor.insertElement( wrapper ); |
1358 |
| - |
1359 |
| - var widget = editor.widgets.getByElement( wrapper ); |
1360 |
| - // Fire postponed #ready event. |
1361 |
| - widget.ready = true; |
1362 |
| - widget.fire( 'ready' ); |
1363 |
| - widget.focus(); |
1364 |
| - } |
| 1396 | + editor.widgets.finalizeCreation( temp ); |
1365 | 1397 | }
|
1366 | 1398 | },
|
1367 | 1399 |
|
|
0 commit comments