2014-08-14 19:53:04 +03:00
|
|
|
|
/**
|
|
|
|
|
* Add friend
|
|
|
|
|
*
|
|
|
|
|
* @module ls/user/friend
|
|
|
|
|
*
|
|
|
|
|
* @license GNU General Public License, version 2
|
|
|
|
|
* @copyright 2013 OOO "ЛС-СОФТ" {@link http://livestreetcms.com}
|
|
|
|
|
* @author Denis Shakhov <denis.shakhov@gmail.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
(function($) {
|
|
|
|
|
"use strict";
|
|
|
|
|
|
2015-02-17 16:40:18 +02:00
|
|
|
|
$.widget( "livestreet.lsUserFriend", $.livestreet.lsComponent, {
|
2014-08-14 19:53:04 +03:00
|
|
|
|
/**
|
|
|
|
|
* Дефолтные опции
|
|
|
|
|
*/
|
|
|
|
|
options: {
|
|
|
|
|
// Ссылки
|
|
|
|
|
urls: {
|
|
|
|
|
// Добавить в друзья
|
|
|
|
|
add: null,
|
|
|
|
|
|
|
|
|
|
// Удалить из друзей
|
|
|
|
|
remove: null,
|
|
|
|
|
|
|
|
|
|
// Подтвердить
|
|
|
|
|
accept: null,
|
|
|
|
|
|
|
|
|
|
// Модальное окно с формой добавления
|
|
|
|
|
modal: null
|
|
|
|
|
},
|
|
|
|
|
selectors: {
|
|
|
|
|
form: '.js-user-friend-form',
|
|
|
|
|
text: '.js-user-friend-text'
|
2015-02-18 17:35:12 +02:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
params: {}
|
2014-08-14 19:53:04 +03:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Конструктор
|
|
|
|
|
*
|
|
|
|
|
* @constructor
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
_create: function () {
|
2015-02-17 16:40:18 +02:00
|
|
|
|
this._super();
|
|
|
|
|
|
2014-08-14 19:53:04 +03:00
|
|
|
|
this.target = this.element.data( 'target' );
|
|
|
|
|
|
2015-02-17 16:40:18 +02:00
|
|
|
|
this._on({ click: 'onClick' });
|
2014-08-14 19:53:04 +03:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Коллбэк вызываемый при клике на кнопку добавления в друзья
|
|
|
|
|
*/
|
|
|
|
|
onClick: function( event ) {
|
|
|
|
|
var status = this.getStatus();
|
|
|
|
|
|
|
|
|
|
if ( status == 'notfriends' ) {
|
|
|
|
|
this.showForm();
|
|
|
|
|
} else if ( status == 'pending' ) {
|
|
|
|
|
this.accept();
|
|
|
|
|
} else if ( status == 'added' ) {
|
|
|
|
|
this.remove();
|
|
|
|
|
} else if ( status == 'linked' ) {
|
|
|
|
|
this.addLinked();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Получение статуса
|
|
|
|
|
*/
|
|
|
|
|
getStatus: function() {
|
|
|
|
|
return this.element.attr( 'data-status' );
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Установка статуса
|
|
|
|
|
*/
|
|
|
|
|
setStatus: function( status ) {
|
|
|
|
|
var textElement = this.element.find( this.option( 'selectors.text' ) ),
|
|
|
|
|
textClass = this.option( 'selectors.text' ).slice( 1 );
|
|
|
|
|
|
|
|
|
|
if ( ~ [ 'sent', 'rejected' ].indexOf( status ) ) {
|
|
|
|
|
textElement.replaceWith( '<span class="' + textClass + '">' + ls.lang.get( 'user.friends.status.' + status ) + '</span>' );
|
|
|
|
|
} else {
|
|
|
|
|
textElement.replaceWith( '<a href="#" class="' + textClass + '">' + ls.lang.get( 'user.friends.status.' + status ) + '</a>' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.element.attr( 'data-status', status );
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Показывает форму
|
|
|
|
|
*/
|
|
|
|
|
showForm: function() {
|
|
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
|
|
ls.modal.load( this.option( 'urls.modal' ), { target: this.target }, {
|
|
|
|
|
aftershow: function( e, modal ) {
|
|
|
|
|
var form = modal.element.find( _this.option( 'selectors.form' ) ),
|
|
|
|
|
textarea = form.find( _this.option( 'selectors.text' ) );
|
|
|
|
|
|
|
|
|
|
textarea.focus();
|
|
|
|
|
|
|
|
|
|
form.on( 'submit', function ( event ) {
|
|
|
|
|
var text = textarea.val();
|
|
|
|
|
|
|
|
|
|
ls.utils.formLock( form );
|
|
|
|
|
|
2015-02-18 17:35:12 +02:00
|
|
|
|
_this._load( 'add', { idUser: _this.target, userText: text }, function( response ) {
|
|
|
|
|
modal.hide();
|
|
|
|
|
_this.setStatus( 'sent' );
|
|
|
|
|
}, {
|
|
|
|
|
onResponse: function () {
|
|
|
|
|
ls.utils.formUnlock( form );
|
2014-08-14 19:53:04 +03:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
}.bind(this))
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Повторное подтверждение
|
|
|
|
|
*/
|
|
|
|
|
addLinked: function() {
|
2015-02-17 16:40:18 +02:00
|
|
|
|
this.accept( 'add' );
|
2014-08-14 19:53:04 +03:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Подтверждение
|
|
|
|
|
*/
|
|
|
|
|
accept: function( url ) {
|
2015-02-17 16:40:18 +02:00
|
|
|
|
this._load( url || 'accept', { idUser: this.target }, function( response ) {
|
|
|
|
|
this.setStatus( 'added' );
|
|
|
|
|
});
|
2014-08-14 19:53:04 +03:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Удаление из друзей
|
|
|
|
|
*/
|
|
|
|
|
remove: function() {
|
2015-02-17 16:40:18 +02:00
|
|
|
|
this._load( 'remove', { idUser: this.target }, function( response ) {
|
|
|
|
|
this.setStatus( 'linked' );
|
|
|
|
|
});
|
2014-08-14 19:53:04 +03:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
})(jQuery);
|