Translated using Weblate (Bulgarian)
[phpmyadmin.git] / js / console.js
blobd011071bab320751223811b5f12176b0aad9f912
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * Used in or for console
4  *
5  * @package phpMyAdmin-Console
6  */
8 /**
9  * Console object
10  */
11 var PMA_console = {
12     /**
13      * @var object, jQuery object, selector is '#pma_console>.content'
14      * @access private
15      */
16     $consoleContent: null,
17     /**
18      * @var object, jQuery object, selector is '#pma_console .content',
19      *  used for resizer
20      * @access private
21      */
22     $consoleAllContents: null,
23     /**
24      * @var object, jQuery object, selector is '#pma_console .toolbar'
25      * @access private
26      */
27     $consoleToolbar: null,
28     /**
29      * @var object, jQuery object, selector is '#pma_console .template'
30      * @access private
31      */
32     $consoleTemplates: null,
33     /**
34      * @var object, jQuery object, form for submit
35      * @access private
36      */
37     $requestForm: null,
38     /**
39      * @var object, contain console config
40      * @access private
41      */
42     config: null,
43     /**
44      * @var bool, if console element exist, it'll be true
45      * @access public
46      */
47     isEnabled: false,
48     /**
49      * @var bool, make sure console events bind only once
50      * @access private
51      */
52     isInitialized: false,
53     /**
54      * Used for console initialize, reinit is ok, just some variable assignment
55      *
56      * @return void
57      */
58     initialize: function() {
60         if($('#pma_console').length === 0) {
61             return;
62         }
64         PMA_console.isEnabled = true;
66         // Cookie var checks and init
67         if(! $.cookie('pma_console_height')) {
68             $.cookie('pma_console_height', 92);
69         }
70         if(! $.cookie('pma_console_mode')) {
71             $.cookie('pma_console_mode', 'info');
72         }
74         // Vars init
75         PMA_console.$consoleToolbar = $('#pma_console>.toolbar');
76         PMA_console.$consoleContent = $('#pma_console>.content');
77         PMA_console.$consoleAllContents = $('#pma_console .content');
78         PMA_console.$consoleTemplates = $('#pma_console>.templates');
80         // Generate a from for post
81         PMA_console.$requestForm = $('<form method="post" action="import.php">' +
82             '<input name="is_js_confirmed" value="0">' +
83             '<textarea name="sql_query"></textarea>' +
84             '<input name="console_message_id" value="0">' +
85             '<input name="db" value="">' +
86             '<input name="table" value="">' +
87             '<input name="token" value="' +
88             PMA_commonParams.get('token') +
89             '">' +
90             '</form>'
91         );
92         PMA_console.$requestForm.bind('submit', AJAX.requestHandler);
94         // Event binds shouldn't run again
95         if(PMA_console.isInitialized === false) {
97             // Load config first
98             var tempConfig = JSON.parse($.cookie('pma_console_config'));
99             if(tempConfig) {
100                 if(tempConfig.alwaysExpand === true) {
101                     $('#pma_console_options input[name=always_expand]').prop('checked', true);
102                 }
103                 if(tempConfig.startHistory === true) {
104                     $('#pma_console_options input[name=start_history]').prop('checked', true);
105                 }
106                 if(tempConfig.currentQuery === true) {
107                     $('#pma_console_options input[name=current_query]').prop('checked', true);
108                 }
109             } else {
110                 $('#pma_console_options input[name=current_query]').prop('checked', true);
111             }
113             PMA_console.updateConfig();
115             PMA_consoleResizer.initialize();
116             PMA_consoleInput.initialize();
117             PMA_consoleMessages.initialize();
118             PMA_consoleBookmarks.initialize();
120             PMA_console.$consoleToolbar.children('.console_switch').click(PMA_console.toggle);
121             $(document).keydown(function(event) {
122                 // 27 keycode is ESC
123                 if(event.keyCode === 27) {
124                     PMA_console.toggle();
125                 }
126             });
128             $('#pma_console .toolbar').children().mousedown(function(event) {
129                 event.preventDefault();
130                 event.stopImmediatePropagation();
131             });
133             $('#pma_console .button.clear').click(function() {
134                 PMA_consoleMessages.clear();
135             });
137             $('#pma_console .button.history').click(function() {
138                 PMA_consoleMessages.showHistory();
139             });
141             $('#pma_console .button.options').click(function() {
142                 PMA_console.showCard('#pma_console_options');
143             });
145             $('#pma_console .mid_layer').click(function() {
146                 PMA_console.hideCard($(this).parent().children('.card'));
147             });
148             $('#pma_bookmarks .switch_button').click(function() {
149                 PMA_console.hideCard($(this).closest('.card'));
150             });
152             $('#pma_console_options input[type=checkbox]').change(function() {
153                 PMA_console.updateConfig();
154             });
156             $('#pma_console_options .button.default').click(function() {
157                 $('#pma_console_options input[name=always_expand]').prop('checked', false);
158                 $('#pma_console_options input[name=start_history]').prop('checked', false);
159                 $('#pma_console_options input[name=current_query]').prop('checked', true);
160                 PMA_console.updateConfig();
161             });
163             $(document).ajaxComplete(function (event, xhr) {
164                 var data = $.parseJSON(xhr.responseText);
165                 PMA_console.ajaxCallback(data);
166             });
168             PMA_console.isInitialized = true;
169         }
171         // Change console mode from cookie
172         switch($.cookie('pma_console_mode')) {
173             case 'collapse':
174                 PMA_console.collapse();
175                 break;
176             /* jshint -W086 */// no break needed in default section
177             default:
178                 $.cookie('pma_console_mode', 'info');
179             case 'info':
180             /* jshint +W086 */
181                 PMA_console.info();
182                 break;
183             case 'show':
184                 PMA_console.show(true);
185                 PMA_console.scrollBottom();
186                 break;
187         }
188     },
189     /**
190      * Excute query and show results in console
191      *
192      * @return void
193      */
194     execute: function(queryString, options) {
195         if(typeof(queryString) != 'string' || ! /[a-z]|[A-Z]/.test(queryString)){
196             return;
197         }
198         PMA_console.$requestForm.children('textarea').val(queryString);
199         if(options && options.db) {
200             PMA_console.$requestForm.children('[name=db]').attr('value', options.db);
201             if(options.table) {
202                 PMA_console.$requestForm.children('[name=table]').attr('value', options.table);
203             } else {
204                 PMA_console.$requestForm.children('[name=table]').attr('value', '');
205             }
206         } else {
207             PMA_console.$requestForm.children('[name=db]').attr('value',
208                 (PMA_commonParams.get('db').length > 0 ? PMA_commonParams.get('db') : ''));
209         }
210         PMA_console.$requestForm.find('[name=profiling]').remove();
211         if(options && options.profiling === true) {
212             PMA_console.$requestForm.append('<input name="profiling" value="on">');
213         }
214         if (! confirmQuery(PMA_console.$requestForm[0], PMA_console.$requestForm.children('textarea')[0])) {
215             return;
216         }
217         PMA_console.$requestForm.children('[name=console_message_id]')
218             .attr('value', PMA_consoleMessages.appendQuery({sql_query: queryString}).message_id);
219         PMA_console.$requestForm.trigger('submit');
220         PMA_consoleInput.clear();
221     },
222     ajaxCallback: function(data) {
223         if(data && data.console_message_id) {
224             PMA_consoleMessages.updateQuery(data.console_message_id, data.success,
225                 (data._reloadQuerywindow ? data._reloadQuerywindow : false));
226         } else if( data && data._reloadQuerywindow) {
227             if(data._reloadQuerywindow.sql_query.length > 0) {
228                 PMA_consoleMessages.appendQuery(data._reloadQuerywindow, 'successed')
229                     .$message.addClass(PMA_console.config.currentQuery ? '' : 'hide');
230             }
231         }
232     },
233     /**
234      * Change console to collapse mode
235      *
236      * @return void
237      */
238     collapse: function() {
239         $.cookie('pma_console_mode', 'collapse');
240         var pmaConsoleHeight = $.cookie('pma_console_height');
242         if(pmaConsoleHeight < 32) {
243             $.cookie('pma_console_height', 92);
244         }
245         PMA_console.$consoleToolbar.addClass('collapsed');
246         PMA_console.$consoleAllContents.height(pmaConsoleHeight);
247         PMA_console.$consoleContent.stop();
248         PMA_console.$consoleContent.animate({'margin-bottom': -1 * PMA_console.$consoleContent.outerHeight() + 'px'},
249             'fast', 'easeOutQuart', function() {
250                 PMA_console.$consoleContent.css({display:'none'});
251                 $(window).trigger('resize');
252             });
253         PMA_console.hideCard();
254     },
255     /**
256      * Show console
257      *
258      * @param bool inputFocus If true, focus the input line after show()
259      * @return void
260      */
261     show: function(inputFocus) {
262         $.cookie('pma_console_mode', 'show');
264         var pmaConsoleHeight = $.cookie('pma_console_height');
266         if(pmaConsoleHeight < 32) {
267             $.cookie('pma_console_height', 32);
268             PMA_console.collapse();
269             return;
270         }
271         PMA_console.$consoleContent.css({display:'block'});
272         if(PMA_console.$consoleToolbar.hasClass('collapsed')) {
273             PMA_console.$consoleToolbar.removeClass('collapsed');
274         }
275         PMA_console.$consoleAllContents.height(pmaConsoleHeight);
276         PMA_console.$consoleContent.stop();
277         PMA_console.$consoleContent.animate({'margin-bottom': 0},
278             'fast', 'easeOutQuart', function() {
279                 $(window).trigger('resize');
280             });
281         if(inputFocus) {
282             PMA_consoleInput.focus();
283         }
284     },
285     /**
286      * Change console to SQL information mode
287      * this mode shows current SQL query
288      * This mode is the default mode
289      *
290      * @return void
291      */
292     info: function() {
293         // Under construction
294         PMA_console.collapse();
295     },
296     /**
297      * Toggle console mode between collsapse/show
298      * Used for toggle buttons and shortcuts
299      *
300      * @return void
301      */
302     toggle: function() {
303         switch($.cookie('pma_console_mode')) {
304             case 'collapse':
305             case 'info':
306                 PMA_console.show(true);
307                 break;
308             case 'show':
309                 PMA_console.collapse();
310                 break;
311             default:
312                 PMA_consoleInitialize();
313         }
314     },
315     /**
316      * Scroll console to bottom
317      *
318      * @return void
319      */
320     scrollBottom: function() {
321         PMA_console.$consoleContent.scrollTop(PMA_console.$consoleContent.prop("scrollHeight"));
322     },
323     /**
324      * Show card
325      *
326      * @param string cardSelector Selector, select string will be "#pma_console " + cardSelector
327      * this param also can be JQuery object, if you need.
328      *
329      * @return void
330      */
331     showCard: function(cardSelector) {
332         var $card = null;
333         if(typeof(cardSelector) !== 'string') {
334             if (cardSelector.length > 0) {
335                 $card = cardSelector;
336             } else {
337                 return;
338             }
339         } else {
340             $card = $("#pma_console " + cardSelector);
341         }
342         if($card.length === 0) {
343             return;
344         }
345         $card.parent().children('.mid_layer').show().fadeTo(0, 0.15);
346         $card.addClass('show');
347         PMA_consoleInput.blur();
348         if($card.parents('.card').length > 0) {
349             PMA_console.showCard($card.parents('.card'));
350         }
351     },
352     /**
353      * Scroll console to bottom
354      *
355      * @param object $targetCard Target card JQuery object, if it's empty, function will hide all cards
356      * @return void
357      */
358     hideCard: function($targetCard) {
359         if(! $targetCard) {
360             $('#pma_console .mid_layer').fadeOut(140);
361             $('#pma_console .card').removeClass('show');
362         } else if($targetCard.length > 0) {
363             $targetCard.parent().find('.mid_layer').fadeOut(140);
364             $targetCard.find('.card').removeClass('show');
365             $targetCard.removeClass('show');
366         }
367     },
368     /**
369      * Used for update console config
370      *
371      * @return void
372      */
373     updateConfig: function() {
374         PMA_console.config = {
375             alwaysExpand: $('#pma_console_options input[name=always_expand]').prop('checked'),
376             startHistory: $('#pma_console_options input[name=start_history]').prop('checked'),
377             currentQuery: $('#pma_console_options input[name=current_query]').prop('checked')
378         };
379         $.cookie('pma_console_config', JSON.stringify(PMA_console.config));
380     },
381     isSelect: function (queryString) {
382         var reg_exp = /^SELECT\s+/i;
383         return reg_exp.test(queryString);
384     }
388  * Resizer object
389  * Careful: this object UI logics highly related with functions under PMA_console
390  * Resizing min-height is 32, if small than it, console will collapse
391  */
392 var PMA_consoleResizer = {
393     _posY: 0,
394     _height: 0,
395     _resultHeight: 0,
396     /**
397      * Mousedown event handler for bind to resizer
398      *
399      * @return void
400      */
401     _mousedown: function(event) {
402         if($.cookie('pma_console_mode') !== 'show') {
403             return;
404         }
405         PMA_consoleResizer._posY = event.pageY;
406         PMA_consoleResizer._height = PMA_console.$consoleContent.height();
407         $(document).mousemove(PMA_consoleResizer._mousemove);
408         $(document).mouseup(PMA_consoleResizer._mouseup);
409         // Disable text selection while resizing
410         $(document).bind('selectstart', function(){ return false; });
411     },
412     /**
413      * Mousemove event handler for bind to resizer
414      *
415      * @return void
416      */
417     _mousemove: function(event) {
418         PMA_consoleResizer._resultHeight = PMA_consoleResizer._height + (PMA_consoleResizer._posY -event.pageY);
419         // Content min-height is 32, if adjusting height small than it we'll move it out of the page
420         if(PMA_consoleResizer._resultHeight <= 32) {
421             PMA_console.$consoleAllContents.height(32);
422             PMA_console.$consoleContent.css('margin-bottom', PMA_consoleResizer._resultHeight - 32);
423         }
424         else {
425             // Logic below makes viewable area always at bottom when adjusting height and content already at bottom
426             if(PMA_console.$consoleContent.scrollTop() + PMA_console.$consoleContent.innerHeight() + 16
427                 >= PMA_console.$consoleContent.prop('scrollHeight')) {
428                 PMA_console.$consoleAllContents.height(PMA_consoleResizer._resultHeight);
429                 PMA_console.scrollBottom();
430             } else {
431                 PMA_console.$consoleAllContents.height(PMA_consoleResizer._resultHeight);
432             }
433         }
434     },
435     /**
436      * Mouseup event handler for bind to resizer
437      *
438      * @return void
439      */
440     _mouseup: function() {
441         $.cookie('pma_console_height', PMA_consoleResizer._resultHeight);
442         PMA_console.show();
443         $(document).unbind('mousemove');
444         $(document).unbind('mouseup');
445         $(document).unbind('selectstart');
446     },
447     /**
448      * Used for console resizer initialize
449      *
450      * @return void
451      */
452     initialize: function() {
453         $('#pma_console .toolbar').unbind('mousedown');
454         $('#pma_console .toolbar').mousedown(PMA_consoleResizer._mousedown);
455     }
460  * Console input object
461  */
462 var PMA_consoleInput = {
463     /**
464      * @var array, contains Codemirror objects or input jQuery objects
465      * @access private
466      */
467     _inputs: null,
468     /**
469      * @var bool, if codemirror enabled
470      * @access private
471      */
472     _codemirror: false,
473     /**
474      * Used for console input initialize
475      *
476      * @return void
477      */
478     initialize: function() {
479         // _cm object can't be reinitialize
480         if(PMA_consoleInput._inputs !== null) {
481             return;
482         }
483         if(typeof CodeMirror !== 'undefined') {
484             PMA_consoleInput._codemirror = true;
485         }
486         PMA_consoleInput._inputs = [];
487         if (PMA_consoleInput._codemirror) {
488             PMA_consoleInput._inputs.console = CodeMirror($('#pma_console .console_query_input')[0], {
489                 theme: 'pma',
490                 mode: 'text/x-sql',
491                 lineWrapping: true,
492                 extraKeys: {"Ctrl-Space": "autocomplete"},
493                 hintOptions: {"completeSingle": false, "completeOnSingleClick": true},
494             });
495             PMA_consoleInput._inputs.console.on("inputRead", codemirrorAutocompleteOnInputRead);
496             if ($('#pma_bookmarks').length !== 0) {
497                 PMA_consoleInput._inputs.bookmark = CodeMirror($('#pma_console .bookmark_add_input')[0], {
498                     theme: 'pma',
499                     mode: 'text/x-sql',
500                     lineWrapping: true,
501                     extraKeys: {"Ctrl-Space": "autocomplete"},
502                     hintOptions: {"completeSingle": false, "completeOnSingleClick": true},
503                 });
504                 PMA_consoleInput._inputs.bookmark.on("inputRead", codemirrorAutocompleteOnInputRead);
505             }
506         } else {
507             PMA_consoleInput._inputs.console =
508                 $('<textarea>').appendTo('#pma_console .console_query_input');
509             if ($('#pma_bookmarks').length !== 0) {
510                 PMA_consoleInput._inputs.bookmark =
511                     $('<textarea>').appendTo('#pma_console .bookmark_add_input');
512             }
513         }
514         $('#pma_console .console_query_input').keydown(PMA_consoleInput._keydown);
515     },
516     /**
517      * Mousedown event handler for bind to input
518      * Shortcut is ESC key
519      *
520      * @return void
521      */
522     _keydown: function(event) {
523         if(event.ctrlKey && event.keyCode === 13) {
524             PMA_consoleInput.execute();
525         }
526     },
527     /**
528      * Used for send text to PMA_console.execute()
529      *
530      * @return void
531      */
532     execute: function() {
533         if (PMA_consoleInput._codemirror) {
534             PMA_console.execute(PMA_consoleInput._inputs.console.getValue());
535         } else {
536             PMA_console.execute(PMA_consoleInput._inputs.console.val());
537         }
538     },
539     /**
540      * Used for clear the input
541      *
542      * @param string target, default target is console input
543      * @return void
544      */
545     clear: function(target) {
546         PMA_consoleInput.setText('', target);
547     },
548     /**
549      * Used for set focus to input
550      *
551      * @return void
552      */
553     focus: function() {
554         PMA_consoleInput._inputs.console.focus();
555     },
556     /**
557      * Used for blur input
558      *
559      * @return void
560      */
561     blur: function() {
562         if (PMA_consoleInput._codemirror) {
563             PMA_consoleInput._inputs.console.getInputField().blur();
564         } else {
565             PMA_consoleInput._inputs.console.blur();
566         }
567     },
568     /**
569      * Used for set text in input
570      *
571      * @param string text
572      * @param string target
573      * @return void
574      */
575     setText: function(text, target) {
576         if (PMA_consoleInput._codemirror) {
577             switch(target) {
578                 case 'bookmark':
579                     PMA_console.execute(PMA_consoleInput._inputs.bookmark.setValue(text));
580                     break;
581                 default:
582                 case 'console':
583                     PMA_console.execute(PMA_consoleInput._inputs.console.setValue(text));
584             }
585         } else {
586             switch(target) {
587                 case 'bookmark':
588                     PMA_console.execute(PMA_consoleInput._inputs.bookmark.val(text));
589                     break;
590                 default:
591                 case 'console':
592                     PMA_console.execute(PMA_consoleInput._inputs.console.val(text));
593             }
594         }
595     },
596     getText: function(target) {
597         if (PMA_consoleInput._codemirror) {
598             switch(target) {
599                 case 'bookmark':
600                     return PMA_consoleInput._inputs.bookmark.getValue();
601                 default:
602                 case 'console':
603                     return PMA_consoleInput._inputs.console.getValue();
604             }
605         } else {
606             switch(target) {
607                 case 'bookmark':
608                     return PMA_consoleInput._inputs.bookmark.val();
609                 default:
610                 case 'console':
611                     return PMA_consoleInput._inputs.console.val();
612             }
613         }
614     }
620  * Console messages, and message items management object
621  */
622 var PMA_consoleMessages = {
623     /**
624      * Used for clear the messages
625      *
626      * @return void
627      */
628     clear: function() {
629         $('#pma_console .content .console_message_container .message:not(.welcome)').addClass('hide');
630         $('#pma_console .content .console_message_container .message.failed').remove();
631         $('#pma_console .content .console_message_container .message.expanded').find('.action.collapse').click();
632     },
633     /**
634      * Used for show history messages
635      *
636      * @return void
637      */
638     showHistory: function() {
639         $('#pma_console .content .console_message_container .message.hide').removeClass('hide');
640     },
641     /**
642      * Used for log new message
643      *
644      * @param string msgString Message to show
645      * @param string msgType Message type
646      * @return object, {message_id, $message}
647      */
648     append: function(msgString, msgType) {
649         if(typeof(msgString) !== 'string') {
650             return false;
651         }
652         // Generate an ID for each message, we can find them later
653         var msgId = Math.round(Math.random()*(899999999999)+100000000000);
654         var now = new Date();
655         var $newMessage =
656             $('<div class="message '
657                 + (PMA_console.config.alwaysExpand ? 'expanded' : 'collapsed')
658                 +'" msgid="' + msgId + '"><div class="action_content"></div></div>');
659         switch(msgType) {
660             case 'query':
661                 $newMessage.append('<div class="query highlighted"></div>');
662                 if(PMA_consoleInput._codemirror) {
663                     CodeMirror.runMode(msgString,
664                         'text/x-sql', $newMessage.children('.query')[0]);
665                 } else {
666                     $newMessage.children('.query').text(msgString);
667                 }
668                 $newMessage.children('.action_content')
669                     .append(PMA_console.$consoleTemplates.children('.query_actions').html());
670                 break;
671             default:
672             case 'normal':
673                 $newMessage.append('<div>' + msgString + '</div>');
674         }
675         PMA_consoleMessages._msgEventBinds($newMessage);
676         $newMessage.find('span.text.query_time span')
677             .text(now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds())
678             .parent().attr('title', now);
679         return {message_id: msgId,
680                 $message: $newMessage.appendTo('#pma_console .content .console_message_container')};
681     },
682     /**
683      * Used for log new query
684      *
685      * @param string queryData Struct should be
686      * {sql_query: "Query string", db: "Target DB", table: "Target Table"}
687      * @param string state Message state
688      * @return object, {message_id: string message id, $message: JQuery object}
689      */
690     appendQuery: function(queryData, state) {
691         var targetMessage = PMA_consoleMessages.append(queryData.sql_query, 'query');
692         if(! targetMessage) {
693             return false;
694         }
695         if(queryData.db && queryData.table) {
696             targetMessage.$message.attr('targetdb', queryData.db);
697             targetMessage.$message.attr('targettable', queryData.table);
698             targetMessage.$message.find('.text.targetdb span').text(queryData.db);
699         }
700         if(PMA_console.isSelect(queryData.sql_query)) {
701             targetMessage.$message.addClass('select');
702         }
703         switch(state) {
704             case 'failed':
705                 targetMessage.$message.addClass('failed');
706                 break;
707             case 'successed':
708                 targetMessage.$message.addClass('successed');
709                 break;
710             default:
711             case 'pending':
712                 targetMessage.$message.addClass('pending');
713         }
714         return targetMessage;
715     },
716     _msgEventBinds: function($targetMessage) {
717         // Leave unbinded elements, remove binded.
718         $targetMessage = $targetMessage.filter(':not(.binded)');
719         if($targetMessage.length === 0) {
720             return;
721         }
722         $targetMessage.addClass('binded');
724         $targetMessage.find('.action.expand').click(function () {
725             $(this).closest('.message').removeClass('collapsed');
726             $(this).closest('.message').addClass('expanded');
727         });
728         $targetMessage.find('.action.collapse').click(function () {
729             $(this).closest('.message').addClass('collapsed');
730             $(this).closest('.message').removeClass('expanded');
731         });
732         $targetMessage.find('.action.edit').click(function () {
733             PMA_consoleInput.setText($(this).parent().siblings('.query').text());
734             PMA_consoleInput.focus();
735         });
736         $targetMessage.find('.action.requery').click(function () {
737             var query = $(this).parent().siblings('.query').text();
738             var $message = $(this).closest('.message');
739             if(confirm(PMA_messages.strConsoleRequeryConfirm + '\n'
740                 + (query.length<100 ? query : query.slice(0, 100) + '...'))) {
741                 PMA_console.execute(query, {db: $message.attr('targetdb'), table: $message.attr('targettable')});
742             }
743         });
744         $targetMessage.find('.action.bookmark').click(function () {
745             var query = $(this).parent().siblings('.query').text();
746             var $message = $(this).closest('.message');
747             PMA_consoleBookmarks.addBookmark(query, $message.attr('targetdb'));
748             PMA_console.showCard('#pma_bookmarks .card.add');
749         });
750         $targetMessage.find('.action.edit_bookmark').click(function () {
751             var query = $(this).parent().siblings('.query').text();
752             var $message = $(this).closest('.message');
753             var isShared = $message.find('span.bookmark_label').hasClass('shared');
754             var label = $message.find('span.bookmark_label').text();
755             PMA_consoleBookmarks.addBookmark(query, $message.attr('targetdb'), label, isShared);
756             PMA_console.showCard('#pma_bookmarks .card.add');
757         });
758         $targetMessage.find('.action.delete_bookmark').click(function () {
759             var $message = $(this).closest('.message');
760             if(confirm(PMA_messages.strConsoleDeleteBookmarkConfirm + '\n' + $message.find('.bookmark_label').text())) {
761                 $.post('import.php',
762                     {token: PMA_commonParams.get('token'),
763                     action_bookmark: 2,
764                     ajax_request: true,
765                     id_bookmark: $message.attr('bookmarkid')},
766                     function () {
767                         PMA_consoleBookmarks.refresh();
768                     });
769             }
770         });
771         $targetMessage.find('.action.profiling').click(function () {
772             var $message = $(this).closest('.message');
773             PMA_console.execute($(this).parent().siblings('.query').text(),
774                 {db: $message.attr('targetdb'),
775                 table: $message.attr('targettable'),
776                 profiling: true});
777         });
778         $targetMessage.find('.action.explain').click(function () {
779             var $message = $(this).closest('.message');
780             PMA_console.execute('EXPLAIN ' + $(this).parent().siblings('.query').text(),
781                 {db: $message.attr('targetdb'),
782                 table: $message.attr('targettable')});
783         });
784         if(PMA_consoleInput._codemirror) {
785             $targetMessage.find('.query:not(.highlighted)').each(function(index, elem) {
786                     CodeMirror.runMode($(elem).text(),
787                         'text/x-sql', elem);
788                     $(this).addClass('highlighted');
789                 });
790         }
791     },
792     msgAppend: function(msgId, msgString, msgType) {
793         var $targetMessage = $('#pma_console .content .console_message_container .message[msgid=' + msgId +']');
794         if($targetMessage.length === 0 || isNaN(parseInt(msgId)) || typeof(msgString) !== 'string') {
795             return false;
796         }
797         $targetMessage.append('<div>' + msgString + '</div>');
798     },
799     updateQuery: function(msgId, isSuccessed, queryData) {
800         var $targetMessage = $('#pma_console .console_message_container .message[msgid=' + parseInt(msgId) +']');
801         if($targetMessage.length === 0 || isNaN(parseInt(msgId))) {
802             return false;
803         }
804         $targetMessage.removeClass('pending failed successed');
805         if(isSuccessed) {
806             $targetMessage.addClass('successed');
807             if(queryData) {
808                 $targetMessage.children('.query').text('');
809                 $targetMessage.removeClass('select');
810                 if(PMA_console.isSelect(queryData.sql_query)) {
811                     $targetMessage.addClass('select');
812                 }
813                 if(PMA_consoleInput._codemirror) {
814                     CodeMirror.runMode(queryData.sql_query, 'text/x-sql', $targetMessage.children('.query')[0]);
815                 } else {
816                     $targetMessage.children('.query').text(queryData.sql_query);
817                 }
818                 $targetMessage.attr('targetdb', queryData.db);
819                 $targetMessage.attr('targettable', queryData.table);
820                 $targetMessage.find('.text.targetdb span').text(queryData.db);
821             }
822         } else {
823             $targetMessage.addClass('failed');
824         }
825     },
826     /**
827      * Used for console messages initialize
828      *
829      * @return void
830      */
831     initialize: function() {
832         PMA_consoleMessages._msgEventBinds($('#pma_console .message:not(.binded)'));
833         if(PMA_console.config.startHistory) {
834             PMA_consoleMessages.showHistory();
835         }
836     }
841  * Console bookmarks card, and bookmarks items management object
842  */
843 var PMA_consoleBookmarks = {
844     _bookmarks: [],
845     addBookmark: function (queryString, targetDb, label, isShared, id) {
846         $('#pma_bookmarks .add [name=shared]').prop('checked', false);
847         $('#pma_bookmarks .add [name=label]').val('');
848         $('#pma_bookmarks .add [name=targetdb]').val('');
849         $('#pma_bookmarks .add [name=id_bookmark]').val('');
850         PMA_consoleInput.setText('', 'bookmark');
852         switch(arguments.length) {
853             case 4:
854                 $('#pma_bookmarks .add [name=shared]').prop('checked', isShared);
855             case 3:
856                 $('#pma_bookmarks .add [name=label]').val(label);
857             case 2:
858                 $('#pma_bookmarks .add [name=targetdb]').val(targetDb);
859             case 1:
860                 PMA_consoleInput.setText(queryString, 'bookmark');
861             default:
862                 break;
863         }
864     },
865     refresh: function () {
866         $.get('import.php?console_bookmark_refresh=refresh&token=' + PMA_commonParams.get('token'),
867             {'ajax_request': true},
868             function(data) {
869                 if(data.console_message_bookmark) {
870                     $('#pma_bookmarks .content.bookmark').html(data.console_message_bookmark);
871                     PMA_consoleMessages._msgEventBinds($('#pma_bookmarks .message:not(.binded)'));
872                 }
873             });
874     },
875     /**
876      * Used for console bookmarks initialize
877      * message events are already binded by PMA_consoleMsg._msgEventBinds
878      *
879      * @return void
880      */
881     initialize: function() {
882         if($('#pma_bookmarks').length === 0) {
883             return;
884         }
885         $('#pma_console .button.bookmarks').click(function() {
886             PMA_console.showCard('#pma_bookmarks');
887         });
888         $('#pma_bookmarks .button.add').click(function() {
889             PMA_console.showCard('#pma_bookmarks .card.add');
890         });
891         $('#pma_bookmarks .card.add [name=submit]').click(function () {
892             if ($('#pma_bookmarks .card.add [name=label]').val().length === 0
893                 || PMA_consoleInput.getText('bookmark').length === 0)
894             {
895                 alert(PMA_messages.strFormEmpty);
896                 return;
897             }
898             $(this).prop('disabled', true);
899             $.post('import.php',
900                 {token: PMA_commonParams.get('token'),
901                 ajax_request: true,
902                 console_bookmark_add: 'true',
903                 label: $('#pma_bookmarks .card.add [name=label]').val(),
904                 db: $('#pma_bookmarks .card.add [name=targetdb]').val(),
905                 bookmark_query: PMA_consoleInput.getText('bookmark'),
906                 shared: $('#pma_bookmarks .card.add [name=shared]').prop('checked')},
907                 function () {
908                     PMA_consoleBookmarks.refresh();
909                     $('#pma_bookmarks .card.add [name=submit]').prop('disabled', false);
910                     PMA_console.hideCard($('#pma_bookmarks .card.add'));
911                 });
912         });
913         $('#pma_console .button.refresh').click(function() {
914             PMA_consoleBookmarks.refresh();
915         });
916     }
920  * Executed on page load
921  */
922 $(function () {
923     PMA_console.initialize();