TinyMCE 4 plugin - preselect listbox option when dialog opens - tinymce-4

How can you preselect a specific listbox option when your plugin dialog opens?
tinymce.PluginManager.add('mybutton', function(editor, url) {
editor.addButton('mybutton', {
icon: true,
image: url + '/img/mybutton.png',
title: 'Select An Option',
onclick: function() {
editor.windowManager.open({
title: 'My options',
body: [
{
type: 'listbox',
name: 'myoptions',
label: 'My Options',
'values': [
{text: 'Option 1', value: '1'},
{text: 'Option 2', value: '2'},
{text: 'Option 3', value: '3'}, /* preselect this option */
{text: 'Option 4', value: '4'},
{text: 'Option 5', value: '5'},
]
}
],
onsubmit: function(v) {
editor.insertContent(v.data.myoptions);
}
});
}
});
});

For some reason this is missing in the Listbox documentation but the solution is quite simple: Add a value property to the listbox object you pass to tinymce and it will preselect it.
Be careful to set the value not to the text/label but the actual value of the listbox item you want to preselect.
tinymce.PluginManager.add('mybutton', function(editor, url) {
editor.addButton('mybutton', {
icon: true,
image: url + '/img/mybutton.png',
title: 'Select An Option',
onclick: function() {
editor.windowManager.open({
title: 'My options',
body: [
{
type: 'listbox',
name: 'myoptions',
label: 'My Options',
values: [
{text: 'Option 1', value: '1'},
{text: 'Option 2', value: '2'},
{text: 'Option 3', value: '3'}, /* preselect this option */
{text: 'Option 4', value: '4'},
{text: 'Option 5', value: '5'},
],
value: '3'
}
],
onsubmit: function(v) {
editor.insertContent(v.data.myoptions);
}
});
}
});
});

I found it much easier to include an external page in the dialog so I can create my own form from scratch and easily control it with Jquery.
// Opens a HTML page inside a TinyMCE dialog and pass in two parameters
editor.windowManager.open({
title: "My PHP/HTML dialog",
url: 'mydialog.php',
width: 700,
height: 600
}, {
content: tinymce.activeEditor.selection.getContent({format: 'html'}),
nodeName: editor.selection.getNode().nodeName
});
Then in mydialog.php interact with the current TinyMCE with:
/* get content from TinyMCE */
console.log(args.content);
console.log(args.nodeName);
/* set content in TinyMCE */
top.tinymce.activeEditor.insertContent('My changed content here');
/* close the dialog */
top.tinymce.activeEditor.windowManager.close();
The reference can be found here:
http://www.tinymce.com/wiki.php/Tutorials:Creating_custom_dialogs

Try to use onPostRender and set the selected value to with the value() function.
Example:
{
type: 'listbox',
name: 'text',
values: [
{text : '1', value : '1'},
{text : '2', value : '2'},
{text : '3', value : '3'}
],
onPostRender: function() {
this.value('2');
}
}

