/** * Javascript code needed to build TinyMCE button to help user building shortcodes for WPDA tables. * * @author Peter Schulz * @since 1.0.0 */ // Default label values. var wpda_tab = 'Table'; var wpda_cols = 'Columns'; var wpda_selcols = 'SELECT COLUMNS'; var wpda_clksel = 'Click to select. Columns are displayed in the order as listed.'; var wpda_curcel = 'Current selection'; var wpda_shwcols = 'SHOW ALL COLUMNS'; var wpda_resp = 'Responsive mode'; var wpda_yes = 'Yes'; var wpda_no = 'No'; var wpda_rescols = 'Number of columns in responsive header'; var wpda_icon = 'Show expand icon'; var wpda_details = 'Show details'; var wpda_col = 'Collapsed'; var wpda_mod = 'In modal window'; var wpda_exp = 'Expanded'; var wpda_war = 'WARNING'; var wpda_wartxt = 'You currently have no access to any database tables and views'; var wpda_act = 'ACTION'; var wpda_acttxt = 'Grant access to desired tables and views in front-end settings'; function translate(txt) { if (jQuery.url( wpda_script_url ).data.param.query[txt]) { eval( 'wpda_' + txt + '= jQuery.url(wpda_script_url).data.param.query.' + txt ); } } wpda_script_url = document.currentScript.src; // Save script url. jQuery( document.currentScript ).ready( function() { // Check for translations. translate( 'tab' ); translate( 'cols' ); translate( 'selcols' ); translate( 'clksel' ); translate( 'curcel' ); translate( 'shwcols' ); translate( 'resp' ); translate( 'yes' ); translate( 'no' ); translate( 'rescols' ); translate( 'icon' ); translate( 'details' ); translate( 'col' ); translate( 'mod' ); translate( 'exp' ); translate( 'war' ); translate( 'wartxt' ); translate( 'act' ); translate( 'acttxt' ); } ); function select_available(e) { var option = jQuery( "#columns_available option:selected" ); var add_to = jQuery( "#columns_selected" ); option.remove(); new_option = add_to.append( option ); if (jQuery( "#columns_selected option[value='']" ).length > 0) { // Remove ALL from selected list. jQuery( "#columns_selected option[value='']" ).remove(); } jQuery( 'select#columns_selected option' ).removeAttr( "selected" ); } function select_selected(e) { var option = jQuery( "#columns_selected option:selected" ); if (option[0].value === '') { // Cannot remove ALL. return; } var add_to = jQuery( "#columns_available" ); option.remove(); add_to.append( option ); if (jQuery( 'select#columns_selected option' ).length === 0) { jQuery( "#columns_selected" ).append( jQuery( '' ).attr( 'value', '' ).text( wpda_shwcols ) ); } jQuery( 'select#columns_available option' ).removeAttr( "selected" ); } (function () { tinymce.create( 'tinymce.plugins.wpdataaccess', { init: function (editor, url) { editor.addButton( 'wpdataaccess_button', { title: 'WP Data Access', icon: 'icon dashicons-editor-table', text: ' WPDA', cmd: 'wpdataaccess_cmd' } ); editor.addCommand( 'wpdataaccess_cmd', function () { if (editor.settings.wpda_table_list === '') { var msg = editor.windowManager.open( { title: "WP Data Access", width: 600, height: 300, body: [ { type: 'label', label: wpda_war, text: wpda_wartxt }, { type: 'label', label: wpda_act, text: wpda_acttxt } ] } ); } else { var win = editor.windowManager.open( { title: "WP Data Access", width: 600, height: 300, body: [ { type: 'listbox', name: 'table', label: wpda_tab, values: editor.settings.wpda_table_list, onselect: function () { column_selection = win.find( "#column_selection" ); column_selection.text( wpda_shwcols ); } }, { type: 'button', name: 'columns_button', label: wpda_cols, text: wpda_selcols, onclick: function () { // Get selected value from table list for post. table = win.find( "#table" ); var wpda_data = { 'action': 'wpda_tinymce_listbox_columns', 'table_name': table[0].state.data.value }; // Get column list for selected table and write to listbox. jQuery.post( ajaxurl, wpda_data, function (response) { var columns_available = jQuery( '' ); var columns_selected = jQuery( '' ); for (var col in response) { columns_available.append( jQuery( '' ).attr( 'value', response[col].text ).text( response[col].text ) ); } var dialog_table = jQuery( '