forked from Public/pics
PhotoPage: update and refactor tagging script slightly
This commit is contained in:
parent
b13701f7c0
commit
e3d481caa1
@ -526,20 +526,15 @@ a#previous_photo:hover, a#next_photo:hover {
|
|||||||
#sub_photo h2, #sub_photo h3 {
|
#sub_photo h2, #sub_photo h3 {
|
||||||
margin-bottom: 1rem;
|
margin-bottom: 1rem;
|
||||||
}
|
}
|
||||||
#sub_photo #tag_list {
|
#sub_photo .tag-list {
|
||||||
list-style: none;
|
list-style: none;
|
||||||
margin: 1em 0;
|
margin: 1em 0;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
}
|
}
|
||||||
#sub_photo #tag_list li {
|
#sub_photo .tag-list > li {
|
||||||
display: inline;
|
display: inline-block;
|
||||||
padding-right: 0.75em;
|
margin-bottom: 0.75em;
|
||||||
}
|
margin-right: 0.75em;
|
||||||
#tag_list .delete-tag {
|
|
||||||
opacity: 0.25;
|
|
||||||
}
|
|
||||||
#tag_list .delete-tag:hover {
|
|
||||||
opacity: 1.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.photo_meta {
|
.photo_meta {
|
||||||
|
@ -120,9 +120,12 @@ class PhotoPage extends Template
|
|||||||
|
|
||||||
private function printTags($header, $tagKind, $allowLinkingNewTags)
|
private function printTags($header, $tagKind, $allowLinkingNewTags)
|
||||||
{
|
{
|
||||||
|
static $nextTagListId = 1;
|
||||||
|
$tagListId = 'tagList' . ($nextTagListId++);
|
||||||
|
|
||||||
echo '
|
echo '
|
||||||
<h3>', $header, '</h3>
|
<h3>', $header, '</h3>
|
||||||
<ul id="tag_list" class="d-flex">';
|
<ul id="', $tagListId, '" class="tag-list">';
|
||||||
|
|
||||||
foreach ($this->photo->getTags() as $tag)
|
foreach ($this->photo->getTags() as $tag)
|
||||||
{
|
{
|
||||||
@ -138,7 +141,7 @@ class PhotoPage extends Template
|
|||||||
if ($tag->kind === 'Person')
|
if ($tag->kind === 'Person')
|
||||||
{
|
{
|
||||||
echo '
|
echo '
|
||||||
<a class="btn btn-danger px-1" title="Unlink this tag from this photo" href="#" data-id="', $tag->id_tag, '">
|
<a class="delete-tag btn btn-danger px-1" title="Unlink this tag from this photo" href="#" data-id="', $tag->id_tag, '">
|
||||||
<i class="bi bi-x"></i>
|
<i class="bi bi-x"></i>
|
||||||
</a>';
|
</a>';
|
||||||
}
|
}
|
||||||
@ -149,71 +152,80 @@ class PhotoPage extends Template
|
|||||||
</li>';
|
</li>';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static $nextNewTagId = 1;
|
||||||
|
$newTagId = 'newTag' . ($nextNewTagId++);
|
||||||
|
|
||||||
if ($allowLinkingNewTags)
|
if ($allowLinkingNewTags)
|
||||||
$this->linkNewTags();
|
{
|
||||||
|
echo '
|
||||||
|
<li style="position: relative">
|
||||||
|
<input class="form-control w-auto" type="text" id="', $newTagId, '" placeholder="Type to link a new tag">
|
||||||
|
</li>';
|
||||||
|
}
|
||||||
|
|
||||||
echo '
|
echo '
|
||||||
</ul>';
|
</ul>';
|
||||||
|
|
||||||
|
$this->printNewTagScript($tagKind, $tagListId, $newTagId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function linkNewTags()
|
private function printNewTagScript($tagKind, $tagListId, $newTagId)
|
||||||
{
|
{
|
||||||
echo '
|
echo '
|
||||||
<li style="position: relative">
|
|
||||||
<input class="form-control w-auto" type="text" id="new_tag" placeholder="Type to link a new tag">
|
|
||||||
</li>
|
|
||||||
<script type="text/javascript" src="', BASEURL, '/js/ajax.js"></script>
|
<script type="text/javascript" src="', BASEURL, '/js/ajax.js"></script>
|
||||||
<script type="text/javascript" src="', BASEURL, '/js/autosuggest.js"></script>
|
<script type="text/javascript" src="', BASEURL, '/js/autosuggest.js"></script>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
var removeTag = function(event) {
|
const removeTag = function(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
var that = this;
|
const request = new HttpRequest("post", "', $this->photo->getPageUrl(), '",
|
||||||
var request = new HttpRequest("post", "', $this->photo->getPageUrl(), '",
|
"id_tag=" + this.dataset["id"] + "&delete", (response) => {
|
||||||
"id_tag=" + this.dataset["id"] + "&delete", function(response) {
|
|
||||||
if (!response.success) {
|
if (!response.success) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var tagNode = document.getElementById("tag-" + that.dataset["id"]);
|
const tagNode = document.getElementById("tag-" + this.dataset["id"]);
|
||||||
tagNode.parentNode.removeChild(tagNode);
|
tagNode.parentNode.removeChild(tagNode);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var tagRemovalTargets = document.getElementsByClassName("delete-tag");
|
let tagRemovalTargets = document.querySelectorAll(".delete-tag");
|
||||||
for (var i = 0; i < tagRemovalTargets.length; i++) {
|
tagRemovalTargets.forEach(el => el.addEventListener("click", removeTag));
|
||||||
tagRemovalTargets[i].addEventListener("click", removeTag);
|
|
||||||
}
|
|
||||||
|
|
||||||
var tag_autosuggest = new TagAutoSuggest({
|
let tag_autosuggest = new TagAutoSuggest({
|
||||||
inputElement: "new_tag",
|
inputElement: "', $newTagId, '",
|
||||||
listElement: "tag_list",
|
listElement: "', $tagListId, '",
|
||||||
baseUrl: "', BASEURL, '",
|
baseUrl: "', BASEURL, '",
|
||||||
appendCallback: function(item) {
|
appendCallback: (item) => {
|
||||||
var request = new HttpRequest("post", "', $this->photo->getPageUrl(), '",
|
const request = new HttpRequest("post", "', $this->photo->getPageUrl(), '",
|
||||||
"id_tag=" + item.id_tag, function(response) {
|
"id_tag=" + item.id_tag, (response) => {
|
||||||
var newLink = document.createElement("a");
|
const newListItem = document.createElement("li");
|
||||||
|
newListItem.id = "tag-" + item.id_tag;
|
||||||
|
|
||||||
|
const newLink = document.createElement("a");
|
||||||
newLink.href = item.url;
|
newLink.href = item.url;
|
||||||
|
newLink.title = "View all posts tagged " + item.label;
|
||||||
|
newListItem.appendChild(newLink);
|
||||||
|
|
||||||
var newLabel = document.createTextNode(item.label);
|
const newInputGroup = document.createElement("div");
|
||||||
newLink.appendChild(newLabel);
|
newInputGroup.className = "input-group";
|
||||||
|
newLink.appendChild(newInputGroup);
|
||||||
|
|
||||||
var removeLink = document.createElement("a");
|
const newLabel = document.createElement("span");
|
||||||
removeLink.className = "delete-tag";
|
newLabel.className = "input-group-text";
|
||||||
|
newLabel.textContent = item.label;
|
||||||
|
newInputGroup.appendChild(newLabel);
|
||||||
|
|
||||||
|
const removeLink = document.createElement("a");
|
||||||
|
removeLink.className = "delete-tag btn btn-danger px-1";
|
||||||
removeLink.dataset["id"] = item.id_tag;
|
removeLink.dataset["id"] = item.id_tag;
|
||||||
removeLink.href = "#";
|
removeLink.href = "#";
|
||||||
|
removeLink.innerHTML = \'<i class="bi bi-x"></i>\';
|
||||||
removeLink.addEventListener("click", removeTag);
|
removeLink.addEventListener("click", removeTag);
|
||||||
|
newInputGroup.appendChild(removeLink);
|
||||||
|
|
||||||
var crossmark = document.createTextNode("❌");
|
const list = document.getElementById("', $tagListId, '");
|
||||||
removeLink.appendChild(crossmark);
|
list.insertBefore(newListItem, list.querySelector("li:last-child"));
|
||||||
|
|
||||||
var newNode = document.createElement("li");
|
|
||||||
newNode.id = "tag-" + item.id_tag;
|
|
||||||
newNode.appendChild(newLink);
|
|
||||||
newNode.appendChild(removeLink);
|
|
||||||
|
|
||||||
var list = document.getElementById("tag_list");
|
|
||||||
list.appendChild(newNode);
|
|
||||||
}, this);
|
}, this);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user