diff --git a/js/functions.js b/js/functions.js index 436805c200..6669f7406f 100644 --- a/js/functions.js +++ b/js/functions.js @@ -2305,6 +2305,136 @@ function PMA_init_slider() { }); } +/** + * var toggleButton This is a function that creates a toggle + * sliding button given a jQuery reference + * to the correct DOM element + */ +var toggleButton = function ($obj) { + // In rtl mode the toggle switch is flipped horizontally + // so we need to take that into account + if ($('.text_direction', $obj).text() == 'ltr') { + var right = 'right'; + } else { + var right = 'left'; + } + /** + * var h Height of the button, used to scale the + * background image and position the layers + */ + var h = $obj.height(); + $('img', $obj).height(h); + $('table', $obj).css('bottom', h-1); + /** + * var on Width of the "ON" part of the toggle switch + * var off Width of the "OFF" part of the toggle switch + */ + var on = $('.toggleOn', $obj).width(); + var off = $('.toggleOff', $obj).width(); + // Make the "ON" and "OFF" parts of the switch the same size + $('.toggleOn > div', $obj).width(Math.max(on, off)); + $('.toggleOff > div', $obj).width(Math.max(on, off)); + /** + * var w Width of the central part of the switch + */ + var w = parseInt(($('img', $obj).height() / 16) * 22, 10); + // Resize the central part of the switch on the top + // layer to match the background + $('table td:nth-child(2) > div', $obj).width(w); + /** + * var imgw Width of the background image + * var tblw Width of the foreground layer + * var offset By how many pixels to move the background + * image, so that it matches the top layer + */ + var imgw = $('img', $obj).width(); + var tblw = $('table', $obj).width(); + var offset = parseInt(((imgw - tblw) / 2), 10); + // Move the background to match the layout of the top layer + $obj.find('img').css(right, offset); + /** + * var offw Outer width of the "ON" part of the toggle switch + * var btnw Outer width of the central part of the switch + */ + var offw = $('.toggleOff', $obj).outerWidth(); + var btnw = $('table td:nth-child(2)', $obj).outerWidth(); + // Resize the main div so that exactly one side of + // the switch plus the central part fit into it. + $obj.width(offw + btnw + 2); + /** + * var move How many pixels to move the + * switch by when toggling + */ + var move = $('.toggleOff', $obj).outerWidth(); + // If the switch is initialized to the + // OFF state we need to move it now. + if ($('.container', $obj).hasClass('off')) { + if (right == 'right') { + $('table, img', $obj).animate({'left': '-=' + move + 'px'}, 0); + } else { + $('table, img', $obj).animate({'left': '+=' + move + 'px'}, 0); + } + } + // Attach an 'onclick' event to the switch + $('.container', $obj).click(function () { + if ($(this).hasClass('isActive')) { + return false; + } else { + $(this).addClass('isActive'); + } + var $msg = PMA_ajaxShowMessage(PMA_messages['strLoading']); + var $container = $(this); + var callback = $('.callback', this).text(); + // Perform the actual toggle + if ($(this).hasClass('on')) { + if (right == 'right') { + var operator = '-='; + } else { + var operator = '+='; + } + var url = $(this).find('.toggleOff > span').text(); + var removeClass = 'on'; + var addClass = 'off'; + } else { + if (right == 'right') { + var operator = '+='; + } else { + var operator = '-='; + } + var url = $(this).find('.toggleOn > span').text(); + var removeClass = 'off'; + var addClass = 'on'; + } + $.post(url, {'ajax_request': true}, function(data) { + if(data.success == true) { + PMA_ajaxRemoveMessage($msg); + $container + .removeClass(removeClass) + .addClass(addClass) + .animate({'left': operator + move + 'px'}, function () { + $container.removeClass('isActive'); + }); + eval(callback); + } else { + PMA_ajaxShowMessage(data.error); + $container.removeClass('isActive'); + } + }); + }); +}; + +/** + * Initialise all toggle buttons + */ +$(window).load(function () { + $('.toggleAjax').each(function () { + $(this) + .show() + .find('.toggleButton') + toggleButton($(this)); + }); +}); + /** * Vertical pointer */ diff --git a/libraries/common.inc.php b/libraries/common.inc.php index 5bdb45e51b..3bca2d63f1 100644 --- a/libraries/common.inc.php +++ b/libraries/common.inc.php @@ -363,6 +363,7 @@ $goto_whitelist = array( 'db_create.php', 'db_datadict.php', 'db_sql.php', + 'db_events.php', 'db_export.php', 'db_importdocsql.php', 'db_qbe.php', diff --git a/libraries/db_events.inc.php b/libraries/db_events.inc.php index b4ca2e5fc5..40e293f2fa 100644 --- a/libraries/db_events.inc.php +++ b/libraries/db_events.inc.php @@ -7,6 +7,72 @@ if (! defined('PHPMYADMIN')) { exit; } +/** + * TODO: comment + */ +function PMA_toggleButton($action, $select_name, $options, $callback) +{ + // Do the logic first + $link_on = "$action&$select_name=" . urlencode($options[1]['value']); + $link_off = "$action&$select_name=" . urlencode($options[0]['value']); + if ($options[1]['selected'] == true) { + $state = 'on'; + } else if ($options[0]['selected'] == true) { + $state = 'off'; + } else { + $state = 'on'; + } + $selected1 = ''; + $selected0 = ''; + if ($options[1]['selected'] == true) { + $selected1 = " selected='selected'"; + } else if ($options[0]['selected'] == true) { + $selected0 = " selected='selected'"; + } + // Generate output + $retval = "\n"; + $retval .= "