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

205 lines
5.5 KiB
JavaScript
Raw Normal View History

/**
* Стена пользователя
*
* @module ls/wall
*
* @license GNU General Public License, version 2
* @copyright 2013 OOO "ЛС-СОФТ" {@link http://livestreetcms.com}
* @author Denis Shakhov <denis.shakhov@gmail.com>
*/
2014-07-07 13:50:14 +03:00
(function($) {
2014-01-13 08:33:20 +02:00
"use strict";
2014-07-07 13:50:14 +03:00
$.widget( "livestreet.lsWall", {
/**
* Дефолтные опции
*/
options: {
// Ссылки
urls: {
add: null,
remove: null,
load: null,
load_comments: null
2014-01-13 08:33:20 +02:00
},
2014-07-07 13:50:14 +03:00
// Селекторы
selectors: {
entry: {
self: '.js-wall-entry',
remove: '.js-comment-remove',
reply: '.js-comment-reply'
},
comment: '.js-wall-comment',
post: '.js-wall-post',
form: '.js-wall-form',
more: '.js-wall-more',
more_comments: '.js-wall-more-comments',
comment_wrapper: '.js-wall-comment-wrapper',
container: '.js-wall-entry-container',
empty: '.js-wall-alert-empty'
}
},
2014-01-13 08:33:20 +02:00
2014-07-07 13:50:14 +03:00
/**
* Конструктор
*
* @constructor
* @private
*/
_create: function () {
var _this = this;
this.elements = {
empty: this.element.find( this.option( 'selectors.empty' ) ),
more: this.element.find( this.option( 'selectors.more' ) ),
more_comments: this.element.find( this.option( 'selectors.more_comments' ) ),
entries: this.element.find( this.option( 'selectors.entry.self' ) ),
forms: this.element.find( this.option( 'selectors.form' ) ),
container: this.element.find( this.option( 'selectors.container' ) )
};
this.userId = this.getUserId();
// Подгрузка новых постов
this.elements.more.more({
url: this.option( 'urls.load' ),
params: {
user_id: this.getUserId()
2014-01-13 08:33:20 +02:00
}
});
2014-07-07 13:50:14 +03:00
// Подгрузка комментариев
this.elements.more_comments.livequery( function () {
$( this ).more({
url: _this.option( 'urls.load_comments' ),
params: {
user_id: _this.getUserId()
2014-01-13 08:33:20 +02:00
}
});
});
2014-01-13 08:33:20 +02:00
2014-07-07 13:50:14 +03:00
// Записи
this.elements.entries.livequery( function () {
$( this ).lsWallEntry({
wall: _this.element,
urls: {
remove: _this.option( 'urls.remove' )
},
selectors: {
wrapper: _this.option( 'selectors.comment_wrapper' ),
remove: _this.option( 'selectors.entry.remove' ),
reply: _this.option( 'selectors.entry.reply' ),
}
})
});
2012-03-12 13:40:26 +02:00
2014-07-07 13:50:14 +03:00
// Формы
this.elements.forms.livequery( function () {
$( this ).lsWallForm({
wall: _this.element
});
});
},
2012-03-12 13:40:26 +02:00
2014-07-07 13:50:14 +03:00
/**
* Добавление
*
* TODO: Оптимизировать
*/
add: function( pid, text ) {
var form = this.getFormById( pid );
2014-07-07 13:50:14 +03:00
ls.ajax.load( this.option( 'urls.add' ), { user_id: this.getUserId(), pid: pid, text: text }, function( response ) {
if ( response.bStateError ) {
ls.msg.error(null, response.sMsg);
} else {
if ( pid === 0 ) this.elements.empty.hide();
2014-01-13 08:33:20 +02:00
2014-07-07 13:50:14 +03:00
this.load( pid );
form.lsWallForm( 'close' );
}
2014-01-13 08:33:20 +02:00
2014-07-07 13:50:14 +03:00
ls.utils.formUnlock( form );
}.bind(this));
},
2014-01-13 08:33:20 +02:00
2014-07-07 13:50:14 +03:00
/**
* Подгружает записи
*
* TODO: Оптимизировать
*/
load: function( pid ) {
var container = this.element.find( this.options.selectors.container + '[data-id=' + pid + ']' ),
firstId = container.find( '>' + this.option( 'selectors.entry.self' ) + ':' + ( pid === 0 ? 'first' : 'last' ) ).data( 'id' ) || -1,
params = { user_id: this.getUserId(), first_id: firstId, target_id: pid };
ls.ajax.load( this.option( pid === 0 ? 'urls.load' : 'urls.load_comments' ), params, function( response ) {
if (response.bStateError) {
ls.msg.error(null, response.sMsg);
} else {
if ( response.count_loaded ) {
container[ pid === 0 ? 'prepend' : 'append' ]( response.html );
}
2012-03-12 13:40:26 +02:00
}
2014-07-07 13:50:14 +03:00
}.bind(this));
},
2014-01-13 08:33:20 +02:00
2014-07-07 13:50:14 +03:00
/**
* Получает посты
*/
getPosts: function() {
return this.element.find( this.option( 'selectors.post' ) );
},
2014-01-13 08:33:20 +02:00
2014-07-07 13:50:14 +03:00
/**
* Получает комментарии по ID поста
*/
getCommentsByPostId: function( pid ) {
return this.getCommentWrapperById( pid ).find( this.option( 'selectors.comment' ) );
},
2012-03-12 13:40:26 +02:00
2014-01-13 08:33:20 +02:00
/**
2014-07-07 13:50:14 +03:00
* Получает запись по ID
2014-01-13 08:33:20 +02:00
*/
2014-07-07 13:50:14 +03:00
getEntryById: function( id ) {
return this.element.find( this.option( 'selectors.entry.self' ) + '[data-id=' + id + ']' ).eq( 0 );
},
2014-01-13 08:33:20 +02:00
/**
2014-07-07 13:50:14 +03:00
* Получает враппер комментариев по ID поста
2014-01-13 08:33:20 +02:00
*/
2014-07-07 13:50:14 +03:00
getCommentWrapperById: function( id ) {
return this.element.find( this.option( 'selectors.comment_wrapper' ) + '[data-id=' + id + ']' ).eq( 0 );
},
2012-03-12 13:40:26 +02:00
2014-01-13 08:33:20 +02:00
/**
2014-07-07 13:50:14 +03:00
* Получает форму по ID поста
2014-01-13 08:33:20 +02:00
*/
2014-07-07 13:50:14 +03:00
getFormById: function( id ) {
return this.element.find( this.option( 'selectors.form' ) + '[data-id=' + id + ']' ).eq( 0 );
},
2014-01-13 08:33:20 +02:00
/**
2014-07-07 13:50:14 +03:00
* Получает ID владельца стены
2014-01-13 08:33:20 +02:00
*/
2014-07-07 13:50:14 +03:00
getUserId: function() {
return this.userId ? this.userId : this.userId = this.element.data( 'user-id' );
},
2014-07-07 13:50:14 +03:00
/**
* Получает развернутые формы
*/
getOpenedForms: function() {
return this.element.find( this.option( 'selectors.form' ) + '.' + ls.options.classes.states.open );
},
2014-07-07 13:50:14 +03:00
/**
* Проверяет и если нужно показывает/скрывает сообщение о пустом списке
*/
checkEmpty: function() {
this.elements.empty[ this.getPosts().length ? 'hide' : 'show' ]();
}
});
})(jQuery);