2014-03-04 10:32:38 +02:00
|
|
|
|
/**
|
|
|
|
|
* Подгрузка контента
|
|
|
|
|
*
|
|
|
|
|
* @module more
|
|
|
|
|
*
|
|
|
|
|
* @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.more", {
|
|
|
|
|
/**
|
|
|
|
|
* Дефолтные опции
|
|
|
|
|
*/
|
|
|
|
|
options: {
|
|
|
|
|
// Селектор блока с содержимым
|
|
|
|
|
target: null,
|
|
|
|
|
// Добавление контента в конец/начало контейнера
|
|
|
|
|
// true - в конец
|
|
|
|
|
// false - в начало
|
|
|
|
|
append: true,
|
|
|
|
|
// Ссылка
|
|
|
|
|
url: null,
|
|
|
|
|
// Название переменной с результатом
|
2014-07-07 14:10:13 +03:00
|
|
|
|
result: 'html',
|
2014-03-04 10:32:38 +02:00
|
|
|
|
// Параметры запроса
|
2014-03-06 14:34:13 +02:00
|
|
|
|
params: {},
|
|
|
|
|
// Проксирующие параметры
|
|
|
|
|
proxy: {}
|
2014-03-04 10:32:38 +02:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Конструктор
|
|
|
|
|
*
|
|
|
|
|
* @constructor
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
_create: function () {
|
|
|
|
|
this.options = $.extend({}, this.options, ls.utils.getDataOptions(this.element, this.widgetName));
|
2014-03-06 14:34:13 +02:00
|
|
|
|
this.options.proxy = $.extend({}, this.options.proxy, ls.utils.getDataOptions(this.element, 'proxy'));
|
2014-03-04 10:32:38 +02:00
|
|
|
|
|
|
|
|
|
this.target = $( this.options.target );
|
|
|
|
|
this.counter = this.element.find('.js-more-count');
|
|
|
|
|
|
|
|
|
|
this._on({
|
|
|
|
|
click: function (e) {
|
|
|
|
|
! this.isLocked && this.load();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Блокирует блок подгрузки
|
|
|
|
|
*/
|
|
|
|
|
lock: function () {
|
|
|
|
|
this.isLocked = true;
|
|
|
|
|
this.element.addClass(ls.options.classes.states.loading);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Разблокировывает блок подгрузки
|
|
|
|
|
*/
|
|
|
|
|
unlock: function () {
|
|
|
|
|
this.isLocked = false;
|
|
|
|
|
this.element.removeClass(ls.options.classes.states.loading);
|
|
|
|
|
},
|
|
|
|
|
|
2014-07-07 14:10:13 +03:00
|
|
|
|
/**
|
|
|
|
|
* Получает значение счетчика
|
|
|
|
|
*/
|
|
|
|
|
getCount: function () {
|
|
|
|
|
return this.counter.length && parseInt( this.counter.text(), 10 );
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Устанавливает значение счетчика
|
|
|
|
|
*/
|
|
|
|
|
setCount: function ( number ) {
|
|
|
|
|
this.counter.length && this.counter.text( number );
|
|
|
|
|
},
|
|
|
|
|
|
2014-03-04 10:32:38 +02:00
|
|
|
|
/**
|
|
|
|
|
* Подгрузка
|
|
|
|
|
*/
|
|
|
|
|
load: function () {
|
|
|
|
|
this._trigger("beforeload", null, this);
|
|
|
|
|
|
2014-03-06 14:34:13 +02:00
|
|
|
|
this.options.params = $.extend({}, this.options.params, ls.utils.getDataOptions(this.element, 'param'));
|
2014-03-04 10:32:38 +02:00
|
|
|
|
this.lock();
|
|
|
|
|
|
2014-03-06 14:34:13 +02:00
|
|
|
|
var params=$.extend({}, this.options.params, this.options.proxy);
|
|
|
|
|
|
|
|
|
|
ls.ajax.load(this.options.url, params, function (oResponse) {
|
2014-07-07 14:10:13 +03:00
|
|
|
|
if (oResponse.count_loaded > 0) {
|
2014-06-24 16:29:04 +03:00
|
|
|
|
var html = $('<div></div>').html( $.trim( oResponse[this.options.result] ) );
|
|
|
|
|
|
|
|
|
|
if ( html.find( this.options.target ).length ) {
|
|
|
|
|
html = html.find( this.options.target ).first();
|
2014-03-06 14:34:13 +02:00
|
|
|
|
}
|
2014-06-24 16:29:04 +03:00
|
|
|
|
|
|
|
|
|
this.target[ this.options.append ? 'append' : 'prepend' ]( html.html() );
|
2014-07-07 14:10:13 +03:00
|
|
|
|
this.element.attr( 'data-param-last_id', oResponse.last_id );
|
2014-03-04 10:32:38 +02:00
|
|
|
|
|
|
|
|
|
// Обновляем счетчик
|
|
|
|
|
if (this.counter.length) {
|
2014-07-07 14:10:13 +03:00
|
|
|
|
var iCountLeft = this.getCount() - oResponse.count_loaded;
|
2014-03-04 10:32:38 +02:00
|
|
|
|
|
|
|
|
|
if (iCountLeft <= 0) {
|
|
|
|
|
this.element.remove();
|
|
|
|
|
} else {
|
2014-07-07 14:10:13 +03:00
|
|
|
|
this.setCount( iCountLeft );
|
2014-03-04 10:32:38 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-24 16:29:04 +03:00
|
|
|
|
|
2014-03-06 14:34:13 +02:00
|
|
|
|
// Обновляем параметры
|
2014-06-24 16:29:04 +03:00
|
|
|
|
$.each(this.options.proxy,function( k, v ) {
|
|
|
|
|
if ( oResponse[k] ) {
|
|
|
|
|
this.options.proxy[k] = oResponse[k];
|
2014-03-06 14:34:13 +02:00
|
|
|
|
}
|
|
|
|
|
}.bind(this));
|
|
|
|
|
|
2014-06-24 16:29:04 +03:00
|
|
|
|
if ( oResponse.bHideMore ) {
|
2014-03-06 14:34:13 +02:00
|
|
|
|
this.element.remove();
|
|
|
|
|
}
|
2014-03-04 10:32:38 +02:00
|
|
|
|
} else {
|
|
|
|
|
// Для блоков без счетчиков
|
2014-06-24 16:29:04 +03:00
|
|
|
|
// TODO: i18n
|
2014-03-04 10:32:38 +02:00
|
|
|
|
ls.msg.notice(null, 'Больше нечего подгружать');
|
|
|
|
|
this.element.remove();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.unlock();
|
|
|
|
|
|
|
|
|
|
this._trigger("afterload", null, { context: this, response: oResponse });
|
|
|
|
|
}.bind(this));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
})(jQuery);
|