1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-07-08 17:34:26 +03:00
ifhub.club/application/frontend/common/js/user.js

390 lines
11 KiB
JavaScript
Raw Normal View History

2011-05-03 11:22:13 +03:00
/**
2012-06-16 02:43:47 +03:00
* Управление пользователями
*
* @module ls/user
*
* @license GNU General Public License, version 2
* @copyright 2013 OOO "ЛС-СОФТ" {@link http://livestreetcms.com}
* @author Denis Shakhov <denis.shakhov@gmail.com>
2012-06-16 02:43:47 +03:00
*/
var ls = ls || {};
2011-05-03 11:22:13 +03:00
ls.user = (function ($) {
this.jcropImage = null;
/**
* Инициализация
*/
this.init = function() {
var self = this;
/* Авторизация */
ls.ajaxForm(aRouter.login + 'ajax-login', '.js-form-login', function (result, status, xhr, form) {
result.sUrlRedirect && (window.location = result.sUrlRedirect);
ls.hook.run('ls_user_login_after', [form, result]);
});
/* Регистрация */
ls.ajaxForm(aRouter.registration + 'ajax-registration', '.js-form-signup', function (result, status, xhr, form) {
result.sUrlRedirect && (window.location = result.sUrlRedirect);
ls.hook.run('ls_user_registration_after', [form, result]);
});
/* Восстановление пароля */
ls.ajaxForm(aRouter.login + 'ajax-reminder', '.js-form-recovery', function (result, status, xhr, form) {
result.sUrlRedirect && (window.location = result.sUrlRedirect);
ls.hook.run('ls_user_recovery_after', [form, result]);
});
/* Повторный запрос на ссылку активации */
ls.ajaxForm(aRouter.login + 'ajax-reactivation', '.js-form-reactivation', function (result, status, xhr, form) {
form.find('input').val('');
ls.hook.run('ls_user_reactivation_after', [form, result]);
});
/* Аякс загрузка изображений */
this.ajaxUploadImageInit({
selectors: {
element: '.js-ajax-avatar-upload'
},
cropOptions: {
aspectRatio: 1
},
urls: {
upload: aRouter['settings'] + 'profile/upload-avatar/',
remove: aRouter['settings'] + 'profile/remove-avatar/',
cancel: aRouter['settings'] + 'profile/cancel-avatar/',
crop: aRouter['settings'] + 'profile/resize-avatar/'
}
});
this.ajaxUploadImageInit({
selectors: {
element: '.js-ajax-photo-upload'
},
urls: {
upload: aRouter['settings'] + 'profile/upload-foto/',
remove: aRouter['settings'] + 'profile/remove-foto/',
cancel: aRouter['settings'] + 'profile/cancel-foto/',
crop: aRouter['settings'] + 'profile/resize-foto/'
}
});
$('.js-ajax-image-upload-crop-cancel').on('click', function (e) {
self.ajaxUploadImageCropCancel();
});
$('.js-ajax-image-upload-crop-submit').on('click', function (e) {
self.ajaxUploadImageCropSubmit();
});
$('.js-modal-toggle-registration').on('click', function (e) {
$('[data-option-target=tab-pane-registration]').tab('activate');
ls.captcha.update();
});
$('.js-modal-toggle-login').on('click', function (e) {
$('[data-option-target=tab-pane-login]').tab('activate');
});
};
2011-05-03 11:22:13 +03:00
/**
2012-06-16 02:43:47 +03:00
* Добавление в друзья
*/
2011-05-03 11:22:13 +03:00
this.addFriend = function(obj, idUser, sAction){
if(sAction != 'link' && sAction != 'accept') {
2012-01-28 17:52:44 +02:00
var sText = $('#add_friend_text').val();
2011-05-03 11:22:13 +03:00
$('#add_friend_form').children().each(function(i, item){$(item).attr('disabled','disabled')});
} else {
2012-01-28 17:52:44 +02:00
var sText='';
2011-04-01 10:49:36 +03:00
}
2011-05-03 11:22:13 +03:00
if(sAction == 'accept') {
2012-01-28 17:52:44 +02:00
var url = aRouter.profile+'ajaxfriendaccept/';
2011-05-03 11:22:13 +03:00
} else {
2012-01-28 17:52:44 +02:00
var url = aRouter.profile+'ajaxfriendadd/';
2011-04-01 10:49:36 +03:00
}
2012-06-13 11:46:40 +03:00
2012-01-28 17:52:44 +02:00
var params = {idUser: idUser, userText: sText};
2012-06-13 11:46:40 +03:00
2012-04-28 18:40:06 +03:00
ls.hook.marker('addFriendBefore');
2012-01-28 17:52:44 +02:00
ls.ajax(url, params, function(result){
2011-05-03 11:22:13 +03:00
$('#add_friend_form').children().each(function(i, item){$(item).removeAttr('disabled')});
if (!result) {
ls.msg.error('Error','Please try again later');
}
if (result.bStateError) {
ls.msg.error(null,result.sMsg);
} else {
ls.msg.notice(null,result.sMsg);
$('#add_friend_form').jqmHide();
$('#add_friend_item').remove();
2013-04-12 06:55:43 +03:00
$('#profile_actions').prepend($($.trim(result.sToggleText)));
ls.hook.run('ls_user_add_friend_after', [idUser,sAction,result], obj);
2011-05-03 11:22:13 +03:00
}
});
return false;
};
/**
2012-06-16 02:43:47 +03:00
* Удаление из друзей
*/
2011-05-03 11:22:13 +03:00
this.removeFriend = function(obj,idUser,sAction) {
2012-01-28 17:52:44 +02:00
var url = aRouter.profile+'ajaxfrienddelete/';
var params = {idUser: idUser,sAction: sAction};
2012-06-13 11:46:40 +03:00
2012-04-28 18:40:06 +03:00
ls.hook.marker('removeFriendBefore');
2012-01-28 17:52:44 +02:00
ls.ajax(url, params, function(result) {
2011-05-03 11:22:13 +03:00
if (result.bStateError) {
ls.msg.error(null,result.sMsg);
} else {
ls.msg.notice(null,result.sMsg);
$('#delete_friend_item').remove();
$('#profile_actions').prepend($($.trim(result.sToggleText)));
ls.hook.run('ls_user_remove_friend_after', [idUser,sAction,result], obj);
}
});
return false;
};
/**
* Поиск пользователей по началу логина
*/
this.searchUsersByPrefix = function(sPrefix,obj) {
obj=$(obj);
var url = aRouter['people']+'ajax-search/';
var params = {user_login: sPrefix, isPrefix: 1};
$('#search-user-login').addClass('loader');
ls.hook.marker('searchUsersByPrefixBefore');
ls.ajax(url, params, function(result){
$('#search-user-login').removeClass('loader');
$('#user-prefix-filter').find('.active').removeClass('active');
obj.parent().addClass('active');
if (result.bStateError) {
$('#users-list-search').hide();
$('#users-list-original').show();
} else {
$('#users-list-original').hide();
$('#users-list-search').html(result.sText).show();
ls.hook.run('ls_user_search_users_by_prefix_after',[sPrefix, obj, result]);
}
});
return false;
};
/**
* Подписка
*/
this.followToggle = function(obj, iUserId) {
if ($(obj).hasClass('followed')) {
ls.stream.unsubscribe(iUserId);
$(obj).toggleClass('followed').text(ls.lang.get('profile_user_follow'));
} else {
ls.stream.subscribe(iUserId);
$(obj).toggleClass('followed').text(ls.lang.get('profile_user_unfollow'));
}
2011-05-03 11:22:13 +03:00
return false;
};
/**
* Поиск пользователей
*/
this.searchUsers = function(form) {
var url = aRouter['people']+'ajax-search/';
var inputSearch=$('#'+form).find('input');
inputSearch.addClass('loader');
ls.hook.marker('searchUsersBefore');
ls.ajaxSubmit(url, form, function(result){
inputSearch.removeClass('loader');
if (result.bStateError) {
$('#users-list-search').hide();
$('#users-list-original').show();
} else {
$('#users-list-original').hide();
$('#users-list-search').html(result.sText).show();
ls.hook.run('ls_user_search_users_after',[form, result]);
}
});
};
/**
* Загрузка временной аватарки
* @param form
* @param input
*/
this.ajaxUploadImageInit = function(options) {
var self = this;
var defaults = {
cropOptions: {
minSize: [32, 32]
},
selectors: {
element: '.js-ajax-image-upload',
image: '.js-ajax-image-upload-image',
image_crop: '.js-image-crop',
remove_button: '.js-ajax-image-upload-remove',
choose_button: '.js-ajax-image-upload-choose',
input_file: '.js-ajax-image-upload-file',
crop_cancel_button: '.js-ajax-image-upload-crop-cancel',
2013-07-29 08:11:36 +03:00
crop_submit_button: '.js-ajax-image-upload-crop-submit'
},
urls: {
upload: aRouter['settings'] + 'profile/upload-avatar/',
remove: aRouter['settings'] + 'profile/remove-avatar/',
cancel: aRouter['settings'] + 'profile/cancel-avatar/',
2013-07-29 08:11:36 +03:00
crop: aRouter['settings'] + 'profile/resize-avatar/'
}
};
var options = $.extend(true, {}, defaults, options);
$(options.selectors.element).each(function () {
var $element = $(this);
var elements = {
element: $element,
remove_button: $element.find(options.selectors.remove_button),
choose_button: $element.find(options.selectors.choose_button),
image: $element.find(options.selectors.image),
2013-07-29 08:11:36 +03:00
image_crop: $element.find(options.selectors.image_crop)
};
$element.find(options.selectors.input_file).on('change', function () {
self.currentElements = elements;
self.currentOptions = options;
self.ajaxUploadImage(null, $(this), options);
});
elements.remove_button.on('click', function (e) {
self.ajaxUploadImageRemove(options, elements);
e.preventDefault();
});
});
};
/**
* Загрузка временной аватарки
* @param form
* @param input
*/
this.ajaxUploadImage = function(form, input, options) {
if ( ! form && input ) {
var form = $('<form method="post" enctype="multipart/form-data"></form>').hide().appendTo('body');
input.clone(true).insertAfter(input);
2012-04-08 01:21:23 +03:00
input.appendTo(form);
}
ls.ajaxSubmit(options.urls.upload, form, function (data) {
if (data.bStateError) {
ls.msg.error(data.sMsgTitle,data.sMsg);
} else {
this.ajaxUploadImageModalCrop(data.sTmpFile, options);
}
form.remove();
}.bind(this));
};
/**
* Показывает форму для ресайза аватарки
* @param sImgFile
*/
this.ajaxUploadImageModalCrop = function(sImgFile, options) {
var self = this;
this.jcropImage && this.jcropImage.destroy();
$('.js-image-crop').attr('src', sImgFile + '?' + Math.random()).css({
'width': 'auto',
'height': 'auto'
});
if ($('#modal-image-crop').length)
$('#modal-image-crop').modal('show');
else {
ls.debug('Error [Ajax Image Upload]:\nМодальное окно ресайза изображения не найдено');
}
$('.js-image-crop').Jcrop(options.cropOptions, function () {
self.jcropImage = this;
this.setSelect([0, 0, 500, 500]);
});
};
/**
* Удаление аватарки
*/
this.ajaxUploadImageRemove = function(options, elements) {
ls.hook.marker('removeAvatarBefore');
ls.ajax(options.urls.remove, {}, function(result) {
if (result.bStateError) {
ls.msg.error(null,result.sMsg);
} else {
elements.image.attr('src', result.sFile + '?' + Math.random());
elements.remove_button.hide();
elements.choose_button.text(result.sTitleUpload);
ls.hook.run('ls_user_remove_avatar_after', [result]);
2012-04-05 11:52:13 +03:00
}
});
};
/**
* Отмена ресайза аватарки, подчищаем временный данные
*/
this.ajaxUploadImageCropCancel = function() {
ls.hook.marker('cancelAvatarBefore');
ls.ajax(this.currentOptions.urls.cancel, {}, function(result) {
if (result.bStateError) {
ls.msg.error(null,result.sMsg);
} else {
$('#modal-image-crop').modal('hide');
ls.hook.run('ls_user_cancel_avatar_after', [result]);
}
2012-04-05 13:15:33 +03:00
});
};
/**
* Выполняет ресайз аватарки
*/
this.ajaxUploadImageCropSubmit = function() {
var self = this;
if ( ! this.jcropImage ) {
return false;
}
2012-06-13 11:46:40 +03:00
var params = {
size: this.jcropImage.tellSelect()
};
2012-06-13 11:46:40 +03:00
ls.hook.marker('resizeAvatarBefore');
ls.ajax(self.currentOptions.urls.crop, params, function(result) {
if (result.bStateError) {
ls.msg.error(null,result.sMsg);
} else {
self.currentElements.image.attr('src',result.sFile+'?'+Math.random());
$('#modal-image-crop').modal('hide');
self.currentElements.remove_button.show();
self.currentElements.choose_button.text(result.sTitleUpload);
ls.hook.run('ls_user_resize_avatar_after', [params, result]);
}
});
return false;
};
2011-05-03 11:22:13 +03:00
return this;
}).call(ls.user || {},jQuery);