1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 ** @fileoverview JavaScript functions used on tbl_select.php
6 ** @requires js/functions.js
13 function displayHelp() {
14 var msgbox = PMA_ajaxShowMessage(PMA_messages['strDisplayHelp'], 10000);
15 msgbox.click(function() {
16 PMA_ajaxRemoveMessage(msgbox);
21 ** Extend the array object for max function
24 Array.max = function (array) {
25 return Math.max.apply( Math, array );
29 ** Extend the array object for min function
32 Array.min = function (array) {
33 return Math.min.apply( Math, array );
37 ** Checks if a string contains only numeric value
38 ** @param n: String (to be checked)
40 function isNumeric(n) {
41 return !isNaN(parseFloat(n)) && isFinite(n);
45 ** Checks if an object is empty
46 ** @param n: Object (to be checked)
48 function isEmpty(obj) {
57 ** Converts a timestamp into the format of its field type
58 ** @param val Integer Timestamp
59 ** @param type String Field type(datetime/timestamp/time/date)
61 function getDate(val, type) {
62 if (type.toString().search(/datetime/i) != -1 || type.toString().search(/timestamp/i) != -1) {
63 return Highcharts.dateFormat('%Y-%m-%e %H:%M:%S', val);
65 else if (type.toString().search(/time/i) != -1) {
66 return Highcharts.dateFormat('%H:%M:%S', val);
68 else if (type.toString().search(/date/i) != -1) {
69 return Highcharts.dateFormat('%Y-%m-%e', val);
74 ** Converts a date/time into timestamp
75 ** @param val String Date
76 ** @param type Sring Field type(datetime/timestamp/time/date)
78 function getTimeStamp(val, type) {
79 if (type.toString().search(/datetime/i) != -1 || type.toString().search(/timestamp/i) != -1) {
80 return getDateFromFormat(val, 'yyyy-MM-dd HH:mm:ss', val);
82 else if (type.toString().search(/time/i) != -1) {
83 return getDateFromFormat('1970-01-01 ' + val, 'yyyy-MM-dd HH:mm:ss');
85 else if (type.toString().search(/date/i) != -1) {
86 return getDateFromFormat(val, 'yyyy-MM-dd');
91 ** Classifies the field type into numeric,timeseries or text
92 ** @param field: field type (as in database structure)
94 function getType(field) {
95 if (field.toString().search(/int/i) != -1 || field.toString().search(/decimal/i) != -1
96 || field.toString().search(/year/i) != -1) {
98 } else if (field.toString().search(/time/i) != -1 || field.toString().search(/date/i) != -1) {
105 ** Converts a categorical array into numeric array
106 ** @param array categorical values array
108 function getCord(arr) {
109 var newCord = new Array();
110 var original = $.extend(true, [], arr);
111 var arr = jQuery.unique(arr).sort();
112 $.each(original, function(index, value) {
113 newCord.push(jQuery.inArray(value, arr));
115 return [newCord, arr, original];
119 ** Scrolls the view to the display section
121 function scrollToChart() {
122 var x = $('#dataDisplay').offset().top - 100; // 100 provides buffer in viewport
123 $('html,body').animate({scrollTop: x}, 500);
127 ** Handlers for panning feature
129 function includePan(currentChart) {
133 var chartWidth = $('#resizer').width() - 3;
134 var chartHeight = $('#resizer').height() - 20;
135 $('#querychart').mousedown(function() {
139 $('#querychart').mouseup(function() {
142 $('#querychart').mousemove(function(e) {
143 if (mouseDown == 1) {
144 if (e.pageX > lastX) {
145 var xExtremes = currentChart.xAxis[0].getExtremes();
146 var diff = (e.pageX - lastX) * (xExtremes.max - xExtremes.min) / chartWidth;
147 currentChart.xAxis[0].setExtremes(xExtremes.min - diff, xExtremes.max - diff);
148 } else if (e.pageX < lastX) {
149 var xExtremes = currentChart.xAxis[0].getExtremes();
150 var diff = (lastX - e.pageX) * (xExtremes.max - xExtremes.min) / chartWidth;
151 currentChart.xAxis[0].setExtremes(xExtremes.min + diff, xExtremes.max + diff);
154 if (e.pageY > lastY) {
155 var yExtremes = currentChart.yAxis[0].getExtremes();
156 var ydiff = 1.0 * (e.pageY - lastY) * (yExtremes.max - yExtremes.min) / chartHeight;
157 currentChart.yAxis[0].setExtremes(yExtremes.min + ydiff, yExtremes.max + ydiff);
158 } else if (e.pageY < lastY) {
159 var yExtremes = currentChart.yAxis[0].getExtremes();
160 var ydiff = 1.0 * (lastY - e.pageY) * (yExtremes.max - yExtremes.min) / chartHeight;
161 currentChart.yAxis[0].setExtremes(yExtremes.min - ydiff, yExtremes.max - ydiff);
169 $(document).ready(function() {
172 ** Set a parameter for all Ajax queries made on this page. Don't let the
173 ** web server serve cached pages
179 var cursorMode = ($("input[name='mode']:checked").val() == 'edit') ? 'crosshair' : 'pointer';
180 var currentChart = null;
181 var currentData = null;
182 var xLabel = $('#tableid_0').val();
183 var yLabel = $('#tableid_1').val();
184 var xType = $('#types_0').val();
185 var yType = $('#types_1').val();
186 var dataLabel = $('#dataLabel').val();
193 var data = jQuery.parseJSON($('#querydata').html());
196 ** Input form submit on field change
198 $('#tableid_0').change(function() {
199 $('#zoom_search_form').submit();
202 $('#tableid_1').change(function() {
203 $('#zoom_search_form').submit();
206 $('#tableid_2').change(function() {
207 $('#zoom_search_form').submit();
210 $('#tableid_3').change(function() {
211 $('#zoom_search_form').submit();
215 * Input form validation
217 $('#inputFormSubmitId').click(function() {
218 if ($('#tableid_0').get(0).selectedIndex == 0 || $('#tableid_1').get(0).selectedIndex == 0) {
219 PMA_ajaxShowMessage(PMA_messages['strInputNull']);
220 } else if (xLabel == yLabel) {
221 PMA_ajaxShowMessage(PMA_messages['strSameInputs']);
226 ** Prepare a div containing a link, otherwise it's incorrectly displayed
227 ** after a couple of clicks
229 $('<div id="togglesearchformdiv"><a id="togglesearchformlink"></a></div>')
230 .insertAfter('#zoom_search_form')
231 // don't show it until we have results on-screen
234 $('#togglesearchformlink')
235 .html(PMA_messages['strShowSearchCriteria'])
236 .bind('click', function() {
238 $('#zoom_search_form').slideToggle();
239 if ($link.text() == PMA_messages['strHideSearchCriteria']) {
240 $link.text(PMA_messages['strShowSearchCriteria']);
242 $link.text(PMA_messages['strHideSearchCriteria']);
244 // avoid default click action
249 ** Set dialog properties for the data display form
251 var buttonOptions = {};
253 * Handle saving of a row in the editor
255 buttonOptions[PMA_messages['strSave']] = function () {
256 //Find changed values by comparing form values with selectedRow Object
257 var newValues = new Object();//Stores the values changed from original
258 var sqlTypes = new Object();
262 for (key in selectedRow) {
263 var oldVal = selectedRow[key];
264 var newVal = ($('#fields_null_id_' + it).attr('checked')) ? null : $('#fieldID_' + it).val();
265 if (newVal instanceof Array) { // when the column is of type SET
266 newVal = $('#fieldID_' + it).map(function(){
267 return $(this).val();
270 if (oldVal != newVal) {
271 selectedRow[key] = newVal;
272 newValues[key] = newVal;
275 data[currentData][xLabel] = newVal;
276 } else if (key == yLabel) {
278 data[currentData][yLabel] = newVal;
281 var $input = $('#fieldID_' + it);
282 if ($input.hasClass('bit')) {
283 sqlTypes[key] = 'bit';
288 //Update the chart series and replot
289 if (xChange || yChange) {
290 var newSeries = new Array();
291 newSeries[0] = new Object();
292 newSeries[0].marker = {
295 //Logic similar to plot generation, replot only if xAxis changes or yAxis changes.
296 //Code includes a lot of checks so as to replot only when necessary
298 xCord[currentData] = selectedRow[xLabel];
299 if (xType == 'numeric') {
300 currentChart.series[0].data[currentData].update({ x : selectedRow[xLabel] });
301 currentChart.xAxis[0].setExtremes(Array.min(xCord) - 6, Array.max(xCord) + 6);
302 } else if (xType == 'time') {
303 currentChart.series[0].data[currentData].update({
304 x : getTimeStamp(selectedRow[xLabel], $('#types_0').val())
307 var tempX = getCord(xCord);
308 var tempY = getCord(yCord);
310 newSeries[0].data = new Array();
314 $.each(data, function(key, value) {
315 if (yType != 'text') {
316 newSeries[0].data.push({
317 name: value[dataLabel],
320 marker: {fillColor: colorCodes[i % 8]},
324 newSeries[0].data.push({
325 name: value[dataLabel],
328 marker: {fillColor: colorCodes[i % 8]},
334 currentSettings.xAxis.labels = {
335 formatter : function() {
336 if (tempX[1][this.value] && tempX[1][this.value].length > 10) {
337 return tempX[1][this.value].substring(0, 10);
339 return tempX[1][this.value];
343 currentSettings.series = newSeries;
344 currentChart = PMA_createChart(currentSettings);
349 yCord[currentData] = selectedRow[yLabel];
350 if (yType == 'numeric') {
351 currentChart.series[0].data[currentData].update({ y : selectedRow[yLabel] });
352 currentChart.yAxis[0].setExtremes(Array.min(yCord) - 6, Array.max(yCord) + 6);
353 } else if (yType == 'time') {
354 currentChart.series[0].data[currentData].update({
355 y : getTimeStamp(selectedRow[yLabel], $('#types_1').val())
358 var tempX = getCord(xCord);
359 var tempY = getCord(yCord);
361 newSeries[0].data = new Array();
365 $.each(data, function(key, value) {
366 if (xType != 'text' ) {
367 newSeries[0].data.push({
368 name: value[dataLabel],
371 marker: {fillColor: colorCodes[i % 8]},
375 newSeries[0].data.push({
376 name: value[dataLabel],
379 marker: {fillColor: colorCodes[i % 8]},
385 currentSettings.yAxis.labels = {
386 formatter : function() {
387 if (tempY[1][this.value] && tempY[1][this.value].length > 10) {
388 return tempY[1][this.value].substring(0, 10);
390 return tempY[1][this.value];
394 currentSettings.series = newSeries;
395 currentChart = PMA_createChart(currentSettings);
398 currentChart.series[0].data[currentData].select();
401 //Generate SQL query for update
402 if (!isEmpty(newValues)) {
403 var sql_query = 'UPDATE `' + window.parent.table + '` SET ';
404 for (key in newValues) {
405 sql_query += '`' + key + '`=' ;
406 var value = newValues[key];
410 sql_query += 'NULL, ';
413 } else if ($.trim(value) == '') {
418 // type explicitly identified
419 if (sqlTypes[key] != null) {
420 if (sqlTypes[key] == 'bit') {
421 sql_query += "b'" + value + "', ";
423 // type not explicitly identified
425 if (!isNumeric(value)) {
426 sql_query += "'" + value + "', ";
428 sql_query += value + ', ';
433 sql_query = sql_query.substring(0, sql_query.length - 2);
434 sql_query += ' WHERE ' + PMA_urldecode(data[currentData]['where_clause']);
436 //Post SQL query to sql.php
438 'token' : window.parent.token,
439 'db' : window.parent.db,
440 'ajax_request' : true,
441 'sql_query' : sql_query,
442 'inline_edit' : false
444 if (data.success == true) {
445 $('#sqlqueryresults').html(data.sql_query);
446 $("#sqlqueryresults").trigger('appendAnchor');
448 PMA_ajaxShowMessage(data.error);
451 }//End database update
452 $("#dataDisplay").dialog('close');
454 buttonOptions[PMA_messages['strCancel']] = function () {
455 $(this).dialog('close');
457 $("#dataDisplay").dialog({
459 title: 'Data point content',
461 buttons: buttonOptions,
462 width: $('#dataDisplay').width() + 24,
464 $(this).find('input[type=checkbox]').css('margin', '0.5em');
468 * Attach Ajax event handlers for input fields
469 * in the dialog. Used to submit the Ajax
470 * request when the ENTER key is pressed.
472 $("#dataDisplay").find(':input').live('keydown', function (e) {
473 if (e.which === 13) { // 13 is the ENTER key
475 if (typeof buttonOptions[PMA_messages['strSave']] === 'function') {
476 buttonOptions[PMA_messages['strSave']].call();
483 * Generate plot using Highcharts
487 $('#zoom_search_form')
490 $('#togglesearchformlink')
491 .text(PMA_messages['strShowSearchCriteria']);
492 $('#togglesearchformdiv').show();
494 var colorCodes = ['#FF0000', '#00FFFF', '#0000FF', '#0000A0', '#FF0080', '#800080', '#FFFF00', '#00FF00', '#FF00FF'];
495 var series = new Array();
496 var xCord = new Array();
497 var yCord = new Array();
500 var xMax; // xAxis extreme max
501 var xMin; // xAxis extreme min
502 var yMax; // yAxis extreme max
503 var yMin; // yAxis extreme min
505 // Set the basic plot settings
506 var currentSettings = {
508 renderTo: 'querychart',
511 width:$('#resizer').width() - 3,
512 height:$('#resizer').height() - 20
517 exporting: { enabled: false },
518 label: { text: $('#dataLabel').val() },
521 allowPointSelect: true,
533 // Make AJAX request to tbl_zoom_select.php for getting the complete row info
535 'ajax_request' : true,
536 'get_data_row' : true,
537 'db' : window.parent.db,
538 'table' : window.parent.table,
539 'where_clause' : data[id]['where_clause'],
540 'token' : window.parent.token
542 $.post('tbl_zoom_select.php', post_params, function(data) {
543 // Row is contained in data.row_info, now fill the displayResultForm with row values
544 for (key in data.row_info) {
545 $field = $('#fieldID_' + fid);
546 $field_null = $('#fields_null_id_' + fid);
547 if (data.row_info[key] == null) {
548 $field_null.attr('checked', true);
551 $field_null.attr('checked', false);
552 if ($field.attr('multiple')) { // when the column is of type SET
553 $field.val(data.row_info[key].split(','));
555 $field.val(data.row_info[key]);
560 selectedRow = new Object();
561 selectedRow = data.row_info;
564 $("#dataDisplay").dialog("open");
571 formatter: function() {
572 return this.point.name;
575 title: { text: 'Query Results' },
577 title: { text: $('#tableid_0').val() },
579 setExtremes: function(e) {
580 this.resetZoom.show();
587 title: { text: $('#tableid_1').val() },
591 setExtremes: function(e) {
592 this.resetZoom.show();
598 // If data label is not set, do not show tooltips
599 if (dataLabel == '') {
600 currentSettings.tooltip.enabled = false;
603 $('#resizer').resizable({
605 currentChart.setSize(
606 this.offsetWidth - 3,
607 this.offsetHeight - 20,
613 // Classify types as either numeric,time,text
614 xType = getType(xType);
615 yType = getType(yType);
617 //Set the axis type based on the field
618 currentSettings.xAxis.type = (xType == 'time') ? 'datetime' : 'linear';
619 currentSettings.yAxis.type = (yType == 'time') ? 'datetime' : 'linear';
621 // Formulate series data for plot
622 series[0] = new Object();
623 series[0].data = new Array();
627 if (xType != 'text' && yType != 'text') {
628 $.each(data, function(key, value) {
629 var xVal = (xType == 'numeric') ? value[xLabel] : getTimeStamp(value[xLabel], $('#types_0').val());
630 var yVal = (yType == 'numeric') ? value[yLabel] : getTimeStamp(value[yLabel], $('#types_1').val());
631 series[0].data.push({
632 name: value[dataLabel],
635 marker: {fillColor: colorCodes[it % 8]},
638 xCord.push(value[xLabel]);
639 yCord.push(value[yLabel]);
642 if (xType == 'numeric') {
643 currentSettings.xAxis.max = Array.max(xCord) + 6;
644 currentSettings.xAxis.min = Array.min(xCord) - 6;
646 currentSettings.xAxis.labels = { formatter : function() {
647 return getDate(this.value, $('#types_0').val());
650 if (yType == 'numeric') {
651 currentSettings.yAxis.max = Array.max(yCord) + 6;
652 currentSettings.yAxis.min = Array.min(yCord) - 6;
654 currentSettings.yAxis.labels = { formatter : function() {
655 return getDate(this.value, $('#types_1').val());
659 } else if (xType == 'text' && yType != 'text') {
660 $.each(data, function(key, value) {
661 xCord.push(value[xLabel]);
662 yCord.push(value[yLabel]);
665 tempX = getCord(xCord);
666 $.each(data, function(key, value) {
667 var yVal = (yType == 'numeric') ? value[yLabel] : getTimeStamp(value[yLabel], $('#types_1').val());
668 series[0].data.push({
669 name: value[dataLabel],
672 marker: {fillColor: colorCodes[it % 8]},
678 currentSettings.xAxis.labels = {
679 formatter : function() {
680 if (tempX[1][this.value] && tempX[1][this.value].length > 10) {
681 return tempX[1][this.value].substring(0, 10);
683 return tempX[1][this.value];
687 if (yType == 'numeric') {
688 currentSettings.yAxis.max = Array.max(yCord) + 6;
689 currentSettings.yAxis.min = Array.min(yCord) - 6;
691 currentSettings.yAxis.labels = {
692 formatter : function() {
693 return getDate(this.value, $('#types_1').val());
699 } else if (xType != 'text' && yType == 'text') {
700 $.each(data, function(key, value) {
701 xCord.push(value[xLabel]);
702 yCord.push(value[yLabel]);
704 tempY = getCord(yCord);
705 $.each(data, function(key, value) {
706 var xVal = (xType == 'numeric') ? value[xLabel] : getTimeStamp(value[xLabel], $('#types_0').val());
707 series[0].data.push({
708 name: value[dataLabel],
711 marker: {fillColor: colorCodes[it % 8]},
716 if (xType == 'numeric') {
717 currentSettings.xAxis.max = Array.max(xCord) + 6;
718 currentSettings.xAxis.min = Array.min(xCord) - 6;
720 currentSettings.xAxis.labels = {
721 formatter : function() {
722 return getDate(this.value, $('#types_0').val());
726 currentSettings.yAxis.labels = {
727 formatter : function() {
728 if (tempY[1][this.value] && tempY[1][this.value].length > 10) {
729 return tempY[1][this.value].substring(0, 10);
731 return tempY[1][this.value];
737 } else if (xType == 'text' && yType == 'text') {
738 $.each(data, function(key, value) {
739 xCord.push(value[xLabel]);
740 yCord.push(value[yLabel]);
742 tempX = getCord(xCord);
743 tempY = getCord(yCord);
744 $.each(data, function(key, value) {
745 series[0].data.push({
746 name: value[dataLabel],
749 marker: {fillColor: colorCodes[it % 8]},
754 currentSettings.xAxis.labels = {
755 formatter : function() {
756 if (tempX[1][this.value] && tempX[1][this.value].length > 10) {
757 return tempX[1][this.value].substring(0, 10);
759 return tempX[1][this.value];
763 currentSettings.yAxis.labels = {
764 formatter : function() {
765 if (tempY[1][this.value] && tempY[1][this.value].length > 10) {
766 return tempY[1][this.value].substring(0, 10);
768 return tempY[1][this.value];
776 currentSettings.series = series;
777 currentChart = PMA_createChart(currentSettings);
778 xMin = currentChart.xAxis[0].getExtremes().min;
779 xMax = currentChart.xAxis[0].getExtremes().max;
780 yMin = currentChart.yAxis[0].getExtremes().min;
781 yMax = currentChart.yAxis[0].getExtremes().max;
782 includePan(currentChart); //Enable panning feature
783 var setZoom = function() {
784 var newxm = xMin + (xMax - xMin) * (1 - zoomRatio) / 2;
785 var newxM = xMax - (xMax - xMin) * (1 - zoomRatio) / 2;
786 var newym = yMin + (yMax - yMin) * (1 - zoomRatio) / 2;
787 var newyM = yMax - (yMax - yMin) * (1 - zoomRatio) / 2;
788 currentChart.xAxis[0].setExtremes(newxm, newxM);
789 currentChart.yAxis[0].setExtremes(newym, newyM);
792 //Enable zoom feature
793 $("#querychart").mousewheel(function(objEvent, intDelta) {
795 if (zoomRatio > 0.1) {
796 zoomRatio = zoomRatio - 0.1;
799 } else if (intDelta < 0) {
800 zoomRatio = zoomRatio + 0.1;
805 //Add reset zoom feature
806 currentChart.yAxis[0].resetZoom = currentChart.xAxis[0].resetZoom = $('<a href="#">Reset zoom</a>')
807 .appendTo(currentChart.container)
809 position: 'absolute',
815 currentChart.xAxis[0].setExtremes(null, null);
816 currentChart.yAxis[0].setExtremes(null, null);
817 this.style.display = 'none';