1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-05-29 14:18:13 +03:00
ifhub.club/application/frontend/skin/synio/components/vote/js/vote.js
2016-09-18 23:37:14 +07:00

135 lines
4.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Голосование
*
* @module ls/vote
*
* @license GNU General Public License, version 2
* @copyright 2013 OOO "ЛС-СОФТ" {@link http://livestreetcms.com}
* @author Denis Shakhov <denis.shakhov@gmail.com>
*/
(function($) {
"use strict";
$.widget( "livestreet.lsVote", $.livestreet.lsComponent, {
/**
* Дефолтные опции
*/
options: {
// Ссылки
urls: {
// Голосование
vote: null,
// Информация о голосовании
info: null
},
// Селекторы
selectors: {
// Кнопки голосования
item: '.js-vote-item',
// Рейтинг
rating: '.js-vote-rating'
},
// Классы
classes : {
// Пользователь проголосовал
voted: 'ls-vote--voted',
// Не проголосовал
not_voted: 'ls-vote--not-voted',
// Понравилось
voted_up: 'ls-vote--voted-up',
// Не понравилось
voted_down: 'ls-vote--voted-down',
// Воздержался
voted_zero: 'ls-vote--voted-abstain',
// Рейтинг больше нуля
count_positive: 'ls-vote--count-positive',
// Меньше нуля
count_negative: 'ls-vote--count-negative',
// Равен нулю
count_zero: 'ls-vote--count-zero',
// Рейтинг скрыт
rating_hidden: 'ls-vote--rating-hidden'
},
// Параметры отправляемые при каждом аякс запросе
params: {},
// Опции тултипа с информацией о голосовании
tooltip_options: {}
},
/**
* Конструктор
*
* @constructor
* @private
*/
_create: function () {
this._super();
// Обработка кликов по кнопкам голосования
if ( ! this._hasClass( 'voted' ) ) {
this._on( this.elements.item, {
click: function ( event ) {
this.vote( $( event.currentTarget ).data( 'vote-value' ) );
event.preventDefault();
}
});
}
// Иниц-ия тултипа с информацией о голосовании
// Показываем инфо-ию только если рейтинг отображается
if ( ! this._hasClass( 'rating_hidden' ) ) {
this.info();
}
},
/**
* Голосование
*
* @param {Number} value Значение
*/
vote: function( value ) {
this.option( 'params.value', value );
this._load( 'vote', function ( response ) {
response.iRating = parseFloat( response.iRating );
// Добавляем/удаляем классы
this._removeClass( 'count_negative count_positive count_zero rating_hidden not_voted' );
this._addClass( 'voted' );
this._addClass( value > 0 ? 'voted_up' : ( value < 0 ? 'voted_down' : 'voted_zero' ) );
this._addClass( response.iRating > 0 ? 'count_positive' : ( response.iRating < 0 ? 'count_negative' : 'count_zero' ) );
// Не обрабатываем клики после голосования
this._off( this.elements.item, 'click' );
// Удаляем подсказки и устанавливаем рейтинг
this.elements.item.removeAttr( 'title' );
this.elements.rating.text( response.iRating );
// Иниц-ия тултипа
this.info().lsTooltip( 'show' );
});
},
/**
* Иниц-ия тултипа с информацией о голосовании
*
* @return {jQuery}
*/
info: function () {
if ( ! this.options.urls.info ) return $();
return this.element.lsTooltip($.extend({}, {
ajax: {
url: this.options.urls.info,
params: this.options.params
}
}, this.options.tooltip_options));
}
});
})(jQuery);