mirror of
https://github.com/Oreolek/ifhub.club.git
synced 2024-07-08 01:14:24 +03:00
109 lines
3.4 KiB
JavaScript
109 lines
3.4 KiB
JavaScript
/**
|
|
* Опрос
|
|
*
|
|
* @module ls/poll
|
|
*
|
|
* @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.lsPoll", $.livestreet.lsComponent, {
|
|
/**
|
|
* Дефолтные опции
|
|
*/
|
|
options: {
|
|
// Ссылки
|
|
urls: {
|
|
// Голосование за вариант
|
|
vote: aRouter.ajax + 'poll/vote/'
|
|
},
|
|
|
|
// Селекторы
|
|
selectors: {
|
|
// Форма голосования
|
|
form: '.js-poll-vote-form',
|
|
|
|
// Кнопка проголосовать
|
|
vote: '.js-poll-vote',
|
|
|
|
// Кнопка воздержаться от голосования
|
|
abstain: '.js-poll-abstain',
|
|
|
|
// Результата опроса
|
|
result: '.js-poll-result',
|
|
|
|
// Результата опроса
|
|
resultContainer: '.js-poll-result-container',
|
|
|
|
// Вариант
|
|
item: '.js-poll-result-item',
|
|
|
|
// Кнопка сортировки вариантов
|
|
sort: '.js-poll-result-sort'
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Конструктор
|
|
*
|
|
* @constructor
|
|
* @private
|
|
*/
|
|
_create: function () {
|
|
this._super();
|
|
|
|
! this.elements.form.length && this.initResult();
|
|
|
|
//
|
|
// События
|
|
//
|
|
|
|
this._on( this.elements.vote, { 'click': this.vote.bind( this, false ) } );
|
|
this._on( this.elements.abstain, { 'click': this.vote.bind( this, true ) } );
|
|
this.element.on( 'click' + this.eventNamespace, this.option( 'selectors.sort' ), this.sort.bind(this) );
|
|
},
|
|
|
|
/**
|
|
* Иниц-ия результата
|
|
*/
|
|
initResult: function() {
|
|
this.elements.sort = this.element.find( this.options.selectors.sort );
|
|
this.elements.items = this.element.find( this.options.selectors.item );
|
|
this.elements.result = this.element.find( this.options.selectors.result );
|
|
},
|
|
|
|
/**
|
|
* Голосование
|
|
*/
|
|
vote: function( abstain ) {
|
|
this._submit( 'vote', this.elements.form, function( response ) {
|
|
this.elements.resultContainer.html( $.trim( response.sText ) );
|
|
this.initResult();
|
|
|
|
this._off( this.elements.vote, 'click' );
|
|
this._off( this.elements.abstain, 'click' );
|
|
}.bind(this), {
|
|
submitButton: this.elements.vote,
|
|
params: { abstain: abstain ? 1 : 0 }
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Сортировка результата
|
|
*/
|
|
sort: function() {
|
|
var type = this.elements.sort.hasClass( ls.options.classes.states.active ) ? 'position' : 'count';
|
|
|
|
this.elements.items.sort( function (a, b) {
|
|
return $(b).data(type) - $(a).data(type);
|
|
});
|
|
|
|
this.elements.sort.toggleClass( ls.options.classes.states.active );
|
|
this.elements.result.html( this.elements.items );
|
|
}
|
|
});
|
|
})(jQuery); |