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 $("#dataDisplay").dialog({
253 title: 'Data point content',
254 modal: false, //false otherwise other dialogues like timepicker may not function properly
255 height: $('#dataDisplay').height() + 80,
256 width: $('#dataDisplay').width() + 80
260 * Handle submit of zoom_display_form
263 $("#submitForm").click(function(event) {
265 //Prevent default submission of form
266 event.preventDefault();
268 //Find changed values by comparing form values with selectedRow Object
269 var newValues = new Object();//Stores the values changed from original
270 var sqlTypes = new Object();
274 for (key in selectedRow) {
275 var oldVal = selectedRow[key];
276 var newVal = ($('#fields_null_id_' + it).attr('checked')) ? null : $('#fieldID_' + it).val();
277 if (newVal instanceof Array) { // when the column is of type SET
278 newVal = $('#fieldID_' + it).map(function(){
279 return $(this).val();
282 if (oldVal != newVal) {
283 selectedRow[key] = newVal;
284 newValues[key] = newVal;
287 data[currentData][xLabel] = newVal;
288 } else if (key == yLabel) {
290 data[currentData][yLabel] = newVal;
293 var $input = $('#fieldID_' + it);
294 if ($input.hasClass('bit')) {
295 sqlTypes[key] = 'bit';
300 //Update the chart series and replot
301 if (xChange || yChange) {
302 var newSeries = new Array();
303 newSeries[0] = new Object();
304 newSeries[0].marker = {
307 //Logic similar to plot generation, replot only if xAxis changes or yAxis changes.
308 //Code includes a lot of checks so as to replot only when necessary
310 xCord[currentData] = selectedRow[xLabel];
311 if (xType == 'numeric') {
312 currentChart.series[0].data[currentData].update({ x : selectedRow[xLabel] });
313 currentChart.xAxis[0].setExtremes(Array.min(xCord) - 6, Array.max(xCord) + 6);
314 } else if (xType == 'time') {
315 currentChart.series[0].data[currentData].update({
316 x : getTimeStamp(selectedRow[xLabel], $('#types_0').val())
319 var tempX = getCord(xCord);
320 var tempY = getCord(yCord);
322 newSeries[0].data = new Array();
326 $.each(data, function(key, value) {
327 if (yType != 'text') {
328 newSeries[0].data.push({
329 name: value[dataLabel],
332 marker: {fillColor: colorCodes[i % 8]},
336 newSeries[0].data.push({
337 name: value[dataLabel],
340 marker: {fillColor: colorCodes[i % 8]},
346 currentSettings.xAxis.labels = {
347 formatter : function() {
348 if (tempX[1][this.value] && tempX[1][this.value].length > 10) {
349 return tempX[1][this.value].substring(0, 10);
351 return tempX[1][this.value];
355 currentSettings.series = newSeries;
356 currentChart = PMA_createChart(currentSettings);
361 yCord[currentData] = selectedRow[yLabel];
362 if (yType == 'numeric') {
363 currentChart.series[0].data[currentData].update({ y : selectedRow[yLabel] });
364 currentChart.yAxis[0].setExtremes(Array.min(yCord) - 6, Array.max(yCord) + 6);
365 } else if (yType == 'time') {
366 currentChart.series[0].data[currentData].update({
367 y : getTimeStamp(selectedRow[yLabel], $('#types_1').val())
370 var tempX = getCord(xCord);
371 var tempY = getCord(yCord);
373 newSeries[0].data = new Array();
377 $.each(data, function(key, value) {
378 if (xType != 'text' ) {
379 newSeries[0].data.push({
380 name: value[dataLabel],
383 marker: {fillColor: colorCodes[i % 8]},
387 newSeries[0].data.push({
388 name: value[dataLabel],
391 marker: {fillColor: colorCodes[i % 8]},
397 currentSettings.yAxis.labels = {
398 formatter : function() {
399 if (tempY[1][this.value] && tempY[1][this.value].length > 10) {
400 return tempY[1][this.value].substring(0, 10);
402 return tempY[1][this.value];
406 currentSettings.series = newSeries;
407 currentChart = PMA_createChart(currentSettings);
410 currentChart.series[0].data[currentData].select();
413 //Generate SQL query for update
414 if (!isEmpty(newValues)) {
415 var sql_query = 'UPDATE `' + window.parent.table + '` SET ';
416 for (key in newValues) {
417 sql_query += '`' + key + '`=' ;
418 var value = newValues[key];
422 sql_query += 'NULL, ';
425 } else if ($.trim(value) == '') {
430 // type explicitly identified
431 if (sqlTypes[key] != null) {
432 if (sqlTypes[key] == 'bit') {
433 sql_query += "b'" + value + "', ";
435 // type not explicitly identified
437 if (!isNumeric(value)) {
438 sql_query += "'" + value + "', ";
440 sql_query += value + ', ';
445 sql_query = sql_query.substring(0, sql_query.length - 2);
446 sql_query += ' WHERE ' + PMA_urldecode(data[currentData]['where_clause']);
448 //Post SQL query to sql.php
450 'token' : window.parent.token,
451 'db' : window.parent.db,
452 'ajax_request' : true,
453 'sql_query' : sql_query,
454 'inline_edit' : false
456 if (data.success == true) {
457 $('#sqlqueryresults').html(data.sql_query);
458 $("#sqlqueryresults").trigger('appendAnchor');
460 PMA_ajaxShowMessage(data.error);
463 }//End database update
464 $("#dataDisplay").dialog("close");
465 });//End submit handler
468 * Generate plot using Highcharts
472 $('#zoom_search_form')
475 $('#togglesearchformlink')
476 .text(PMA_messages['strShowSearchCriteria']);
477 $('#togglesearchformdiv').show();
479 var colorCodes = ['#FF0000', '#00FFFF', '#0000FF', '#0000A0', '#FF0080', '#800080', '#FFFF00', '#00FF00', '#FF00FF'];
480 var series = new Array();
481 var xCord = new Array();
482 var yCord = new Array();
485 var xMax; // xAxis extreme max
486 var xMin; // xAxis extreme min
487 var yMax; // yAxis extreme max
488 var yMin; // yAxis extreme min
490 // Set the basic plot settings
491 var currentSettings = {
493 renderTo: 'querychart',
496 width:$('#resizer').width() - 3,
497 height:$('#resizer').height() - 20
502 exporting: { enabled: false },
503 label: { text: $('#dataLabel').val() },
506 allowPointSelect: true,
518 // Make AJAX request to tbl_zoom_select.php for getting the complete row info
520 'ajax_request' : true,
521 'get_data_row' : true,
522 'db' : window.parent.db,
523 'table' : window.parent.table,
524 'where_clause' : data[id]['where_clause'],
525 'token' : window.parent.token
527 $.post('tbl_zoom_select.php', post_params, function(data) {
528 // Row is contained in data.row_info, now fill the displayResultForm with row values
529 for (key in data.row_info) {
530 $field = $('#fieldID_' + fid);
531 $field_null = $('#fields_null_id_' + fid);
532 if (data.row_info[key] == null) {
533 $field_null.attr('checked', true);
536 $field_null.attr('checked', false);
537 if ($field.attr('multiple')) { // when the column is of type SET
538 $field.val(data.row_info[key].split(','));
540 $field.val(data.row_info[key]);
545 selectedRow = new Object();
546 selectedRow = data.row_info;
549 $("#dataDisplay").dialog("open");
556 formatter: function() {
557 return this.point.name;
560 title: { text: 'Query Results' },
562 title: { text: $('#tableid_0').val() },
564 setExtremes: function(e) {
565 this.resetZoom.show();
572 title: { text: $('#tableid_1').val() },
576 setExtremes: function(e) {
577 this.resetZoom.show();
583 // If data label is not set, do not show tooltips
584 if (dataLabel == '') {
585 currentSettings.tooltip.enabled = false;
588 $('#resizer').resizable({
590 currentChart.setSize(
591 this.offsetWidth - 3,
592 this.offsetHeight - 20,
598 // Classify types as either numeric,time,text
599 xType = getType(xType);
600 yType = getType(yType);
602 //Set the axis type based on the field
603 currentSettings.xAxis.type = (xType == 'time') ? 'datetime' : 'linear';
604 currentSettings.yAxis.type = (yType == 'time') ? 'datetime' : 'linear';
606 // Formulate series data for plot
607 series[0] = new Object();
608 series[0].data = new Array();
612 if (xType != 'text' && yType != 'text') {
613 $.each(data, function(key, value) {
614 var xVal = (xType == 'numeric') ? value[xLabel] : getTimeStamp(value[xLabel], $('#types_0').val());
615 var yVal = (yType == 'numeric') ? value[yLabel] : getTimeStamp(value[yLabel], $('#types_1').val());
616 series[0].data.push({
617 name: value[dataLabel],
620 marker: {fillColor: colorCodes[it % 8]},
623 xCord.push(value[xLabel]);
624 yCord.push(value[yLabel]);
627 if (xType == 'numeric') {
628 currentSettings.xAxis.max = Array.max(xCord) + 6;
629 currentSettings.xAxis.min = Array.min(xCord) - 6;
631 currentSettings.xAxis.labels = { formatter : function() {
632 return getDate(this.value, $('#types_0').val());
635 if (yType == 'numeric') {
636 currentSettings.yAxis.max = Array.max(yCord) + 6;
637 currentSettings.yAxis.min = Array.min(yCord) - 6;
639 currentSettings.yAxis.labels = { formatter : function() {
640 return getDate(this.value, $('#types_1').val());
644 } else if (xType == 'text' && yType != 'text') {
645 $.each(data, function(key, value) {
646 xCord.push(value[xLabel]);
647 yCord.push(value[yLabel]);
650 tempX = getCord(xCord);
651 $.each(data, function(key, value) {
652 var yVal = (yType == 'numeric') ? value[yLabel] : getTimeStamp(value[yLabel], $('#types_1').val());
653 series[0].data.push({
654 name: value[dataLabel],
657 marker: {fillColor: colorCodes[it % 8]},
663 currentSettings.xAxis.labels = {
664 formatter : function() {
665 if (tempX[1][this.value] && tempX[1][this.value].length > 10) {
666 return tempX[1][this.value].substring(0, 10);
668 return tempX[1][this.value];
672 if (yType == 'numeric') {
673 currentSettings.yAxis.max = Array.max(yCord) + 6;
674 currentSettings.yAxis.min = Array.min(yCord) - 6;
676 currentSettings.yAxis.labels = {
677 formatter : function() {
678 return getDate(this.value, $('#types_1').val());
684 } else if (xType != 'text' && yType == 'text') {
685 $.each(data, function(key, value) {
686 xCord.push(value[xLabel]);
687 yCord.push(value[yLabel]);
689 tempY = getCord(yCord);
690 $.each(data, function(key, value) {
691 var xVal = (xType == 'numeric') ? value[xLabel] : getTimeStamp(value[xLabel], $('#types_0').val());
692 series[0].data.push({
693 name: value[dataLabel],
696 marker: {fillColor: colorCodes[it % 8]},
701 if (xType == 'numeric') {
702 currentSettings.xAxis.max = Array.max(xCord) + 6;
703 currentSettings.xAxis.min = Array.min(xCord) - 6;
705 currentSettings.xAxis.labels = {
706 formatter : function() {
707 return getDate(this.value, $('#types_0').val());
711 currentSettings.yAxis.labels = {
712 formatter : function() {
713 if (tempY[1][this.value] && tempY[1][this.value].length > 10) {
714 return tempY[1][this.value].substring(0, 10);
716 return tempY[1][this.value];
722 } else if (xType == 'text' && yType == 'text') {
723 $.each(data, function(key, value) {
724 xCord.push(value[xLabel]);
725 yCord.push(value[yLabel]);
727 tempX = getCord(xCord);
728 tempY = getCord(yCord);
729 $.each(data, function(key, value) {
730 series[0].data.push({
731 name: value[dataLabel],
734 marker: {fillColor: colorCodes[it % 8]},
739 currentSettings.xAxis.labels = {
740 formatter : function() {
741 if (tempX[1][this.value] && tempX[1][this.value].length > 10) {
742 return tempX[1][this.value].substring(0, 10);
744 return tempX[1][this.value];
748 currentSettings.yAxis.labels = {
749 formatter : function() {
750 if (tempY[1][this.value] && tempY[1][this.value].length > 10) {
751 return tempY[1][this.value].substring(0, 10);
753 return tempY[1][this.value];
761 currentSettings.series = series;
762 currentChart = PMA_createChart(currentSettings);
763 xMin = currentChart.xAxis[0].getExtremes().min;
764 xMax = currentChart.xAxis[0].getExtremes().max;
765 yMin = currentChart.yAxis[0].getExtremes().min;
766 yMax = currentChart.yAxis[0].getExtremes().max;
767 includePan(currentChart); //Enable panning feature
768 var setZoom = function() {
769 var newxm = xMin + (xMax - xMin) * (1 - zoomRatio) / 2;
770 var newxM = xMax - (xMax - xMin) * (1 - zoomRatio) / 2;
771 var newym = yMin + (yMax - yMin) * (1 - zoomRatio) / 2;
772 var newyM = yMax - (yMax - yMin) * (1 - zoomRatio) / 2;
773 currentChart.xAxis[0].setExtremes(newxm, newxM);
774 currentChart.yAxis[0].setExtremes(newym, newyM);
777 //Enable zoom feature
778 $("#querychart").mousewheel(function(objEvent, intDelta) {
780 if (zoomRatio > 0.1) {
781 zoomRatio = zoomRatio - 0.1;
784 } else if (intDelta < 0) {
785 zoomRatio = zoomRatio + 0.1;
790 //Add reset zoom feature
791 currentChart.yAxis[0].resetZoom = currentChart.xAxis[0].resetZoom = $('<a href="#">Reset zoom</a>')
792 .appendTo(currentChart.container)
794 position: 'absolute',
800 currentChart.xAxis[0].setExtremes(null, null);
801 currentChart.yAxis[0].setExtremes(null, null);
802 this.style.display = 'none';