mirror of
https://github.com/Neetpone/ponepaste.git
synced 2025-03-12 06:30:07 +01:00
Tag autocomplete.
This commit is contained in:
parent
0bffb397d6
commit
4303d83253
2 changed files with 35 additions and 16 deletions
18
api/tags_autocomplete.php
Normal file
18
api/tags_autocomplete.php
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
define('IN_PONEPASTE', 1);
|
||||
|
||||
require_once(__DIR__ . '/../includes/common.php');
|
||||
require_once(__DIR__ . '/../includes/Tag.class.php');
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
if (empty($_GET['tag'])) {
|
||||
die(json_encode(['error' => true, 'message' => 'No tag name provided']));
|
||||
}
|
||||
|
||||
$tag_name = Tag::cleanTagName($_GET['tag']);
|
||||
$tag_name = str_replace('%', '', $tag_name); /* get rid of MySQL LIKE wildcards */
|
||||
|
||||
$results = $conn->query('SELECT name FROM tags WHERE name LIKE ?', [$tag_name . '%']);
|
||||
|
||||
echo json_encode($results->fetchAll());
|
|
@ -25,15 +25,25 @@
|
|||
});
|
||||
</script>
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
function setupTagsInput() {
|
||||
const tagsInput = document.getElementById('tags-with-source');
|
||||
new BulmaTagsInput(tagsInput, {
|
||||
autocomplete: {
|
||||
<!-- Json to be completed -->
|
||||
source: "",
|
||||
source: async function(value) {
|
||||
// Value equal input value
|
||||
// We can then use it to request data from external API
|
||||
return await fetch("/api/tags_autocomplete.php?tag=" + encodeURIComponent(value))
|
||||
.then(function(response) {
|
||||
return response.json();
|
||||
});
|
||||
}
|
||||
});
|
||||
}, false);
|
||||
}
|
||||
|
||||
if (document.readyState !== 'loading') {
|
||||
setupTagsInput();
|
||||
} else {
|
||||
document.addEventListener('DOMContentLoaded', setupTagsInput);
|
||||
}
|
||||
</script>
|
||||
<main class="bd-main">
|
||||
<div class="bd-side-background"></div>
|
||||
|
@ -237,19 +247,15 @@
|
|||
<div class='row is-full'>
|
||||
<div class="columns">
|
||||
<div class="column">
|
||||
|
||||
|
||||
<div class="field">
|
||||
<label class="label">Tags</label>
|
||||
<div class="control">
|
||||
<input id="tags-with-source" name="tag_input" class="input" data-max-tags="10"
|
||||
data-max-chars="40" type="text" data-item-text="name"
|
||||
data-max-chars="40" type="text" data-item-text="name" data-item-value="name"
|
||||
data-case-sensitive="false" placeholder="10 Tags Maximum"
|
||||
value="<?php echo (isset($_POST['tag_input'])) ? $_POST['tag_input'] : ''; // Pre-populate if we come here on an error" ?>">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -420,12 +426,7 @@
|
|||
</div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
function roundToTwo(num) {
|
||||
|
|
Loading…
Add table
Reference in a new issue