forked from phpmyadmin/phpmyadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakegrid.js
1840 lines (1675 loc) · 76.8 KB
/
makegrid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Create advanced table (resize, reorder, and show/hide columns; and also grid editing).
* This function is designed mainly for table DOM generated from browsing a table in the database.
* For using this function in other table DOM, you may need to:
* - add "draggable" class in the table header <th>, in order to make it resizable, sortable or hidable
* - have at least one non-"draggable" header in the table DOM for placing column visibility drop-down arrow
* - pass the value "false" for the parameter "enableGridEdit"
* - adjust other parameter value, to select which features that will be enabled
*
* @param t the table DOM element
* @param enableResize Optional, if false, column resizing feature will be disabled
* @param enableReorder Optional, if false, column reordering feature will be disabled
* @param enableVisib Optional, if false, show/hide column feature will be disabled
* @param enableGridEdit Optional, if false, grid editing feature will be disabled
*/
function PMA_makegrid(t, enableResize, enableReorder, enableVisib, enableGridEdit) {
var g = {
/***********
* Constant
***********/
minColWidth: 15,
/***********
* Variables, assigned with default value, changed later
***********/
actionSpan: 5, // number of colspan in Actions header in a table
tableCreateTime: null, // table creation time, used for saving column order and visibility to server, only available in "Browse tab"
// Column reordering variables
colOrder: [], // array of column order
// Column visibility variables
colVisib: [], // array of column visibility
showAllColText: '', // string, text for "show all" button under column visibility list
visibleHeadersCount: 0, // number of visible data headers
// Table hint variables
reorderHint: '', // string, hint for column reordering
sortHint: '', // string, hint for column sorting
markHint: '', // string, hint for column marking
copyHint: '', // string, hint for copy column name
showReorderHint: false,
showSortHint: false,
showMarkHint: false,
// Grid editing
isCellEditActive: false, // true if current focus is in edit cell
isEditCellTextEditable: false, // true if current edit cell is editable in the text input box (not textarea)
currentEditCell: null, // reference to <td> that currently being edited
cellEditHint: '', // hint shown when doing grid edit
gotoLinkText: '', // "Go to link" text
wasEditedCellNull: false, // true if last value of the edited cell was NULL
maxTruncatedLen: 0, // number of characters that can be displayed in a cell
saveCellsAtOnce: false, // $cfg[saveCellsAtOnce]
isCellEdited: false, // true if at least one cell has been edited
saveCellWarning: '', // string, warning text when user want to leave a page with unsaved edited data
lastXHR : null, // last XHR object used in AJAX request
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
alertNonUnique: '', // string, alert shown when saving edited nonunique table
// Common hidden inputs
token: null,
server: null,
db: null,
table: null,
/************
* Functions
************/
/**
* Start to resize column. Called when clicking on column separator.
*
* @param e event
* @param obj dragged div object
*/
dragStartRsz: function (e, obj) {
var n = $(g.cRsz).find('div').index(obj); // get the index of separator (i.e., column index)
$(obj).addClass('colborder_active');
g.colRsz = {
x0: e.pageX,
n: n,
obj: obj,
objLeft: $(obj).position().left,
objWidth: $(g.t).find('th.draggable:visible:eq(' + n + ') span').outerWidth()
};
$(document.body).css('cursor', 'col-resize').noSelect();
if (g.isCellEditActive) {
g.hideEditCell();
}
},
/**
* Start to reorder column. Called when clicking on table header.
*
* @param e event
* @param obj table header object
*/
dragStartReorder: function (e, obj) {
// prepare the cCpy (column copy) and cPointer (column pointer) from the dragged column
$(g.cCpy).text($(obj).text());
var objPos = $(obj).position();
$(g.cCpy).css({
top: objPos.top + 20,
left: objPos.left,
height: $(obj).height(),
width: $(obj).width()
});
$(g.cPointer).css({
top: objPos.top
});
// get the column index, zero-based
var n = g.getHeaderIdx(obj);
g.colReorder = {
x0: e.pageX,
y0: e.pageY,
n: n,
newn: n,
obj: obj,
objTop: objPos.top,
objLeft: objPos.left
};
$(document.body).css('cursor', 'move').noSelect();
if (g.isCellEditActive) {
g.hideEditCell();
}
},
/**
* Handle mousemove event when dragging.
*
* @param e event
*/
dragMove: function (e) {
if (g.colRsz) {
var dx = e.pageX - g.colRsz.x0;
if (g.colRsz.objWidth + dx > g.minColWidth) {
$(g.colRsz.obj).css('left', g.colRsz.objLeft + dx + 'px');
}
} else if (g.colReorder) {
// dragged column animation
var dx = e.pageX - g.colReorder.x0;
$(g.cCpy)
.css('left', g.colReorder.objLeft + dx)
.show();
// pointer animation
var hoveredCol = g.getHoveredCol(e);
if (hoveredCol) {
var newn = g.getHeaderIdx(hoveredCol);
g.colReorder.newn = newn;
if (newn != g.colReorder.n) {
// show the column pointer in the right place
var colPos = $(hoveredCol).position();
var newleft = newn < g.colReorder.n ?
colPos.left :
colPos.left + $(hoveredCol).outerWidth();
$(g.cPointer)
.css({
left: newleft,
visibility: 'visible'
});
} else {
// no movement to other column, hide the column pointer
$(g.cPointer).css('visibility', 'hidden');
}
}
}
},
/**
* Stop the dragging action.
*
* @param e event
*/
dragEnd: function (e) {
if (g.colRsz) {
var dx = e.pageX - g.colRsz.x0;
var nw = g.colRsz.objWidth + dx;
if (nw < g.minColWidth) {
nw = g.minColWidth;
}
var n = g.colRsz.n;
// do the resizing
g.resize(n, nw);
g.reposRsz();
g.reposDrop();
g.colRsz = false;
$(g.cRsz).find('div').removeClass('colborder_active');
} else if (g.colReorder) {
// shift columns
if (g.colReorder.newn != g.colReorder.n) {
g.shiftCol(g.colReorder.n, g.colReorder.newn);
// assign new position
var objPos = $(g.colReorder.obj).position();
g.colReorder.objTop = objPos.top;
g.colReorder.objLeft = objPos.left;
g.colReorder.n = g.colReorder.newn;
// send request to server to remember the column order
if (g.tableCreateTime) {
g.sendColPrefs();
}
g.refreshRestoreButton();
}
// animate new column position
$(g.cCpy).stop(true, true)
.animate({
top: g.colReorder.objTop,
left: g.colReorder.objLeft
}, 'fast')
.fadeOut();
$(g.cPointer).css('visibility', 'hidden');
g.colReorder = false;
}
$(document.body).css('cursor', 'inherit').noSelect(false);
},
/**
* Resize column n to new width "nw"
*
* @param n zero-based column index
* @param nw new width of the column in pixel
*/
resize: function (n, nw) {
$(g.t).find('tr').each(function () {
$(this).find('th.draggable:visible:eq(' + n + ') span,' +
'td:visible:eq(' + (g.actionSpan + n) + ') span')
.css('width', nw);
});
},
/**
* Reposition column resize bars.
*/
reposRsz: function () {
$(g.cRsz).find('div').hide();
var $firstRowCols = $(g.t).find('tr:first th.draggable:visible');
var $resizeHandles = $(g.cRsz).find('div').removeClass('condition');
$('table.pma_table').find('thead th:first').removeClass('before-condition');
for (var n = 0, l = $firstRowCols.length; n < l; n++) {
var $col = $($firstRowCols[n]);
$($resizeHandles[n]).css('left', $col.position().left + $col.outerWidth(true))
.show();
if ($col.hasClass('condition')) {
$($resizeHandles[n]).addClass('condition');
if (n > 0) {
$($resizeHandles[n - 1]).addClass('condition');
}
}
}
if ($($resizeHandles[0]).hasClass('condition')) {
$('table.pma_table').find('thead th:first').addClass('before-condition');
}
$(g.cRsz).css('height', $(g.t).height());
},
/**
* Shift column from index oldn to newn.
*
* @param oldn old zero-based column index
* @param newn new zero-based column index
*/
shiftCol: function (oldn, newn) {
$(g.t).find('tr').each(function () {
if (newn < oldn) {
$(this).find('th.draggable:eq(' + newn + '),' +
'td:eq(' + (g.actionSpan + newn) + ')')
.before($(this).find('th.draggable:eq(' + oldn + '),' +
'td:eq(' + (g.actionSpan + oldn) + ')'));
} else {
$(this).find('th.draggable:eq(' + newn + '),' +
'td:eq(' + (g.actionSpan + newn) + ')')
.after($(this).find('th.draggable:eq(' + oldn + '),' +
'td:eq(' + (g.actionSpan + oldn) + ')'));
}
});
// reposition the column resize bars
g.reposRsz();
// adjust the column visibility list
if (newn < oldn) {
$(g.cList).find('.lDiv div:eq(' + newn + ')')
.before($(g.cList).find('.lDiv div:eq(' + oldn + ')'));
} else {
$(g.cList).find('.lDiv div:eq(' + newn + ')')
.after($(g.cList).find('.lDiv div:eq(' + oldn + ')'));
}
// adjust the colOrder
var tmp = g.colOrder[oldn];
g.colOrder.splice(oldn, 1);
g.colOrder.splice(newn, 0, tmp);
// adjust the colVisib
if (g.colVisib.length > 0) {
tmp = g.colVisib[oldn];
g.colVisib.splice(oldn, 1);
g.colVisib.splice(newn, 0, tmp);
}
},
/**
* Find currently hovered table column's header (excluding actions column).
*
* @param e event
* @return the hovered column's th object or undefined if no hovered column found.
*/
getHoveredCol: function (e) {
var hoveredCol;
$headers = $(g.t).find('th.draggable:visible');
$headers.each(function () {
var left = $(this).offset().left;
var right = left + $(this).outerWidth();
if (left <= e.pageX && e.pageX <= right) {
hoveredCol = this;
}
});
return hoveredCol;
},
/**
* Get a zero-based index from a <th class="draggable"> tag in a table.
*
* @param obj table header <th> object
* @return zero-based index of the specified table header in the set of table headers (visible or not)
*/
getHeaderIdx: function (obj) {
return $(obj).parents('tr').find('th.draggable').index(obj);
},
/**
* Reposition the columns back to normal order.
*/
restoreColOrder: function () {
// use insertion sort, since we already have shiftCol function
for (var i = 1; i < g.colOrder.length; i++) {
var x = g.colOrder[i];
var j = i - 1;
while (j >= 0 && x < g.colOrder[j]) {
j--;
}
if (j != i - 1) {
g.shiftCol(i, j + 1);
}
}
if (g.tableCreateTime) {
// send request to server to remember the column order
g.sendColPrefs();
}
g.refreshRestoreButton();
},
/**
* Send column preferences (column order and visibility) to the server.
*/
sendColPrefs: function () {
if ($(g.t).is('.ajax')) { // only send preferences if ajax class
var post_params = {
ajax_request: true,
db: g.db,
table: g.table,
token: g.token,
server: g.server,
set_col_prefs: true,
table_create_time: g.tableCreateTime
};
if (g.colOrder.length > 0) {
$.extend(post_params, {col_order: g.colOrder.toString()});
}
if (g.colVisib.length > 0) {
$.extend(post_params, {col_visib: g.colVisib.toString()});
}
$.post('sql.php', post_params, function (data) {
if (data.success !== true) {
var $temp_div = $(document.createElement('div'));
$temp_div.html(data.error);
$temp_div.addClass("error");
PMA_ajaxShowMessage($temp_div, false);
}
});
}
},
/**
* Refresh restore button state.
* Make restore button disabled if the table is similar with initial state.
*/
refreshRestoreButton: function () {
// check if table state is as initial state
var isInitial = true;
for (var i = 0; i < g.colOrder.length; i++) {
if (g.colOrder[i] != i) {
isInitial = false;
break;
}
}
// check if only one visible column left
var isOneColumn = g.visibleHeadersCount == 1;
// enable or disable restore button
if (isInitial || isOneColumn) {
$('div.restore_column').hide();
} else {
$('div.restore_column').show();
}
},
/**
* Update current hint using the boolean values (showReorderHint, showSortHint, etc.).
*
*/
updateHint: function () {
var text = '';
if (!g.colRsz && !g.colReorder) { // if not resizing or dragging
if (g.visibleHeadersCount > 1) {
g.showReorderHint = true;
}
if ($(t).find('th.marker').length > 0) {
g.showMarkHint = true;
}
if (g.showReorderHint && g.reorderHint) {
text += g.reorderHint;
}
if (g.showSortHint && g.sortHint) {
text += text.length > 0 ? '<br />' : '';
text += g.sortHint;
}
if (g.showMarkHint && g.markHint &&
!g.showSortHint // we do not show mark hint, when sort hint is shown
) {
text += text.length > 0 ? '<br />' : '';
text += g.markHint;
text += text.length > 0 ? '<br />' : '';
text += g.copyHint;
}
}
return text;
},
/**
* Toggle column's visibility.
* After calling this function and it returns true, afterToggleCol() must be called.
*
* @return boolean True if the column is toggled successfully.
*/
toggleCol: function (n) {
if (g.colVisib[n]) {
// can hide if more than one column is visible
if (g.visibleHeadersCount > 1) {
$(g.t).find('tr').each(function () {
$(this).find('th.draggable:eq(' + n + '),' +
'td:eq(' + (g.actionSpan + n) + ')')
.hide();
});
g.colVisib[n] = 0;
$(g.cList).find('.lDiv div:eq(' + n + ') input').prop('checked', false);
} else {
// cannot hide, force the checkbox to stay checked
$(g.cList).find('.lDiv div:eq(' + n + ') input').prop('checked', true);
return false;
}
} else { // column n is not visible
$(g.t).find('tr').each(function () {
$(this).find('th.draggable:eq(' + n + '),' +
'td:eq(' + (g.actionSpan + n) + ')')
.show();
});
g.colVisib[n] = 1;
$(g.cList).find('.lDiv div:eq(' + n + ') input').prop('checked', true);
}
return true;
},
/**
* This must be called if toggleCol() returns is true.
*
* This function is separated from toggleCol because, sometimes, we want to toggle
* some columns together at one time and do just one adjustment after it, e.g. in showAllColumns().
*/
afterToggleCol: function () {
// some adjustments after hiding column
g.reposRsz();
g.reposDrop();
g.sendColPrefs();
// check visible first row headers count
g.visibleHeadersCount = $(g.t).find('tr:first th.draggable:visible').length;
g.refreshRestoreButton();
},
/**
* Show columns' visibility list.
*
* @param obj The drop down arrow of column visibility list
*/
showColList: function (obj) {
// only show when not resizing or reordering
if (!g.colRsz && !g.colReorder) {
var pos = $(obj).position();
// check if the list position is too right
if (pos.left + $(g.cList).outerWidth(true) > $(document).width()) {
pos.left = $(document).width() - $(g.cList).outerWidth(true);
}
$(g.cList).css({
left: pos.left,
top: pos.top + $(obj).outerHeight(true)
})
.show();
$(obj).addClass('coldrop-hover');
}
},
/**
* Hide columns' visibility list.
*/
hideColList: function () {
$(g.cList).hide();
$(g.cDrop).find('.coldrop-hover').removeClass('coldrop-hover');
},
/**
* Reposition the column visibility drop-down arrow.
*/
reposDrop: function () {
var $th = $(t).find('th:not(.draggable)');
for (var i = 0; i < $th.length; i++) {
var $cd = $(g.cDrop).find('div:eq(' + i + ')'); // column drop-down arrow
var pos = $($th[i]).position();
$cd.css({
left: pos.left + $($th[i]).width() - $cd.width(),
top: pos.top
});
}
},
/**
* Show all hidden columns.
*/
showAllColumns: function () {
for (var i = 0; i < g.colVisib.length; i++) {
if (!g.colVisib[i]) {
g.toggleCol(i);
}
}
g.afterToggleCol();
},
/**
* Show edit cell, if it can be shown
*
* @param cell <td> element to be edited
*/
showEditCell: function (cell) {
if ($(cell).is('.grid_edit') &&
!g.colRsz && !g.colReorder)
{
if (!g.isCellEditActive) {
var $cell = $(cell);
// remove all edit area and hide it
$(g.cEdit).find('.edit_area').empty().hide();
// reposition the cEdit element
$(g.cEdit).css({
top: $cell.position().top,
left: $cell.position().left
})
.show()
.find('.edit_box')
.css({
width: $cell.outerWidth(),
height: $cell.outerHeight()
});
// fill the cell edit with text from <td>
var value = PMA_getCellValue(cell);
$(g.cEdit).find('.edit_box').val(value);
g.currentEditCell = cell;
$(g.cEdit).find('.edit_box').focus();
$(g.cEdit).find('*').removeProp('disabled');
}
}
},
/**
* Remove edit cell and the edit area, if it is shown.
*
* @param force Optional, force to hide edit cell without saving edited field.
* @param data Optional, data from the POST AJAX request to save the edited field
* or just specify "true", if we want to replace the edited field with the new value.
* @param field Optional, the edited <td>. If not specified, the function will
* use currently edited <td> from g.currentEditCell.
*/
hideEditCell: function (force, data, field) {
if (g.isCellEditActive && !force) {
// cell is being edited, save or post the edited data
g.saveOrPostEditedCell();
return;
}
// cancel any previous request
if (g.lastXHR !== null) {
g.lastXHR.abort();
g.lastXHR = null;
}
if (data) {
if (g.currentEditCell) { // save value of currently edited cell
// replace current edited field with the new value
var $this_field = $(g.currentEditCell);
var is_null = $this_field.data('value') === null;
if (is_null) {
$this_field.find('span').html('NULL');
$this_field.addClass('null');
} else {
$this_field.removeClass('null');
var new_html = data.isNeedToRecheck
? data.truncatableFieldValue
: $this_field.data('value');
if ($this_field.is('.truncated')) {
if (new_html.length > g.maxTruncatedLen) {
new_html = new_html.substring(0, g.maxTruncatedLen) + '...';
}
}
$this_field.find('span').text(new_html);
}
}
if (data.transformations !== undefined) {
$.each(data.transformations, function (cell_index, value) {
var $this_field = $(g.t).find('.to_be_saved:eq(' + cell_index + ')');
$this_field.find('span').html(value);
});
}
if (data.relations !== undefined) {
$.each(data.relations, function (cell_index, value) {
var $this_field = $(g.t).find('.to_be_saved:eq(' + cell_index + ')');
$this_field.find('span').html(value);
});
}
// refresh the grid
g.reposRsz();
g.reposDrop();
}
// hide the cell editing area
$(g.cEdit).hide();
$(g.cEdit).find('.edit_box').blur();
g.isCellEditActive = false;
g.currentEditCell = null;
// destroy datepicker in edit area, if exist
var $dp = $(g.cEdit).find('.hasDatepicker');
if ($dp.length > 0) {
$dp.datepicker('destroy');
// change the cursor in edit box back to normal
// (the cursor become a hand pointer when we add datepicker)
$(g.cEdit).find('.edit_box').css('cursor', 'inherit');
}
},
/**
* Show drop-down edit area when edit cell is focused.
*/
showEditArea: function () {
if (!g.isCellEditActive) { // make sure the edit area has not been shown
g.isCellEditActive = true;
g.isEditCellTextEditable = false;
/**
* @var $td current edited cell
*/
var $td = $(g.currentEditCell);
/**
* @var $editArea the editing area
*/
var $editArea = $(g.cEdit).find('.edit_area');
/**
* @var where_clause WHERE clause for the edited cell
*/
var where_clause = $td.parent('tr').find('.where_clause').val();
/**
* @var field_name String containing the name of this field.
* @see getFieldName()
*/
var field_name = getFieldName($td);
/**
* @var relation_curr_value String current value of the field (for fields that are foreign keyed).
*/
var relation_curr_value = $td.text();
/**
* @var relation_key_or_display_column String relational key if in 'Relational display column' mode,
* relational display column if in 'Relational key' mode (for fields that are foreign keyed).
*/
var relation_key_or_display_column = $td.find('a').attr('title');
/**
* @var curr_value String current value of the field (for fields that are of type enum or set).
*/
var curr_value = $td.find('span').text();
// empty all edit area, then rebuild it based on $td classes
$editArea.empty();
// add show data row link if the data resulted by 'browse distinct values' in table structure
if ($td.find('input').hasClass('data_browse_link')) {
var showDataRowLink = document.createElement('div');
showDataRowLink.className = 'goto_link';
$(showDataRowLink).append("<a href='" + $td.find('.data_browse_link').val() + "'>" + g.showDataRowLinkText + "</a>");
$editArea.append(showDataRowLink);
}
// add goto link, if this cell contains a link
if ($td.find('a').length > 0) {
var gotoLink = document.createElement('div');
gotoLink.className = 'goto_link';
$(gotoLink).append(g.gotoLinkText + ': ').append($td.find('a').clone());
$editArea.append(gotoLink);
}
g.wasEditedCellNull = false;
if ($td.is(':not(.not_null)')) {
// append a null checkbox
$editArea.append('<div class="null_div">Null :<input type="checkbox"></div>');
var $checkbox = $editArea.find('.null_div input');
// check if current <td> is NULL
if ($td.is('.null')) {
$checkbox.prop('checked', true);
g.wasEditedCellNull = true;
}
// if the select/editor is changed un-check the 'checkbox_null_<field_name>_<row_index>'.
if ($td.is('.enum, .set')) {
$editArea.find('select').live('change', function (e) {
$checkbox.prop('checked', false);
});
} else if ($td.is('.relation')) {
$editArea.find('select').live('change', function (e) {
$checkbox.prop('checked', false);
});
$editArea.find('.browse_foreign').live('click', function (e) {
$checkbox.prop('checked', false);
});
} else {
$(g.cEdit).find('.edit_box').live('keypress change', function (e) {
$checkbox.prop('checked', false);
});
// Capture ctrl+v (on IE and Chrome)
$(g.cEdit).find('.edit_box').live('keydown', function (e) {
if (e.ctrlKey && e.which == 86) {
$checkbox.prop('checked', false);
}
});
$editArea.find('textarea').live('keydown', function (e) {
$checkbox.prop('checked', false);
});
}
// if null checkbox is clicked empty the corresponding select/editor.
$checkbox.click(function (e) {
if ($td.is('.enum')) {
$editArea.find('select').val('');
} else if ($td.is('.set')) {
$editArea.find('select').find('option').each(function () {
var $option = $(this);
$option.prop('selected', false);
});
} else if ($td.is('.relation')) {
// if the dropdown is there to select the foreign value
if ($editArea.find('select').length > 0) {
$editArea.find('select').val('');
}
} else {
$editArea.find('textarea').val('');
}
$(g.cEdit).find('.edit_box').val('');
});
}
if ($td.is('.relation')) {
//handle relations
$editArea.addClass('edit_area_loading');
// initialize the original data
$td.data('original_data', null);
/**
* @var post_params Object containing parameters for the POST request
*/
var post_params = {
'ajax_request' : true,
'get_relational_values' : true,
'server' : g.server,
'db' : g.db,
'table' : g.table,
'column' : field_name,
'token' : g.token,
'curr_value' : relation_curr_value,
'relation_key_or_display_column' : relation_key_or_display_column
};
g.lastXHR = $.post('sql.php', post_params, function (data) {
g.lastXHR = null;
$editArea.removeClass('edit_area_loading');
if ($(data.dropdown).is('select')) {
// save original_data
var value = $(data.dropdown).val();
$td.data('original_data', value);
// update the text input field, in case where the "Relational display column" is checked
$(g.cEdit).find('.edit_box').val(value);
}
$editArea.append(data.dropdown);
$editArea.append('<div class="cell_edit_hint">' + g.cellEditHint + '</div>');
// for 'Browse foreign values' options,
// hide the value next to 'Browse foreign values' link
$editArea.find('span.curr_value').hide();
// handle update for new values selected from new window
$editArea.find('span.curr_value').change(function () {
$(g.cEdit).find('.edit_box').val($(this).text());
});
}); // end $.post()
$editArea.show();
$editArea.find('select').live('change', function (e) {
$(g.cEdit).find('.edit_box').val($(this).val());
});
g.isEditCellTextEditable = true;
}
else if ($td.is('.enum')) {
//handle enum fields
$editArea.addClass('edit_area_loading');
/**
* @var post_params Object containing parameters for the POST request
*/
var post_params = {
'ajax_request' : true,
'get_enum_values' : true,
'server' : g.server,
'db' : g.db,
'table' : g.table,
'column' : field_name,
'token' : g.token,
'curr_value' : curr_value
};
g.lastXHR = $.post('sql.php', post_params, function (data) {
g.lastXHR = null;
$editArea.removeClass('edit_area_loading');
$editArea.append(data.dropdown);
$editArea.append('<div class="cell_edit_hint">' + g.cellEditHint + '</div>');
}); // end $.post()
$editArea.show();
$editArea.find('select').live('change', function (e) {
$(g.cEdit).find('.edit_box').val($(this).val());
});
}
else if ($td.is('.set')) {
//handle set fields
$editArea.addClass('edit_area_loading');
/**
* @var post_params Object containing parameters for the POST request
*/
var post_params = {
'ajax_request' : true,
'get_set_values' : true,
'server' : g.server,
'db' : g.db,
'table' : g.table,
'column' : field_name,
'token' : g.token,
'curr_value' : curr_value
};
g.lastXHR = $.post('sql.php', post_params, function (data) {
g.lastXHR = null;
$editArea.removeClass('edit_area_loading');
$editArea.append(data.select);
$editArea.append('<div class="cell_edit_hint">' + g.cellEditHint + '</div>');
}); // end $.post()
$editArea.show();
$editArea.find('select').live('change', function (e) {
$(g.cEdit).find('.edit_box').val($(this).val());
});
}
else if ($td.is('.truncated, .transformed')) {
if ($td.is('.to_be_saved')) { // cell has been edited
var value = $td.data('value');
$(g.cEdit).find('.edit_box').val(value);
$editArea.append('<textarea></textarea>');
$editArea.find('textarea')
.val(value)
.live('keyup', function (e) {
$(g.cEdit).find('.edit_box').val($(this).val());
});
$(g.cEdit).find('.edit_box').live('keyup', function (e) {
$editArea.find('textarea').val($(this).val());
});
$editArea.append('<div class="cell_edit_hint">' + g.cellEditHint + '</div>');
} else {
//handle truncated/transformed values values
$editArea.addClass('edit_area_loading');
// initialize the original data
$td.data('original_data', null);
/**
* @var sql_query String containing the SQL query used to retrieve value of truncated/transformed data
*/
var sql_query = 'SELECT `' + field_name + '` FROM `' + g.table + '` WHERE ' + PMA_urldecode(where_clause);
// Make the Ajax call and get the data, wrap it and insert it
g.lastXHR = $.post('sql.php', {
'token' : g.token,
'server' : g.server,
'db' : g.db,
'ajax_request' : true,
'sql_query' : sql_query,
'grid_edit' : true
}, function (data) {
g.lastXHR = null;
$editArea.removeClass('edit_area_loading');
if (data.success === true) {
if ($td.is('.truncated')) {
// get the truncated data length
g.maxTruncatedLen = $(g.currentEditCell).text().length - 3;
}
$td.data('original_data', data.value);
$(g.cEdit).find('.edit_box').val(data.value);
$editArea.append('<textarea></textarea>');
$editArea.find('textarea')
.val(data.value)
.live('keyup', function (e) {
$(g.cEdit).find('.edit_box').val($(this).val());
});
$(g.cEdit).find('.edit_box').live('keyup', function (e) {
$editArea.find('textarea').val($(this).val());
});
$editArea.append('<div class="cell_edit_hint">' + g.cellEditHint + '</div>');
} else {
PMA_ajaxShowMessage(data.error, false);
}
}); // end $.post()
$editArea.show();
}
g.isEditCellTextEditable = true;
} else if ($td.is('.datefield, .datetimefield, .timestampfield')) {
var $input_field = $(g.cEdit).find('.edit_box');
// remember current datetime value in $input_field, if it is not null
var is_null = $td.is('.null');
var current_datetime_value = !is_null ? $input_field.val() : '';
var showTimeOption = true;
if ($td.is('.datefield')) {
showTimeOption = false;
}
PMA_addDatepicker($editArea, {
altField: $input_field,
showTimepicker: showTimeOption,
onSelect: function (dateText, inst) {
// remove null checkbox if it exists
$(g.cEdit).find('.null_div input[type=checkbox]').prop('checked', false);
}
});
// cancel any click on the datepicker element
$editArea.find('> *').click(function (e) {
e.stopPropagation();
});
// force to restore modified $input_field value after adding datepicker
// (after adding a datepicker, the input field doesn't display the time anymore, only the date)
if (is_null
|| current_datetime_value == '0000-00-00'
|| current_datetime_value == '0000-00-00 00:00:00.000000'
) {
$input_field.val(current_datetime_value);
} else {
var date;
if (current_datetime_value.match(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{6}$/)) {
date = new Date(current_datetime_value.substring(0, 10));
var hour = current_datetime_value.substring(11, 13);
var min = current_datetime_value.substring(14, 16);
var sec = current_datetime_value.substring(17, 19);
var milli = current_datetime_value.substring(20, 23);
var micro = current_datetime_value.substring(23);
date.setHours(hour, min, sec, milli);
date.setMicroseconds(micro);
} else {
date = new Date(current_datetime_value);
}
$editArea.datetimepicker('setDate', date);