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/vote.js

201 lines
5.4 KiB
JavaScript
Raw Normal View History

/**
* Голосование
*
* @module ls/vote
*
* @license GNU General Public License, version 2
* @copyright 2013 OOO "ЛС-СОФТ" {@link http://livestreetcms.com}
* @author Denis Shakhov <denis.shakhov@gmail.com>
*/
2011-05-03 13:10:28 +03:00
var ls = ls || {};
ls.vote = (function ($) {
2013-07-09 11:40:06 +03:00
"use strict";
2011-05-03 13:10:28 +03:00
/**
2013-07-09 11:40:06 +03:00
* Дефолтные опции
*
* @member {Object}
* @private
2013-07-09 11:40:06 +03:00
*/
var _defaults = {
2013-07-09 11:40:06 +03:00
// Селекторы
selectors : {
vote: '.js-vote',
up: '.js-vote-up',
down: '.js-vote-down',
abstain: '.js-vote-abstain',
count: '.js-vote-count',
2013-07-29 08:11:36 +03:00
rating: '.js-vote-rating'
2013-07-09 11:40:06 +03:00
},
// Классы
classes : {
voted: 'vote-voted',
voted_up: 'vote-voted-up',
voted_down: 'vote-voted-down',
voted_zero: 'vote-voted-zero',
positive: 'vote-count-positive',
negative: 'vote-count-negative',
zero: 'vote-count-zero',
2012-05-27 08:18:27 +03:00
not_voted: 'not-voted'
2011-04-01 10:49:36 +03:00
},
2012-01-28 17:52:44 +02:00
2011-05-03 13:10:28 +03:00
type: {
comment: {
2013-07-09 11:40:06 +03:00
url: aRouter['ajax'] + 'vote/comment/',
2011-05-03 13:10:28 +03:00
targetName: 'idComment'
},
topic: {
2013-07-09 11:40:06 +03:00
url: aRouter['ajax'] + 'vote/topic/',
2011-05-03 13:10:28 +03:00
targetName: 'idTopic'
},
blog: {
2013-07-09 11:40:06 +03:00
url: aRouter['ajax'] + 'vote/blog/',
2011-05-03 13:10:28 +03:00
targetName: 'idBlog'
},
user: {
2013-07-09 11:40:06 +03:00
url: aRouter['ajax'] + 'vote/user/',
2011-05-03 13:10:28 +03:00
targetName: 'idUser'
}
2011-04-01 10:49:36 +03:00
}
2011-05-03 13:10:28 +03:00
};
2013-07-09 11:40:06 +03:00
/**
* Инициализация
*
* @param {Object} options Опции
2013-07-09 11:40:06 +03:00
*/
this.init = function(options) {
var self = this;
2012-01-28 17:52:44 +02:00
this.options = $.extend({}, _defaults, options);
2011-04-01 10:49:36 +03:00
2013-07-09 11:40:06 +03:00
$(this.options.selectors.vote).each(function () {
var oVote = $(this);
var oVars = {
vote: oVote,
up: oVote.find(self.options.selectors.up),
down: oVote.find(self.options.selectors.down),
abstain: oVote.find(self.options.selectors.abstain),
count: oVote.find(self.options.selectors.count),
rating: oVote.find(self.options.selectors.rating),
id: oVote.data('vote-id'),
2013-07-29 08:11:36 +03:00
type: oVote.data('vote-type')
2013-07-09 11:40:06 +03:00
};
// Плюс
oVars.up.on('click', function (e) {
self.vote(oVars.id, 1, oVars.type, oVars);
e.preventDefault();
});
// Минус
oVars.down.on('click', function (e) {
self.vote(oVars.id, -1, oVars.type, oVars);
e.preventDefault();
});
// Воздержаться
oVars.abstain.on('click', function (e) {
self.vote(oVars.id, 0, oVars.type, oVars);
e.preventDefault();
});
});
};
/**
* Голосование
*
* @param {Number} iTargetId ID объекта
* @param {Number} iValue Значение
* @param {String} sType Тип объекта за который голосуют
2013-07-09 11:40:06 +03:00
* @param {Object} oVars Переменные текущего голосования
*/
this.vote = function(iTargetId, iValue, sType, oVars) {
var self = this;
if ( ! this.options.type[sType] ) return false;
var params = {
value: iValue
};
params[this.options.type[sType].targetName] = iTargetId;
2012-04-28 18:40:06 +03:00
ls.hook.marker('voteBefore');
2013-07-09 11:40:06 +03:00
ls.ajax.load(this.options.type[sType].url, params, function (result) {
2013-07-09 11:40:06 +03:00
var args = [iTargetId, iValue, sType, oVars, result];
this.onVote.apply(this, args);
2011-05-03 13:10:28 +03:00
}.bind(this));
2013-07-09 11:40:06 +03:00
};
2011-05-03 13:10:28 +03:00
2013-07-09 11:40:06 +03:00
/**
* Коллбэк вызываемый при успешном голосовании
*
* @param {Number} iTargetId ID объекта
* @param {Number} iValue Значение
* @param {String} sType Тип голосования
* @param {Object} oVars Переменные текущего голосования
* @param {Object} result Объект возвращемый сервером
*/
this.onVote = function(iTargetId, iValue, sType, oVars, result) {
2011-04-01 10:49:36 +03:00
if (result.bStateError) {
2011-05-03 13:10:28 +03:00
ls.msg.error(null, result.sMsg);
2011-04-01 10:49:36 +03:00
} else {
2011-05-03 13:10:28 +03:00
ls.msg.notice(null, result.sMsg);
2012-01-28 17:52:44 +02:00
2013-07-09 11:40:06 +03:00
oVars.vote
.addClass(this.options.classes.voted)
.removeClass(this.options.classes.negative + ' ' + this.options.classes.positive + ' ' + this.options.classes.not_voted + ' ' + this.options.classes.zero);
2011-05-03 13:10:28 +03:00
2013-07-09 11:40:06 +03:00
if (iValue > 0) {
oVars.vote.addClass(this.options.classes.voted_up);
2013-07-09 11:40:06 +03:00
} else if (iValue < 0) {
oVars.vote.addClass(this.options.classes.voted_down);
2013-07-09 11:40:06 +03:00
} else if (iValue == 0) {
oVars.vote.addClass(this.options.classes.voted_zero);
2012-05-27 08:18:27 +03:00
}
2011-04-01 10:49:36 +03:00
2013-07-09 11:40:06 +03:00
if (oVars.count.length > 0 && result.iCountVote) {
oVars.count.text(parseInt(result.iCountVote));
2011-09-06 13:43:14 +03:00
}
2011-05-03 13:10:28 +03:00
result.iRating = parseFloat(result.iRating);
if (result.iRating > 0) {
2013-07-09 11:40:06 +03:00
oVars.vote.addClass(this.options.classes.positive);
oVars.rating.text(result.iRating);
2013-07-09 11:40:06 +03:00
} else if (result.iRating < 0) {
oVars.vote.addClass(this.options.classes.negative);
oVars.rating.text(result.iRating);
} else if (result.iRating == 0) {
oVars.vote.addClass(this.options.classes.zero);
oVars.rating.text(0);
2011-04-01 10:49:36 +03:00
}
2011-05-03 13:10:28 +03:00
var method = 'onVote' + ls.utilities.ucfirst(sType);
2013-07-09 11:40:06 +03:00
if (typeof this[method] == 'function') {
this[method].apply(this, [iTargetId, iValue, sType, oVars, result]);
}
2011-05-03 13:10:28 +03:00
}
2013-07-09 11:40:06 +03:00
};
2012-01-28 17:52:44 +02:00
2013-07-09 11:40:06 +03:00
/**
* Голосование за топик
*
* @param {Number} iTargetId ID объекта
* @param {Number} iValue Значение
* @param {String} sType Тип голосования
* @param {Object} oVars Переменные текущего голосования
* @param {Object} result Объект возвращемый сервером
*/
this.onVoteTopic = function(iTargetId, iValue, sType, oVars, result) {
oVars.vote.addClass('js-tooltip-vote-topic').tooltip('enter');
};
2011-05-03 13:10:28 +03:00
return this;
}).call(ls.vote || {},jQuery);