2013-07-08 07:42:49 +03:00
|
|
|
|
/**
|
|
|
|
|
* Опросы
|
2013-08-29 12:30:04 +03:00
|
|
|
|
*
|
|
|
|
|
* @module ls/poll
|
|
|
|
|
*
|
|
|
|
|
* @license GNU General Public License, version 2
|
|
|
|
|
* @copyright 2013 OOO "ЛС-СОФТ" {@link http://livestreetcms.com}
|
|
|
|
|
* @author Denis Shakhov <denis.shakhov@gmail.com>
|
2013-07-08 07:42:49 +03:00
|
|
|
|
*/
|
|
|
|
|
|
2011-05-03 18:39:24 +03:00
|
|
|
|
var ls = ls || {};
|
2011-04-01 10:49:36 +03:00
|
|
|
|
|
2011-05-03 18:39:24 +03:00
|
|
|
|
ls.poll = (function ($) {
|
|
|
|
|
/**
|
2013-07-08 07:42:49 +03:00
|
|
|
|
* Дефолтные опции
|
|
|
|
|
*/
|
|
|
|
|
var defaults = {
|
2014-02-10 13:09:17 +02:00
|
|
|
|
// Роутеры
|
|
|
|
|
routers: {
|
|
|
|
|
vote: aRouter['ajax'] + 'poll/vote/',
|
|
|
|
|
add: aRouter['ajax'] + 'poll/create/',
|
|
|
|
|
update: aRouter['ajax'] + 'poll/update/',
|
2014-02-10 13:27:47 +02:00
|
|
|
|
remove: aRouter['ajax'] + 'poll/remove/'
|
2014-02-10 13:09:17 +02:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Селекторы
|
|
|
|
|
selectors: {
|
|
|
|
|
modal: '#modal-poll-create',
|
|
|
|
|
|
|
|
|
|
poll: {
|
|
|
|
|
poll: '.js-poll',
|
|
|
|
|
vote_form: '.js-poll-vote-form',
|
|
|
|
|
vote: '.js-poll-vote',
|
2014-02-10 13:27:47 +02:00
|
|
|
|
abstain: '.js-poll-abstain'
|
2014-02-10 13:09:17 +02:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
form: {
|
2014-02-11 09:26:49 +02:00
|
|
|
|
form: '#js-poll-form',
|
2014-02-10 13:09:17 +02:00
|
|
|
|
list: '.js-poll-form-list',
|
|
|
|
|
item: '.js-poll-form-list-item',
|
|
|
|
|
item_remove: '.js-poll-form-list-item-remove',
|
|
|
|
|
submit: '.js-poll-form-submit'
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
answer: {
|
|
|
|
|
list: '.js-poll-form-answer-list',
|
|
|
|
|
add: '.js-poll-form-answer-add',
|
|
|
|
|
item: {
|
|
|
|
|
item: '.js-poll-form-answer-item',
|
|
|
|
|
text: '.js-poll-form-answer-item-text',
|
|
|
|
|
id: '.js-poll-form-answer-item-id',
|
2014-02-10 13:27:47 +02:00
|
|
|
|
remove: '.js-poll-form-answer-item-remove'
|
2014-02-10 13:09:17 +02:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Селекторы результата опроса
|
|
|
|
|
result: {
|
|
|
|
|
container: '.js-poll-result',
|
|
|
|
|
item: '.js-poll-result-item',
|
2014-02-10 13:27:47 +02:00
|
|
|
|
sort: '.js-poll-result-sort'
|
2014-02-10 13:09:17 +02:00
|
|
|
|
}
|
|
|
|
|
},
|
2013-07-08 07:42:49 +03:00
|
|
|
|
|
|
|
|
|
// Максимальное кол-во вариантов ответов
|
2014-02-10 13:27:47 +02:00
|
|
|
|
iMaxAnswers: 20
|
2013-07-08 07:42:49 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Инициализация
|
|
|
|
|
*
|
|
|
|
|
* @param {Object} options Опции
|
|
|
|
|
*/
|
|
|
|
|
this.init = function(options) {
|
2014-02-11 09:26:49 +02:00
|
|
|
|
var _this = this,
|
|
|
|
|
oDocument = $(document);
|
2013-07-08 07:42:49 +03:00
|
|
|
|
|
|
|
|
|
this.options = $.extend({}, defaults, options);
|
|
|
|
|
|
2014-02-10 13:09:17 +02:00
|
|
|
|
/**
|
|
|
|
|
* Форма добавления
|
|
|
|
|
*/
|
2014-02-04 15:48:53 +02:00
|
|
|
|
|
2014-02-10 13:09:17 +02:00
|
|
|
|
// Сабмит формы
|
2014-02-11 09:26:49 +02:00
|
|
|
|
oDocument.on('submit', this.options.selectors.form.form, function (e) {
|
2014-02-10 13:09:17 +02:00
|
|
|
|
var oForm = $(this);
|
2014-02-04 15:48:53 +02:00
|
|
|
|
|
2014-02-10 13:09:17 +02:00
|
|
|
|
_this[ oForm.data('action') == 'add' ? 'add' : 'update' ](oForm, $(_this.options.selectors.form.submit));
|
2014-02-04 15:48:53 +02:00
|
|
|
|
|
2014-02-10 13:09:17 +02:00
|
|
|
|
e.preventDefault();
|
2014-02-04 15:48:53 +02:00
|
|
|
|
});
|
|
|
|
|
|
2014-02-10 13:09:17 +02:00
|
|
|
|
// Добавление варианта
|
2014-02-11 09:26:49 +02:00
|
|
|
|
oDocument.on('click', this.options.selectors.answer.add, function () {
|
2014-02-10 13:09:17 +02:00
|
|
|
|
_this.answerAdd($(_this.options.selectors.answer.list));
|
|
|
|
|
});
|
2014-02-04 15:48:53 +02:00
|
|
|
|
|
2014-02-10 13:09:17 +02:00
|
|
|
|
// Добавление варианта по нажатию Ctrl + Enter
|
2014-02-11 09:26:49 +02:00
|
|
|
|
oDocument.on('keyup', this.options.selectors.answer.text, function (e) {
|
2014-02-10 13:09:17 +02:00
|
|
|
|
var key = e.keyCode || e.which;
|
2014-02-04 15:48:53 +02:00
|
|
|
|
|
2014-02-10 13:09:17 +02:00
|
|
|
|
if (e.ctrlKey && key == 13) {
|
|
|
|
|
_this.answerAdd($(_this.options.selectors.answer.list));
|
|
|
|
|
}
|
2014-02-04 15:48:53 +02:00
|
|
|
|
});
|
|
|
|
|
|
2014-02-10 13:09:17 +02:00
|
|
|
|
// Удаление варианта
|
2014-02-11 09:26:49 +02:00
|
|
|
|
oDocument.on('click', this.options.selectors.answer.item.remove, function () {
|
2014-02-10 13:09:17 +02:00
|
|
|
|
_this.answerRemove($(this));
|
|
|
|
|
});
|
2014-02-04 15:48:53 +02:00
|
|
|
|
|
2014-02-10 13:09:17 +02:00
|
|
|
|
// Удаление опроса
|
2014-02-11 09:26:49 +02:00
|
|
|
|
oDocument.on('click', this.options.selectors.form.item_remove, function () {
|
2014-02-10 13:09:17 +02:00
|
|
|
|
var oButton = $(this),
|
|
|
|
|
oItem = oButton.closest(_this.options.selectors.form.item);
|
2013-07-08 07:42:49 +03:00
|
|
|
|
|
2014-02-10 13:09:17 +02:00
|
|
|
|
_this.remove(oItem.data('poll-id'), oItem.data('poll-target-tmp'));
|
|
|
|
|
});
|
2013-07-08 07:42:49 +03:00
|
|
|
|
|
|
|
|
|
|
2014-02-10 13:09:17 +02:00
|
|
|
|
/**
|
|
|
|
|
* Опрос
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
$(this.options.selectors.poll.poll).each(function () {
|
|
|
|
|
var oPoll = $(this),
|
|
|
|
|
iPollId = oPoll.data('poll-id');
|
|
|
|
|
|
|
|
|
|
// Голосование за вариант
|
|
|
|
|
oPoll.find(_this.options.selectors.poll.vote).on('click', function () {
|
|
|
|
|
var form = oPoll.find('form');
|
|
|
|
|
|
|
|
|
|
_this.vote(form, $(this));
|
2013-07-08 07:42:49 +03:00
|
|
|
|
});
|
|
|
|
|
|
2014-02-10 13:09:17 +02:00
|
|
|
|
// Воздержаться
|
|
|
|
|
oPoll.find(_this.options.selectors.poll.abstain).on('click', function () {
|
|
|
|
|
var form = oPoll.find('form');
|
|
|
|
|
|
|
|
|
|
_this.vote(form, $(this), true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Сортировка
|
|
|
|
|
oPoll.on('click', _this.options.selectors.result.sort, function () {
|
|
|
|
|
_this.toggleSort(oPoll);
|
2013-07-08 07:42:49 +03:00
|
|
|
|
});
|
|
|
|
|
});
|
2014-02-04 15:48:53 +02:00
|
|
|
|
};
|
2013-07-08 07:42:49 +03:00
|
|
|
|
|
2014-02-10 13:09:17 +02:00
|
|
|
|
/**
|
|
|
|
|
* Добавление опроса
|
2014-02-11 09:26:49 +02:00
|
|
|
|
*
|
|
|
|
|
* @param {Object} oForm Форма добавления опроса
|
|
|
|
|
* @param {Object} oButton Кнопка добавления
|
2014-02-10 13:09:17 +02:00
|
|
|
|
*/
|
|
|
|
|
this.add = function(oForm, oButton) {
|
2014-02-11 09:26:49 +02:00
|
|
|
|
this.answerIndex(oForm);
|
|
|
|
|
|
|
|
|
|
ls.ajax.submit(this.options.routers.add, oForm, function(oResponse) {
|
|
|
|
|
$(this.options.selectors.form.list).append(oResponse.sPollItem);
|
2014-02-10 13:09:17 +02:00
|
|
|
|
$(this.options.selectors.modal).modal('hide');
|
|
|
|
|
}.bind(this), { submitButton: oButton });
|
2014-02-04 15:48:53 +02:00
|
|
|
|
};
|
2013-07-08 07:42:49 +03:00
|
|
|
|
|
2014-02-10 13:09:17 +02:00
|
|
|
|
/**
|
|
|
|
|
* Обновление опроса
|
2014-02-11 09:26:49 +02:00
|
|
|
|
*
|
|
|
|
|
* @param {Object} oForm Форма добавления опроса
|
|
|
|
|
* @param {Object} oButton Кнопка сохранения
|
2014-02-10 13:09:17 +02:00
|
|
|
|
*/
|
|
|
|
|
this.update = function(oForm, oButton) {
|
2014-02-11 09:26:49 +02:00
|
|
|
|
this.answerIndex(oForm);
|
|
|
|
|
|
|
|
|
|
ls.ajax.submit(this.options.routers.update, oForm, function(oResponse) {
|
|
|
|
|
$(this.options.selectors.form.item + '[data-poll-id=' + oResponse.iPollId + ']').replaceWith(oResponse.sPollItem);
|
2014-02-10 13:09:17 +02:00
|
|
|
|
$(this.options.selectors.modal).modal('hide');
|
|
|
|
|
}.bind(this), { submitButton: oButton });
|
2014-02-04 15:48:53 +02:00
|
|
|
|
};
|
2013-07-08 07:42:49 +03:00
|
|
|
|
|
2014-02-10 13:09:17 +02:00
|
|
|
|
/**
|
|
|
|
|
* Удаление опроса
|
2014-02-11 09:26:49 +02:00
|
|
|
|
*
|
|
|
|
|
* @param {Number} iId ID ответа
|
|
|
|
|
* @param {String} sTempHash Хэш объекта
|
2014-02-10 13:09:17 +02:00
|
|
|
|
*/
|
2014-02-11 09:26:49 +02:00
|
|
|
|
this.remove = function(iId, sTempHash) {
|
|
|
|
|
ls.ajax.load(this.options.routers.remove, { id: iId, tmp: sTempHash }, function (oResponse) {
|
|
|
|
|
if (oResponse.bStateError) {
|
|
|
|
|
ls.msg.error(null, oResponse.sMsg);
|
2014-02-04 15:48:53 +02:00
|
|
|
|
} else {
|
2014-02-10 13:09:17 +02:00
|
|
|
|
$(this.options.selectors.form.item + '[data-poll-id=' + iId + ']').fadeOut('slow', function() {
|
2014-02-04 15:48:53 +02:00
|
|
|
|
$(this).remove();
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-02-10 13:09:17 +02:00
|
|
|
|
}.bind(this));
|
2012-04-03 19:11:21 +03:00
|
|
|
|
};
|
2011-04-01 10:49:36 +03:00
|
|
|
|
|
2011-05-03 18:39:24 +03:00
|
|
|
|
/**
|
2013-07-08 07:42:49 +03:00
|
|
|
|
* Добавляет вариант ответа
|
|
|
|
|
*
|
2014-02-10 13:09:17 +02:00
|
|
|
|
* @param {Object} oAnswerList Блок с ответами
|
2013-07-08 07:42:49 +03:00
|
|
|
|
*/
|
2014-02-10 13:09:17 +02:00
|
|
|
|
this.answerAdd = function(oAnswerList) {
|
2014-02-11 09:26:49 +02:00
|
|
|
|
// Ограничиваем кол-во добавляемых ответов
|
|
|
|
|
if (oAnswerList.find(this.options.selectors.answer.item.item).length == this.options.iMaxAnswers) {
|
2014-02-10 13:09:17 +02:00
|
|
|
|
ls.msg.error(null, ls.lang.get('poll.notices.error_answers_max'));
|
2011-05-03 18:39:24 +03:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-07-08 07:42:49 +03:00
|
|
|
|
|
2014-02-11 09:26:49 +02:00
|
|
|
|
var oAnswerItem = $(this.options.selectors.answer.item.item + '[data-is-template=true]').clone().removeAttr('data-is-template').show();
|
2014-02-04 15:48:53 +02:00
|
|
|
|
|
2014-02-10 13:09:17 +02:00
|
|
|
|
oAnswerList.append(oAnswerItem);
|
2014-02-11 09:26:49 +02:00
|
|
|
|
oAnswerItem.find(this.options.selectors.answer.item.text).focus();
|
2014-02-04 15:48:53 +02:00
|
|
|
|
};
|
|
|
|
|
|
2013-07-08 07:42:49 +03:00
|
|
|
|
/**
|
|
|
|
|
* Удаляет вариант ответа
|
|
|
|
|
*
|
2014-02-11 09:26:49 +02:00
|
|
|
|
* @param {Object} oRemoveButton Кнопка удаления
|
2013-07-08 07:42:49 +03:00
|
|
|
|
*/
|
2014-02-10 13:09:17 +02:00
|
|
|
|
this.answerRemove = function(oRemoveButton) {
|
|
|
|
|
oRemoveButton.closest(this.options.selectors.answer.item.item).fadeOut(200, function () {
|
|
|
|
|
$(this).remove();
|
|
|
|
|
});
|
2012-04-03 19:11:21 +03:00
|
|
|
|
};
|
2014-02-10 13:09:17 +02:00
|
|
|
|
|
2014-02-11 09:26:49 +02:00
|
|
|
|
/**
|
|
|
|
|
* Проставляет индексы инпутам ответа
|
|
|
|
|
*
|
|
|
|
|
* @param {Object} oForm Форма добавления опроса
|
|
|
|
|
*/
|
|
|
|
|
this.answerIndex = function(oForm) {
|
|
|
|
|
oForm.find(this.options.selectors.answer.item.item).each(function (iIndex, oElement) {
|
|
|
|
|
var oAnswerItem = $(oElement),
|
|
|
|
|
oAnswerItemId = oAnswerItem.find(this.options.selectors.answer.item.id),
|
|
|
|
|
oAnswerItemText = oAnswerItem.find(this.options.selectors.answer.item.text);
|
|
|
|
|
|
|
|
|
|
oAnswerItemId.attr('name', 'answers[' + iIndex + '][id]');
|
|
|
|
|
oAnswerItemText.attr('name', 'answers[' + iIndex + '][title]');
|
|
|
|
|
}.bind(this));
|
|
|
|
|
};
|
|
|
|
|
|
2011-05-03 18:39:24 +03:00
|
|
|
|
/**
|
2013-07-08 07:42:49 +03:00
|
|
|
|
* Голосование в опросе
|
|
|
|
|
*
|
2014-02-10 13:09:17 +02:00
|
|
|
|
* @param {Object} oForm Форма с данными опроса
|
|
|
|
|
* @param {Object} oButton Копка для анимации загрузки
|
|
|
|
|
* @param {Boolean} bAbstain Воздержаться при голосовании
|
2013-07-08 07:42:49 +03:00
|
|
|
|
*/
|
2014-02-10 13:09:17 +02:00
|
|
|
|
this.vote = function(oForm, oButton, bAbstain) {
|
|
|
|
|
var oFormData = oForm.serializeJSON();
|
2013-07-08 07:42:49 +03:00
|
|
|
|
|
|
|
|
|
ls.hook.marker('voteBefore');
|
|
|
|
|
|
2014-02-10 13:09:17 +02:00
|
|
|
|
ls.ajax.submit(this.options.routers.vote, oForm, function(result) {
|
|
|
|
|
var oPoll = $(this.options.selectors.poll.poll + '[data-poll-id=' + oFormData.id + ']').find(this.options.selectors.poll.vote_form);
|
|
|
|
|
|
2014-02-05 15:06:04 +02:00
|
|
|
|
oPoll.html(result.sText);
|
2013-07-08 07:42:49 +03:00
|
|
|
|
|
2014-02-10 13:09:17 +02:00
|
|
|
|
ls.hook.run('ls_pool_vote_after', [oForm, result], oPoll);
|
|
|
|
|
}.bind(this), { submitButton: oButton, params: { abstain: bAbstain ? 1 : 0 } });
|
2012-04-03 19:11:21 +03:00
|
|
|
|
};
|
|
|
|
|
|
2013-07-02 05:58:58 +03:00
|
|
|
|
/**
|
|
|
|
|
* Сортировка результатов
|
|
|
|
|
*
|
2013-07-08 07:42:49 +03:00
|
|
|
|
* @param {Object} oPoll Блок опроса
|
2013-07-02 05:58:58 +03:00
|
|
|
|
*/
|
2013-07-08 07:42:49 +03:00
|
|
|
|
this.toggleSort = function(oPoll) {
|
2014-02-10 13:09:17 +02:00
|
|
|
|
var oButton = oPoll.find(this.options.selectors.result.sort),
|
|
|
|
|
oPollResult = oPoll.find(this.options.selectors.result.container),
|
|
|
|
|
aItems = oPollResult.find(this.options.selectors.result.item),
|
|
|
|
|
sSortType = oButton.hasClass(ls.options.classes.states.active) ? 'poll-item-pos' : 'poll-item-count';
|
2013-07-02 05:58:58 +03:00
|
|
|
|
|
|
|
|
|
aItems.sort(function (a, b) {
|
|
|
|
|
a = $(a).data(sSortType);
|
|
|
|
|
b = $(b).data(sSortType);
|
|
|
|
|
|
|
|
|
|
if (a > b) {
|
|
|
|
|
return -1;
|
|
|
|
|
} else if (a < b) {
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2014-02-10 13:09:17 +02:00
|
|
|
|
oButton.toggleClass(ls.options.classes.states.active);
|
2013-07-08 07:42:49 +03:00
|
|
|
|
oPollResult.empty().append(aItems);
|
2012-04-03 19:11:21 +03:00
|
|
|
|
};
|
2011-05-03 18:39:24 +03:00
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}).call(ls.poll || {},jQuery);
|