According to their listbox example (https://www.tiny.cloud/docs-4x/demo/custom-toolbar-listbox/) it can be done with onPostRender. I have the following code working in WordPress 5.3.2 (classic editor plugin enabled):
tinymce.PluginManager.add('mybutton', function( editor, url ) {
editor.addButton( 'mybutton', {
type: 'listbox',
text: tinyMCE_object.button_name,
icon: false,
onselect: function (e) {
editor.insertContent(this.value());
},
values: [
{ text: '[one_half]', value: '[one_half] [/one_half]' },
{ text: '[one_third]', value: '[one_third] [/one_third]' },
{ text: '[one_fourth]', value: '[one_fourth] [/one_fourth]' },
{ text: '[one_fifth]', value: '[one_fifth] [/one_fifth]' },
{ text: '[grid]', value: '[grid] [/grid]' },
{ text: '[grid_element]', value: '[grid_element] [/grid_element]' }
],
onPostRender: function () {
// Select the second item by default
this.value('[one_third] [/one_third]');
}
});
});

Related

TinyMCE textarea in modal form

I am trying to create form with some common inputs
<input type="text">
Now, I need to create textarea input because of more text input
<textarea>
I have not found any post about that. My code is bellow. Thanks for advices.
function(editor, type, name) {
editor.windowManager.open( {
title: 'Advert type: ' + name,
body: [
{
type: 'textbox',
name: 'target',
label: 'Target',
autofocus: true
},
{
type: 'checkbox',
name: 'blank',
checked: true,
label: 'Open in new tab'
},
{
type: 'textbox',
name: 'text',
label: 'Main text',
minWidth: '600',
},
{
type: 'listbox',
name: 'align',
label: 'Text align',
maxWidth: 100,
values: [
{
text: 'Left',
value: 'left',
icon: 'icon dashicons-align-left'
},
{
text: Right',
value: 'right',
icon: 'icon dashicons-align-right'
},
{
text: 'Cenetr',
value: 'center',
icon: 'icon dashicons-align-center'}
]
}
],
onsubmit: function(e) {
editor.insertContent('[widget widget_name="TinyMCEAdvWidget" type="' + type + '" target="' + makeTarget(e.data.target) + '" blank="' + e.data.blank + '" text="' + e.data.text + '" align="' + e.data.align + '"]');
}
});
}
I would like to display the main text as textarea col=3.
went through the same problem, you can use the following example:
{
type: 'textbox',
multiline: true
}

extjs dropdown menu creation with link inside

I'm trying to do a dropdown menu with for each button in it, a link to a page.
I made the following code but I'm not able to create some link inside, i get an error each time :
Ext.create('Ext.menu.Menu', {
layout: 'hbox',
width: 500,
height: 40,
margin: '10 0 0 10',
renderTo: Ext.getBody(),
floating: false,
items: [{
text: 'Home'
},
{
text: 'Inventory',
menuAlign: 'tr-br',
menu:{
items:[
{
text: 'Show',
},
{
text: 'Search'
},
{
text: 'Service Catalog'
}
]
}
},
{
text: 'Request',
menuAlign: 'tr-br',
menu:{
items:[
{
text: 'New Request'
},
{
text: 'My requests'
}
]
}
},
{
text: 'Management',
menuAlign: 'tr-br',
menu:{
items:[
{
text: 'Sites'
},
{
text: 'Users'
},
{
text: 'Stocks'
},
{
text: 'Export'
}
]
}
},
]
});
Does anyone know how to create link in a button ?
Solution find :
I just added a handler in my code :
{
text: 'Search',
handler : menuH
},
and the handler :
var menuH = function() {
window.location = "/myapplication";
}

Buttons in a same row

Here my code in sencha touch, i tried to add 2 button horizontaly, Means one to other.No,luck its keep on coming one in upper side and the other one lower.
var profilese = {
standardSubmit : false,
items: [tab,{
xtype: 'button',
text: 'ADD',
ui: 'confirm',
handler: function() {
view.setActiveItem(2, {type:'fade', direction:'right'});
}
},{
xtype: 'DELETE',
text: 'Search Friends',
ui: 'Search',
handler: function() {
view.setActiveItem(3, {type:'fade', direction:'right'});
}
}]
};
How to set button in a same row.Please help me to sort it out.
I'm not sure what tab is in your code, but what you need to get the buttons aligned horizontally is an hbox layout:
layout: 'hbox',
items: [
{
xtype: 'button',
text: 'ADD',
ui: 'confirm',
handler: function() {
view.setActiveItem(2, {type:'fade', direction:'right'});
}
},{
xtype: 'button',
text: 'Search Friends',
ui: 'Search',
handler: function() {
view.setActiveItem(3, {type:'fade', direction:'right'});
}
}
]
http://docs.sencha.com/touch/2.2.1/#!/api/Ext.layout.HBox
In ExtJS point, there must be a container as parent to hold the children(Buttons). And the set the layout config as "hbox".
The code must look like,
items:[
tab,
{
xtype:'container',
layout:'hbox',
items:[
{
xtype:'button1'
},
{
xtype:'button2'
}
]
}
]
layout: {
type: 'column'
},
items: [
{
xtype: 'button',
columnWidth: 0.5,
text: 'ADD',
ui: 'confirm',
handler: function() {
view.setActiveItem(2, {type:'fade', direction:'right'});
}
},{
xtype: 'button',
columnWidth: 0.5,
text: 'Search Friends',
ui: 'Search',
handler: function() {
view.setActiveItem(3, {type:'fade', direction:'right'});
}
}
]

ExtJs ref property

I have a code as below,
window = Ext.extend(Ext.Window, {
initComponent: function(){
Ext.applyIf(this,{
fbar: {
xtype: 'toolbar',
items: [{
xtype: 'button',
id: 'button1',
text: 'button1'
ref: ??
}
]
},
items:[{
xtype: 'form',
items: [{
layout: 'form',
items:[{
xtype: 'field'
id: 'field1'
ref: ??
}]
}]
}]
})
}
})
what can i have for ref property for field and button above, so that i can use with window variable? Please help me in this.
i tried with '../../button', but could not get access through window variable.
i figure it myself.
the ref for button1 will be "../button" and ref for field will be "../../fieldname"... The window is invoked as,
testWindow = new window();
And the button1 can be accessed as,
testWindow.button
and the field as
testWindow.fieldname

How do i add a button in an Actioncolumn in extjs

I hope this is my last question of today. I found a nice actioncolumn option in designer 2. I add one to my grid and it looks like this:
xtype: 'gridpanel',
title: 'Reports',
forceFit: true,
store: 'ReportsStore',
columns: [
{
xtype: 'gridcolumn',
dataIndex: 'Name',
text: 'Name'
},
{
xtype: 'gridcolumn',
dataIndex: 'Type',
text: 'Type'
},
{
xtype: 'gridcolumn',
dataIndex: 'Description',
text: 'Description'
},
{
xtype: 'actioncolumn',
dataIndex: 'Name',
items: [
{
handler: function(view, rowIndex, colIndex, item, e) {
console.log(row, col);
},
altText: 'Run report',
iconCls: 'runReport'
}
]
}
],
viewConfig: {
},
dockedItems: [
{
xtype: 'toolbar',
dock: 'top',
items: [
{
xtype: 'tbfill'
},
{
xtype: 'button',
iconCls: 'addReport',
text: 'Add report',
listeners: {
click: {
fn: me.onButtonClick,
scope: me
}
But there is nothing happening. What i want is that there is an button/icon and when you click on it it will open a ext.window with the reportId from that row. I can't find how i do this with designer.
I don't see an icon now so there is nothing yet to click on. What do i miss?
Thanks,
TJ
You miss listener for this button:
add something like that into your item definition:
handler: function(grid, row, col) {
console.log(row, col);
}

Resources