1 // TODO: change the axis
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 ** @fileoverview JavaScript functions used on tbl_select.php
7 ** @requires js/functions.js
14 function displayHelp() {
15 PMA_ajaxShowMessage(PMA_messages.strDisplayHelp, 10000);
19 ** Extend the array object for max function
22 Array.max = function (array) {
23 return Math.max.apply(Math, array);
27 ** Extend the array object for min function
30 Array.min = function (array) {
31 return Math.min.apply(Math, array);
35 ** Checks if a string contains only numeric value
36 ** @param n: String (to be checked)
38 function isNumeric(n) {
39 return !isNaN(parseFloat(n)) && isFinite(n);
43 ** Checks if an object is empty
44 ** @param n: Object (to be checked)
46 function isEmpty(obj) {
55 ** Converts a date/time into timestamp
56 ** @param val String Date
57 ** @param type Sring Field type(datetime/timestamp/time/date)
59 function getTimeStamp(val, type) {
60 if (type.toString().search(/datetime/i) != -1 ||
61 type.toString().search(/timestamp/i) != -1
63 return $.datepicker.parseDateTime('yy-mm-dd', 'HH:mm:ss', val);
65 else if (type.toString().search(/time/i) != -1) {
66 return $.datepicker.parseDateTime('yy-mm-dd', 'HH:mm:ss', '1970-01-01 ' + val);
68 else if (type.toString().search(/date/i) != -1) {
69 return $.datepicker.parseDate('yy-mm-dd', val);
74 ** Classifies the field type into numeric,timeseries or text
75 ** @param field: field type (as in database structure)
77 function getType(field) {
78 if (field.toString().search(/int/i) != -1 ||
79 field.toString().search(/decimal/i) != -1 ||
80 field.toString().search(/year/i) != -1
83 } else if (field.toString().search(/time/i) != -1 ||
84 field.toString().search(/date/i) != -1
92 ** Converts a categorical array into numeric array
93 ** @param array categorical values array
95 function getCord(arr) {
97 var original = $.extend(true, [], arr);
98 arr = jQuery.unique(arr).sort();
99 $.each(original, function (index, value) {
100 newCord.push(jQuery.inArray(value, arr));
102 return [newCord, arr, original];
106 ** Scrolls the view to the display section
108 function scrollToChart() {
109 var x = $('#dataDisplay').offset().top - 100; // 100 provides buffer in viewport
110 $('html,body').animate({scrollTop: x}, 500);
114 * Unbind all event handlers before tearing down a page
116 AJAX.registerTeardown('tbl_zoom_plot_jqplot.js', function () {
117 $('#tableid_0').unbind('change');
118 $('#tableid_1').unbind('change');
119 $('#tableid_2').unbind('change');
120 $('#tableid_3').unbind('change');
121 $('#inputFormSubmitId').unbind('click');
122 $('#togglesearchformlink').unbind('click');
123 $(document).on('keydown', "#dataDisplay :input");
124 $('button.button-reset').unbind('click');
125 $('div#resizer').unbind('resizestop');
126 $('div#querychart').unbind('jqplotDataClick');
129 AJAX.registerOnload('tbl_zoom_plot_jqplot.js', function () {
130 var cursorMode = ($("input[name='mode']:checked").val() == 'edit') ? 'crosshair' : 'pointer';
131 var currentChart = null;
132 var searchedDataKey = null;
133 var xLabel = $('#tableid_0').val();
134 var yLabel = $('#tableid_1').val();
135 // will be updated via Ajax
136 var xType = $('#types_0').val();
137 var yType = $('#types_1').val();
138 var dataLabel = $('#dataLabel').val();
147 searchedData = jQuery.parseJSON($('#querydata').html());
153 ** Input form submit on field change
156 // first column choice corresponds to the X axis
157 $('#tableid_0').change(function () {
158 //AJAX request for field type, collation, operators, and value field
159 $.post('tbl_zoom_select.php', {
160 'ajax_request' : true,
161 'change_tbl_info' : true,
162 'server' : PMA_commonParams.get('server'),
163 'db' : PMA_commonParams.get('db'),
164 'table' : PMA_commonParams.get('table'),
165 'field' : $('#tableid_0').val(),
167 'token' : PMA_commonParams.get('token')
169 $('#tableFieldsId tr:eq(1) td:eq(0)').html(data.field_type);
170 $('#tableFieldsId tr:eq(1) td:eq(1)').html(data.field_collation);
171 $('#tableFieldsId tr:eq(1) td:eq(2)').html(data.field_operators);
172 $('#tableFieldsId tr:eq(1) td:eq(3)').html(data.field_value);
173 xLabel = $('#tableid_0').val();
174 $('#types_0').val(data.field_type);
175 xType = data.field_type;
176 $('#collations_0').val(data.field_collations);
181 // second column choice corresponds to the Y axis
182 $('#tableid_1').change(function () {
183 //AJAX request for field type, collation, operators, and value field
184 $.post('tbl_zoom_select.php', {
185 'ajax_request' : true,
186 'change_tbl_info' : true,
187 'server' : PMA_commonParams.get('server'),
188 'db' : PMA_commonParams.get('db'),
189 'table' : PMA_commonParams.get('table'),
190 'field' : $('#tableid_1').val(),
192 'token' : PMA_commonParams.get('token')
194 $('#tableFieldsId tr:eq(3) td:eq(0)').html(data.field_type);
195 $('#tableFieldsId tr:eq(3) td:eq(1)').html(data.field_collation);
196 $('#tableFieldsId tr:eq(3) td:eq(2)').html(data.field_operators);
197 $('#tableFieldsId tr:eq(3) td:eq(3)').html(data.field_value);
198 yLabel = $('#tableid_1').val();
199 $('#types_1').val(data.field_type);
200 yType = data.field_type;
201 $('#collations_1').val(data.field_collations);
206 $('#tableid_2').change(function () {
207 //AJAX request for field type, collation, operators, and value field
208 $.post('tbl_zoom_select.php', {
209 'ajax_request' : true,
210 'change_tbl_info' : true,
211 'server' : PMA_commonParams.get('server'),
212 'db' : PMA_commonParams.get('db'),
213 'table' : PMA_commonParams.get('table'),
214 'field' : $('#tableid_2').val(),
216 'token' : PMA_commonParams.get('token')
218 $('#tableFieldsId tr:eq(6) td:eq(0)').html(data.field_type);
219 $('#tableFieldsId tr:eq(6) td:eq(1)').html(data.field_collation);
220 $('#tableFieldsId tr:eq(6) td:eq(2)').html(data.field_operators);
221 $('#tableFieldsId tr:eq(6) td:eq(3)').html(data.field_value);
222 $('#types_2').val(data.field_type);
223 $('#collations_2').val(data.field_collations);
228 $('#tableid_3').change(function () {
229 //AJAX request for field type, collation, operators, and value field
230 $.post('tbl_zoom_select.php', {
231 'ajax_request' : true,
232 'change_tbl_info' : true,
233 'server' : PMA_commonParams.get('server'),
234 'db' : PMA_commonParams.get('db'),
235 'table' : PMA_commonParams.get('table'),
236 'field' : $('#tableid_3').val(),
238 'token' : PMA_commonParams.get('token')
240 $('#tableFieldsId tr:eq(8) td:eq(0)').html(data.field_type);
241 $('#tableFieldsId tr:eq(8) td:eq(1)').html(data.field_collation);
242 $('#tableFieldsId tr:eq(8) td:eq(2)').html(data.field_operators);
243 $('#tableFieldsId tr:eq(8) td:eq(3)').html(data.field_value);
244 $('#types_3').val(data.field_type);
245 $('#collations_3').val(data.field_collations);
251 * Input form validation
253 $('#inputFormSubmitId').click(function () {
254 if ($('#tableid_0').get(0).selectedIndex === 0 || $('#tableid_1').get(0).selectedIndex === 0) {
255 PMA_ajaxShowMessage(PMA_messages.strInputNull);
256 } else if (xLabel == yLabel) {
257 PMA_ajaxShowMessage(PMA_messages.strSameInputs);
262 ** Prepare a div containing a link, otherwise it's incorrectly displayed
263 ** after a couple of clicks
265 $('<div id="togglesearchformdiv"><a id="togglesearchformlink"></a></div>')
266 .insertAfter('#zoom_search_form')
267 // don't show it until we have results on-screen
270 $('#togglesearchformlink')
271 .html(PMA_messages.strShowSearchCriteria)
272 .bind('click', function () {
274 $('#zoom_search_form').slideToggle();
275 if ($link.text() == PMA_messages.strHideSearchCriteria) {
276 $link.text(PMA_messages.strShowSearchCriteria);
278 $link.text(PMA_messages.strHideSearchCriteria);
280 // avoid default click action
285 ** Set dialog properties for the data display form
287 var buttonOptions = {};
289 * Handle saving of a row in the editor
291 buttonOptions[PMA_messages.strSave] = function () {
292 //Find changed values by comparing form values with selectedRow Object
293 var newValues = {};//Stores the values changed from original
299 var tempGetVal = function () {
300 return $(this).val();
302 for (key in selectedRow) {
303 var oldVal = selectedRow[key];
304 var newVal = ($('#edit_fields_null_id_' + it).prop('checked')) ? null : $('#edit_fieldID_' + it).val();
305 if (newVal instanceof Array) { // when the column is of type SET
306 newVal = $('#edit_fieldID_' + it).map(tempGetVal).get().join(",");
308 if (oldVal != newVal) {
309 selectedRow[key] = newVal;
310 newValues[key] = newVal;
313 searchedData[searchedDataKey][xLabel] = newVal;
314 } else if (key == yLabel) {
316 searchedData[searchedDataKey][yLabel] = newVal;
319 var $input = $('#edit_fieldID_' + it);
320 if ($input.hasClass('bit')) {
321 sqlTypes[key] = 'bit';
323 sqlTypes[key] = null;
328 //Update the chart series and replot
329 if (xChange || yChange) {
330 //Logic similar to plot generation, replot only if xAxis changes or yAxis changes.
331 //Code includes a lot of checks so as to replot only when necessary
333 xCord[searchedDataKey] = selectedRow[xLabel];
334 // [searchedDataKey][0] contains the x value
335 if (xType == 'numeric') {
336 series[0][searchedDataKey][0] = selectedRow[xLabel];
337 } else if (xType == 'time') {
338 series[0][searchedDataKey][0] =
339 getTimeStamp(selectedRow[xLabel], $('#types_0').val());
341 series[0][searchedDataKey][0] = '';
344 currentChart.series[0].data = series[0];
345 // TODO: axis changing
346 currentChart.replot();
350 yCord[searchedDataKey] = selectedRow[yLabel];
351 // [searchedDataKey][1] contains the y value
352 if (yType == 'numeric') {
353 series[0][searchedDataKey][1] = selectedRow[yLabel];
354 } else if (yType == 'time') {
355 series[0][searchedDataKey][1] =
356 getTimeStamp(selectedRow[yLabel], $('#types_1').val());
358 series[0][searchedDataKey][1] = '';
361 currentChart.series[0].data = series[0];
362 // TODO: axis changing
363 currentChart.replot();
367 //Generate SQL query for update
368 if (!isEmpty(newValues)) {
369 var sql_query = 'UPDATE `' + PMA_commonParams.get('table') + '` SET ';
370 for (key in newValues) {
371 sql_query += '`' + key + '`=';
372 var value = newValues[key];
375 if (value === null) {
376 sql_query += 'NULL, ';
379 } else if ($.trim(value) === '') {
384 // type explicitly identified
385 if (sqlTypes[key] !== null) {
386 if (sqlTypes[key] == 'bit') {
387 sql_query += "b'" + value + "', ";
389 // type not explicitly identified
391 if (!isNumeric(value)) {
392 sql_query += "'" + value + "', ";
394 sql_query += value + ', ';
399 // remove two extraneous characters ', '
400 sql_query = sql_query.substring(0, sql_query.length - 2);
401 sql_query += ' WHERE ' + PMA_urldecode(searchedData[searchedDataKey].where_clause);
403 //Post SQL query to sql.php
405 'token' : PMA_commonParams.get('token'),
406 'server' : PMA_commonParams.get('server'),
407 'db' : PMA_commonParams.get('db'),
408 'ajax_request' : true,
409 'sql_query' : sql_query,
410 'inline_edit' : false
412 if (typeof data !== 'undefined' && data.success === true) {
413 $('#sqlqueryresultsouter').html(data.sql_query);
414 PMA_highlightSQL($('#sqlqueryresultsouter'));
416 PMA_ajaxShowMessage(data.error, false);
419 }//End database update
420 $("#dataDisplay").dialog('close');
422 buttonOptions[PMA_messages.strCancel] = function () {
423 $(this).dialog('close');
425 $("#dataDisplay").dialog({
427 title: PMA_messages.strDataPointContent,
429 buttons: buttonOptions,
430 width: $('#dataDisplay').width() + 80,
432 $(this).find('input[type=checkbox]').css('margin', '0.5em');
436 * Attach Ajax event handlers for input fields
437 * in the dialog. Used to submit the Ajax
438 * request when the ENTER key is pressed.
440 $(document).on('keydown', "#dataDisplay :input", function (e) {
441 if (e.which === 13) { // 13 is the ENTER key
443 if (typeof buttonOptions[PMA_messages.strSave] === 'function') {
444 buttonOptions[PMA_messages.strSave].call();
451 * Generate plot using jqplot
454 if (searchedData !== null) {
455 $('#zoom_search_form')
458 $('#togglesearchformlink')
459 .text(PMA_messages.strShowSearchCriteria);
460 $('#togglesearchformdiv').show();
462 var colorCodes = ['#FF0000', '#00FFFF', '#0000FF', '#0000A0', '#FF0080', '#800080', '#FFFF00', '#00FF00', '#FF00FF'];
468 var xMax; // xAxis extreme max
469 var xMin; // xAxis extreme min
470 var yMax; // yAxis extreme max
471 var yMin; // yAxis extreme min
478 // for a scatter plot
484 background: 'rgba(0,0,0,0)'
488 label: $('#tableid_0').val(),
489 labelRenderer: $.jqplot.CanvasAxisLabelRenderer
492 label: $('#tableid_1').val(),
493 labelRenderer: $.jqplot.CanvasAxisLabelRenderer
500 // hide the first y value
501 formatString: '<span class="hide">%s</span>%s'
510 // If data label is not set, do not show tooltips
511 if (dataLabel === '') {
512 options.highlighter.show = false;
515 // Classify types as either numeric,time,text
516 xType = getType(xType);
517 yType = getType(yType);
519 // could have multiple series but we'll have just one
522 if (xType == 'time') {
523 var originalXType = $('#types_0').val();
524 if (originalXType == 'date') {
527 // TODO: does not seem to work
528 //else if (originalXType == 'time') {
531 // format = '%Y-%m-%d %H:%M';
533 $.extend(options.axes.xaxis, {
534 renderer: $.jqplot.DateAxisRenderer,
540 if (yType == 'time') {
541 var originalYType = $('#types_1').val();
542 if (originalYType == 'date') {
545 $.extend(options.axes.yaxis, {
546 renderer: $.jqplot.DateAxisRenderer,
553 $.each(searchedData, function (key, value) {
554 if (xType == 'numeric') {
555 xVal = parseFloat(value[xLabel]);
557 if (xType == 'time') {
558 xVal = getTimeStamp(value[xLabel], originalXType);
560 if (yType == 'numeric') {
561 yVal = parseFloat(value[yLabel]);
563 if (yType == 'time') {
564 yVal = getTimeStamp(value[yLabel], originalYType);
570 value[dataLabel], // for highlighter
571 // (may set an undefined value)
572 value.where_clause, // for click on point
573 key // key from searchedData
577 // under IE 8, the initial display is mangled; after a manual
579 // under IE 9, everything is fine
580 currentChart = $.jqplot('querychart', series, options);
581 currentChart.resetZoom();
583 $('button.button-reset').click(function (event) {
584 event.preventDefault();
585 currentChart.resetZoom();
588 $('div#resizer').resizable();
589 $('div#resizer').bind('resizestop', function (event, ui) {
590 // make room so that the handle will still appear
591 $('div#querychart').height($('div#resizer').height() * 0.96);
592 $('div#querychart').width($('div#resizer').width() * 0.96);
593 currentChart.replot({resetAxes: true});
596 $('div#querychart').bind('jqplotDataClick',
597 function (event, seriesIndex, pointIndex, data) {
598 searchedDataKey = data[4]; // key from searchedData (global)
601 'ajax_request' : true,
602 'get_data_row' : true,
603 'server' : PMA_commonParams.get('server'),
604 'db' : PMA_commonParams.get('db'),
605 'table' : PMA_commonParams.get('table'),
606 'where_clause' : data[3],
607 'token' : PMA_commonParams.get('token')
610 $.post('tbl_zoom_select.php', post_params, function (data) {
611 // Row is contained in data.row_info,
612 // now fill the displayResultForm with row values
614 for (key in data.row_info) {
615 var $field = $('#edit_fieldID_' + field_id);
616 var $field_null = $('#edit_fields_null_id_' + field_id);
617 if (data.row_info[key] === null) {
618 $field_null.prop('checked', true);
621 $field_null.prop('checked', false);
622 if ($field.attr('multiple')) { // when the column is of type SET
623 $field.val(data.row_info[key].split(','));
625 $field.val(data.row_info[key]);
631 selectedRow = data.row_info;
634 $("#dataDisplay").dialog("open");