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() {
170 var cursorMode = ($("input[name='mode']:checked").val() == 'edit') ? 'crosshair' : 'pointer';
171 var currentChart = null;
172 var currentData = null;
173 var xLabel = $('#tableid_0').val();
174 var yLabel = $('#tableid_1').val();
175 var xType = $('#types_0').val();
176 var yType = $('#types_1').val();
177 var dataLabel = $('#dataLabel').val();
184 var data = jQuery.parseJSON($('#querydata').html());
187 ** Input form submit on field change
189 $('#tableid_0').change(function() {
190 $('#zoom_search_form').submit();
193 $('#tableid_1').change(function() {
194 $('#zoom_search_form').submit();
197 $('#tableid_2').change(function() {
198 $('#zoom_search_form').submit();
201 $('#tableid_3').change(function() {
202 $('#zoom_search_form').submit();
206 * Input form validation
208 $('#inputFormSubmitId').click(function() {
209 if ($('#tableid_0').get(0).selectedIndex == 0 || $('#tableid_1').get(0).selectedIndex == 0) {
210 PMA_ajaxShowMessage(PMA_messages['strInputNull']);
211 } else if (xLabel == yLabel) {
212 PMA_ajaxShowMessage(PMA_messages['strSameInputs']);
217 ** Prepare a div containing a link, otherwise it's incorrectly displayed
218 ** after a couple of clicks
220 $('<div id="togglesearchformdiv"><a id="togglesearchformlink"></a></div>')
221 .insertAfter('#zoom_search_form')
222 // don't show it until we have results on-screen
225 $('#togglesearchformlink')
226 .html(PMA_messages['strShowSearchCriteria'])
227 .bind('click', function() {
229 $('#zoom_search_form').slideToggle();
230 if ($link.text() == PMA_messages['strHideSearchCriteria']) {
231 $link.text(PMA_messages['strShowSearchCriteria']);
233 $link.text(PMA_messages['strHideSearchCriteria']);
235 // avoid default click action
240 ** Set dialog properties for the data display form
242 var buttonOptions = {};
244 * Handle saving of a row in the editor
246 buttonOptions[PMA_messages['strSave']] = function () {
247 //Find changed values by comparing form values with selectedRow Object
248 var newValues = new Object();//Stores the values changed from original
249 var sqlTypes = new Object();
253 for (key in selectedRow) {
254 var oldVal = selectedRow[key];
255 var newVal = ($('#fields_null_id_' + it).attr('checked')) ? null : $('#fieldID_' + it).val();
256 if (newVal instanceof Array) { // when the column is of type SET
257 newVal = $('#fieldID_' + it).map(function(){
258 return $(this).val();
261 if (oldVal != newVal) {
262 selectedRow[key] = newVal;
263 newValues[key] = newVal;
266 data[currentData][xLabel] = newVal;
267 } else if (key == yLabel) {
269 data[currentData][yLabel] = newVal;
272 var $input = $('#fieldID_' + it);
273 if ($input.hasClass('bit')) {
274 sqlTypes[key] = 'bit';
279 //Update the chart series and replot
280 if (xChange || yChange) {
281 var newSeries = new Array();
282 newSeries[0] = new Object();
283 newSeries[0].marker = {
286 //Logic similar to plot generation, replot only if xAxis changes or yAxis changes.
287 //Code includes a lot of checks so as to replot only when necessary
289 xCord[currentData] = selectedRow[xLabel];
290 if (xType == 'numeric') {
291 currentChart.series[0].data[currentData].update({ x : selectedRow[xLabel] });
292 currentChart.xAxis[0].setExtremes(Array.min(xCord) - 6, Array.max(xCord) + 6);
293 } else if (xType == 'time') {
294 currentChart.series[0].data[currentData].update({
295 x : getTimeStamp(selectedRow[xLabel], $('#types_0').val())
298 var tempX = getCord(xCord);
299 var tempY = getCord(yCord);
301 newSeries[0].data = new Array();
305 $.each(data, function(key, value) {
306 if (yType != 'text') {
307 newSeries[0].data.push({
308 name: value[dataLabel],
311 marker: {fillColor: colorCodes[i % 8]},
315 newSeries[0].data.push({
316 name: value[dataLabel],
319 marker: {fillColor: colorCodes[i % 8]},
325 currentSettings.xAxis.labels = {
326 formatter : function() {
327 if (tempX[1][this.value] && tempX[1][this.value].length > 10) {
328 return tempX[1][this.value].substring(0, 10);
330 return tempX[1][this.value];
334 currentSettings.series = newSeries;
335 currentChart = PMA_createChart(currentSettings);
340 yCord[currentData] = selectedRow[yLabel];
341 if (yType == 'numeric') {
342 currentChart.series[0].data[currentData].update({ y : selectedRow[yLabel] });
343 currentChart.yAxis[0].setExtremes(Array.min(yCord) - 6, Array.max(yCord) + 6);
344 } else if (yType == 'time') {
345 currentChart.series[0].data[currentData].update({
346 y : getTimeStamp(selectedRow[yLabel], $('#types_1').val())
349 var tempX = getCord(xCord);
350 var tempY = getCord(yCord);
352 newSeries[0].data = new Array();
356 $.each(data, function(key, value) {
357 if (xType != 'text' ) {
358 newSeries[0].data.push({
359 name: value[dataLabel],
362 marker: {fillColor: colorCodes[i % 8]},
366 newSeries[0].data.push({
367 name: value[dataLabel],
370 marker: {fillColor: colorCodes[i % 8]},
376 currentSettings.yAxis.labels = {
377 formatter : function() {
378 if (tempY[1][this.value] && tempY[1][this.value].length > 10) {
379 return tempY[1][this.value].substring(0, 10);
381 return tempY[1][this.value];
385 currentSettings.series = newSeries;
386 currentChart = PMA_createChart(currentSettings);
389 currentChart.series[0].data[currentData].select();
392 //Generate SQL query for update
393 if (!isEmpty(newValues)) {
394 var sql_query = 'UPDATE `' + window.parent.table + '` SET ';
395 for (key in newValues) {
396 sql_query += '`' + key + '`=' ;
397 var value = newValues[key];
401 sql_query += 'NULL, ';
404 } else if ($.trim(value) == '') {
409 // type explicitly identified
410 if (sqlTypes[key] != null) {
411 if (sqlTypes[key] == 'bit') {
412 sql_query += "b'" + value + "', ";
414 // type not explicitly identified
416 if (!isNumeric(value)) {
417 sql_query += "'" + value + "', ";
419 sql_query += value + ', ';
424 sql_query = sql_query.substring(0, sql_query.length - 2);
425 sql_query += ' WHERE ' + PMA_urldecode(data[currentData]['where_clause']);
427 //Post SQL query to sql.php
429 'token' : window.parent.token,
430 'db' : window.parent.db,
431 'ajax_request' : true,
432 'sql_query' : sql_query,
433 'inline_edit' : false
435 if (data.success == true) {
436 $('#sqlqueryresults').html(data.sql_query);
437 $("#sqlqueryresults").trigger('appendAnchor');
439 PMA_ajaxShowMessage(data.error, false);
442 }//End database update
443 $("#dataDisplay").dialog('close');
445 buttonOptions[PMA_messages['strCancel']] = function () {
446 $(this).dialog('close');
448 $("#dataDisplay").dialog({
450 title: PMA_messages['strDataPointContent'],
452 buttons: buttonOptions,
453 width: $('#dataDisplay').width() + 24,
455 $(this).find('input[type=checkbox]').css('margin', '0.5em');
459 * Attach Ajax event handlers for input fields
460 * in the dialog. Used to submit the Ajax
461 * request when the ENTER key is pressed.
463 $("#dataDisplay").find(':input').live('keydown', function (e) {
464 if (e.which === 13) { // 13 is the ENTER key
466 if (typeof buttonOptions[PMA_messages['strSave']] === 'function') {
467 buttonOptions[PMA_messages['strSave']].call();
474 * Generate plot using Highcharts
478 $('#zoom_search_form')
481 $('#togglesearchformlink')
482 .text(PMA_messages['strShowSearchCriteria']);
483 $('#togglesearchformdiv').show();
485 var colorCodes = ['#FF0000', '#00FFFF', '#0000FF', '#0000A0', '#FF0080', '#800080', '#FFFF00', '#00FF00', '#FF00FF'];
486 var series = new Array();
487 var xCord = new Array();
488 var yCord = new Array();
491 var xMax; // xAxis extreme max
492 var xMin; // xAxis extreme min
493 var yMax; // yAxis extreme max
494 var yMin; // yAxis extreme min
496 // Set the basic plot settings
497 var currentSettings = {
499 renderTo: 'querychart',
502 width:$('#resizer').width() - 3,
503 height:$('#resizer').height() - 20
508 exporting: { enabled: false },
509 label: { text: $('#dataLabel').val() },
512 allowPointSelect: true,
524 // Make AJAX request to tbl_zoom_select.php for getting the complete row info
526 'ajax_request' : true,
527 'get_data_row' : true,
528 'db' : window.parent.db,
529 'table' : window.parent.table,
530 'where_clause' : data[id]['where_clause'],
531 'token' : window.parent.token
533 $.post('tbl_zoom_select.php', post_params, function(data) {
534 // Row is contained in data.row_info, now fill the displayResultForm with row values
535 for (key in data.row_info) {
536 $field = $('#fieldID_' + fid);
537 $field_null = $('#fields_null_id_' + fid);
538 if (data.row_info[key] == null) {
539 $field_null.attr('checked', true);
542 $field_null.attr('checked', false);
543 if ($field.attr('multiple')) { // when the column is of type SET
544 $field.val(data.row_info[key].split(','));
546 $field.val(data.row_info[key]);
551 selectedRow = new Object();
552 selectedRow = data.row_info;
555 $("#dataDisplay").dialog("open");
562 formatter: function() {
563 return this.point.name;
566 title: { text: PMA_messages['strQueryResults'] },
568 title: { text: $('#tableid_0').val() },
570 setExtremes: function(e) {
571 this.resetZoom.show();
578 title: { text: $('#tableid_1').val() },
582 setExtremes: function(e) {
583 this.resetZoom.show();
589 // If data label is not set, do not show tooltips
590 if (dataLabel == '') {
591 currentSettings.tooltip.enabled = false;
594 $('#resizer').resizable({
596 currentChart.setSize(
597 this.offsetWidth - 3,
598 this.offsetHeight - 20,
604 // Classify types as either numeric,time,text
605 xType = getType(xType);
606 yType = getType(yType);
608 //Set the axis type based on the field
609 currentSettings.xAxis.type = (xType == 'time') ? 'datetime' : 'linear';
610 currentSettings.yAxis.type = (yType == 'time') ? 'datetime' : 'linear';
612 // Formulate series data for plot
613 series[0] = new Object();
614 series[0].data = new Array();
618 if (xType != 'text' && yType != 'text') {
619 $.each(data, function(key, value) {
620 var xVal = (xType == 'numeric') ? value[xLabel] : getTimeStamp(value[xLabel], $('#types_0').val());
621 var yVal = (yType == 'numeric') ? value[yLabel] : getTimeStamp(value[yLabel], $('#types_1').val());
622 series[0].data.push({
623 name: value[dataLabel],
626 marker: {fillColor: colorCodes[it % 8]},
629 xCord.push(value[xLabel]);
630 yCord.push(value[yLabel]);
633 if (xType == 'numeric') {
634 currentSettings.xAxis.max = Array.max(xCord) + 6;
635 currentSettings.xAxis.min = Array.min(xCord) - 6;
637 currentSettings.xAxis.labels = { formatter : function() {
638 return getDate(this.value, $('#types_0').val());
641 if (yType == 'numeric') {
642 currentSettings.yAxis.max = Array.max(yCord) + 6;
643 currentSettings.yAxis.min = Array.min(yCord) - 6;
645 currentSettings.yAxis.labels = { formatter : function() {
646 return getDate(this.value, $('#types_1').val());
650 } else if (xType == 'text' && yType != 'text') {
651 $.each(data, function(key, value) {
652 xCord.push(value[xLabel]);
653 yCord.push(value[yLabel]);
656 tempX = getCord(xCord);
657 $.each(data, function(key, value) {
658 var yVal = (yType == 'numeric') ? value[yLabel] : getTimeStamp(value[yLabel], $('#types_1').val());
659 series[0].data.push({
660 name: value[dataLabel],
663 marker: {fillColor: colorCodes[it % 8]},
669 currentSettings.xAxis.labels = {
670 formatter : function() {
671 if (tempX[1][this.value] && tempX[1][this.value].length > 10) {
672 return tempX[1][this.value].substring(0, 10);
674 return tempX[1][this.value];
678 if (yType == 'numeric') {
679 currentSettings.yAxis.max = Array.max(yCord) + 6;
680 currentSettings.yAxis.min = Array.min(yCord) - 6;
682 currentSettings.yAxis.labels = {
683 formatter : function() {
684 return getDate(this.value, $('#types_1').val());
690 } else if (xType != 'text' && yType == 'text') {
691 $.each(data, function(key, value) {
692 xCord.push(value[xLabel]);
693 yCord.push(value[yLabel]);
695 tempY = getCord(yCord);
696 $.each(data, function(key, value) {
697 var xVal = (xType == 'numeric') ? value[xLabel] : getTimeStamp(value[xLabel], $('#types_0').val());
698 series[0].data.push({
699 name: value[dataLabel],
702 marker: {fillColor: colorCodes[it % 8]},
707 if (xType == 'numeric') {
708 currentSettings.xAxis.max = Array.max(xCord) + 6;
709 currentSettings.xAxis.min = Array.min(xCord) - 6;
711 currentSettings.xAxis.labels = {
712 formatter : function() {
713 return getDate(this.value, $('#types_0').val());
717 currentSettings.yAxis.labels = {
718 formatter : function() {
719 if (tempY[1][this.value] && tempY[1][this.value].length > 10) {
720 return tempY[1][this.value].substring(0, 10);
722 return tempY[1][this.value];
728 } else if (xType == 'text' && yType == 'text') {
729 $.each(data, function(key, value) {
730 xCord.push(value[xLabel]);
731 yCord.push(value[yLabel]);
733 tempX = getCord(xCord);
734 tempY = getCord(yCord);
735 $.each(data, function(key, value) {
736 series[0].data.push({
737 name: value[dataLabel],
740 marker: {fillColor: colorCodes[it % 8]},
745 currentSettings.xAxis.labels = {
746 formatter : function() {
747 if (tempX[1][this.value] && tempX[1][this.value].length > 10) {
748 return tempX[1][this.value].substring(0, 10);
750 return tempX[1][this.value];
754 currentSettings.yAxis.labels = {
755 formatter : function() {
756 if (tempY[1][this.value] && tempY[1][this.value].length > 10) {
757 return tempY[1][this.value].substring(0, 10);
759 return tempY[1][this.value];
767 currentSettings.series = series;
768 currentChart = PMA_createChart(currentSettings);
769 xMin = currentChart.xAxis[0].getExtremes().min;
770 xMax = currentChart.xAxis[0].getExtremes().max;
771 yMin = currentChart.yAxis[0].getExtremes().min;
772 yMax = currentChart.yAxis[0].getExtremes().max;
773 includePan(currentChart); //Enable panning feature
774 var setZoom = function() {
775 var newxm = xMin + (xMax - xMin) * (1 - zoomRatio) / 2;
776 var newxM = xMax - (xMax - xMin) * (1 - zoomRatio) / 2;
777 var newym = yMin + (yMax - yMin) * (1 - zoomRatio) / 2;
778 var newyM = yMax - (yMax - yMin) * (1 - zoomRatio) / 2;
779 currentChart.xAxis[0].setExtremes(newxm, newxM);
780 currentChart.yAxis[0].setExtremes(newym, newyM);
783 //Enable zoom feature
784 $("#querychart").mousewheel(function(objEvent, intDelta) {
786 if (zoomRatio > 0.1) {
787 zoomRatio = zoomRatio - 0.1;
790 } else if (intDelta < 0) {
791 zoomRatio = zoomRatio + 0.1;
796 //Add reset zoom feature
797 currentChart.yAxis[0].resetZoom = currentChart.xAxis[0].resetZoom = $('<a href="#">Reset zoom</a>')
798 .appendTo(currentChart.container)
800 position: 'absolute',
806 currentChart.xAxis[0].setExtremes(null, null);
807 currentChart.yAxis[0].setExtremes(null, null);
808 this.style.display = 'none';