1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * Create advanced table (resize, reorder, and show/hide columns; and also grid editing).
4 * This function is designed mainly for table DOM generated from browsing a table in the database.
5 * For using this function in other table DOM, you may need to:
6 * - add "draggable" class in the table header <th>, in order to make it resizable, sortable or hidable
7 * - have at least one non-"draggable" header in the table DOM for placing column visibility drop-down arrow
8 * - pass the value "false" for the parameter "enableGridEdit"
9 * - adjust other parameter value, to select which features that will be enabled
11 * @param t the table DOM element
12 * @param enableResize Optional, if false, column resizing feature will be disabled
13 * @param enableReorder Optional, if false, column reordering feature will be disabled
14 * @param enableVisib Optional, if false, show/hide column feature will be disabled
15 * @param enableGridEdit Optional, if false, grid editing feature will be disabled
17 // eslint-disable-next-line no-unused-vars
18 var makeGrid = function (t, enableResize, enableReorder, enableVisib, enableGridEdit) {
19 var isResizeEnabled = enableResize === undefined ? true : enableResize;
20 var isReorderEnabled = enableReorder === undefined ? true : enableReorder;
21 var isVisibEnabled = enableVisib === undefined ? true : enableVisib;
22 var isGridEditEnabled = enableGridEdit === undefined ? true : enableGridEdit;
32 * Variables, assigned with default value, changed later
34 actionSpan: 5, // number of colspan in Actions header in a table
35 tableCreateTime: null, // table creation time, used for saving column order and visibility to server, only available in "Browse tab"
37 // Column reordering variables
38 colOrder: [], // array of column order
40 // Column visibility variables
41 colVisib: [], // array of column visibility
42 showAllColText: '', // string, text for "show all" button under column visibility list
43 visibleHeadersCount: 0, // number of visible data headers
45 // Table hint variables
46 reorderHint: '', // string, hint for column reordering
47 sortHint: '', // string, hint for column sorting
48 markHint: '', // string, hint for column marking
49 copyHint: '', // string, hint for copy column name
50 showReorderHint: false,
55 isCellEditActive: false, // true if current focus is in edit cell
56 isEditCellTextEditable: false, // true if current edit cell is editable in the text input box (not textarea)
57 currentEditCell: null, // reference to <td> that currently being edited
58 cellEditHint: '', // hint shown when doing grid edit
59 gotoLinkText: '', // "Go to link" text
60 wasEditedCellNull: false, // true if last value of the edited cell was NULL
61 maxTruncatedLen: 0, // number of characters that can be displayed in a cell
62 saveCellsAtOnce: false, // $cfg[saveCellsAtOnce]
63 isCellEdited: false, // true if at least one cell has been edited
64 saveCellWarning: '', // string, warning text when user want to leave a page with unsaved edited data
65 lastXHR : null, // last XHR object used in AJAX request
66 isSaving: false, // true when currently saving edited data, used to handle double posting caused by pressing ENTER in grid edit text box in Chrome browser
67 alertNonUnique: '', // string, alert shown when saving edited nonunique table
69 // Common hidden inputs
81 * Start to resize column. Called when clicking on column separator.
84 * @param obj dragged div object
86 dragStartRsz: function (e, obj) {
87 var n = $(g.cRsz).find('div').index(obj); // get the index of separator (i.e., column index)
88 $(obj).addClass('colborder_active');
93 objLeft: $(obj).position().left,
94 objWidth: $(g.t).find('th.draggable:visible:eq(' + n + ') span').outerWidth()
96 $(document.body).css('cursor', 'col-resize').noSelect();
97 if (g.isCellEditActive) {
103 * Start to reorder column. Called when clicking on table header.
106 * @param obj table header object
108 dragStartReorder: function (e, obj) {
109 // prepare the cCpy (column copy) and cPointer (column pointer) from the dragged column
110 $(g.cCpy).text($(obj).text());
111 var objPos = $(obj).position();
113 top: objPos.top + 20,
115 height: $(obj).height(),
116 width: $(obj).width()
122 // get the column index, zero-based
123 var n = g.getHeaderIdx(obj);
135 $(document.body).css('cursor', 'move').noSelect();
136 if (g.isCellEditActive) {
142 * Handle mousemove event when dragging.
146 dragMove: function (e) {
149 dx = e.pageX - g.colRsz.x0;
150 if (g.colRsz.objWidth + dx > g.minColWidth) {
151 $(g.colRsz.obj).css('left', g.colRsz.objLeft + dx + 'px');
153 } else if (g.colReorder) {
154 // dragged column animation
155 dx = e.pageX - g.colReorder.x0;
157 .css('left', g.colReorder.objLeft + dx)
161 var hoveredCol = g.getHoveredCol(e);
163 var newn = g.getHeaderIdx(hoveredCol);
164 g.colReorder.newn = newn;
165 if (newn !== g.colReorder.n) {
166 // show the column pointer in the right place
167 var colPos = $(hoveredCol).position();
168 var newleft = newn < g.colReorder.n ?
170 colPos.left + $(hoveredCol).outerWidth();
174 visibility: 'visible'
177 // no movement to other column, hide the column pointer
178 $(g.cPointer).css('visibility', 'hidden');
185 * Stop the dragging action.
189 dragEnd: function (e) {
191 var dx = e.pageX - g.colRsz.x0;
192 var nw = g.colRsz.objWidth + dx;
193 if (nw < g.minColWidth) {
203 $(g.cRsz).find('div').removeClass('colborder_active');
204 Sql.rearrangeStickyColumns($(t).prev('.sticky_columns'), $(t));
205 } else if (g.colReorder) {
207 if (g.colReorder.newn !== g.colReorder.n) {
208 g.shiftCol(g.colReorder.n, g.colReorder.newn);
209 // assign new position
210 var objPos = $(g.colReorder.obj).position();
211 g.colReorder.objTop = objPos.top;
212 g.colReorder.objLeft = objPos.left;
213 g.colReorder.n = g.colReorder.newn;
214 // send request to server to remember the column order
215 if (g.tableCreateTime) {
218 g.refreshRestoreButton();
221 // animate new column position
222 $(g.cCpy).stop(true, true)
224 top: g.colReorder.objTop,
225 left: g.colReorder.objLeft
228 $(g.cPointer).css('visibility', 'hidden');
230 g.colReorder = false;
231 Sql.rearrangeStickyColumns($(t).prev('.sticky_columns'), $(t));
233 $(document.body).css('cursor', 'inherit').noSelect(false);
237 * Resize column n to new width "nw"
239 * @param n zero-based column index
240 * @param nw new width of the column in pixel
242 resize: function (n, nw) {
243 $(g.t).find('tr').each(function () {
244 $(this).find('th.draggable:visible:eq(' + n + ') span,' +
245 'td:visible:eq(' + (g.actionSpan + n) + ') span')
251 * Reposition column resize bars.
253 reposRsz: function () {
254 $(g.cRsz).find('div').hide();
255 var $firstRowCols = $(g.t).find('tr:first th.draggable:visible');
256 var $resizeHandles = $(g.cRsz).find('div').removeClass('condition');
257 $(g.t).find('table.pma_table').find('thead th:first').removeClass('before-condition');
258 for (var n = 0, l = $firstRowCols.length; n < l; n++) {
259 var $col = $($firstRowCols[n]);
261 if (navigator.userAgent.toLowerCase().indexOf('safari') !== -1) {
262 colWidth = $col.outerWidth();
264 colWidth = $col.outerWidth(true);
266 $($resizeHandles[n]).css('left', $col.position().left + colWidth)
268 if ($col.hasClass('condition')) {
269 $($resizeHandles[n]).addClass('condition');
271 $($resizeHandles[n - 1]).addClass('condition');
275 if ($($resizeHandles[0]).hasClass('condition')) {
276 $(g.t).find('thead th:first').addClass('before-condition');
278 $(g.cRsz).css('height', $(g.t).height());
282 * Shift column from index oldn to newn.
284 * @param oldn old zero-based column index
285 * @param newn new zero-based column index
287 shiftCol: function (oldn, newn) {
288 $(g.t).find('tr').each(function () {
290 $(this).find('th.draggable:eq(' + newn + '),' +
291 'td:eq(' + (g.actionSpan + newn) + ')')
292 .before($(this).find('th.draggable:eq(' + oldn + '),' +
293 'td:eq(' + (g.actionSpan + oldn) + ')'));
295 $(this).find('th.draggable:eq(' + newn + '),' +
296 'td:eq(' + (g.actionSpan + newn) + ')')
297 .after($(this).find('th.draggable:eq(' + oldn + '),' +
298 'td:eq(' + (g.actionSpan + oldn) + ')'));
301 // reposition the column resize bars
304 // adjust the column visibility list
306 $(g.cList).find('.lDiv div:eq(' + newn + ')')
307 .before($(g.cList).find('.lDiv div:eq(' + oldn + ')'));
309 $(g.cList).find('.lDiv div:eq(' + newn + ')')
310 .after($(g.cList).find('.lDiv div:eq(' + oldn + ')'));
312 // adjust the colOrder
313 var tmp = g.colOrder[oldn];
314 g.colOrder.splice(oldn, 1);
315 g.colOrder.splice(newn, 0, tmp);
316 // adjust the colVisib
317 if (g.colVisib.length > 0) {
318 tmp = g.colVisib[oldn];
319 g.colVisib.splice(oldn, 1);
320 g.colVisib.splice(newn, 0, tmp);
325 * Find currently hovered table column's header (excluding actions column).
328 * @return the hovered column's th object or undefined if no hovered column found.
330 getHoveredCol: function (e) {
332 var $headers = $(g.t).find('th.draggable:visible');
333 $headers.each(function () {
334 var left = $(this).offset().left;
335 var right = left + $(this).outerWidth();
336 if (left <= e.pageX && e.pageX <= right) {
344 * Get a zero-based index from a <th class="draggable"> tag in a table.
346 * @param obj table header <th> object
347 * @return zero-based index of the specified table header in the set of table headers (visible or not)
349 getHeaderIdx: function (obj) {
350 return $(obj).parents('tr').find('th.draggable').index(obj);
354 * Reposition the columns back to normal order.
356 restoreColOrder: function () {
357 // use insertion sort, since we already have shiftCol function
358 for (var i = 1; i < g.colOrder.length; i++) {
359 var x = g.colOrder[i];
361 while (j >= 0 && x < g.colOrder[j]) {
365 g.shiftCol(i, j + 1);
368 if (g.tableCreateTime) {
369 // send request to server to remember the column order
372 g.refreshRestoreButton();
376 * Send column preferences (column order and visibility) to the server.
378 sendColPrefs: function () {
379 if ($(g.t).is('.ajax')) { // only send preferences if ajax class
381 'ajax_request': true,
386 'set_col_prefs': true,
387 'table_create_time': g.tableCreateTime
389 if (g.colOrder.length > 0) {
390 $.extend(postParams, { 'col_order': g.colOrder.toString() });
392 if (g.colVisib.length > 0) {
393 $.extend(postParams, { 'col_visib': g.colVisib.toString() });
395 $.post('sql.php', postParams, function (data) {
396 if (data.success !== true) {
397 var $tempDiv = $(document.createElement('div'));
398 $tempDiv.html(data.error);
399 $tempDiv.addClass('error');
400 Functions.ajaxShowMessage($tempDiv, false);
407 * Refresh restore button state.
408 * Make restore button disabled if the table is similar with initial state.
410 refreshRestoreButton: function () {
411 // check if table state is as initial state
412 var isInitial = true;
413 for (var i = 0; i < g.colOrder.length; i++) {
414 if (g.colOrder[i] !== i) {
419 // check if only one visible column left
420 var isOneColumn = g.visibleHeadersCount === 1;
421 // enable or disable restore button
422 if (isInitial || isOneColumn) {
423 $(g.o).find('div.restore_column').hide();
425 $(g.o).find('div.restore_column').show();
430 * Update current hint using the boolean values (showReorderHint, showSortHint, etc.).
433 updateHint: function () {
435 if (!g.colRsz && !g.colReorder) { // if not resizing or dragging
436 if (g.visibleHeadersCount > 1) {
437 g.showReorderHint = true;
439 if ($(t).find('th.marker').length > 0) {
440 g.showMarkHint = true;
442 if (g.showSortHint && g.sortHint) {
443 text += text.length > 0 ? '<br>' : '';
444 text += '- ' + g.sortHint;
446 if (g.showMultiSortHint && g.strMultiSortHint) {
447 text += text.length > 0 ? '<br>' : '';
448 text += '- ' + g.strMultiSortHint;
450 if (g.showMarkHint &&
452 ! g.showSortHint && // we do not show mark hint, when sort hint is shown
456 text += text.length > 0 ? '<br>' : '';
457 text += '- ' + g.reorderHint;
458 text += text.length > 0 ? '<br>' : '';
459 text += '- ' + g.markHint;
460 text += text.length > 0 ? '<br>' : '';
461 text += '- ' + g.copyHint;
468 * Toggle column's visibility.
469 * After calling this function and it returns true, afterToggleCol() must be called.
471 * @return boolean True if the column is toggled successfully.
473 toggleCol: function (n) {
475 // can hide if more than one column is visible
476 if (g.visibleHeadersCount > 1) {
477 $(g.t).find('tr').each(function () {
478 $(this).find('th.draggable:eq(' + n + '),' +
479 'td:eq(' + (g.actionSpan + n) + ')')
483 $(g.cList).find('.lDiv div:eq(' + n + ') input').prop('checked', false);
485 // cannot hide, force the checkbox to stay checked
486 $(g.cList).find('.lDiv div:eq(' + n + ') input').prop('checked', true);
489 } else { // column n is not visible
490 $(g.t).find('tr').each(function () {
491 $(this).find('th.draggable:eq(' + n + '),' +
492 'td:eq(' + (g.actionSpan + n) + ')')
496 $(g.cList).find('.lDiv div:eq(' + n + ') input').prop('checked', true);
502 * This must be called if toggleCol() returns is true.
504 * This function is separated from toggleCol because, sometimes, we want to toggle
505 * some columns together at one time and do just one adjustment after it, e.g. in showAllColumns().
507 afterToggleCol: function () {
508 // some adjustments after hiding column
513 // check visible first row headers count
514 g.visibleHeadersCount = $(g.t).find('tr:first th.draggable:visible').length;
515 g.refreshRestoreButton();
519 * Show columns' visibility list.
521 * @param obj The drop down arrow of column visibility list
523 showColList: function (obj) {
524 // only show when not resizing or reordering
525 if (!g.colRsz && !g.colReorder) {
526 var pos = $(obj).position();
527 // check if the list position is too right
528 if (pos.left + $(g.cList).outerWidth(true) > $(document).width()) {
529 pos.left = $(document).width() - $(g.cList).outerWidth(true);
533 top: pos.top + $(obj).outerHeight(true)
536 $(obj).addClass('coldrop-hover');
541 * Hide columns' visibility list.
543 hideColList: function () {
545 $(g.cDrop).find('.coldrop-hover').removeClass('coldrop-hover');
549 * Reposition the column visibility drop-down arrow.
551 reposDrop: function () {
552 var $th = $(t).find('th:not(.draggable)');
553 for (var i = 0; i < $th.length; i++) {
554 var $cd = $(g.cDrop).find('div:eq(' + i + ')'); // column drop-down arrow
555 var pos = $($th[i]).position();
557 left: pos.left + $($th[i]).width() - $cd.width(),
564 * Show all hidden columns.
566 showAllColumns: function () {
567 for (var i = 0; i < g.colVisib.length; i++) {
568 if (!g.colVisib[i]) {
576 * Show edit cell, if it can be shown
578 * @param cell <td> element to be edited
580 showEditCell: function (cell) {
581 if ($(cell).is('.grid_edit') &&
582 !g.colRsz && !g.colReorder) {
583 if (!g.isCellEditActive) {
586 if ('string' === $cell.attr('data-type') ||
587 'blob' === $cell.attr('data-type') ||
588 'json' === $cell.attr('data-type')
590 g.cEdit = g.cEditTextarea;
592 g.cEdit = g.cEditStd;
595 // remove all edit area and hide it
596 $(g.cEdit).find('.edit_area').empty().hide();
597 // reposition the cEdit element
599 top: $cell.position().top,
600 left: $cell.position().left
605 width: $cell.outerWidth(),
606 height: $cell.outerHeight()
608 // fill the cell edit with text from <td>
609 var value = Functions.getCellValue(cell);
610 if ($cell.attr('data-type') === 'json' && $cell.is('.truncated') === false) {
612 value = JSON.stringify(JSON.parse(value), null, 4);
617 $(g.cEdit).find('.edit_box').val(value);
619 g.currentEditCell = cell;
620 $(g.cEdit).find('.edit_box').trigger('focus');
621 moveCursorToEnd($(g.cEdit).find('.edit_box'));
622 $(g.cEdit).find('*').prop('disabled', false);
626 function moveCursorToEnd (input) {
627 var originalValue = input.val();
628 var originallength = originalValue.length;
630 input.trigger('blur').trigger('focus').val(originalValue);
631 input[0].setSelectionRange(originallength, originallength);
636 * Remove edit cell and the edit area, if it is shown.
638 * @param force Optional, force to hide edit cell without saving edited field.
639 * @param data Optional, data from the POST AJAX request to save the edited field
640 * or just specify "true", if we want to replace the edited field with the new value.
641 * @param field Optional, the edited <td>. If not specified, the function will
642 * use currently edited <td> from g.currentEditCell.
643 * @param field Optional, this object contains a boolean named move (true, if called from move* functions)
644 * and a <td> to which the grid_edit should move
646 hideEditCell: function (force, data, field, options) {
647 if (g.isCellEditActive && !force) {
648 // cell is being edited, save or post the edited data
649 if (options !== undefined) {
650 g.saveOrPostEditedCell(options);
652 g.saveOrPostEditedCell();
657 // cancel any previous request
658 if (g.lastXHR !== null) {
664 if (g.currentEditCell) { // save value of currently edited cell
665 // replace current edited field with the new value
666 var $thisField = $(g.currentEditCell);
667 var isNull = $thisField.data('value') === null;
669 $thisField.find('span').html('NULL');
670 $thisField.addClass('null');
672 $thisField.removeClass('null');
673 var value = data.isNeedToRecheck
674 ? data.truncatableFieldValue
675 : $thisField.data('value');
677 // Truncates the text.
678 $thisField.removeClass('truncated');
679 if (CommonParams.get('pftext') === 'P' && value.length > g.maxTruncatedLen) {
680 $thisField.addClass('truncated');
681 value = value.substring(0, g.maxTruncatedLen) + '...';
684 // Add <br> before carriage return.
685 var newHtml = Functions.escapeHtml(value);
686 newHtml = newHtml.replace(/\n/g, '<br>\n');
688 // remove decimal places if column type not supported
689 if (($thisField.attr('data-decimals') === 0) && ($thisField.attr('data-type').indexOf('time') !== -1)) {
690 newHtml = newHtml.substring(0, newHtml.indexOf('.'));
693 // remove addtional decimal places
694 if (($thisField.attr('data-decimals') > 0) && ($thisField.attr('data-type').indexOf('time') !== -1)) {
695 newHtml = newHtml.substring(0, newHtml.length - (6 - $thisField.attr('data-decimals')));
698 var selector = 'span';
699 if ($thisField.hasClass('hex') && $thisField.find('a').length) {
703 // Updates the code keeping highlighting (if any).
704 var $target = $thisField.find(selector);
705 if (!Functions.updateCode($target, newHtml, value)) {
706 $target.html(newHtml);
709 if ($thisField.is('.bit')) {
710 $thisField.find('span').text($thisField.data('value'));
713 if (data.transformations !== undefined) {
714 $.each(data.transformations, function (cellIndex, value) {
715 var $thisField = $(g.t).find('.to_be_saved:eq(' + cellIndex + ')');
716 $thisField.find('span').html(value);
719 if (data.relations !== undefined) {
720 $.each(data.relations, function (cellIndex, value) {
721 var $thisField = $(g.t).find('.to_be_saved:eq(' + cellIndex + ')');
722 $thisField.find('span').html(value);
731 // hide the cell editing area
733 $(g.cEdit).find('.edit_box').trigger('blur');
734 g.isCellEditActive = false;
735 g.currentEditCell = null;
736 // destroy datepicker in edit area, if exist
737 var $dp = $(g.cEdit).find('.hasDatepicker');
738 if ($dp.length > 0) {
739 // eslint-disable-next-line no-underscore-dangle
740 $(document).on('mousedown', $.datepicker._checkExternalClick);
741 $dp.datepicker('destroy');
742 // change the cursor in edit box back to normal
743 // (the cursor become a hand pointer when we add datepicker)
744 $(g.cEdit).find('.edit_box').css('cursor', 'inherit');
749 * Show drop-down edit area when edit cell is focused.
751 showEditArea: function () {
752 if (!g.isCellEditActive) { // make sure the edit area has not been shown
753 g.isCellEditActive = true;
754 g.isEditCellTextEditable = false;
756 * @var $td current edited cell
758 var $td = $(g.currentEditCell);
760 * @var $editArea the editing area
762 var $editArea = $(g.cEdit).find('.edit_area');
764 * @var where_clause WHERE clause for the edited cell
766 var whereClause = $td.parent('tr').find('.where_clause').val();
768 * @var field_name String containing the name of this field.
769 * @see Sql.getFieldName()
771 var fieldName = Sql.getFieldName($(t), $td);
773 * @var relation_curr_value String current value of the field (for fields that are foreign keyed).
775 var relationCurrValue = $td.text();
777 * @var relation_key_or_display_column String relational key if in 'Relational display column' mode,
778 * relational display column if in 'Relational key' mode (for fields that are foreign keyed).
780 var relationKeyOrDisplayColumn = $td.find('a').attr('title');
782 * @var curr_value String current value of the field (for fields that are of type enum or set).
784 var currValue = $td.find('span').text();
786 // empty all edit area, then rebuild it based on $td classes
789 // remember this instead of testing more than once
790 var isNull = $td.is('.null');
792 // add goto link, if this cell contains a link
793 if ($td.find('a').length > 0) {
794 var gotoLink = document.createElement('div');
795 gotoLink.className = 'goto_link';
796 $(gotoLink).append(g.gotoLinkText + ' ').append($td.find('a').clone());
797 $editArea.append(gotoLink);
800 g.wasEditedCellNull = false;
801 if ($td.is(':not(.not_null)')) {
802 // append a null checkbox
803 $editArea.append('<div class="null_div"><label>Null:<input type="checkbox"></label></div>');
805 var $checkbox = $editArea.find('.null_div input');
806 // check if current <td> is NULL
808 $checkbox.prop('checked', true);
809 g.wasEditedCellNull = true;
812 // if the select/editor is changed un-check the 'checkbox_null_<field_name>_<row_index>'.
813 if ($td.is('.enum, .set')) {
814 $editArea.on('change', 'select', function () {
815 $checkbox.prop('checked', false);
817 } else if ($td.is('.relation')) {
818 $editArea.on('change', 'select', function () {
819 $checkbox.prop('checked', false);
821 $editArea.on('click', '.browse_foreign', function () {
822 $checkbox.prop('checked', false);
825 $(g.cEdit).on('keypress change paste', '.edit_box', function () {
826 $checkbox.prop('checked', false);
828 // Capture ctrl+v (on IE and Chrome)
829 $(g.cEdit).on('keydown', '.edit_box', function (e) {
830 if (e.ctrlKey && e.which === 86) {
831 $checkbox.prop('checked', false);
834 $editArea.on('keydown', 'textarea', function () {
835 $checkbox.prop('checked', false);
838 // if some text is written in textbox automatically unmark the null checkbox and if it is emptied again mark the checkbox.
839 $(g.cEdit).find('.edit_box').on('input', function () {
840 if ($(g.cEdit).find('.edit_box').val() !== '') {
841 $checkbox.prop('checked', false);
843 $checkbox.prop('checked', true);
846 // if null checkbox is clicked empty the corresponding select/editor.
847 $checkbox.on('click', function () {
848 if ($td.is('.enum')) {
849 $editArea.find('select').val('');
850 } else if ($td.is('.set')) {
851 $editArea.find('select').find('option').each(function () {
852 var $option = $(this);
853 $option.prop('selected', false);
855 } else if ($td.is('.relation')) {
856 // if the dropdown is there to select the foreign value
857 if ($editArea.find('select').length > 0) {
858 $editArea.find('select').val('');
861 $editArea.find('textarea').val('');
863 $(g.cEdit).find('.edit_box').val('');
867 // reset the position of the edit_area div after closing datetime picker
868 $(g.cEdit).find('.edit_area').css({ 'top' :'0','position':'' });
871 if ($td.is('.relation')) {
873 $editArea.addClass('edit_area_loading');
875 // initialize the original data
876 $td.data('original_data', null);
879 * @var post_params Object containing parameters for the POST request
882 'ajax_request' : true,
883 'get_relational_values' : true,
887 'column' : fieldName,
888 'curr_value' : relationCurrValue,
889 'relation_key_or_display_column' : relationKeyOrDisplayColumn
892 g.lastXHR = $.post('sql.php', postParams, function (data) {
894 $editArea.removeClass('edit_area_loading');
895 if ($(data.dropdown).is('select')) {
896 // save original_data
897 var value = $(data.dropdown).val();
898 $td.data('original_data', value);
899 // update the text input field, in case where the "Relational display column" is checked
900 $(g.cEdit).find('.edit_box').val(value);
903 $editArea.append(data.dropdown);
904 $editArea.append('<div class="cell_edit_hint">' + g.cellEditHint + '</div>');
906 // for 'Browse foreign values' options,
907 // hide the value next to 'Browse foreign values' link
908 $editArea.find('span.curr_value').hide();
909 // handle update for new values selected from new window
910 $editArea.find('span.curr_value').on('change', function () {
911 $(g.cEdit).find('.edit_box').val($(this).text());
916 $editArea.on('change', 'select', function () {
917 $(g.cEdit).find('.edit_box').val($(this).val());
919 g.isEditCellTextEditable = true;
920 } else if ($td.is('.enum')) {
921 // handle enum fields
922 $editArea.addClass('edit_area_loading');
925 * @var post_params Object containing parameters for the POST request
928 'ajax_request' : true,
929 'get_enum_values' : true,
933 'column' : fieldName,
934 'curr_value' : currValue
936 g.lastXHR = $.post('sql.php', postParams, function (data) {
938 $editArea.removeClass('edit_area_loading');
939 $editArea.append(data.dropdown);
940 $editArea.append('<div class="cell_edit_hint">' + g.cellEditHint + '</div>');
944 $editArea.on('change', 'select', function () {
945 $(g.cEdit).find('.edit_box').val($(this).val());
947 } else if ($td.is('.set')) {
949 $editArea.addClass('edit_area_loading');
951 // if the data is truncated, get the full data
952 if ($td.is('.truncated')) {
954 'ajax_request': true,
955 'get_set_values': true,
960 'curr_value': currValue,
961 'get_full_values': true,
962 'where_clause': whereClause
966 'ajax_request': true,
967 'get_set_values': true,
972 'curr_value': currValue
976 g.lastXHR = $.post('sql.php', postParams, function (data) {
978 $editArea.removeClass('edit_area_loading');
979 $editArea.append(data.select);
980 $td.data('original_data', $(data.select).val().join());
981 $editArea.append('<div class="cell_edit_hint">' + g.cellEditHint + '</div>');
985 $editArea.on('change', 'select', function () {
986 $(g.cEdit).find('.edit_box').val($(this).val());
988 } else if ($td.is('.truncated, .transformed')) {
989 if ($td.is('.to_be_saved')) { // cell has been edited
990 var value = $td.data('value');
991 $(g.cEdit).find('.edit_box').val(value);
992 $editArea.append('<textarea></textarea>');
993 $editArea.find('textarea').val(value);
995 .on('keyup', 'textarea', function () {
996 $(g.cEdit).find('.edit_box').val($(this).val());
998 $(g.cEdit).on('keyup', '.edit_box', function () {
999 $editArea.find('textarea').val($(this).val());
1001 $editArea.append('<div class="cell_edit_hint">' + g.cellEditHint + '</div>');
1003 // handle truncated/transformed values values
1004 $editArea.addClass('edit_area_loading');
1006 // initialize the original data
1007 $td.data('original_data', null);
1010 * @var sql_query String containing the SQL query used to retrieve value of truncated/transformed data
1012 var sqlQuery = 'SELECT `' + fieldName + '` FROM `' + g.table + '` WHERE ' + whereClause;
1014 // Make the Ajax call and get the data, wrap it and insert it
1015 g.lastXHR = $.post('sql.php', {
1016 'server' : g.server,
1018 'ajax_request' : true,
1019 'sql_query' : sqlQuery,
1021 }, function (data) {
1023 $editArea.removeClass('edit_area_loading');
1024 if (typeof data !== 'undefined' && data.success === true) {
1025 if ($td.attr('data-type') === 'json') {
1027 data.value = JSON.stringify(JSON.parse(data.value), null, 4);
1032 $td.data('original_data', data.value);
1033 $(g.cEdit).find('.edit_box').val(data.value);
1035 Functions.ajaxShowMessage(data.error, false);
1039 g.isEditCellTextEditable = true;
1040 } else if ($td.is('.timefield, .datefield, .datetimefield, .timestampfield')) {
1041 var $inputField = $(g.cEdit).find('.edit_box');
1043 // remember current datetime value in $input_field, if it is not null
1044 var datetimeValue = !isNull ? $inputField.val() : '';
1046 var showMillisec = false;
1047 var showMicrosec = false;
1048 var timeFormat = 'HH:mm:ss';
1049 // check for decimal places of seconds
1050 if (($td.attr('data-decimals') > 0) && ($td.attr('data-type').indexOf('time') !== -1)) {
1051 if (datetimeValue && datetimeValue.indexOf('.') === false) {
1052 datetimeValue += '.';
1054 if ($td.attr('data-decimals') > 3) {
1055 showMillisec = true;
1056 showMicrosec = true;
1057 timeFormat = 'HH:mm:ss.lc';
1059 if (datetimeValue) {
1060 datetimeValue += '000000';
1061 datetimeValue = datetimeValue.substring(0, datetimeValue.indexOf('.') + 7);
1062 $inputField.val(datetimeValue);
1065 showMillisec = true;
1066 timeFormat = 'HH:mm:ss.l';
1068 if (datetimeValue) {
1069 datetimeValue += '000';
1070 datetimeValue = datetimeValue.substring(0, datetimeValue.indexOf('.') + 4);
1071 $inputField.val(datetimeValue);
1076 // add datetime picker
1077 Functions.addDatepicker($inputField, $td.attr('data-type'), {
1078 showMillisec: showMillisec,
1079 showMicrosec: showMicrosec,
1080 timeFormat: timeFormat
1083 $inputField.on('keyup', function (e) {
1084 if (e.which === 13) {
1085 // post on pressing "Enter"
1087 e.stopPropagation();
1088 g.saveOrPostEditedCell();
1089 } else if (e.which !== 27) {
1090 Functions.toggleDatepickerIfInvalid($td, $inputField);
1094 $inputField.datepicker('show');
1095 Functions.toggleDatepickerIfInvalid($td, $inputField);
1097 // unbind the mousedown event to prevent the problem of
1098 // datepicker getting closed, needs to be checked for any
1099 // change in names when updating
1100 // eslint-disable-next-line no-underscore-dangle
1101 $(document).off('mousedown', $.datepicker._checkExternalClick);
1103 // move ui-datepicker-div inside cEdit div
1104 var datepickerDiv = $('#ui-datepicker-div');
1105 datepickerDiv.css({ 'top': 0, 'left': 0, 'position': 'relative' });
1106 $(g.cEdit).append(datepickerDiv);
1108 // cancel any click on the datepicker element
1109 $editArea.find('> *').on('click', function (e) {
1110 e.stopPropagation();
1113 g.isEditCellTextEditable = true;
1115 g.isEditCellTextEditable = true;
1116 // only append edit area hint if there is a null checkbox
1117 if ($editArea.children().length > 0) {
1118 $editArea.append('<div class="cell_edit_hint">' + g.cellEditHint + '</div>');
1121 if ($editArea.children().length > 0) {
1128 * Post the content of edited cell.
1130 * @param field Optional, this object contains a boolean named move (true, if called from move* functions)
1131 * and a <td> to which the grid_edit should move
1133 postEditedCell: function (options) {
1139 * @var relation_fields Array containing the name/value pairs of relational fields
1141 var relationFields = {};
1143 * @var relational_display string 'K' if relational key, 'D' if relational display column
1145 var relationalDisplay = $(g.o).find('input[name=relational_display]:checked').val();
1147 * @var transform_fields Array containing the name/value pairs for transformed fields
1149 var transformFields = {};
1151 * @var transformation_fields Boolean, if there are any transformed fields in the edited cells
1153 var transformationFields = false;
1155 * @var full_sql_query String containing the complete SQL query to update this table
1157 var fullSqlQuery = '';
1159 * @var rel_fields_list String, url encoded representation of {@link relations_fields}
1161 var relFieldsList = '';
1163 * @var transform_fields_list String, url encoded representation of {@link transformFields}
1165 var transformFieldsList = '';
1167 * @var where_clause Array containing where clause for updated fields
1169 var fullWhereClause = [];
1171 * @var is_unique Boolean, whether the rows in this table is unique or not
1173 var isUnique = $(g.t).find('td.edit_row_anchor').is('.nonunique') ? 0 : 1;
1175 * multi edit variables
1177 var multiEditFieldsName = [];
1178 var multiEditFieldsType = [];
1179 var multiEditFields = [];
1180 var multiEditFieldsNull = [];
1182 // alert user if edited table is not unique
1184 alert(g.alertNonUnique);
1187 // loop each edited row
1188 $(g.t).find('td.to_be_saved').parents('tr').each(function () {
1190 var whereClause = $tr.find('.where_clause').val();
1191 if (typeof whereClause === 'undefined') {
1194 fullWhereClause.push(whereClause);
1195 var conditionArray = JSON.parse($tr.find('.condition_array').val());
1198 * multi edit variables, for current row
1199 * @TODO array indices are still not correct, they should be md5 of field's name
1201 var fieldsName = [];
1202 var fieldsType = [];
1204 var fieldsNull = [];
1206 // loop each edited cell in a row
1207 $tr.find('.to_be_saved').each(function () {
1209 * @var $this_field Object referring to the td that is being edited
1211 var $thisField = $(this);
1214 * @var field_name String containing the name of this field.
1215 * @see Sql.getFieldName()
1217 var fieldName = Sql.getFieldName($(g.t), $thisField);
1220 * @var this_field_params Array temporary storage for the name/value of current field
1222 var thisFieldParams = {};
1224 if ($thisField.is('.transformed')) {
1225 transformationFields = true;
1227 thisFieldParams[fieldName] = $thisField.data('value');
1230 * @var is_null String capturing whether 'checkbox_null_<field_name>_<row_index>' is checked.
1232 var isNull = thisFieldParams[fieldName] === null;
1234 fieldsName.push(fieldName);
1237 fieldsNull.push('on');
1240 if ($thisField.is('.bit')) {
1241 fieldsType.push('bit');
1242 } else if ($thisField.hasClass('hex')) {
1243 fieldsType.push('hex');
1245 fieldsNull.push('');
1246 // Convert \n to \r\n to be consistent with form submitted value.
1247 // The internal browser representation has to be just \n
1248 // while form submitted value \r\n, see specification:
1249 // https://www.w3.org/TR/html5/forms.html#the-textarea-element
1250 fields.push($thisField.data('value').replace(/\n/g, '\r\n'));
1252 var cellIndex = $thisField.index('.to_be_saved');
1253 if ($thisField.is(':not(.relation, .enum, .set, .bit)')) {
1254 if ($thisField.is('.transformed')) {
1255 transformFields[cellIndex] = {};
1256 $.extend(transformFields[cellIndex], thisFieldParams);
1258 } else if ($thisField.is('.relation')) {
1259 relationFields[cellIndex] = {};
1260 $.extend(relationFields[cellIndex], thisFieldParams);
1263 // check if edited field appears in WHERE clause
1264 if (whereClause.indexOf(Sql.urlEncode(fieldName)) > -1) {
1265 var fieldStr = '`' + g.table + '`.' + '`' + fieldName + '`';
1266 for (var field in conditionArray) {
1267 if (field.indexOf(fieldStr) > -1) {
1268 conditionArray[field] = isNull ? 'IS NULL' : '= \'' + thisFieldParams[fieldName].replace(/'/g, '\'\'') + '\'';
1273 }); // end of loop for every edited cells in a row
1277 for (var field in conditionArray) {
1278 newClause += field + ' ' + conditionArray[field] + ' AND ';
1280 newClause = newClause.substring(0, newClause.length - 5); // remove the last AND
1281 $tr.data('new_clause', newClause);
1282 // save condition_array
1283 $tr.find('.condition_array').val(JSON.stringify(conditionArray));
1285 multiEditFieldsName.push(fieldsName);
1286 multiEditFieldsType.push(fieldsType);
1287 multiEditFields.push(fields);
1288 multiEditFieldsNull.push(fieldsNull);
1289 }); // end of loop for every edited rows
1291 relFieldsList = $.param(relationFields);
1292 transformFieldsList = $.param(transformFields);
1294 // Make the Ajax post after setting all parameters
1296 * @var post_params Object containing parameters for the POST request
1298 var postParams = { 'ajax_request' : true,
1299 'sql_query' : fullSqlQuery,
1300 'server' : g.server,
1303 'clause_is_unique' : isUnique,
1304 'where_clause' : fullWhereClause,
1305 'fields[multi_edit]' : multiEditFields,
1306 'fields_name[multi_edit]' : multiEditFieldsName,
1307 'fields_type[multi_edit]' : multiEditFieldsType,
1308 'fields_null[multi_edit]' : multiEditFieldsNull,
1309 'rel_fields_list' : relFieldsList,
1310 'do_transformations' : transformationFields,
1311 'transform_fields_list' : transformFieldsList,
1312 'relational_display' : relationalDisplay,
1314 'submit_type' : 'save'
1317 if (!g.saveCellsAtOnce) {
1318 $(g.cEdit).find('*').prop('disabled', true);
1319 $(g.cEdit).find('.edit_box').addClass('edit_box_posting');
1321 $(g.o).find('div.save_edited').addClass('saving_edited_data')
1322 .find('input').prop('disabled', true); // disable the save button
1327 url: 'tbl_replace.php',
1332 if (!g.saveCellsAtOnce) {
1333 $(g.cEdit).find('*').prop('disabled', false);
1334 $(g.cEdit).find('.edit_box').removeClass('edit_box_posting');
1336 $(g.o).find('div.save_edited').removeClass('saving_edited_data')
1337 .find('input').prop('disabled', false); // enable the save button back
1339 if (typeof data !== 'undefined' && data.success === true) {
1340 if (typeof options === 'undefined' || ! options.move) {
1341 Functions.ajaxShowMessage(data.message);
1344 // update where_clause related data in each edited row
1345 $(g.t).find('td.to_be_saved').parents('tr').each(function () {
1346 var newClause = $(this).data('new_clause');
1347 var $whereClause = $(this).find('.where_clause');
1348 var oldClause = $whereClause.val();
1349 var decodedOldClause = oldClause;
1350 var decodedNewClause = newClause;
1352 $whereClause.val(newClause);
1353 // update Edit, Copy, and Delete links also
1354 $(this).find('a').each(function () {
1355 $(this).attr('href', $(this).attr('href').replace(oldClause, newClause));
1356 // update delete confirmation in Delete link
1357 if ($(this).attr('href').indexOf('DELETE') > -1) {
1358 $(this).removeAttr('onclick')
1360 .on('click', function () {
1361 return Functions.confirmLink(this, 'DELETE FROM `' + g.db + '`.`' + g.table + '` WHERE ' +
1362 decodedNewClause + (isUnique ? '' : ' LIMIT 1'));
1366 // update the multi edit checkboxes
1367 $(this).find('input[type=checkbox]').each(function () {
1368 var $checkbox = $(this);
1369 var checkboxName = $checkbox.attr('name');
1370 var checkboxValue = $checkbox.val();
1372 $checkbox.attr('name', checkboxName.replace(oldClause, newClause));
1373 $checkbox.val(checkboxValue.replace(decodedOldClause, decodedNewClause));
1376 // update the display of executed SQL query command
1377 if (typeof data.sql_query !== 'undefined') {
1378 // extract query box
1379 var $resultQuery = $($.parseHTML(data.sql_query));
1380 var sqlOuter = $resultQuery.find('.sqlOuter').wrap('<p>').parent().html();
1381 var tools = $resultQuery.find('.tools').wrap('<p>').parent().html();
1382 // sqlOuter and tools will not be present if 'Show SQL queries' configuration is off
1383 if (typeof sqlOuter !== 'undefined' && typeof tools !== 'undefined') {
1384 $(g.o).find('.result_query:not(:last)').remove();
1385 var $existingQuery = $(g.o).find('.result_query');
1386 // If two query box exists update query in second else add a second box
1387 if ($existingQuery.find('div.sqlOuter').length > 1) {
1388 $existingQuery.children(':nth-child(4)').remove();
1389 $existingQuery.children(':nth-child(4)').remove();
1390 $existingQuery.append(sqlOuter + tools);
1392 $existingQuery.append(sqlOuter + tools);
1394 Functions.highlightSql($existingQuery);
1397 // hide and/or update the successfully saved cells
1398 g.hideEditCell(true, data);
1400 // remove the "Save edited cells" button
1401 $(g.o).find('div.save_edited').hide();
1402 // update saved fields
1403 $(g.t).find('.to_be_saved')
1404 .removeClass('to_be_saved')
1405 .data('value', null)
1406 .data('original_data', null);
1408 g.isCellEdited = false;
1410 Functions.ajaxShowMessage(data.error, false);
1411 if (!g.saveCellsAtOnce) {
1412 $(g.t).find('.to_be_saved')
1413 .removeClass('to_be_saved');
1417 }).done(function () {
1418 if (options !== undefined && options.move) {
1419 g.showEditCell(options.cell);
1425 * Save edited cell, so it can be posted later.
1427 saveEditedCell: function () {
1429 * @var $this_field Object referring to the td that is being edited
1431 var $thisField = $(g.currentEditCell);
1432 var $testElement = ''; // to test the presence of a element
1434 var needToPost = false;
1437 * @var field_name String containing the name of this field.
1438 * @see Sql.getFieldName()
1440 var fieldName = Sql.getFieldName($(g.t), $thisField);
1443 * @var this_field_params Array temporary storage for the name/value of current field
1445 var thisFieldParams = {};
1448 * @var is_null String capturing whether 'checkbox_null_<field_name>_<row_index>' is checked.
1450 var isNull = $(g.cEdit).find('input:checkbox').is(':checked');
1452 if ($(g.cEdit).find('.edit_area').is('.edit_area_loading')) {
1453 // the edit area is still loading (retrieving cell data), no need to post
1455 } else if (isNull) {
1456 if (!g.wasEditedCellNull) {
1457 thisFieldParams[fieldName] = null;
1461 if ($thisField.is('.bit')) {
1462 thisFieldParams[fieldName] = $(g.cEdit).find('.edit_box').val();
1463 } else if ($thisField.is('.set')) {
1464 $testElement = $(g.cEdit).find('select');
1465 thisFieldParams[fieldName] = $testElement.map(function () {
1466 return $(this).val();
1468 } else if ($thisField.is('.relation, .enum')) {
1469 // for relation and enumeration, take the results from edit box value,
1470 // because selected value from drop-down, new window or multiple
1471 // selection list will always be updated to the edit box
1472 thisFieldParams[fieldName] = $(g.cEdit).find('.edit_box').val();
1473 } else if ($thisField.hasClass('hex')) {
1474 if ($(g.cEdit).find('.edit_box').val().match(/^(0x)?[a-f0-9]*$/i) !== null) {
1475 thisFieldParams[fieldName] = $(g.cEdit).find('.edit_box').val();
1477 var hexError = '<div class="error">' + Messages.strEnterValidHex + '</div>';
1478 Functions.ajaxShowMessage(hexError, false);
1479 thisFieldParams[fieldName] = Functions.getCellValue(g.currentEditCell);
1482 thisFieldParams[fieldName] = $(g.cEdit).find('.edit_box').val();
1484 if (g.wasEditedCellNull || thisFieldParams[fieldName] !== Functions.getCellValue(g.currentEditCell)) {
1490 $(g.currentEditCell).addClass('to_be_saved')
1491 .data('value', thisFieldParams[fieldName]);
1492 if (g.saveCellsAtOnce) {
1493 $(g.o).find('div.save_edited').show();
1495 g.isCellEdited = true;
1502 * Save or post currently edited cell, depending on the "saveCellsAtOnce" configuration.
1504 * @param field Optional, this object contains a boolean named move (true, if called from move* functions)
1505 * and a <td> to which the grid_edit should move
1507 saveOrPostEditedCell: function (options) {
1508 var saved = g.saveEditedCell();
1509 // Check if $cfg['SaveCellsAtOnce'] is false
1510 if (!g.saveCellsAtOnce) {
1511 // Check if need_to_post is true
1513 // Check if this function called from 'move' functions
1514 if (options !== undefined && options.move) {
1515 g.postEditedCell(options);
1519 // need_to_post is false
1521 // Check if this function called from 'move' functions
1522 if (options !== undefined && options.move) {
1523 g.hideEditCell(true);
1524 g.showEditCell(options.cell);
1525 // NOT called from 'move' functions
1527 g.hideEditCell(true);
1530 // $cfg['SaveCellsAtOnce'] is true
1534 // If this function called from 'move' functions
1535 if (options !== undefined && options.move) {
1536 g.hideEditCell(true, true, false, options);
1537 g.showEditCell(options.cell);
1538 // NOT called from 'move' functions
1540 g.hideEditCell(true, true);
1543 // If this function called from 'move' functions
1544 if (options !== undefined && options.move) {
1545 g.hideEditCell(true, false, false, options);
1546 g.showEditCell(options.cell);
1547 // NOT called from 'move' functions
1549 g.hideEditCell(true);
1556 * Initialize column resize feature.
1558 initColResize: function () {
1559 // create column resizer div
1560 g.cRsz = document.createElement('div');
1561 g.cRsz.className = 'cRsz';
1563 // get data columns in the first row of the table
1564 var $firstRowCols = $(g.t).find('tr:first th.draggable');
1566 // create column borders
1567 $firstRowCols.each(function () {
1568 var cb = document.createElement('div'); // column border
1569 $(cb).addClass('colborder')
1570 .on('mousedown', function (e) {
1571 g.dragStartRsz(e, this);
1573 $(g.cRsz).append(cb);
1577 // attach to global div
1578 $(g.gDiv).prepend(g.cRsz);
1582 * Initialize column reordering feature.
1584 initColReorder: function () {
1585 g.cCpy = document.createElement('div'); // column copy, to store copy of dragged column header
1586 g.cPointer = document.createElement('div'); // column pointer, used when reordering column
1589 g.cCpy.className = 'cCpy';
1592 // adjust g.cPointer
1593 g.cPointer.className = 'cPointer';
1594 $(g.cPointer).css('visibility', 'hidden'); // set visibility to hidden instead of calling hide() to force browsers to cache the image in cPointer class
1596 // assign column reordering hint
1597 g.reorderHint = Messages.strColOrderHint;
1599 // get data columns in the first row of the table
1600 var $firstRowCols = $(g.t).find('tr:first th.draggable');
1602 // initialize column order
1603 var $colOrder = $(g.o).find('.col_order'); // check if column order is passed from PHP
1605 if ($colOrder.length > 0) {
1606 g.colOrder = $colOrder.val().split(',');
1607 for (i = 0; i < g.colOrder.length; i++) {
1608 g.colOrder[i] = parseInt(g.colOrder[i], 10);
1612 for (i = 0; i < $firstRowCols.length; i++) {
1618 $(g.t).find('th.draggable')
1619 .on('mousedown', function (e) {
1620 $(g.o).addClass('turnOffSelect');
1621 if (g.visibleHeadersCount > 1) {
1622 g.dragStartReorder(e, this);
1625 .on('mouseenter', function () {
1626 if (g.visibleHeadersCount > 1) {
1627 $(this).css('cursor', 'move');
1629 $(this).css('cursor', 'inherit');
1632 .on('mouseleave', function () {
1633 g.showReorderHint = false;
1634 $(this).tooltip('option', {
1635 content: g.updateHint()
1638 .on('dblclick', function (e) {
1641 .prop('title', Messages.strColNameCopyTitle)
1642 .addClass('modal-copy')
1643 .text(Messages.strColNameCopyText)
1646 .prop('readonly', true)
1647 .val($(this).data('column'))
1653 .find('input').trigger('focus').trigger('select');
1655 $(g.t).find('th.draggable a')
1656 .on('dblclick', function (e) {
1657 e.stopPropagation();
1659 // restore column order when the restore button is clicked
1660 $(g.o).find('div.restore_column').on('click', function () {
1661 g.restoreColOrder();
1664 // attach to global div
1665 $(g.gDiv).append(g.cPointer);
1666 $(g.gDiv).append(g.cCpy);
1668 // prevent default "dragstart" event when dragging a link
1669 $(g.t).find('th a').on('dragstart', function () {
1673 // refresh the restore column button state
1674 g.refreshRestoreButton();
1678 * Initialize column visibility feature.
1680 initColVisib: function () {
1681 g.cDrop = document.createElement('div'); // column drop-down arrows
1682 g.cList = document.createElement('div'); // column visibility list
1685 g.cDrop.className = 'cDrop';
1688 g.cList.className = 'cList';
1691 // assign column visibility related hints
1692 g.showAllColText = Messages.strShowAllCol;
1694 // get data columns in the first row of the table
1695 var $firstRowCols = $(g.t).find('tr:first th.draggable');
1698 // initialize column visibility
1699 var $colVisib = $(g.o).find('.col_visib'); // check if column visibility is passed from PHP
1700 if ($colVisib.length > 0) {
1701 g.colVisib = $colVisib.val().split(',');
1702 for (i = 0; i < g.colVisib.length; i++) {
1703 g.colVisib[i] = parseInt(g.colVisib[i], 10);
1707 for (i = 0; i < $firstRowCols.length; i++) {
1712 // make sure we have more than one column
1713 if ($firstRowCols.length > 1) {
1714 var $colVisibTh = $(g.t).find('th:not(.draggable)');
1718 Messages.strColVisibHint
1721 // create column visibility drop-down arrow(s)
1722 $colVisibTh.each(function () {
1723 var cd = document.createElement('div'); // column drop-down arrow
1724 $(cd).addClass('coldrop')
1725 .on('click', function () {
1726 if (g.cList.style.display === 'none') {
1727 g.showColList(this);
1732 $(g.cDrop).append(cd);
1735 // add column visibility control
1736 g.cList.innerHTML = '<div class="lDiv"></div>';
1737 var $listDiv = $(g.cList).find('div');
1739 var tempClick = function () {
1740 if (g.toggleCol($(this).index())) {
1745 for (i = 0; i < $firstRowCols.length; i++) {
1746 var currHeader = $firstRowCols[i];
1747 var listElmt = document.createElement('div');
1748 $(listElmt).text($(currHeader).text())
1749 .prepend('<input type="checkbox" ' + (g.colVisib[i] ? 'checked="checked" ' : '') + '>');
1750 $listDiv.append(listElmt);
1751 // add event on click
1752 $(listElmt).on('click', tempClick);
1754 // add "show all column" button
1755 var showAll = document.createElement('div');
1756 $(showAll).addClass('showAllColBtn')
1757 .text(g.showAllColText);
1758 $(g.cList).append(showAll);
1759 $(showAll).on('click', function () {
1762 // prepend "show all column" button at top if the list is too long
1763 if ($firstRowCols.length > 10) {
1764 var clone = showAll.cloneNode(true);
1765 $(g.cList).prepend(clone);
1766 $(clone).on('click', function () {
1772 // hide column visibility list if we move outside the list
1773 $(g.t).find('td, th.draggable').on('mouseenter', function () {
1777 // attach to global div
1778 $(g.gDiv).append(g.cDrop);
1779 $(g.gDiv).append(g.cList);
1786 * Move currently Editing Cell to Up
1788 moveUp: function (e) {
1790 var $thisField = $(g.currentEditCell);
1791 var fieldName = Sql.getFieldName($(g.t), $thisField);
1793 var whereClause = $thisField.parents('tr').first().find('.where_clause').val();
1794 if (typeof whereClause === 'undefined') {
1800 $thisField.parents('tr').first().parents('tbody').children().each(function () {
1801 if ($(this).find('.where_clause').val() === whereClause) {
1811 if (found && $prevRow) {
1812 $prevRow.children('td').each(function () {
1813 if (Sql.getFieldName($(g.t), $(this)) === fieldName) {
1820 g.hideEditCell(false, false, false, { move : true, cell : newCell });
1825 * Move currently Editing Cell to Down
1827 moveDown: function (e) {
1830 var $thisField = $(g.currentEditCell);
1831 var fieldName = Sql.getFieldName($(g.t), $thisField);
1833 var whereClause = $thisField.parents('tr').first().find('.where_clause').val();
1834 if (typeof whereClause === 'undefined') {
1840 var nextRowFound = false;
1841 $thisField.parents('tr').first().parents('tbody').children().each(function () {
1842 if ($(this).find('.where_clause').val() === whereClause) {
1846 if (j >= 1 && ! nextRowFound) {
1848 nextRowFound = true;
1856 if (found && $nextRow) {
1857 $nextRow.children('td').each(function () {
1858 if (Sql.getFieldName($(g.t), $(this)) === fieldName) {
1865 g.hideEditCell(false, false, false, { move : true, cell : newCell });
1870 * Move currently Editing Cell to Left
1872 moveLeft: function (e) {
1875 var $thisField = $(g.currentEditCell);
1876 var fieldName = Sql.getFieldName($(g.t), $thisField);
1878 var whereClause = $thisField.parents('tr').first().find('.where_clause').val();
1879 if (typeof whereClause === 'undefined') {
1884 $thisField.parents('tr').first().parents('tbody').children().each(function () {
1885 if ($(this).find('.where_clause').val() === whereClause) {
1887 $foundRow = $(this);
1892 var cellFound = false;
1894 $foundRow.children('td.grid_edit').each(function () {
1895 if (Sql.getFieldName($(g.t), $(this)) === fieldName) {
1905 g.hideEditCell(false, false, false, { move : true, cell : leftCell });
1910 * Move currently Editing Cell to Right
1912 moveRight: function (e) {
1915 var $thisField = $(g.currentEditCell);
1916 var fieldName = Sql.getFieldName($(g.t), $thisField);
1918 var whereClause = $thisField.parents('tr').first().find('.where_clause').val();
1919 if (typeof whereClause === 'undefined') {
1925 $thisField.parents('tr').first().parents('tbody').children().each(function () {
1926 if ($(this).find('.where_clause').val() === whereClause) {
1928 $foundRow = $(this);
1933 var cellFound = false;
1934 var nextCellFound = false;
1936 $foundRow.children('td.grid_edit').each(function () {
1937 if (Sql.getFieldName($(g.t), $(this)) === fieldName) {
1941 if (j >= 1 && ! nextCellFound) {
1943 nextCellFound = true;
1952 g.hideEditCell(false, false, false, { move : true, cell : rightCell });
1957 * Initialize grid editing feature.
1959 initGridEdit: function () {
1960 function startGridEditing (e, cell) {
1961 if (g.isCellEditActive) {
1962 g.saveOrPostEditedCell();
1964 g.showEditCell(cell);
1966 e.stopPropagation();
1969 function handleCtrlNavigation (e) {
1970 if ((e.ctrlKey && e.which === 38) || (e.altKey && e.which === 38)) {
1972 } else if ((e.ctrlKey && e.which === 40) || (e.altKey && e.which === 40)) {
1974 } else if ((e.ctrlKey && e.which === 37) || (e.altKey && e.which === 37)) {
1976 } else if ((e.ctrlKey && e.which === 39) || (e.altKey && e.which === 39)) {
1981 // create cell edit wrapper element
1982 g.cEditStd = document.createElement('div');
1983 g.cEdit = g.cEditStd;
1984 g.cEditTextarea = document.createElement('div');
1986 // adjust g.cEditStd
1987 g.cEditStd.className = 'cEdit';
1988 $(g.cEditStd).html('<input class="edit_box" rows="1"><div class="edit_area"></div>');
1989 $(g.cEditStd).hide();
1992 g.cEditTextarea.className = 'cEdit';
1993 $(g.cEditTextarea).html('<textarea class="edit_box" rows="1"></textarea><div class="edit_area"></div>');
1994 $(g.cEditTextarea).hide();
1996 // assign cell editing hint
1997 g.cellEditHint = Messages.strCellEditHint;
1998 g.saveCellWarning = Messages.strSaveCellWarning;
1999 g.alertNonUnique = Messages.strAlertNonUnique;
2000 g.gotoLinkText = Messages.strGoToLink;
2002 // initialize cell editing configuration
2003 g.saveCellsAtOnce = $(g.o).find('.save_cells_at_once').val();
2004 g.maxTruncatedLen = CommonParams.get('LimitChars');
2007 $(g.t).find('td.data.click1')
2008 .on('click', function (e) {
2009 startGridEditing(e, this);
2010 // prevent default action when clicking on "link" in a table
2011 if ($(e.target).is('.grid_edit a')) {
2016 $(g.t).find('td.data.click2')
2017 .on('click', function (e) {
2018 var $cell = $(this);
2019 // In the case of relational link, We want single click on the link
2020 // to goto the link and double click to start grid-editing.
2021 var $link = $(e.target);
2022 if ($link.is('.grid_edit.relation a')) {
2024 // get the click count and increase
2025 var clicks = $cell.data('clicks');
2026 clicks = (typeof clicks === 'undefined') ? 1 : clicks + 1;
2029 // if there are no previous clicks,
2030 // start the single click timer
2031 var timer = setTimeout(function () {
2032 // temporarily remove ajax class so the page loader will not handle it,
2033 // submit and then add it back
2034 $link.removeClass('ajax');
2035 AJAX.requestHandler.call($link[0]);
2036 $link.addClass('ajax');
2037 $cell.data('clicks', 0);
2039 $cell.data('clicks', clicks);
2040 $cell.data('timer', timer);
2041 } else {// When double clicking a link, switch to edit mode
2042 // this is a double click, cancel the single click timer
2043 // and make the click count 0
2044 clearTimeout($cell.data('timer'));
2045 $cell.data('clicks', 0);
2046 // start grid-editing
2047 startGridEditing(e, this);
2049 } else {// If it is not a link or it is a double tap then call startGridEditing
2050 // this is a double click, cancel the single click timer
2051 // and make the click count 0
2052 clearTimeout($cell.data('timer'));
2053 $cell.data('clicks', 0);
2054 // start grid-editing
2055 startGridEditing(e, this);
2058 .on('dblclick', function (e) {
2059 if ($(e.target).is('.grid_edit a')) {
2062 startGridEditing(e, this);
2066 $(g.cEditStd).on('keydown', 'input.edit_box, select', handleCtrlNavigation);
2068 $(g.cEditStd).find('.edit_box').on('focus', function () {
2071 $(g.cEditStd).on('keydown', '.edit_box, select', function (e) {
2072 if (e.which === 13) {
2073 // post on pressing "Enter"
2075 g.saveOrPostEditedCell();
2078 $(g.cEditStd).on('keydown', function (e) {
2079 if (!g.isEditCellTextEditable) {
2080 // prevent text editing
2085 $(g.cEditTextarea).on('keydown', 'textarea.edit_box, select', handleCtrlNavigation);
2087 $(g.cEditTextarea).find('.edit_box').on('focus', function () {
2090 $(g.cEditTextarea).on('keydown', '.edit_box, select', function (e) {
2091 if (e.which === 13 && !e.shiftKey) {
2092 // post on pressing "Enter"
2094 g.saveOrPostEditedCell();
2097 $(g.cEditTextarea).on('keydown', function (e) {
2098 if (!g.isEditCellTextEditable) {
2099 // prevent text editing
2103 $('html').on('click', function (e) {
2104 // hide edit cell if the click is not fromDat edit area
2105 if ($(e.target).parents().index($(g.cEdit)) === -1 &&
2106 !$(e.target).parents('.ui-datepicker-header').length &&
2107 !$('.browse_foreign_modal.ui-dialog:visible').length &&
2108 !$(e.target).closest('.dismissable').length
2112 }).on('keydown', function (e) {
2113 if (e.which === 27 && g.isCellEditActive) {
2114 // cancel on pressing "Esc"
2115 g.hideEditCell(true);
2118 $(g.o).find('div.save_edited').on('click', function () {
2122 $(window).on('beforeunload', function () {
2123 if (g.isCellEdited) {
2124 return g.saveCellWarning;
2128 // attach to global div
2129 $(g.gDiv).append(g.cEditStd);
2130 $(g.gDiv).append(g.cEditTextarea);
2132 // add hint for grid editing feature when hovering "Edit" link in each table row
2133 if (Messages.strGridEditFeatureHint !== undefined) {
2135 $(g.t).find('.edit_row_anchor a'),
2137 Messages.strGridEditFeatureHint
2143 /** ****************
2147 // wrap all truncated data cells with span indicating the original length
2148 // todo update the original length after a grid edit
2149 $(t).find('td.data.truncated:not(:has(span))')
2150 .wrapInner(function () {
2151 return '<span title="' + Messages.strOriginalLength + ' ' +
2152 $(this).data('originallength') + '"></span>';
2155 // wrap remaining cells, except actions cell, with span
2156 $(t).find('th, td:not(:has(span))')
2157 .wrapInner('<span></span>');
2159 // create grid elements
2160 g.gDiv = document.createElement('div'); // create global div
2162 // initialize the table variable
2165 // enclosing .sqlqueryresults div
2166 g.o = $(t).parents('.sqlqueryresults');
2168 // get data columns in the first row of the table
2169 var $firstRowCols = $(t).find('tr:first th.draggable');
2171 // initialize visible headers count
2172 g.visibleHeadersCount = $firstRowCols.filter(':visible').length;
2174 // assign first column (actions) span
2175 if (! $(t).find('tr:first th:first').hasClass('draggable')) { // action header exist
2176 g.actionSpan = $(t).find('tr:first th:first').prop('colspan');
2181 // assign table create time
2182 // table_create_time will only available if we are in "Browse" tab
2183 g.tableCreateTime = $(g.o).find('.table_create_time').val();
2186 g.sortHint = Messages.strSortHint;
2187 g.strMultiSortHint = Messages.strMultiSortHint;
2188 g.markHint = Messages.strColMarkHint;
2189 g.copyHint = Messages.strColNameCopyHint;
2191 // assign common hidden inputs
2192 var $commonHiddenInputs = $(g.o).find('div.common_hidden_inputs');
2193 g.server = $commonHiddenInputs.find('input[name=server]').val();
2194 g.db = $commonHiddenInputs.find('input[name=db]').val();
2195 g.table = $commonHiddenInputs.find('input[name=table]').val();
2198 $(t).addClass('pma_table');
2200 // add relative position to global div so that resize handlers are correctly positioned
2201 $(g.gDiv).css('position', 'relative');
2203 // link the global div
2204 $(t).before(g.gDiv);
2205 $(g.gDiv).append(t);
2208 if (isResizeEnabled) {
2211 // disable reordering for result from EXPLAIN or SHOW syntax, which do not have a table navigation panel
2212 if (isReorderEnabled &&
2213 $(g.o).find('table.navigation').length > 0) {
2216 if (isVisibEnabled) {
2219 // make sure we have the ajax class
2220 if (isGridEditEnabled &&
2225 // create tooltip for each <th> with draggable class
2227 $(t).find('th.draggable'),
2232 // register events for hint tooltip (anchors inside draggable th)
2233 $(t).find('th.draggable a')
2234 .on('mouseenter', function () {
2235 g.showSortHint = true;
2236 g.showMultiSortHint = true;
2237 $(t).find('th.draggable').tooltip('option', {
2238 content: g.updateHint()
2241 .on('mouseleave', function () {
2242 g.showSortHint = false;
2243 g.showMultiSortHint = false;
2244 $(t).find('th.draggable').tooltip('option', {
2245 content: g.updateHint()
2249 // register events for dragging-related feature
2250 if (isResizeEnabled || isReorderEnabled) {
2251 $(document).on('mousemove', function (e) {
2254 $(document).on('mouseup', function (e) {
2255 $(g.o).removeClass('turnOffSelect');
2261 $(t).removeClass('data');
2262 $(g.gDiv).addClass('data');
2266 * jQuery plugin to cancel selection in HTML code.
2269 $.fn.noSelect = function (p) { // no select plugin by Paulo P.Marinas
2270 var prevent = (p === null) ? true : p;
2271 var isMsie = navigator.userAgent.indexOf('MSIE') > -1 || !!window.navigator.userAgent.match(/Trident.*rv:11\./);
2272 var isFirefox = navigator.userAgent.indexOf('Firefox') > -1;
2273 var isSafari = navigator.userAgent.indexOf('Safari') > -1;
2274 var isOpera = navigator.userAgent.indexOf('Presto') > -1;
2276 return this.each(function () {
2277 if (isMsie || isSafari) {
2278 $(this).on('selectstart', false);
2279 } else if (isFirefox) {
2280 $(this).css('MozUserSelect', 'none');
2281 $('body').trigger('focus');
2282 } else if (isOpera) {
2283 $(this).on('mousedown', false);
2285 $(this).attr('unselectable', 'on');
2289 return this.each(function () {
2290 if (isMsie || isSafari) {
2291 $(this).off('selectstart');
2292 } else if (isFirefox) {
2293 $(this).css('MozUserSelect', 'inherit');
2294 } else if (isOpera) {
2295 $(this).off('mousedown');
2297 $(this).removeAttr('unselectable');