Skip to content

Commit 891fb4d

Browse files
committed
Handle API error and add micro-animation
1 parent 59ad3ba commit 891fb4d

File tree

3 files changed

+90
-9
lines changed

3 files changed

+90
-9
lines changed

plugins/SegmentEditor/API.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,13 @@ public function add(
355355
* @param int $idSegment
356356
* @throws Exception if the user is not logged in or does not have the required permissions.
357357
*/
358-
public function star(int $idSegment): void
358+
public function star(int $idSegment): bool
359359
{
360360
$segment = $this->getSegmentOrFail($idSegment);
361361
$this->checkUserCanEditOrDeleteSegment($segment);
362362
$bind = ['starred' => 1];
363363

364-
$this->getModel()->updateSegment($idSegment, $bind);
364+
return $this->getModel()->updateSegment($idSegment, $bind);
365365
}
366366

367367
/**
@@ -370,13 +370,13 @@ public function star(int $idSegment): void
370370
* @param int $idSegment
371371
* @throws Exception if the user is not logged in or does not have the required permissions.
372372
*/
373-
public function unstar(int $idSegment): void
373+
public function unstar(int $idSegment): bool
374374
{
375375
$segment = $this->getSegmentOrFail($idSegment);
376376
$this->checkUserCanEditOrDeleteSegment($segment);
377377
$bind = ['starred' => 0];
378378

379-
$this->getModel()->updateSegment($idSegment, $bind);
379+
return $this->getModel()->updateSegment($idSegment, $bind);
380380
}
381381

382382
/**

plugins/SegmentEditor/javascripts/Segmentation.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,18 +411,30 @@ Segmentation = (function($) {
411411
e.preventDefault();
412412
});
413413

414+
function updateStarredSegment($segment, isStarred, isError = false) {
415+
const $starButton = $segment.find('.starSegment');
416+
const title = self.translations[isStarred ? 'General_RemoveFromFavorites' : 'General_AddToFavorites'].toLocaleLowerCase();
417+
addTooltip($starButton, title);
418+
$segment.toggleClass('segmentStarred', isStarred);
419+
$segment.one('animationend', function avoidAnimationRepetion() {
420+
$segment.removeClass('segmentStarAnimation');
421+
$segment.removeClass('segmentUnstarAnimation');
422+
$segment.removeClass('segmentStarErrorAnimation');
423+
});
424+
$segment.toggleClass('segmentStarAnimation', !isError && isStarred);
425+
$segment.toggleClass('segmentUnstarAnimation', !isError && !isStarred);
426+
$segment.toggleClass('segmentStarErrorAnimation', isError);
427+
}
428+
414429
self.target.on('click', '[data-star]', function (e) {
415430
e.stopPropagation();
416431
e.preventDefault();
417432
const $root = $(this).closest('li');
418-
const $starButton = $root.find('.starSegment');
419433
const idSegment = $root.data('idsegment');
420434
const segment = getSegmentFromId(idSegment);
421435
segment.starred = !segment.starred;
422-
const title = self.translations[segment.starred ? 'General_RemoveFromFavorites' : 'General_AddToFavorites'].toLocaleLowerCase();
423-
addTooltip($starButton, title);
424436
const method = segment.starred ? 'star' : 'unstar';
425-
$root.toggleClass('segmentStarred', segment.starred);
437+
updateStarredSegment($root, segment.starred);
426438

427439
var ajaxHandler = new ajaxHelper();
428440
ajaxHandler.addParams({
@@ -431,7 +443,11 @@ Segmentation = (function($) {
431443
"method": 'SegmentEditor.' + method,
432444
"userLogin": piwik.userLogin,
433445
"idSegment": idSegment,
434-
}, 'GET');
446+
}, 'POST');
447+
ajaxHandler.setErrorCallback(function (response) {
448+
segment.starred = !segment.starred;
449+
updateStarredSegment($root, segment.starred, true);
450+
});
435451
ajaxHandler.send();
436452
});
437453

plugins/SegmentEditor/stylesheets/segmentation.less

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,43 @@ div.scrollable {
278278
margin-bottom: 5px;
279279
}
280280

281+
@keyframes starAnimation {
282+
0% { transform: scale(1) rotate(0deg); }
283+
50% { transform: scale(1.2) rotate(180deg); opacity: 1; }
284+
100% { transform: scale(1) rotate(360deg); }
285+
}
286+
287+
@keyframes starAnimationPath {
288+
0% { fill: black; }
289+
50% { fill: @theme-color-brand; stroke: @theme-color-brand; }
290+
100% { fill: black; }
291+
}
292+
293+
@keyframes unstarAnimation {
294+
0% { transform: scale(1) rotate(0deg); }
295+
50% { transform: scale(1.2) rotate(-180deg);}
296+
100% { transform: scale(1) rotate(-360deg); }
297+
}
298+
299+
@keyframes unstarAnimationPath {
300+
0% { fill: black; }
301+
100% { fill: transparent; }
302+
}
303+
304+
@keyframes starErrorAnimation {
305+
0% { opacity: 0.5; }
306+
5%, 15%, 25%, 35%, 45% { transform: translate(-2px)}
307+
10%, 20%, 30%, 40% { transform: translate(2px)}
308+
50% { opacity: 1; }
309+
100% { transform: translate(0px); opacity: 0.5; }
310+
}
311+
312+
@keyframes starErrorAnimationPath {
313+
0% { stroke: black; fill: black; }
314+
25%, 75% { stroke: red; fill: red; }
315+
100% { stroke: black; fill: transparent; }
316+
}
317+
281318
.segmentationContainer ul.submenu > li {
282319
span.editSegment,
283320
span.compareSegment,
@@ -310,6 +347,34 @@ div.scrollable {
310347
order: 2;
311348
}
312349

350+
.segmentStarAnimation .starSegment {
351+
animation: starAnimation 0.5s 1;
352+
}
353+
354+
.segmentStarAnimation .starSegment path {
355+
animation: starAnimationPath 0.5s 1;
356+
}
357+
358+
.segmentUnstarAnimation .starSegment {
359+
animation: unstarAnimation 0.5s 1;
360+
}
361+
362+
.segmentUnstarAnimation .starSegment path {
363+
animation: unstarAnimationPath 0.5s 1;
364+
}
365+
366+
.segmentStarErrorAnimation .starSegment {
367+
animation: starErrorAnimation 2s 1;
368+
}
369+
370+
.segmentStarErrorAnimation .starSegment path {
371+
animation: starErrorAnimationPath 2s 1;
372+
}
373+
374+
.segmentStarred.segmentStarErrorAnimation .starSegment path {
375+
animation-direction: reverse;
376+
}
377+
313378
.segmentStarred .starSegment path {
314379
fill: black;
315380
}

0 commit comments

Comments
 (0)