Skip to content

Commit b278c36

Browse files
committed
Add history pop button
1 parent aa51204 commit b278c36

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

static/css/mpg.css

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@ button, input {
5555

5656
}
5757

58+
.CodeMirror .mpg-history-pop-button {
59+
60+
position: absolute;
61+
right: 0;
62+
top: 0;
63+
z-index: 100;
64+
65+
border: none;
66+
background: none;
67+
68+
}
69+
70+
.CodeMirror .mpg-history-pop-button:focus {
71+
72+
outline: none;
73+
74+
}
75+
5876
.CodeMirror-hints {
5977

6078
font-family: Consolas, monospace;

static/js/mpg.database.query.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ MPG.databaseName = '';
2727
*/
2828
MPG.collectionName = '';
2929

30+
/**
31+
* History of queries.
32+
*
33+
* @type {Array}
34+
*/
35+
MPG.queryHistory = [];
36+
3037
/**
3138
* List of MongoDB and SQL keywords.
3239
* XXX Used for autocompletion.
@@ -83,6 +90,13 @@ MPG.initializeCodeMirror = function() {
8390
document.querySelector('#mpg-filter-or-doc-textarea')
8491
);
8592

93+
var historyPopButton = document.createElement('button');
94+
95+
historyPopButton.className = 'mpg-history-pop-button';
96+
historyPopButton.innerHTML = '<i class="fa fa-history" aria-hidden="true"></i>';
97+
98+
document.querySelector('.CodeMirror').appendChild(historyPopButton);
99+
86100
};
87101

88102
/**
@@ -653,6 +667,34 @@ MPG.eventListeners.addCodeMirror = function() {
653667

654668
};
655669

670+
/**
671+
* Adds an event listener on "History pop" button.
672+
*
673+
* @returns {void}
674+
*/
675+
MPG.eventListeners.addHistoryPop = function() {
676+
677+
document.querySelector('.CodeMirror .mpg-history-pop-button')
678+
.addEventListener('click', function(_event) {
679+
680+
var lastQuery = MPG.queryHistory.pop();
681+
682+
if ( lastQuery === MPG.codeMirror.getValue() ) {
683+
lastQuery = MPG.queryHistory.pop();
684+
}
685+
686+
if ( typeof lastQuery === 'string' ) {
687+
MPG.codeMirror.setValue(lastQuery);
688+
MPG.codeMirror.save();
689+
} else {
690+
MPG.codeMirror.setValue('');
691+
MPG.codeMirror.save();
692+
}
693+
694+
});
695+
696+
};
697+
656698
/**
657699
* Adds an event listener on "Find" button.
658700
*
@@ -698,6 +740,7 @@ MPG.eventListeners.addFind = function() {
698740
if ( filterOrDocTextAreaValue === '' ) {
699741
requestBody.filter = {};
700742
} else {
743+
MPG.queryHistory.push(filterOrDocTextAreaValue);
701744
requestBody.filter = JSON.parse(filterOrDocTextAreaValue);
702745
}
703746

@@ -789,6 +832,7 @@ window.addEventListener('DOMContentLoaded', function(_event) {
789832
MPG.eventListeners.addCount();
790833
MPG.eventListeners.addDeleteOne();
791834
MPG.eventListeners.addCodeMirror();
835+
MPG.eventListeners.addHistoryPop();
792836
MPG.eventListeners.addFind();
793837
MPG.eventListeners.addCtrlSpace();
794838
MPG.eventListeners.addExport();

0 commit comments

Comments
 (0)