Opened 3 years ago

Last modified 3 years ago

#12374 confirmed New Feature

Widget dialogs - provide an easy way to access edited widget in onOk / onShow

Reported by: Wiktor Walc Owned by:
Priority: Normal Milestone:
Component: UI : Widgets Version: 4.3
Keywords: Cc:

Description (last modified by Wiktor Walc)

While working on a test widget I found it quite hard to get a reference to a widget instance for which a dialog is launched.

Take the code snippet dialog (dialogs/codesnippet.js) and add there:

onOk: function( evt ) {
	console.log(evt);
	console.log(this);
},
onShow: function( evt ) {
	console.log(evt);
	console.log(this);
},

See that there is no access to a widget object which is created on the fly by the widget system, in case of a new widget. As a result the usage of onOk / onShow as a replacement for commit/setup functions in each UI element is quite troublesome.

There is a workaround for it:

onShow

onShow: function() {
	var widget = editor.widgets.focused;
}

onOk

  1. We need to add event handler in an init method executed while initializing a widget:
editor.widgets.add( 'xyz', {
	// ...
	init : function() {
		// ...
		// Pass the reference to this widget to the dialog. See "onOk" in the dialog definition, we needed widget there.
		this.on( 'dialog', function( evt ) {
			evt.data.widget = this;
		}, this );
	},
	// ...
}
  1. Then this will work:
onOk: function() {
	var widget = this.widget;
}

In ideal world this could be simpler though.

Change History (3)

comment:1 Changed 3 years ago by Wiktor Walc

Description: modified (diff)

comment:2 Changed 3 years ago by Wiktor Walc

Description: modified (diff)

comment:3 Changed 3 years ago by Piotrek Koszuliński

Status: newconfirmed
Note: See TracTickets for help on using tickets.
© 2003 – 2017 CKSource – Frederico Knabben. All rights reserved. | Terms of use | Privacy policy