1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-07-08 09:24:25 +03:00
ifhub.club/application/frontend/common/js/poll.js

112 lines
2.8 KiB
JavaScript
Raw Normal View History

/**
2014-06-23 15:51:40 +03:00
* Опрос
*
* @module ls/poll
2014-06-23 15:51:40 +03:00
*
* @license GNU General Public License, version 2
* @copyright 2013 OOO "ЛС-СОФТ" {@link http://livestreetcms.com}
* @author Denis Shakhov <denis.shakhov@gmail.com>
*/
2014-06-23 15:51:40 +03:00
(function($) {
"use strict";
2011-04-01 10:49:36 +03:00
2014-06-23 15:51:40 +03:00
$.widget( "livestreet.lsPoll", {
/**
* Дефолтные опции
*/
options: {
// Ссылки
urls: {
// Голосование за вариант
vote: aRouter.ajax + 'poll/vote/'
2014-02-10 13:09:17 +02:00
},
2014-06-23 15:51:40 +03:00
// Селекторы
selectors: {
// Форма голосования
form: '.js-poll-vote-form',
2014-02-10 13:09:17 +02:00
2014-06-23 15:51:40 +03:00
// Кнопка проголосовать
vote: '.js-poll-vote',
2014-02-10 13:09:17 +02:00
2014-06-23 15:51:40 +03:00
// Кнопка воздержаться от голосования
abstain: '.js-poll-abstain',
2014-06-23 15:51:40 +03:00
// Результата опроса
result: '.js-poll-result',
2014-06-23 15:51:40 +03:00
// Вариант
item: '.js-poll-result-item',
2014-06-23 15:51:40 +03:00
// Кнопка сортировки вариантов
sort: '.js-poll-result-sort'
}
},
2014-02-10 13:09:17 +02:00
/**
2014-06-23 15:51:40 +03:00
* Конструктор
*
* @constructor
* @private
2014-02-10 13:09:17 +02:00
*/
2014-06-23 15:51:40 +03:00
_create: function () {
var _this = this;
2014-06-23 15:51:40 +03:00
this.elements = {
form: this.element.find( this.options.selectors.form ),
vote: this.element.find( this.options.selectors.vote ),
abstain: this.element.find( this.options.selectors.abstain )
};
2014-06-23 15:51:40 +03:00
! this.elements.form.length && this.initResult();
2014-06-23 15:51:40 +03:00
//
// События
//
2014-06-23 15:51:40 +03:00
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) );
},
2014-02-10 13:09:17 +02:00
/**
2014-06-23 15:51:40 +03:00
* Иниц-ия результата
2014-02-10 13:09:17 +02:00
*/
2014-06-23 15:51:40 +03:00
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 );
},
2014-02-10 13:09:17 +02:00
2014-06-23 15:51:40 +03:00
/**
* Голосование
*/
vote: function( abstain ) {
ls.ajax.submit( this.option( 'urls.vote' ), this.elements.form, function( response ) {
this.element.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 }
});
2014-06-23 15:51:40 +03:00
},
2014-06-23 15:51:40 +03:00
/**
* Сортировка результата
*/
sort: function() {
var type = this.elements.sort.hasClass( ls.options.classes.states.active ) ? 'position' : 'count';
2014-02-10 13:09:17 +02:00
2014-06-23 15:51:40 +03:00
this.elements.items.sort( function (a, b) {
return $(b).data(type) - $(a).data(type);
});
2014-06-23 15:51:40 +03:00
this.elements.sort.toggleClass( ls.options.classes.states.active );
this.elements.result.html( this.elements.items );
2011-05-03 18:39:24 +03:00
}
2014-06-23 15:51:40 +03:00
});
})(jQuery);