Welcome to mirror list, hosted at ThFree Co, Russian Federation.

tbl_zoom_plot_jqplot.js « js - github.com/phpmyadmin/phpmyadmin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a97dfa0113dd27e02ba13a7563af6b043960a493 (plain)
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
// TODO: change the axis
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
 ** @fileoverview JavaScript functions used on tbl_select.php
 **
 ** @requires    jQuery
 ** @requires    js/functions.js
 **/


/**
 **  Display Help/Info
 **/
function displayHelp() {
    PMA_ajaxShowMessage(PMA_messages.strDisplayHelp, 10000);
}

/**
 ** Extend the array object for max function
 ** @param array
 **/
Array.max = function (array) {
    return Math.max.apply(Math, array);
};

/**
 ** Extend the array object for min function
 ** @param array
 **/
Array.min = function (array) {
    return Math.min.apply(Math, array);
};

/**
 ** Checks if a string contains only numeric value
 ** @param n: String (to be checked)
 **/
function isNumeric(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

/**
 ** Checks if an object is empty
 ** @param n: Object (to be checked)
 **/
function isEmpty(obj) {
    var name;
    for (name in obj) {
        return false;
    }
    return true;
}

/**
 ** Converts a date/time into timestamp
 ** @param val  String Date
 ** @param type Sring  Field type(datetime/timestamp/time/date)
 **/
function getTimeStamp(val, type) {
    if (type.toString().search(/datetime/i) != -1 ||
        type.toString().search(/timestamp/i) != -1
    ) {
        return $.datepicker.parseDateTime('yy-mm-dd', 'HH:mm:ss', val);
    }
    else if (type.toString().search(/time/i) != -1) {
        return $.datepicker.parseDateTime('yy-mm-dd', 'HH:mm:ss', '1970-01-01 ' + val);
    }
    else if (type.toString().search(/date/i) != -1) {
        return $.datepicker.parseDate('yy-mm-dd', val);
    }
}

/**
 ** Classifies the field type into numeric,timeseries or text
 ** @param field: field type (as in database structure)
 **/
function getType(field) {
    if (field.toString().search(/int/i) != -1 ||
        field.toString().search(/decimal/i) != -1 ||
        field.toString().search(/year/i) != -1
    ) {
        return 'numeric';
    } else if (field.toString().search(/time/i) != -1 ||
        field.toString().search(/date/i) != -1
    ) {
        return 'time';
    } else {
        return 'text';
    }
}
/**
 ** Converts a categorical array into numeric array
 ** @param array categorical values array
 **/
function getCord(arr) {
    var newCord = [];
    var original = $.extend(true, [], arr);
    arr = jQuery.unique(arr).sort();
    $.each(original, function (index, value) {
        newCord.push(jQuery.inArray(value, arr));
    });
    return [newCord, arr, original];
}

/**
 ** Scrolls the view to the display section
 **/
function scrollToChart() {
    var x = $('#dataDisplay').offset().top - 100; // 100 provides buffer in viewport
    $('html,body').animate({scrollTop: x}, 500);
}

/**
 * Unbind all event handlers before tearing down a page
 */
AJAX.registerTeardown('tbl_zoom_plot_jqplot.js', function () {
    $('#tableid_0').unbind('change');
    $('#tableid_1').unbind('change');
    $('#tableid_2').unbind('change');
    $('#tableid_3').unbind('change');
    $('#inputFormSubmitId').unbind('click');
    $('#togglesearchformlink').unbind('click');
    $(document).on('keydown', "#dataDisplay :input");
    $('button.button-reset').unbind('click');
    $('div#resizer').unbind('resizestop');
    $('div#querychart').unbind('jqplotDataClick');
});

AJAX.registerOnload('tbl_zoom_plot_jqplot.js', function () {
    var cursorMode = ($("input[name='mode']:checked").val() == 'edit') ? 'crosshair' : 'pointer';
    var currentChart = null;
    var searchedDataKey = null;
    var xLabel = $('#tableid_0').val();
    var yLabel = $('#tableid_1').val();
    // will be updated via Ajax
    var xType = $('#types_0').val();
    var yType = $('#types_1').val();
    var dataLabel = $('#dataLabel').val();
    var lastX;
    var lastY;
    var zoomRatio = 1;


    // Get query result
    var searchedData;
    try {
        searchedData = jQuery.parseJSON($('#querydata').html());
    } catch (err) {
        searchedData = null;
    }

    /**
     ** Input form submit on field change
     **/

    // first column choice corresponds to the X axis
    $('#tableid_0').change(function () {
        //AJAX request for field type, collation, operators, and value field
        $.post('tbl_zoom_select.php', {
            'ajax_request' : true,
            'change_tbl_info' : true,
            'server' : PMA_commonParams.get('server'),
            'db' : PMA_commonParams.get('db'),
            'table' : PMA_commonParams.get('table'),
            'field' : $('#tableid_0').val(),
            'it' : 0,
            'token' : PMA_commonParams.get('token')
        }, function (data) {
            $('#tableFieldsId tr:eq(1) td:eq(0)').html(data.field_type);
            $('#tableFieldsId tr:eq(1) td:eq(1)').html(data.field_collation);
            $('#tableFieldsId tr:eq(1) td:eq(2)').html(data.field_operators);
            $('#tableFieldsId tr:eq(1) td:eq(3)').html(data.field_value);
            xLabel = $('#tableid_0').val();
            $('#types_0').val(data.field_type);
            xType = data.field_type;
            $('#collations_0').val(data.field_collations);
            addDateTimePicker();
        });
    });

    // second column choice corresponds to the Y axis
    $('#tableid_1').change(function () {
        //AJAX request for field type, collation, operators, and value field
        $.post('tbl_zoom_select.php', {
            'ajax_request' : true,
            'change_tbl_info' : true,
            'server' : PMA_commonParams.get('server'),
            'db' : PMA_commonParams.get('db'),
            'table' : PMA_commonParams.get('table'),
            'field' : $('#tableid_1').val(),
            'it' : 1,
            'token' : PMA_commonParams.get('token')
        }, function (data) {
            $('#tableFieldsId tr:eq(3) td:eq(0)').html(data.field_type);
            $('#tableFieldsId tr:eq(3) td:eq(1)').html(data.field_collation);
            $('#tableFieldsId tr:eq(3) td:eq(2)').html(data.field_operators);
            $('#tableFieldsId tr:eq(3) td:eq(3)').html(data.field_value);
            yLabel = $('#tableid_1').val();
            $('#types_1').val(data.field_type);
            yType = data.field_type;
            $('#collations_1').val(data.field_collations);
            addDateTimePicker();
        });
    });

    $('#tableid_2').change(function () {
        //AJAX request for field type, collation, operators, and value field
        $.post('tbl_zoom_select.php', {
            'ajax_request' : true,
            'change_tbl_info' : true,
            'server' : PMA_commonParams.get('server'),
            'db' : PMA_commonParams.get('db'),
            'table' : PMA_commonParams.get('table'),
            'field' : $('#tableid_2').val(),
            'it' : 2,
            'token' : PMA_commonParams.get('token')
        }, function (data) {
            $('#tableFieldsId tr:eq(6) td:eq(0)').html(data.field_type);
            $('#tableFieldsId tr:eq(6) td:eq(1)').html(data.field_collation);
            $('#tableFieldsId tr:eq(6) td:eq(2)').html(data.field_operators);
            $('#tableFieldsId tr:eq(6) td:eq(3)').html(data.field_value);
            $('#types_2').val(data.field_type);
            $('#collations_2').val(data.field_collations);
            addDateTimePicker();
        });
    });

    $('#tableid_3').change(function () {
        //AJAX request for field type, collation, operators, and value field
        $.post('tbl_zoom_select.php', {
            'ajax_request' : true,
            'change_tbl_info' : true,
            'server' : PMA_commonParams.get('server'),
            'db' : PMA_commonParams.get('db'),
            'table' : PMA_commonParams.get('table'),
            'field' : $('#tableid_3').val(),
            'it' : 3,
            'token' : PMA_commonParams.get('token')
        }, function (data) {
            $('#tableFieldsId tr:eq(8) td:eq(0)').html(data.field_type);
            $('#tableFieldsId tr:eq(8) td:eq(1)').html(data.field_collation);
            $('#tableFieldsId tr:eq(8) td:eq(2)').html(data.field_operators);
            $('#tableFieldsId tr:eq(8) td:eq(3)').html(data.field_value);
            $('#types_3').val(data.field_type);
            $('#collations_3').val(data.field_collations);
            addDateTimePicker();
        });
    });

    /**
     * Input form validation
     **/
    $('#inputFormSubmitId').click(function () {
        if ($('#tableid_0').get(0).selectedIndex === 0 || $('#tableid_1').get(0).selectedIndex === 0) {
            PMA_ajaxShowMessage(PMA_messages.strInputNull);
        } else if (xLabel == yLabel) {
            PMA_ajaxShowMessage(PMA_messages.strSameInputs);
        }
    });

    /**
      ** Prepare a div containing a link, otherwise it's incorrectly displayed
      ** after a couple of clicks
      **/
    $('<div id="togglesearchformdiv"><a id="togglesearchformlink"></a></div>')
        .insertAfter('#zoom_search_form')
        // don't show it until we have results on-screen
        .hide();

    $('#togglesearchformlink')
        .html(PMA_messages.strShowSearchCriteria)
        .bind('click', function () {
            var $link = $(this);
            $('#zoom_search_form').slideToggle();
            if ($link.text() == PMA_messages.strHideSearchCriteria) {
                $link.text(PMA_messages.strShowSearchCriteria);
            } else {
                $link.text(PMA_messages.strHideSearchCriteria);
            }
            // avoid default click action
            return false;
        });

    /**
     ** Set dialog properties for the data display form
     **/
    var buttonOptions = {};
    /*
     * Handle saving of a row in the editor
     */
    buttonOptions[PMA_messages.strSave] = function () {
        //Find changed values by comparing form values with selectedRow Object
        var newValues = {};//Stores the values changed from original
        var sqlTypes = {};
        var it = 0;
        var xChange = false;
        var yChange = false;
        var key;
        var tempGetVal = function () {
            return $(this).val();
        };
        for (key in selectedRow) {
            var oldVal = selectedRow[key];
            var newVal = ($('#edit_fields_null_id_' + it).prop('checked')) ? null : $('#edit_fieldID_' + it).val();
            if (newVal instanceof Array) { // when the column is of type SET
                newVal =  $('#edit_fieldID_' + it).map(tempGetVal).get().join(",");
            }
            if (oldVal != newVal) {
                selectedRow[key] = newVal;
                newValues[key] = newVal;
                if (key == xLabel) {
                    xChange = true;
                    searchedData[searchedDataKey][xLabel] = newVal;
                } else if (key == yLabel) {
                    yChange = true;
                    searchedData[searchedDataKey][yLabel] = newVal;
                }
            }
            var $input = $('#edit_fieldID_' + it);
            if ($input.hasClass('bit')) {
                sqlTypes[key] = 'bit';
            } else {
                sqlTypes[key] = null;
            }
            it++;
        } //End data update

        //Update the chart series and replot
        if (xChange || yChange) {
            //Logic similar to plot generation, replot only if xAxis changes or yAxis changes.
            //Code includes a lot of checks so as to replot only when necessary
            if (xChange) {
                xCord[searchedDataKey] = selectedRow[xLabel];
                // [searchedDataKey][0] contains the x value
                if (xType == 'numeric') {
                    series[0][searchedDataKey][0] = selectedRow[xLabel];
                } else if (xType == 'time') {
                    series[0][searchedDataKey][0] =
                        getTimeStamp(selectedRow[xLabel], $('#types_0').val());
                } else {
                    series[0][searchedDataKey][0] = '';
                    // TODO: text values
                }
                currentChart.series[0].data = series[0];
                // TODO: axis changing
                currentChart.replot();

            }
            if (yChange) {
                yCord[searchedDataKey] = selectedRow[yLabel];
                // [searchedDataKey][1] contains the y value
                if (yType == 'numeric') {
                    series[0][searchedDataKey][1] = selectedRow[yLabel];
                } else if (yType == 'time') {
                    series[0][searchedDataKey][1] =
                        getTimeStamp(selectedRow[yLabel], $('#types_1').val());
                } else {
                    series[0][searchedDataKey][1] = '';
                    // TODO: text values
                }
                currentChart.series[0].data = series[0];
                // TODO: axis changing
                currentChart.replot();
            }
        } //End plot update

        //Generate SQL query for update
        if (!isEmpty(newValues)) {
            var sql_query = 'UPDATE `' + PMA_commonParams.get('table') + '` SET ';
            for (key in newValues) {
                sql_query += '`' + key + '`=';
                var value = newValues[key];

                // null
                if (value === null) {
                    sql_query += 'NULL, ';

                // empty
                } else if ($.trim(value) === '') {
                    sql_query += "'', ";

                // other
                } else {
                    // type explicitly identified
                    if (sqlTypes[key] !== null) {
                        if (sqlTypes[key] == 'bit') {
                            sql_query += "b'" + value + "', ";
                        }
                    // type not explicitly identified
                    } else {
                        if (!isNumeric(value)) {
                            sql_query += "'" + value + "', ";
                        } else {
                            sql_query += value + ', ';
                        }
                    }
                }
            }
            // remove two extraneous characters ', '
            sql_query = sql_query.substring(0, sql_query.length - 2);
            sql_query += ' WHERE ' + PMA_urldecode(searchedData[searchedDataKey].where_clause);

            //Post SQL query to sql.php
            $.post('sql.php', {
                    'token' : PMA_commonParams.get('token'),
                    'server' : PMA_commonParams.get('server'),
                    'db' : PMA_commonParams.get('db'),
                    'ajax_request' : true,
                    'sql_query' : sql_query,
                    'inline_edit' : false
                }, function (data) {
                    if (typeof data !== 'undefined' && data.success === true) {
                        $('#sqlqueryresultsouter').html(data.sql_query);
                        PMA_highlightSQL($('#sqlqueryresultsouter'));
                    } else {
                        PMA_ajaxShowMessage(data.error, false);
                    }
                }); //End $.post
        }//End database update
        $("#dataDisplay").dialog('close');
    };
    buttonOptions[PMA_messages.strCancel] = function () {
        $(this).dialog('close');
    };
    $("#dataDisplay").dialog({
        autoOpen: false,
        title: PMA_messages.strDataPointContent,
        modal: true,
        buttons: buttonOptions,
        width: $('#dataDisplay').width() + 80,
        open: function () {
            $(this).find('input[type=checkbox]').css('margin', '0.5em');
        }
    });
    /**
     * Attach Ajax event handlers for input fields
     * in the dialog. Used to submit the Ajax
     * request when the ENTER key is pressed.
     */
    $(document).on('keydown', "#dataDisplay :input", function (e) {
        if (e.which === 13) { // 13 is the ENTER key
            e.preventDefault();
            if (typeof buttonOptions[PMA_messages.strSave] === 'function') {
                buttonOptions[PMA_messages.strSave].call();
            }
        }
    });


    /*
     * Generate plot using jqplot
     */

    if (searchedData !== null) {
        $('#zoom_search_form')
         .slideToggle()
         .hide();
        $('#togglesearchformlink')
         .text(PMA_messages.strShowSearchCriteria);
        $('#togglesearchformdiv').show();
        var selectedRow;
        var colorCodes = ['#FF0000', '#00FFFF', '#0000FF', '#0000A0', '#FF0080', '#800080', '#FFFF00', '#00FF00', '#FF00FF'];
        var series = [];
        var xCord = [];
        var yCord = [];
        var tempX, tempY;
        var it = 0;
        var xMax; // xAxis extreme max
        var xMin; // xAxis extreme min
        var yMax; // yAxis extreme max
        var yMin; // yAxis extreme min
        var xVal;
        var yVal;
        var format;

        var options = {
            series: [
                // for a scatter plot
                { showLine: false }
            ],
            grid: {
                drawBorder: false,
                shadow: false,
                background: 'rgba(0,0,0,0)'
            },
            axes: {
                xaxis: {
                    label: $('#tableid_0').val(),
                    labelRenderer: $.jqplot.CanvasAxisLabelRenderer
                },
                yaxis: {
                    label: $('#tableid_1').val(),
                    labelRenderer: $.jqplot.CanvasAxisLabelRenderer
                }
            },
            highlighter: {
                show: true,
                tooltipAxes: 'y',
                yvalues: 2,
                // hide the first y value
                formatString: '<span class="hide">%s</span>%s'
            },
            cursor: {
                show: true,
                zoom: true,
                showTooltip: false
            }
        };

        // If data label is not set, do not show tooltips
        if (dataLabel === '') {
            options.highlighter.show = false;
        }

        // Classify types as either numeric,time,text
        xType = getType(xType);
        yType = getType(yType);

        // could have multiple series but we'll have just one
        series[0] = [];

        if (xType == 'time') {
            var originalXType = $('#types_0').val();
            if (originalXType == 'date') {
                format = '%Y-%m-%d';
            }
            // TODO: does not seem to work
            //else if (originalXType == 'time') {
              //  format = '%H:%M';
            //} else {
            //    format = '%Y-%m-%d %H:%M';
            //}
            $.extend(options.axes.xaxis, {
                renderer: $.jqplot.DateAxisRenderer,
                tickOptions: {
                    formatString: format
                }
            });
        }
        if (yType == 'time') {
            var originalYType = $('#types_1').val();
            if (originalYType == 'date') {
                format = '%Y-%m-%d';
            }
            $.extend(options.axes.yaxis, {
                renderer: $.jqplot.DateAxisRenderer,
                tickOptions: {
                    formatString: format
                }
            });
        }

        $.each(searchedData, function (key, value) {
            if (xType == 'numeric') {
                xVal = parseFloat(value[xLabel]);
            }
            if (xType == 'time') {
                xVal = getTimeStamp(value[xLabel], originalXType);
            }
            if (yType == 'numeric') {
                yVal = parseFloat(value[yLabel]);
            }
            if (yType == 'time') {
                yVal = getTimeStamp(value[yLabel], originalYType);
            }
            series[0].push([
                xVal,
                yVal,
                // extra Y values
                value[dataLabel], // for highlighter
                                  // (may set an undefined value)
                value.where_clause, // for click on point
                key               // key from searchedData
            ]);
        });

        // under IE 8, the initial display is mangled; after a manual
        // resizing, it's ok
        // under IE 9, everything is fine
        currentChart = $.jqplot('querychart', series, options);
        currentChart.resetZoom();

        $('button.button-reset').click(function (event) {
            event.preventDefault();
            currentChart.resetZoom();
        });

        $('div#resizer').resizable();
        $('div#resizer').bind('resizestop', function (event, ui) {
            // make room so that the handle will still appear
            $('div#querychart').height($('div#resizer').height() * 0.96);
            $('div#querychart').width($('div#resizer').width() * 0.96);
            currentChart.replot({resetAxes: true});
        });

        $('div#querychart').bind('jqplotDataClick',
            function (event, seriesIndex, pointIndex, data) {
                searchedDataKey = data[4]; // key from searchedData (global)
                var field_id = 0;
                var post_params = {
                    'ajax_request' : true,
                    'get_data_row' : true,
                    'server' : PMA_commonParams.get('server'),
                    'db' : PMA_commonParams.get('db'),
                    'table' : PMA_commonParams.get('table'),
                    'where_clause' : data[3],
                    'token' : PMA_commonParams.get('token')
                };

                $.post('tbl_zoom_select.php', post_params, function (data) {
                    // Row is contained in data.row_info,
                    // now fill the displayResultForm with row values
                    var key;
                    for (key in data.row_info) {
                        var $field = $('#edit_fieldID_' + field_id);
                        var $field_null = $('#edit_fields_null_id_' + field_id);
                        if (data.row_info[key] === null) {
                            $field_null.prop('checked', true);
                            $field.val('');
                        } else {
                            $field_null.prop('checked', false);
                            if ($field.attr('multiple')) { // when the column is of type SET
                                $field.val(data.row_info[key].split(','));
                            } else {
                                $field.val(data.row_info[key]);
                            }
                        }
                        field_id++;
                    }
                    selectedRow = {};
                    selectedRow = data.row_info;
                });

                $("#dataDisplay").dialog("open");
            }
        );
    }
});