Skip to content

Commit 87a5b71

Browse files
committed
Add autocompletion
1 parent 5f6c34f commit 87a5b71

File tree

10 files changed

+674
-6
lines changed

10 files changed

+674
-6
lines changed

index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @var string
2222
*/
23-
define('MPG_APP_VERSION', '0.9.2');
23+
define('MPG_APP_VERSION', '0.9.3');
2424

2525
/**
2626
* Development mode?

routes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,8 @@
4545
'/ajax/database/{databaseName}/collection/{collectionName}/find',
4646
CollectionController::class . '@findAction'
4747
);
48+
49+
$router->get(
50+
'/ajax/database/{databaseName}/collection/{collectionName}/enumFields',
51+
CollectionController::class . '@enumFieldsAction'
52+
);

src/Controllers/CollectionController.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Controllers;
44

55
use Capsule\Response;
6+
use Helpers\MongoDBHelper;
67

78
class CollectionController extends Controller {
89

@@ -120,16 +121,32 @@ public function findAction($databaseName, $collectionName) : Response {
120121

121122
$documents = $collection->find(
122123
$decodedRequestBody['filter'], $decodedRequestBody['options']
124+
)->toArray();
125+
126+
return new Response(
127+
200, json_encode($documents), ['Content-Type' => 'application/json']
123128
);
124129

125-
$responseBody = [];
130+
}
131+
132+
public function enumFieldsAction($databaseName, $collectionName) : Response {
126133

127-
foreach ($documents as $document) {
128-
$responseBody[] = $document;
134+
global $mongoDBClient;
135+
136+
$collection = $mongoDBClient->$databaseName->$collectionName;
137+
138+
$documents = $collection->find([], ['limit' => 1])->toArray();
139+
140+
if ( empty($documents) ) {
141+
return new Response(404, 'Collection is empty');
129142
}
130-
143+
144+
$documentFields = MongoDBHelper::arrayKeysMulti(
145+
json_decode(json_encode($documents[0]), JSON_OBJECT_AS_ARRAY)
146+
);
147+
131148
return new Response(
132-
200, json_encode($responseBody), ['Content-Type' => 'application/json']
149+
200, json_encode($documentFields), ['Content-Type' => 'application/json']
133150
);
134151

135152
}

src/Helpers/MongoDBHelper.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,28 @@ public static function createClient() : MongoDBClient {
3333

3434
}
3535

36+
/**
37+
* Gets all keys from a multidimensional array.
38+
* @see https://gist.github.com/JohnQUnknown/8761761
39+
*
40+
* @param array $array
41+
*
42+
* @return array
43+
*/
44+
public static function arrayKeysMulti(array $array) : array {
45+
46+
$keys = [];
47+
48+
foreach ($array as $key => $_value) {
49+
$keys[] = $key;
50+
51+
if ( is_array($array[$key]) ) {
52+
$keys = array_merge($keys, self::arrayKeysMulti($array[$key]));
53+
}
54+
}
55+
56+
return $keys;
57+
58+
}
59+
3660
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
.CodeMirror-hints {
2+
position: absolute;
3+
z-index: 10;
4+
overflow: hidden;
5+
list-style: none;
6+
7+
margin: 0;
8+
padding: 2px;
9+
10+
-webkit-box-shadow: 2px 3px 5px rgba(0,0,0,.2);
11+
-moz-box-shadow: 2px 3px 5px rgba(0,0,0,.2);
12+
box-shadow: 2px 3px 5px rgba(0,0,0,.2);
13+
border-radius: 3px;
14+
border: 1px solid silver;
15+
16+
background: white;
17+
font-size: 90%;
18+
font-family: monospace;
19+
20+
max-height: 20em;
21+
overflow-y: auto;
22+
}
23+
24+
.CodeMirror-hint {
25+
margin: 0;
26+
padding: 0 4px;
27+
border-radius: 2px;
28+
white-space: pre;
29+
color: black;
30+
cursor: pointer;
31+
}
32+
33+
li.CodeMirror-hint-active {
34+
background: #08f;
35+
color: white;
36+
}

static/css/mpg.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ button, input {
2323

2424
}
2525

26+
.CodeMirror-hints {
27+
28+
font-family: Consolas, monospace;
29+
font-size: 100%;
30+
31+
}
32+
2633
.json-container {
2734

2835
font-family: Consolas, monospace;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// CodeMirror, copyright (c) by Marijn Haverbeke and others
2+
// Distributed under an MIT license: https://codemirror.net/LICENSE
3+
4+
(function(mod) {
5+
if (typeof exports == "object" && typeof module == "object") // CommonJS
6+
mod(require("../../lib/codemirror"));
7+
else if (typeof define == "function" && define.amd) // AMD
8+
define(["../../lib/codemirror"], mod);
9+
else // Plain browser env
10+
mod(CodeMirror);
11+
})(function(CodeMirror) {
12+
"use strict";
13+
14+
var WORD = /[\w$]+/, RANGE = 500;
15+
16+
CodeMirror.registerHelper("hint", "anyword", function(editor, options) {
17+
18+
var word = options && options.word || WORD;
19+
var range = options && options.range || RANGE;
20+
var cur = editor.getCursor(), curLine = editor.getLine(cur.line);
21+
var end = cur.ch, start = end;
22+
while (start && word.test(curLine.charAt(start - 1))) --start;
23+
var curWord = start != end && curLine.slice(start, end);
24+
25+
options.list = MPG.mongoDBKeywords.concat(MPG.collectionFields);
26+
27+
/**
28+
* Code block taken from Mongolo project.
29+
* @see https://github.com/tetreum/mongolo/blob/develop/htdocs/js/src/custom-hint.js
30+
*/
31+
32+
var list = [],
33+
suggestedWord,
34+
k;
35+
36+
if (curWord)
37+
{
38+
for (k in options.list)
39+
{
40+
if (!options.list.hasOwnProperty(k)) {continue;}
41+
suggestedWord = options.list[k];
42+
43+
if (suggestedWord.toLowerCase().startsWith(curWord.toLowerCase()) && list.indexOf(suggestedWord) == -1) {
44+
list.push(suggestedWord);
45+
}
46+
}
47+
}
48+
49+
return {list: list, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)};
50+
});
51+
});

0 commit comments

Comments
 (0)