Skip to content

Commit a2eb30f

Browse files
committed
Add export feature
1 parent b335652 commit a2eb30f

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @var string
2121
*/
22-
define('MPG_APP_VERSION', '0.9.7');
22+
define('MPG_APP_VERSION', '0.9.8');
2323

2424
/**
2525
* Development mode?

static/css/mpg.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
.navbar-brand, .navbar-nav .nav-link {
1515

1616
position: relative;
17+
left: 0;
1718
top: 15px;
19+
1820
padding-bottom: 15px;
1921

2022
}
@@ -98,6 +100,16 @@ button, input {
98100

99101
}
100102

103+
#mpg-export-button {
104+
105+
position: relative;
106+
top: -5px;
107+
left: 0;
108+
109+
margin-left: 10px;
110+
111+
}
112+
101113
#mpg-index-order-select, #mpg-unique-index-select {
102114

103115
width: auto;

static/js/mpg.database.query.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ MPG.documentId = '';
6363
*/
6464
MPG.documentIdType = '';
6565

66+
/**
67+
* Cached output.
68+
*
69+
* @type {string}
70+
*/
71+
MPG.cachedOutput = '';
72+
6673
/**
6774
* Initializes CodeMirror instance.
6875
*
@@ -186,6 +193,38 @@ MPG.helpers.convertAnyToString = function(any) {
186193

187194
};
188195

196+
/**
197+
* Downloads a file.
198+
*
199+
* @see https://stackoverflow.com/questions/3665115/how-to-create-a-file-in-memory-for-user-to-download-but-not-through-server
200+
*
201+
* @param {string} filename
202+
* @param {string} data
203+
* @param {string} type
204+
*
205+
* @returns {void}
206+
*/
207+
MPG.helpers.downloadFile = function(filename, data, type) {
208+
209+
var blob = new Blob([data], {type: type});
210+
211+
if ( window.navigator.msSaveOrOpenBlob ) {
212+
213+
window.navigator.msSaveBlob(blob, filename);
214+
215+
} else {
216+
217+
var elem = window.document.createElement('a');
218+
elem.href = window.URL.createObjectURL(blob);
219+
elem.download = filename;
220+
document.body.appendChild(elem);
221+
elem.click();
222+
document.body.removeChild(elem);
223+
224+
}
225+
226+
};
227+
189228
/**
190229
* Reloads collections of a specific database.
191230
*
@@ -544,6 +583,8 @@ MPG.eventListeners.addFind = function() {
544583
+ MPG.collectionName + '/find',
545584
function(response) {
546585

586+
MPG.cachedOutput = response;
587+
547588
var outputCode = document.querySelector('#mpg-output-code');
548589
outputCode.innerHTML = '';
549590

@@ -577,6 +618,29 @@ MPG.eventListeners.addCtrlSpace = function() {
577618

578619
};
579620

621+
/**
622+
* Adds an event listener on "Export" button.
623+
*
624+
* @returns {void}
625+
*/
626+
MPG.eventListeners.addExport = function() {
627+
628+
document.querySelector('#mpg-export-button').addEventListener('click', function(_event) {
629+
630+
if ( MPG.cachedOutput === '' ) {
631+
return window.alert('There is nothing to export for now...');
632+
}
633+
634+
MPG.helpers.downloadFile(
635+
'mongodb-php-gui-export-' + (new Date()).getTime() + '.json',
636+
JSON.stringify(JSON.parse(MPG.cachedOutput), null, 4),
637+
'application/json'
638+
);
639+
640+
});
641+
642+
};
643+
580644
// When document is ready:
581645
window.addEventListener('DOMContentLoaded', function(_event) {
582646

@@ -588,6 +652,7 @@ window.addEventListener('DOMContentLoaded', function(_event) {
588652
MPG.eventListeners.addDeleteOne();
589653
MPG.eventListeners.addFind();
590654
MPG.eventListeners.addCtrlSpace();
655+
MPG.eventListeners.addExport();
591656

592657
window.location.hash = '';
593658

views/database.query.tpl.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,13 @@
145145

146146
<div id="mpg-output-column" class="col-md-12">
147147

148-
<h2>Output</h2>
149-
<code id="mpg-output-code"></code>
148+
<h2 class="d-inline-block">Output</h2>
149+
150+
<button id="mpg-export-button" class="btn btn-secondary">Export</button>
151+
152+
<div>
153+
<code id="mpg-output-code"></code>
154+
</div>
150155

151156
</div>
152157

0 commit comments

Comments
 (0)