forked from phpmyadmin/phpmyadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
4346 lines (4071 loc) · 140 KB
/
functions.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: */
/**
* general function, usually for data manipulation pages
*
*/
/**
* @var $table_clone reference to the action links on the tbl_structure page
*/
var $table_clone = false;
/**
* @var sql_box_locked lock for the sqlbox textarea in the querybox
*/
var sql_box_locked = false;
/**
* @var array holds elements which content should only selected once
*/
var only_once_elements = [];
/**
* @var int ajax_message_count Number of AJAX messages shown since page load
*/
var ajax_message_count = 0;
/**
* @var codemirror_editor object containing CodeMirror editor of the query editor in SQL tab
*/
var codemirror_editor = false;
/**
* @var codemirror_editor object containing CodeMirror editor of the inline query editor
*/
var codemirror_inline_editor = false;
/**
* @var sql_autocomplete object containing list of columns in each table
*/
var sql_autocomplete = false;
/**
* @var sql_autocomplete_default_table string containing default table to autocomplete columns
*/
var sql_autocomplete_default_table = '';
/**
* @var chart_activeTimeouts object active timeouts that refresh the charts. When disabling a realtime chart, this can be used to stop the continuous ajax requests
*/
var chart_activeTimeouts = {};
/**
* @var central_column_list array to hold the columns in central list per db.
*/
var central_column_list = [];
/**
* @var primary_indexes array to hold 'Primary' index columns.
*/
var primary_indexes = [];
/**
* @var unique_indexes array to hold 'Unique' index columns.
*/
var unique_indexes = [];
/**
* @var indexes array to hold 'Index' columns.
*/
var indexes = [];
/**
* @var fulltext_indexes array to hold 'Fulltext' columns.
*/
var fulltext_indexes = [];
/**
* Make sure that ajax requests will not be cached
* by appending a random variable to their parameters
*/
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
var nocache = new Date().getTime() + "" + Math.floor(Math.random() * 1000000);
if (typeof options.data == "string") {
options.data += "&_nocache=" + nocache;
} else if (typeof options.data == "object") {
options.data = $.extend(originalOptions.data, {'_nocache' : nocache});
}
});
/**
* Clear text selection
*/
function PMA_clearSelection() {
if (document.selection && document.selection.empty) {
document.selection.empty();
} else if (window.getSelection) {
var sel = window.getSelection();
if (sel.empty) {
sel.empty();
}
if (sel.removeAllRanges) {
sel.removeAllRanges();
}
}
}
/**
* Create a jQuery UI tooltip
*
* @param $elements jQuery object representing the elements
* @param item the item
* (see http://api.jqueryui.com/tooltip/#option-items)
* @param myContent content of the tooltip
* @param additionalOptions to override the default options
*
*/
function PMA_tooltip($elements, item, myContent, additionalOptions)
{
if ($('#no_hint').length > 0) {
return;
}
var defaultOptions = {
content: myContent,
items: item,
tooltipClass: "tooltip",
track: true,
show: false,
hide: false
};
$elements.tooltip($.extend(true, defaultOptions, additionalOptions));
}
/**
* HTML escaping
*/
function escapeHtml(unsafe) {
if (typeof(unsafe) != 'undefined') {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
} else {
return false;
}
}
function PMA_sprintf() {
return sprintf.apply(this, arguments);
}
/**
* Hides/shows the default value input field, depending on the default type
* Ticks the NULL checkbox if NULL is chosen as default value.
*/
function PMA_hideShowDefaultValue($default_type)
{
if ($default_type.val() == 'USER_DEFINED') {
$default_type.siblings('.default_value').show().focus();
} else {
$default_type.siblings('.default_value').hide();
if ($default_type.val() == 'NULL') {
var $null_checkbox = $default_type.closest('tr').find('.allow_null');
$null_checkbox.prop('checked', true);
}
}
}
/**
* Show notices for ENUM columns; add/hide the default value
*
*/
function PMA_verifyColumnsProperties()
{
$("select.column_type").each(function () {
PMA_showNoticeForEnum($(this));
});
$("select.default_type").each(function () {
PMA_hideShowDefaultValue($(this));
});
}
/**
* Add a hidden field to the form to indicate that this will be an
* Ajax request (only if this hidden field does not exist)
*
* @param $form object the form
*/
function PMA_prepareForAjaxRequest($form)
{
if (! $form.find('input:hidden').is('#ajax_request_hidden')) {
$form.append('<input type="hidden" id="ajax_request_hidden" name="ajax_request" value="true" />');
}
}
/**
* Generate a new password and copy it to the password input areas
*
* @param passwd_form object the form that holds the password fields
*
* @return boolean always true
*/
function suggestPassword(passwd_form)
{
// restrict the password to just letters and numbers to avoid problems:
// "editors and viewers regard the password as multiple words and
// things like double click no longer work"
var pwchars = "abcdefhjmnpqrstuvwxyz23456789ABCDEFGHJKLMNPQRSTUVWYXZ";
var passwordlength = 16; // do we want that to be dynamic? no, keep it simple :)
var passwd = passwd_form.generated_pw;
passwd.value = '';
for (var i = 0; i < passwordlength; i++) {
passwd.value += pwchars.charAt(Math.floor(Math.random() * pwchars.length));
}
passwd_form.text_pma_pw.value = passwd.value;
passwd_form.text_pma_pw2.value = passwd.value;
return true;
}
/**
* Version string to integer conversion.
*/
function parseVersionString(str)
{
if (typeof(str) != 'string') { return false; }
var add = 0;
// Parse possible alpha/beta/rc/
var state = str.split('-');
if (state.length >= 2) {
if (state[1].substr(0, 2) == 'rc') {
add = - 20 - parseInt(state[1].substr(2), 10);
} else if (state[1].substr(0, 4) == 'beta') {
add = - 40 - parseInt(state[1].substr(4), 10);
} else if (state[1].substr(0, 5) == 'alpha') {
add = - 60 - parseInt(state[1].substr(5), 10);
} else if (state[1].substr(0, 3) == 'dev') {
/* We don't handle dev, it's git snapshot */
add = 0;
}
}
// Parse version
var x = str.split('.');
// Use 0 for non existing parts
var maj = parseInt(x[0], 10) || 0;
var min = parseInt(x[1], 10) || 0;
var pat = parseInt(x[2], 10) || 0;
var hotfix = parseInt(x[3], 10) || 0;
return maj * 100000000 + min * 1000000 + pat * 10000 + hotfix * 100 + add;
}
/**
* Indicates current available version on main page.
*/
function PMA_current_version(data)
{
if (data && data.version && data.date) {
var current = parseVersionString(pmaversion);
var latest = parseVersionString(data.version);
var version_information_message = '<span>'+
PMA_messages.strLatestAvailable +
' ' + escapeHtml(data.version) +
'</span>';
if (latest > current) {
var message = PMA_sprintf(
PMA_messages.strNewerVersion,
escapeHtml(data.version),
escapeHtml(data.date)
);
var htmlClass = 'notice';
if (Math.floor(latest / 10000) === Math.floor(current / 10000)) {
/* Security update */
htmlClass = 'error';
}
$('#maincontainer').after('<div class="' + htmlClass + '">' + message + '</div>');
}
if (latest === current) {
version_information_message = ' (' + PMA_messages.strUpToDate + ')';
}
$('#li_pma_version span').remove();
$('#li_pma_version').append(version_information_message);
}
}
/**
* Loads Git revision data from ajax for index.php
*/
function PMA_display_git_revision()
{
$('#is_git_revision').remove();
$('#li_pma_version_git').remove();
$.get(
"index.php",
{
"server": PMA_commonParams.get('server'),
"token": PMA_commonParams.get('token'),
"git_revision": true,
"ajax_request": true
},
function (data) {
if (typeof data !== 'undefined' && data.success === true) {
$(data.message).insertAfter('#li_pma_version');
}
}
);
}
/**
* for libraries/display_change_password.lib.php
* libraries/user_password.php
*
*/
function displayPasswordGenerateButton()
{
$('#tr_element_before_generate_password').parent().append('<tr class="vmiddle"><td>' + PMA_messages.strGeneratePassword + '</td><td><input type="button" class="button" id="button_generate_password" value="' + PMA_messages.strGenerate + '" onclick="suggestPassword(this.form)" /><input type="text" name="generated_pw" id="generated_pw" /></td></tr>');
$('#div_element_before_generate_password').parent().append('<div class="item"><label for="button_generate_password">' + PMA_messages.strGeneratePassword + ':</label><span class="options"><input type="button" class="button" id="button_generate_password" value="' + PMA_messages.strGenerate + '" onclick="suggestPassword(this.form)" /></span><input type="text" name="generated_pw" id="generated_pw" /></div>');
}
/*
* Adds a date/time picker to an element
*
* @param object $this_element a jQuery object pointing to the element
*/
function PMA_addDatepicker($this_element, type, options)
{
var showTimepicker = true;
if (type=="date") {
showTimepicker = false;
}
var defaultOptions = {
showOn: 'button',
buttonImage: themeCalendarImage, // defined in js/messages.php
buttonImageOnly: true,
stepMinutes: 1,
stepHours: 1,
showSecond: true,
showMillisec: true,
showMicrosec: true,
showTimepicker: showTimepicker,
showButtonPanel: false,
dateFormat: 'yy-mm-dd', // yy means year with four digits
timeFormat: 'HH:mm:ss.lc',
constrainInput: false,
altFieldTimeOnly: false,
showAnim: '',
beforeShow: function (input, inst) {
// Remember that we came from the datepicker; this is used
// in tbl_change.js by verificationsAfterFieldChange()
$this_element.data('comes_from', 'datepicker');
// Fix wrong timepicker z-index, doesn't work without timeout
setTimeout(function () {
$('#ui-timepicker-div').css('z-index', $('#ui-datepicker-div').css('z-index'));
}, 0);
},
onClose: function (dateText, dp_inst) {
// The value is no more from the date picker
$this_element.data('comes_from', '');
}
};
if (type == "datetime" || type == "timestamp") {
$this_element.datetimepicker($.extend(defaultOptions, options));
}
else if (type == "date") {
$this_element.datetimepicker($.extend(defaultOptions, options));
}
else if (type == "time") {
$this_element.timepicker($.extend(defaultOptions, options));
}
}
/**
* selects the content of a given object, f.e. a textarea
*
* @param element object element of which the content will be selected
* @param lock var variable which holds the lock for this element
* or true, if no lock exists
* @param only_once boolean if true this is only done once
* f.e. only on first focus
*/
function selectContent(element, lock, only_once)
{
if (only_once && only_once_elements[element.name]) {
return;
}
only_once_elements[element.name] = true;
if (lock) {
return;
}
element.select();
}
/**
* Displays a confirmation box before submitting a "DROP/DELETE/ALTER" query.
* This function is called while clicking links
*
* @param theLink object the link
* @param theSqlQuery object the sql query to submit
*
* @return boolean whether to run the query or not
*/
function confirmLink(theLink, theSqlQuery)
{
// Confirmation is not required in the configuration file
// or browser is Opera (crappy js implementation)
if (PMA_messages.strDoYouReally === '' || typeof(window.opera) != 'undefined') {
return true;
}
var is_confirmed = confirm(PMA_sprintf(PMA_messages.strDoYouReally, theSqlQuery));
if (is_confirmed) {
if ($(theLink).hasClass('formLinkSubmit')) {
var name = 'is_js_confirmed';
if ($(theLink).attr('href').indexOf('usesubform') != -1) {
name = 'subform[' + $(theLink).attr('href').substr('#').match(/usesubform\[(\d+)\]/i)[1] + '][is_js_confirmed]';
}
$(theLink).parents('form').append('<input type="hidden" name="' + name + '" value="1" />');
} else if (typeof(theLink.href) != 'undefined') {
theLink.href += '&is_js_confirmed=1';
} else if (typeof(theLink.form) != 'undefined') {
theLink.form.action += '?is_js_confirmed=1';
}
}
return is_confirmed;
} // end of the 'confirmLink()' function
/**
* Displays an error message if a "DROP DATABASE" statement is submitted
* while it isn't allowed, else confirms a "DROP/DELETE/ALTER" query before
* submitting it if required.
* This function is called by the 'checkSqlQuery()' js function.
*
* @param theForm1 object the form
* @param sqlQuery1 object the sql query textarea
*
* @return boolean whether to run the query or not
*
* @see checkSqlQuery()
*/
function confirmQuery(theForm1, sqlQuery1)
{
// Confirmation is not required in the configuration file
if (PMA_messages.strDoYouReally === '') {
return true;
}
// "DROP DATABASE" statement isn't allowed
if (PMA_messages.strNoDropDatabases !== '') {
var drop_re = new RegExp('(^|;)\\s*DROP\\s+(IF EXISTS\\s+)?DATABASE\\s', 'i');
if (drop_re.test(sqlQuery1.value)) {
alert(PMA_messages.strNoDropDatabases);
theForm1.reset();
sqlQuery1.focus();
return false;
} // end if
} // end if
// Confirms a "DROP/DELETE/ALTER/TRUNCATE" statement
//
// TODO: find a way (if possible) to use the parser-analyser
// for this kind of verification
// For now, I just added a ^ to check for the statement at
// beginning of expression
var do_confirm_re_0 = new RegExp('^\\s*DROP\\s+(IF EXISTS\\s+)?(TABLE|DATABASE|PROCEDURE)\\s', 'i');
var do_confirm_re_1 = new RegExp('^\\s*ALTER\\s+TABLE\\s+((`[^`]+`)|([A-Za-z0-9_$]+))\\s+DROP\\s', 'i');
var do_confirm_re_2 = new RegExp('^\\s*DELETE\\s+FROM\\s', 'i');
var do_confirm_re_3 = new RegExp('^\\s*TRUNCATE\\s', 'i');
if (do_confirm_re_0.test(sqlQuery1.value) ||
do_confirm_re_1.test(sqlQuery1.value) ||
do_confirm_re_2.test(sqlQuery1.value) ||
do_confirm_re_3.test(sqlQuery1.value)) {
var message;
if (sqlQuery1.value.length > 100) {
message = sqlQuery1.value.substr(0, 100) + '\n ...';
} else {
message = sqlQuery1.value;
}
var is_confirmed = confirm(PMA_sprintf(PMA_messages.strDoYouReally, message));
// statement is confirmed -> update the
// "is_js_confirmed" form field so the confirm test won't be
// run on the server side and allows to submit the form
if (is_confirmed) {
theForm1.elements.is_js_confirmed.value = 1;
return true;
}
// statement is rejected -> do not submit the form
else {
window.focus();
sqlQuery1.focus();
return false;
} // end if (handle confirm box result)
} // end if (display confirm box)
return true;
} // end of the 'confirmQuery()' function
/**
* Displays an error message if the user submitted the sql query form with no
* sql query, else checks for "DROP/DELETE/ALTER" statements
*
* @param theForm object the form
*
* @return boolean always false
*
* @see confirmQuery()
*/
function checkSqlQuery(theForm)
{
// get the textarea element containing the query
var sqlQuery;
if (codemirror_editor) {
codemirror_editor.save();
sqlQuery = codemirror_editor.getValue();
} else {
sqlQuery = theForm.elements.sql_query.value;
}
var isEmpty = 1;
var space_re = new RegExp('\\s+');
if (typeof(theForm.elements.sql_file) != 'undefined' &&
theForm.elements.sql_file.value.replace(space_re, '') !== '') {
return true;
}
if (typeof(theForm.elements.sql_localfile) != 'undefined' &&
theForm.elements.sql_localfile.value.replace(space_re, '') !== '') {
return true;
}
if (isEmpty && typeof(theForm.elements.id_bookmark) != 'undefined' &&
(theForm.elements.id_bookmark.value !== null || theForm.elements.id_bookmark.value !== '') &&
theForm.elements.id_bookmark.selectedIndex !== 0) {
return true;
}
// Checks for "DROP/DELETE/ALTER" statements
if (sqlQuery.replace(space_re, '') !== '') {
return confirmQuery(theForm, sqlQuery);
}
theForm.reset();
isEmpty = 1;
if (isEmpty) {
alert(PMA_messages.strFormEmpty);
codemirror_editor.focus();
return false;
}
return true;
} // end of the 'checkSqlQuery()' function
/**
* Check if a form's element is empty.
* An element containing only spaces is also considered empty
*
* @param object the form
* @param string the name of the form field to put the focus on
*
* @return boolean whether the form field is empty or not
*/
function emptyCheckTheField(theForm, theFieldName)
{
var theField = theForm.elements[theFieldName];
var space_re = new RegExp('\\s+');
return theField.value.replace(space_re, '') === '';
} // end of the 'emptyCheckTheField()' function
/**
* Ensures a value submitted in a form is numeric and is in a range
*
* @param object the form
* @param string the name of the form field to check
* @param integer the minimum authorized value
* @param integer the maximum authorized value
*
* @return boolean whether a valid number has been submitted or not
*/
function checkFormElementInRange(theForm, theFieldName, message, min, max)
{
var theField = theForm.elements[theFieldName];
var val = parseInt(theField.value, 10);
if (typeof(min) == 'undefined') {
min = 0;
}
if (typeof(max) == 'undefined') {
max = Number.MAX_VALUE;
}
// It's not a number
if (isNaN(val)) {
theField.select();
alert(PMA_messages.strEnterValidNumber);
theField.focus();
return false;
}
// It's a number but it is not between min and max
else if (val < min || val > max) {
theField.select();
alert(PMA_sprintf(message, val));
theField.focus();
return false;
}
// It's a valid number
else {
theField.value = val;
}
return true;
} // end of the 'checkFormElementInRange()' function
function checkTableEditForm(theForm, fieldsCnt)
{
// TODO: avoid sending a message if user just wants to add a line
// on the form but has not completed at least one field name
var atLeastOneField = 0;
var i, elm, elm2, elm3, val, id;
for (i = 0; i < fieldsCnt; i++) {
id = "#field_" + i + "_2";
elm = $(id);
val = elm.val();
if (val == 'VARCHAR' || val == 'CHAR' || val == 'BIT' || val == 'VARBINARY' || val == 'BINARY') {
elm2 = $("#field_" + i + "_3");
val = parseInt(elm2.val(), 10);
elm3 = $("#field_" + i + "_1");
if (isNaN(val) && elm3.val() !== "") {
elm2.select();
alert(PMA_messages.strEnterValidLength);
elm2.focus();
return false;
}
}
if (atLeastOneField === 0) {
id = "field_" + i + "_1";
if (!emptyCheckTheField(theForm, id)) {
atLeastOneField = 1;
}
}
}
if (atLeastOneField === 0) {
var theField = theForm.elements.field_0_1;
alert(PMA_messages.strFormEmpty);
theField.focus();
return false;
}
// at least this section is under jQuery
if ($("input.textfield[name='table']").val() === "") {
alert(PMA_messages.strFormEmpty);
$("input.textfield[name='table']").focus();
return false;
}
return true;
} // enf of the 'checkTableEditForm()' function
/**
* True if last click is to check a row.
*/
var last_click_checked = false;
/**
* Zero-based index of last clicked row.
* Used to handle the shift + click event in the code above.
*/
var last_clicked_row = -1;
/**
* Zero-based index of last shift clicked row.
*/
var last_shift_clicked_row = -1;
var _idleSecondsCounter = 0;
var IncInterval;
var updateInterval;
AJAX.registerTeardown('functions.js', function () {
clearInterval(updateInterval);
clearInterval(IncInterval);
$(document).off('mousemove');
});
AJAX.registerOnload('functions.js', function () {
document.onclick = function() {
_idleSecondsCounter = 0;
};
$(document).on('mousemove',function() {
_idleSecondsCounter = 0;
});
document.onkeypress = function() {
_idleSecondsCounter = 0;
};
function SetIdleTime() {
_idleSecondsCounter++;
}
function UpdateIdleTime() {
var href = 'index.php';
var params = {
'ajax_request' : true,
'token' : PMA_commonParams.get('token'),
'db' : PMA_commonParams.get('db'),
'access_time':_idleSecondsCounter
};
$.ajax({
type: 'POST',
url: href,
data: params,
success: function (data) {
clearInterval(updateInterval);
if (data.success) {
if (PMA_commonParams.get('LoginCookieValidity')-_idleSecondsCounter > 5) {
updateInterval = window.setInterval(UpdateIdleTime, (PMA_commonParams.get('LoginCookieValidity')-_idleSecondsCounter-5)*1000);
} else {
updateInterval = window.setInterval(UpdateIdleTime, 2000);
}
} else { //timeout occurred
window.location.reload(true);
clearInterval(IncInterval);
}
}
});
}
if (PMA_commonParams.get('logged_in')) {
IncInterval = window.setInterval(SetIdleTime, 1000);
updateInterval = window.setInterval(UpdateIdleTime, (PMA_commonParams.get('LoginCookieValidity')-5)*1000);
}
});
/**
* Unbind all event handlers before tearing down a page
*/
AJAX.registerTeardown('functions.js', function () {
$('input:checkbox.checkall').die('click');
});
AJAX.registerOnload('functions.js', function () {
/**
* Row marking in horizontal mode (use "live" so that it works also for
* next pages reached via AJAX); a tr may have the class noclick to remove
* this behavior.
*/
$('input:checkbox.checkall').live('click', function (e) {
var $tr = $(this).closest('tr');
// make the table unselectable (to prevent default highlighting when shift+click)
//$tr.parents('table').noSelect();
if (!e.shiftKey || last_clicked_row == -1) {
// usual click
// XXX: FF fires two click events for <label> (label and checkbox), so we need to handle this differently
var $checkbox = $tr.find(':checkbox');
if ($checkbox.length) {
// checkbox in a row, add or remove class depending on checkbox state
var checked = $checkbox.prop('checked');
if (!$(e.target).is(':checkbox, label')) {
checked = !checked;
$checkbox.prop('checked', checked).trigger('change');
}
if (checked) {
$tr.addClass('marked');
} else {
$tr.removeClass('marked');
}
last_click_checked = checked;
} else {
// normal data table, just toggle class
$tr.toggleClass('marked');
last_click_checked = false;
}
// remember the last clicked row
last_clicked_row = last_click_checked ? $('tr.odd:not(.noclick), tr.even:not(.noclick)').index($tr) : -1;
last_shift_clicked_row = -1;
} else {
// handle the shift click
PMA_clearSelection();
var start, end;
// clear last shift click result
if (last_shift_clicked_row >= 0) {
if (last_shift_clicked_row >= last_clicked_row) {
start = last_clicked_row;
end = last_shift_clicked_row;
} else {
start = last_shift_clicked_row;
end = last_clicked_row;
}
$tr.parent().find('tr.odd:not(.noclick), tr.even:not(.noclick)')
.slice(start, end + 1)
.removeClass('marked')
.find(':checkbox')
.prop('checked', false)
.trigger('change');
}
// handle new shift click
var curr_row = $('tr.odd:not(.noclick), tr.even:not(.noclick)').index($tr);
if (curr_row >= last_clicked_row) {
start = last_clicked_row;
end = curr_row;
} else {
start = curr_row;
end = last_clicked_row;
}
$tr.parent().find('tr.odd:not(.noclick), tr.even:not(.noclick)')
.slice(start, end + 1)
.addClass('marked')
.find(':checkbox')
.prop('checked', true)
.trigger('change');
// remember the last shift clicked row
last_shift_clicked_row = curr_row;
}
});
addDateTimePicker();
/**
* Add attribute to text boxes for iOS devices (based on bugID: 3508912)
*/
if (navigator.userAgent.match(/(iphone|ipod|ipad)/i)) {
$('input[type=text]').attr('autocapitalize', 'off').attr('autocorrect', 'off');
}
});
/**
* Row highlighting in horizontal mode (use "live"
* so that it works also for pages reached via AJAX)
*/
/*AJAX.registerOnload('functions.js', function () {
$('tr.odd, tr.even').live('hover',function (event) {
var $tr = $(this);
$tr.toggleClass('hover',event.type=='mouseover');
$tr.children().toggleClass('hover',event.type=='mouseover');
});
})*/
/**
* This array is used to remember mark status of rows in browse mode
*/
var marked_row = [];
/**
* marks all rows and selects its first checkbox inside the given element
* the given element is usually a table or a div containing the table or tables
*
* @param container DOM element
*/
function markAllRows(container_id)
{
$("#" + container_id).find("input:checkbox:enabled").prop('checked', true)
.trigger("change")
.parents("tr").addClass("marked");
return true;
}
/**
* marks all rows and selects its first checkbox inside the given element
* the given element is usually a table or a div containing the table or tables
*
* @param container DOM element
*/
function unMarkAllRows(container_id)
{
$("#" + container_id).find("input:checkbox:enabled").prop('checked', false)
.trigger("change")
.parents("tr").removeClass("marked");
return true;
}
/**
* Checks/unchecks all checkbox in given container (f.e. a form, fieldset or div)
*
* @param string container_id the container id
* @param boolean state new value for checkbox (true or false)
* @return boolean always true
*/
function setCheckboxes(container_id, state)
{
$("#" + container_id).find("input:checkbox").prop('checked', state);
return true;
} // end of the 'setCheckboxes()' function
/**
* Checks/unchecks all options of a <select> element
*
* @param string the form name
* @param string the element name
* @param boolean whether to check or to uncheck options
*
* @return boolean always true
*/
function setSelectOptions(the_form, the_select, do_check)
{
$("form[name='" + the_form + "'] select[name='" + the_select + "']").find("option").prop('selected', do_check);
return true;
} // end of the 'setSelectOptions()' function
/**
* Sets current value for query box.
*/
function setQuery(query)
{
if (codemirror_editor) {
codemirror_editor.setValue(query);
codemirror_editor.focus();
} else {
document.sqlform.sql_query.value = query;
document.sqlform.sql_query.focus();
}
}
/**
* Handles 'Simulate query' button on SQL query box.
*
* @return void
*/
function PMA_handleSimulateQueryButton()
{
var update_re = new RegExp('^\\s*UPDATE\\s+((`[^`]+`)|([A-Za-z0-9_$]+))\\s+SET\\s', 'i');
var delete_re = new RegExp('^\\s*DELETE\\s+FROM\\s', 'i');
var query = '';
if (codemirror_editor) {
query = codemirror_editor.getValue();
} else {
query = $('#sqlquery').val();
}
if (update_re.test(query) || delete_re.test(query)) {
if (! $('#simulate_dml').length) {
$('#button_submit_query')
.before('<input type="button" id="simulate_dml"' +
'tabindex="199" value="' +
PMA_messages.strSimulateDML +
'" />');
}
} else {
if ($('#simulate_dml').length) {
$('#simulate_dml').remove();
}
}
}
/**
* Create quick sql statements.
*
*/
function insertQuery(queryType)
{
if (queryType == "clear") {
setQuery('');
return;
}
var query = "";
var myListBox = document.sqlform.dummy;
var table = document.sqlform.table.value;
if (myListBox.options.length > 0) {
sql_box_locked = true;
var columnsList = "";
var valDis = "";
var editDis = "";
var NbSelect = 0;
for (var i = 0; i < myListBox.options.length; i++) {
NbSelect++;
if (NbSelect > 1) {
columnsList += ", ";
valDis += ",";
editDis += ",";
}
columnsList += myListBox.options[i].value;
valDis += "[value-" + NbSelect + "]";
editDis += myListBox.options[i].value + "=[value-" + NbSelect + "]";
}
if (queryType == "selectall") {
query = "SELECT * FROM `" + table + "` WHERE 1";
} else if (queryType == "select") {
query = "SELECT " + columnsList + " FROM `" + table + "` WHERE 1";
} else if (queryType == "insert") {
query = "INSERT INTO `" + table + "`(" + columnsList + ") VALUES (" + valDis + ")";
} else if (queryType == "update") {