/* vim: set expandtab sw=4 ts=4 sts=4: */ /** * @var RTE a JavaScript namespace containing the functionality * for Routines, Triggers and Events. * * This namespace is extended by the functionality required * to handle a specific item (a routine, trigger or event) * in the relevant javascript files in this folder. */ var RTE = { /** * @var $ajaxDialog jQuery object containing the reference to the * dialog that contains the editor. */ $ajaxDialog: null, /** * @var syntaxHiglighter Reference to the codemirror editor. */ syntaxHiglighter: null, /** * @var buttonOptions Object containing options for * the jQueryUI dialog buttons */ buttonOptions: {}, /** * Validate editor form fields. */ validate: function () { /** * @var $elm a jQuery object containing the reference * to an element that is being validated. */ var $elm = null; // Common validation. At the very least the name // and the definition must be provided for an item $elm = $('table.rte_table').last().find('input[name=item_name]'); if ($elm.val() === '') { $elm.focus(); alert(PMA_messages['strFormEmpty']); return false; } $elm = $('table.rte_table').find('textarea[name=item_definition]'); if ($elm.val() === '') { this.syntaxHiglighter.focus(); alert(PMA_messages['strFormEmpty']); return false; } // The validation has so far passed, so now // we can validate item-specific fields. return RTE.validateCustom(); }, // end validate() /** * Validate custom editor form fields. * This function can be overridden by * other files in this folder. */ validateCustom: function () { return true; }, // end validateCustom() /** * Execute some code after the ajax * dialog for the ditor is shown. * This function can be overridden by * other files in this folder. */ postDialogShow: function () { // Nothing by default } // end postDialogShow() }; // end RTE namespace /** * Attach Ajax event handlers for the Routines, Triggers and Events editor. * * @see $cfg['AjaxEnable'] */ $(document).ready(function () { /** * Attach Ajax event handlers for the Add/Edit functionality. */ $('a.ajax_add_anchor, a.ajax_edit_anchor').live('click', function (event) { event.preventDefault(); /** * @var $edit_row jQuery object containing the reference to * the row of the the item being edited * from the list of items . */ var $edit_row = null; if ($(this).hasClass('ajax_edit_anchor')) { // Remeber the row of the item being edited for later, // so that if the edit is successful, we can replace the // row with info about the modified item. $edit_row = $(this).parents('tr'); } /** * @var $msg jQuery object containing the reference to * the AJAX message shown to the user. */ var $msg = PMA_ajaxShowMessage(); $.get($(this).attr('href'), {'ajax_request': true}, function (data) { if (data.success === true) { // We have successfully fetched the editor form PMA_ajaxRemoveMessage($msg); // Now define the function that is called when // the user presses the "Go" button RTE.buttonOptions[PMA_messages['strGo']] = function () { // Move the data from the codemirror editor back to the // textarea, where it can be used in the form submission. RTE.syntaxHiglighter.save(); // Validate editor and submit request, if passed. if (RTE.validate()) { /** * @var data Form data to be sent in the AJAX request. */ var data = $('form.rte_form').last().serialize(); $msg = PMA_ajaxShowMessage(PMA_messages['strProcessingRequest']); $.post($('form.rte_form').last().attr('action'), data, function (data) { if (data.success === true) { // Item created successfully PMA_ajaxRemoveMessage($msg); PMA_slidingMessage(data.message); RTE.$ajaxDialog.dialog('close'); // If we are in 'edit' mode, we must remove the reference to the old row. if (mode === 'edit') { $edit_row.remove(); } // Sometimes, like when moving a trigger from a table to // another one, the new row should not be inserted into the // list. In this case "data.insert" will be set to false. if (data.insert) { // Insert the new row at the correct location in the list of items /** * @var text Contains the name of an item from the list * that is used in comparisons to find the correct * location where to insert a new row. */ var text = ''; /** * @var inserted Whether a new item has been inserted * in the list or not. */ var inserted = false; $('table.data').find('tr').each(function () { text = $(this) .children('td') .eq(0) .find('strong') .text() .toUpperCase(); text = $.trim(text); if (text !== '' && text > data.name) { $(this).before(data.new_row); inserted = true; return false; } }); if (! inserted) { // If we didn't manage to insert the row yet, // it must belong at the end of the list, // so we insert it there. $('table.data').append(data.new_row); } // Fade-in the new row $('.ajaxInsert').show('slow').removeClass('ajaxInsert'); } else if ($('table.data').find('tr').has('td').length === 0) { // If we are not supposed to insert the new row, we will now // check if the table is empty and needs to be hidden. This // will be the case if we were editing the only item in the // list, which we removed and will not be inserting something // else in its place. $('table.data').hide("slow", function () { $('#nothing2display').show("slow"); }); } // Now we have inserted the row at the correct position, but surely // at least some row classes are wrong now. So we will itirate // throught all rows and assign correct classes to them. /** * @var ct Count of processed rows. */ var ct = 0; /** * @var rowclass Class to be attached to the row * that is being processed */ var rowclass = ''; $('table.data').find('tr').has('td').each(function () { rowclass = (ct % 2 === 0) ? 'odd' : 'even'; $(this).removeClass().addClass(rowclass); ct++; }); // If this is the first item being added, remove // the "No items" message and show the list. if ($('table.data').find('tr').has('td').length > 0 && $('#nothing2display').is(':visible')) { $('#nothing2display').hide("slow", function () { $('table.data').show("slow"); }); } } else { PMA_ajaxShowMessage(data.error, false); } }); // end $.post() } // end "if (RTE.validate())" }; // end of function that handles the submission of the Editor RTE.buttonOptions[PMA_messages['strClose']] = function () { $(this).dialog("close"); }; /** * Display the dialog to the user */ RTE.$ajaxDialog = $('