Been trying all kinds of approaches without success and being that this is brand new functionality, there isn't much out there to use as a reference...
Is there a way to easily modify the template structure of a widget instance?
Take for example the following widget template:
'<div class="a-widget">' +
'<h2 class="an-editable-heading">{headingContent}</h2>' +
'<div class="an-editable-content">' +
'<p>{contentContent}</p>' +
'</div>' +
'</div>'
How would i go about dynamically remove or add editable parts to an existing instance in the editor? For example, if I had a dialog with the following:
{
id: 'hasHeading',
type: 'checkbox',
label: 'Has heading?',
'default': 'checked',
setup: function( widget ) {
this.setValue( widget.data.hasHeading );
},
commit: function( widget ) {
widget.setData( 'hasHeading', this.getValue() );
}
}
Which would result in not having a heading when option is unchecked:
'<div class="a-widget">' +
'<div class="an-editable-content">' +
'<p>{contentContent}</p>' +
'</div>' +
'</div>'
I'd be okay with losing the contents of the heading when it is "unchecked"...
Kind regards,
To avoid complications I
To avoid complications I would just hide (display:none) the heading when hasHeading is set to false. It should be done in widget#data. Then, to correcly output data you would have to write a downcast method (reverse to upcast) so the hidden heading is completely removed. And similarly, in upcast method, if there's no heading, it should be created and hidden (that will be actually done by widget#data fired after initialization) and hasHeading should be set to false.
It is also possible to initialize and destroy nested editables during runtime. See initEditable and destroyEditable methods. But the general rule still is that you have to work on DOM, not with HTML strings. Most likely we will add an option to change widget structure using templates only, but it's not yet possible.
Piotrek (Reinmar) Koszuliński
CKEditor JavaScript Developer
--
CKSource - http://cksource.com
--
Follow CKEditor on: Twitter | Facebook | Google+
Certainly a need
I think it'd be great if widget templates were modifiable dynamically that we can change in dialog's onOk() or like..
I just wasted a day trying out all possible ways to modify the widget template (and surprisingly this post didn't come in search untill i signed up and searched again).
Need this functionality badly, so had to switch to a normal plugin, manipulating DOM now. sort of achieved the desired requirement, but loosing the feature of treating it as 'one unit'...
Thanks for looking into it.
In case anyone else is trying to do this
After spending some time figuring this out for a custom plugin/widget I built, here's how I would do it for retrohunter's example:
Removing the element is pretty straightforward. In the 'data' attribute function of the widget, check that hasHeading is false and that the widget currently has a heading. Then, just find and remove that element:
Adding is similar, but you need to make sure to initizialize the editable after adding. In the 'data' attribute of the widget, after checking that 'hasHeading' is true and there's not already a heading in the widget, you can add one like this:
Hopefully that's useful to other people building mutable widgets.
Thanks for sharing. This is
Thanks for sharing. This is the right method. Maybe I would also destroy the old editable before initialising new using widget.destroyEditable. But it's not critically important.
Piotrek (Reinmar) Koszuliński
CKEditor JavaScript Developer
--
CKSource - http://cksource.com
--
Follow CKEditor on: Twitter | Facebook | Google+
Kind of similar problem
Hey all t
ried to create some widget plugin and found this thread if where looking for something similar. So I did create widget plugin by using some cool examples and a bit of my time.
Still I got one last problem. I can not remove contenteditable="true" .This will stick even after I save my post (Also building Drupal module). My work is public at https://github.com/kaido24/ckeditor_bootstrap_grid. Hope you can help me out so I can finish it or develop it a bit further.
Figured out it allready. My
Figured out it allready. My editables hadn't unique ids.
I just wasted a day trying
I just wasted a day trying out all possible ways to modify the widget template (and surprisingly this post didn't come in search untill i signed up and searched again). Need this functionality badly, so had to switch to a normal plugin, manipulating DOM now. sort of achieved the desired requirement, but loosing the feature of treating it as 'one unit'...??
_________________________
GUL