Skip to content
Snippets Groups Projects
Commit 3e0976b7 authored by Sven Kästle's avatar Sven Kästle
Browse files

feat: Add delete button to card if the annotation is from user

parent e733293d
No related branches found
No related tags found
No related merge requests found
...@@ -109,8 +109,16 @@ ol { ...@@ -109,8 +109,16 @@ ol {
text-align: right; text-align: right;
font-size: 9px; font-size: 9px;
color: lightgrey; color: lightgrey;
display: flex;
} }
.annotation-footer span { .annotation-footer span {
margin-left: 5px; margin-left: 5px;
} }
.annotation-footer-delete {
margin-right: 5px;
cursor: pointer;
}
.annotation-footer-date {
flex: 1
}
...@@ -69,8 +69,8 @@ $(document).ready(function() { ...@@ -69,8 +69,8 @@ $(document).ready(function() {
/** /**
* POST: Save an annotation in the database * POST: Save an annotation in the database
* *
* @param annotationPostRequest the post request * @param annotationPostRequest The post request
* @param responseHandler the response handler * @param responseHandler The response handler
*/ */
function createAnnotation(annotationPostRequest, responseHandler) { function createAnnotation(annotationPostRequest, responseHandler) {
var url = "http://localhost:8080/rest/annotations/"; var url = "http://localhost:8080/rest/annotations/";
...@@ -88,11 +88,11 @@ function createAnnotation(annotationPostRequest, responseHandler) { ...@@ -88,11 +88,11 @@ function createAnnotation(annotationPostRequest, responseHandler) {
} }
/** /**
* PATCH: Alter an annotation * PATCH: Alter an annotation in database
* *
* @param id the annotation id * @param id The annotation id
* @param annotationPatchRequest the patch request * @param annotationPatchRequest The patch request
* @param responseHandler the response handler * @param responseHandler The response handler
*/ */
function alterAnnotation(id, annotationPatchRequest, responseHandler) { function alterAnnotation(id, annotationPatchRequest, responseHandler) {
var url = "http://localhost:8080/rest/annotations/" + id; var url = "http://localhost:8080/rest/annotations/" + id;
...@@ -110,11 +110,28 @@ function alterAnnotation(id, annotationPatchRequest, responseHandler) { ...@@ -110,11 +110,28 @@ function alterAnnotation(id, annotationPatchRequest, responseHandler) {
} }
/** /**
* GET: Get all annotations for a specific target * DELETE: Delete an annotation from database
* *
* @param id The annotation id
*/
function deleteAnnotation(id) {
var url = "http://localhost:8080/rest/annotations/" + id;
$.ajax({
url: url,
type: "DELETE",
dataType: "json",
success: function (response) {
// Nothing to do
}
});
}
/**
* GET: Get all annotations from database for a specific target
* *
* @param targetId the target id *
* @param responseHandler the response handler * @param targetId The target id
* @param responseHandler The response handler
*/ */
function getAnnotations(targetId, responseHandler) { function getAnnotations(targetId, responseHandler) {
var url = "http://localhost:8080/rest/annotations/target/" + targetId; var url = "http://localhost:8080/rest/annotations/target/" + targetId;
...@@ -133,37 +150,39 @@ function getAnnotations(targetId, responseHandler) { ...@@ -133,37 +150,39 @@ function getAnnotations(targetId, responseHandler) {
}); });
} }
/**
* Delete annotation from list
*
* @param elem The parent li element
* @param id The id of the annotation
*/
function deleteAnnotationHandler(elem, id) {
// remove annotation from list
elem.remove()
// remove highlighted text
deleteHighlightedText();
// remove annotation from database
deleteAnnotation(id)
}
/** /**
* Display annotation in the list * Display annotation in the list
* *
* @param annotation the annotation to be displayed * @param annotation The annotation to be displayed
*/ */
function displayAnnotation(annotation) { function displayAnnotation(annotation) {
// fetch list of annotations // fetch list of annotations
var list = $('#annotations') var list = $('#annotations')
// insert spacing if we need one var deleteIcon = "fas fa-trash";
if ($('#annotations li').filter( ".listelement" ).length > 0) {
list.prepend(
$('<li>').attr('class', 'spacing')
)
}
var dateIcon = "fas fa-calendar"; var dateIcon = "fas fa-calendar";
if (timestampIsToday(annotation.timestamp)) { if (isTimestampToday(annotation.timestamp)) {
dateIcon = "fas fa-clock"; dateIcon = "fas fa-clock";
} }
// insert annotation card // insert annotation card
list.prepend( list.prepend(
$('<li>') $('<li>')
.mouseenter(function () {
addHighlightedText(annotation.startCharacter, annotation.endCharacter, annotation.userId);
})
.mouseleave(function () {
deleteHighlightedText();
})
.data('annotation', annotation)
.attr('class', 'listelement') .attr('class', 'listelement')
.append( .append(
$('<div>').attr('class', 'annotation-card') $('<div>').attr('class', 'annotation-card')
...@@ -216,23 +235,52 @@ function displayAnnotation(annotation) { ...@@ -216,23 +235,52 @@ function displayAnnotation(annotation) {
.append( .append(
$('<div>').attr('class', 'annotation-footer') $('<div>').attr('class', 'annotation-footer')
.append( .append(
$('<i>').attr('class', dateIcon) function () {
if (userId == annotation.userId) {
return $('<div>').attr('class', 'annotation-footer-delete')
.append(
$('<i>').attr('class', deleteIcon)
)
.click(function () {
deleteAnnotationHandler($(this).closest('li'), annotation.id)
})
}
}
) )
.append( .append(
$('<span>').append(timestampToReadableTime(annotation.timestamp)) $('<div>').attr('class', 'annotation-footer-date overflow-hidden')
.append(
$('<i>').attr('class', dateIcon)
)
.append(
$('<span>').append(timestampToReadableTime(annotation.timestamp))
)
) )
) )
) )
.data('annotation', annotation)
.mouseenter(function () {
addHighlightedText(annotation.startCharacter, annotation.endCharacter, annotation.userId);
})
.mouseleave(function () {
deleteHighlightedText();
})
.append(function () {
if ($('#annotations li').filter( ".listelement" ).length > 0) {
return $('<div>').attr('class', 'spacing')
}
})
); );
} }
/** /**
* Add a highlighted text at specific position * Add a highlighted text at specific position
* *
* @param startCharacter the offset of the start character * @param startCharacter The offset of the start character
* @param endCharacter the offset of the end character * @param endCharacter The offset of the end character
* @param userId the user id * @param userId The user id
*/ */
function addHighlightedText(startCharacter, endCharacter, userId) { function addHighlightedText(startCharacter, endCharacter, userId) {
// create <span> tag with the annotated text // create <span> tag with the annotated text
...@@ -258,7 +306,7 @@ function deleteHighlightedText() { ...@@ -258,7 +306,7 @@ function deleteHighlightedText() {
/** /**
* Get the text value of the selected text * Get the text value of the selected text
* *
* @returns {string} the text * @returns {string} The text
*/ */
function getSelectedText() { function getSelectedText() {
if(window.getSelection){ if(window.getSelection){
...@@ -275,8 +323,8 @@ function getSelectedText() { ...@@ -275,8 +323,8 @@ function getSelectedText() {
/** /**
* Get color based on user id * Get color based on user id
* *
* @param userId the id of the user * @param userId The id of the user
* @returns {string} the user color * @returns {string} The user color
*/ */
function getUserColor(userId) { function getUserColor(userId) {
// insert new color if there is no userId key // insert new color if there is no userId key
...@@ -290,8 +338,8 @@ function getUserColor(userId) { ...@@ -290,8 +338,8 @@ function getUserColor(userId) {
/** /**
* Get dark color based on user id * Get dark color based on user id
* *
* @param userId the id of the user * @param userId The id of the user
* @returns {string} the dark user color * @returns {string} The dark user color
*/ */
function getDarkUserColor(userId) { function getDarkUserColor(userId) {
// insert new color if there is no userId key // insert new color if there is no userId key
...@@ -305,7 +353,7 @@ function getDarkUserColor(userId) { ...@@ -305,7 +353,7 @@ function getDarkUserColor(userId) {
/** /**
* Generate a random color of the format 'rgb(r, g, b)' * Generate a random color of the format 'rgb(r, g, b)'
* *
* @param userId the given user id * @param userId The given user id
*/ */
function generateRandomColor(userId) { function generateRandomColor(userId) {
var r = Math.floor(Math.random()*56)+170; var r = Math.floor(Math.random()*56)+170;
...@@ -325,8 +373,8 @@ function generateRandomColor(userId) { ...@@ -325,8 +373,8 @@ function generateRandomColor(userId) {
/** /**
* Calculate and build a readable timestamp from an unix timestamp * Calculate and build a readable timestamp from an unix timestamp
* *
* @param timestamp a unix timestamp * @param timestamp A unix timestamp
* @returns {string} a readable timestamp * @returns {string} A readable timestamp
*/ */
function timestampToReadableTime(timestamp) { function timestampToReadableTime(timestamp) {
// build Date object from timestamp // build Date object from timestamp
...@@ -335,7 +383,7 @@ function timestampToReadableTime(timestamp) { ...@@ -335,7 +383,7 @@ function timestampToReadableTime(timestamp) {
var responseTimestamp; var responseTimestamp;
// if annotation is from today // if annotation is from today
if (timestampIsToday(timestamp)) { if (isTimestampToday(timestamp)) {
// get hours from date // get hours from date
var hours = annotationDate.getHours(); var hours = annotationDate.getHours();
// get minutes from date // get minutes from date
...@@ -365,10 +413,10 @@ function timestampToReadableTime(timestamp) { ...@@ -365,10 +413,10 @@ function timestampToReadableTime(timestamp) {
/** /**
* Check if given timestamp is from today * Check if given timestamp is from today
* *
* @param timestamp the given timestamp in milliseconds * @param timestamp The given timestamp in milliseconds
* @returns {boolean} returns true if the timestamp is from today * @returns {boolean} Returns true if the timestamp is from today
*/ */
function timestampIsToday(timestamp) { function isTimestampToday(timestamp) {
// now // now
var now = new Date(); var now = new Date();
// build Date object from timestamp // build Date object from timestamp
...@@ -386,7 +434,7 @@ function timestampIsToday(timestamp) { ...@@ -386,7 +434,7 @@ function timestampIsToday(timestamp) {
/** /**
* Toggle between the toggle button status * Toggle between the toggle button status
* *
* @param element the given toggle button * @param element The given toggle button
*/ */
function toggleButtonHandler(element) { function toggleButtonHandler(element) {
// open and close annotation text // open and close annotation text
...@@ -399,6 +447,6 @@ function toggleButtonHandler(element) { ...@@ -399,6 +447,6 @@ function toggleButtonHandler(element) {
MOCKUP FUNCTIONS MOCKUP FUNCTIONS
*/ */
function randomUserId() { function randomUserId() {
return Math.floor((Math.random() * 10) + 1);; return Math.floor((Math.random() * 12) + 1);;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment