2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2020-08-20 05:09:55 -04:00
|
|
|
import { deprecatedCreateFlash as Flash } from './flash';
|
2018-02-20 17:20:48 -05:00
|
|
|
import axios from './lib/utils/axios_utils';
|
2021-02-14 13:09:20 -05:00
|
|
|
import { spriteIcon } from './lib/utils/common_utils';
|
|
|
|
import { __, s__ } from './locale';
|
2017-07-21 06:43:04 -04:00
|
|
|
|
2017-07-06 14:19:41 -04:00
|
|
|
export default class Star {
|
2018-12-13 07:37:54 -05:00
|
|
|
constructor(container = '.project-home-panel') {
|
|
|
|
$(`${container} .toggle-star`).on('click', function toggleStarClickCallback() {
|
2018-02-20 17:20:48 -05:00
|
|
|
const $this = $(this);
|
|
|
|
const $starSpan = $this.find('span');
|
2018-12-13 07:37:54 -05:00
|
|
|
const $starIcon = $this.find('svg');
|
|
|
|
const iconClasses = $starIcon.attr('class').split(' ');
|
2017-10-11 04:10:35 -04:00
|
|
|
|
2018-10-10 03:13:34 -04:00
|
|
|
axios
|
|
|
|
.post($this.data('endpoint'))
|
2018-02-20 17:20:48 -05:00
|
|
|
.then(({ data }) => {
|
|
|
|
const isStarred = $starSpan.hasClass('starred');
|
2020-12-23 07:10:26 -05:00
|
|
|
$this.parent().find('.count').text(data.star_count);
|
2018-02-20 17:20:48 -05:00
|
|
|
|
2017-10-11 04:10:35 -04:00
|
|
|
if (isStarred) {
|
|
|
|
$starSpan.removeClass('starred').text(s__('StarProject|Star'));
|
2018-12-13 07:37:54 -05:00
|
|
|
$starIcon.remove();
|
|
|
|
$this.prepend(spriteIcon('star-o', iconClasses));
|
2017-10-11 04:10:35 -04:00
|
|
|
} else {
|
|
|
|
$starSpan.addClass('starred').text(__('Unstar'));
|
2018-12-13 07:37:54 -05:00
|
|
|
$starIcon.remove();
|
|
|
|
$this.prepend(spriteIcon('star', iconClasses));
|
2017-10-11 04:10:35 -04:00
|
|
|
}
|
2018-02-20 17:20:48 -05:00
|
|
|
})
|
2019-05-07 02:40:58 -04:00
|
|
|
.catch(() => Flash(__('Star toggle failed. Try again later.')));
|
2018-02-20 17:20:48 -05:00
|
|
|
});
|
2017-07-05 13:20:41 -04:00
|
|
|
}
|
2017-06-30 17:27:31 -04:00
|
|
|
}
|