You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
434 lines
12 KiB
JavaScript
434 lines
12 KiB
JavaScript
10 years ago
|
"use strict";
|
||
|
|
||
7 years ago
|
/*globals ajaxify, config, utils, app, socket, NProgress*/
|
||
10 years ago
|
|
||
10 years ago
|
$(document).ready(function() {
|
||
10 years ago
|
setupNProgress();
|
||
|
setupTaskbar();
|
||
10 years ago
|
setupEditedByIcon();
|
||
10 years ago
|
setupMobileMenu();
|
||
8 years ago
|
setupQuickReply();
|
||
7 years ago
|
configureNavbarHiding();
|
||
6 years ago
|
fixHeaderPadding();
|
||
10 years ago
|
|
||
7 years ago
|
$(window).on('resize', utils.debounce(configureNavbarHiding, 200));
|
||
6 years ago
|
$(window).on('resize', fixHeaderPadding);
|
||
|
|
||
6 years ago
|
$(window).on('action:app.loggedIn', function () {
|
||
|
setupMobileMenu();
|
||
|
});
|
||
|
|
||
6 years ago
|
function fixHeaderPadding() {
|
||
|
var env = utils.findBootstrapEnvironment();
|
||
|
if (env === 'sm' || env === 'xs' || env === 'md') {
|
||
|
$('#panel').css('padding-top', $('#header-menu').outerHeight(true));
|
||
|
} else {
|
||
6 years ago
|
$('#panel').css('padding-top', $('#header-menu').outerHeight(true) - 70);
|
||
6 years ago
|
}
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
function configureNavbarHiding () {
|
||
|
var navbarEl = $(".navbar-fixed-top");
|
||
|
navbarEl.autoHidingNavbar('destroy');
|
||
|
navbarEl.css('top', '');
|
||
|
|
||
|
var env = utils.findBootstrapEnvironment();
|
||
|
if (env === 'xs' || env ==='sm') {
|
||
|
navbarEl.autoHidingNavbar({
|
||
|
showOnBottom: false,
|
||
|
});
|
||
|
}
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
10 years ago
|
function setupNProgress() {
|
||
10 years ago
|
$(window).on('action:ajaxify.start', function() {
|
||
10 years ago
|
NProgress.set(0.7);
|
||
10 years ago
|
});
|
||
10 years ago
|
|
||
9 years ago
|
$(window).on('action:ajaxify.end', function(ev, data) {
|
||
10 years ago
|
NProgress.done();
|
||
9 years ago
|
setupHoverCards();
|
||
9 years ago
|
|
||
8 years ago
|
if (data.url && data.url.match('user/')) {
|
||
8 years ago
|
setupFavouriteButtonOnProfile();
|
||
9 years ago
|
}
|
||
10 years ago
|
});
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
function setupTaskbar() {
|
||
10 years ago
|
$(window).on('filter:taskbar.push', function(ev, data) {
|
||
|
data.options.className = 'taskbar-' + data.module;
|
||
|
if (data.module === 'composer') {
|
||
7 years ago
|
data.options.icon = 'fa-commenting-o';
|
||
10 years ago
|
} else if (data.module === 'chat') {
|
||
6 years ago
|
if (data.element.length && !data.element.hasClass('active')) {
|
||
|
increaseChatCount(data.element);
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
$(window).on('action:taskbar.pushed', function(ev, data) {
|
||
|
if (data.module === 'chat') {
|
||
|
createChatIcon(data);
|
||
|
var elData = data.element.data();
|
||
|
if (elData && elData.options && !elData.options.isSelf) {
|
||
|
increaseChatCount(data.element);
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
});
|
||
10 years ago
|
|
||
9 years ago
|
socket.on('event:chats.markedAsRead', function(data) {
|
||
6 years ago
|
$('#taskbar [data-roomid="' + data.roomId + '"]')
|
||
9 years ago
|
.removeClass('new')
|
||
|
.attr('data-content', 0);
|
||
|
});
|
||
|
|
||
10 years ago
|
function createChatIcon(data) {
|
||
7 years ago
|
$.getJSON(config.relative_path + '/api/user/' + app.user.userslug + '/chats/' + data.options.roomId, function(chatObj) {
|
||
10 years ago
|
var el = $('#taskbar [data-uuid="' + data.uuid + '"] a');
|
||
9 years ago
|
el.parent('[data-uuid]').attr('data-roomId', data.options.roomId);
|
||
|
|
||
7 years ago
|
if (chatObj.users.length === 1) {
|
||
|
var user = chatObj.users[0];
|
||
|
el.find('i').remove();
|
||
|
|
||
|
if (user.picture) {
|
||
|
el.css('background-image', 'url(' + user.picture + ')');
|
||
5 years ago
|
el.css('background-size', 'cover');
|
||
7 years ago
|
} else {
|
||
|
el .css('background-color', user['icon:bgColor'])
|
||
|
.text(user['icon:text'])
|
||
|
.addClass('user-icon');
|
||
|
}
|
||
9 years ago
|
}
|
||
10 years ago
|
});
|
||
|
}
|
||
|
|
||
6 years ago
|
function increaseChatCount(el) {
|
||
10 years ago
|
var count = (parseInt($(el).attr('data-content'), 10) || 0) + 1;
|
||
|
$(el).attr('data-content', count);
|
||
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
function setupEditedByIcon() {
|
||
|
function activateEditedTooltips() {
|
||
|
$('[data-pid] [component="post/editor"]').each(function() {
|
||
10 years ago
|
var el = $(this), icon;
|
||
10 years ago
|
|
||
|
if (!el.attr('data-editor')) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
8 years ago
|
icon = el.closest('[data-pid]').find('.edit-icon').first();
|
||
10 years ago
|
icon.prop('title', el.text()).tooltip('fixTitle').removeClass('hidden');
|
||
|
});
|
||
|
}
|
||
|
|
||
|
$(window).on('action:posts.edited', function(ev, data) {
|
||
8 years ago
|
var parent = $('[data-pid="' + data.post.pid + '"]');
|
||
|
var icon = parent.find('.edit-icon').filter(function (index, el) {
|
||
|
return parseInt($(el).closest('[data-pid]').attr('data-pid'), 10) === parseInt(data.post.pid, 10);
|
||
|
});
|
||
|
var el = parent.find('[component="post/editor"]').first();
|
||
10 years ago
|
icon.prop('title', el.text()).tooltip('fixTitle').removeClass('hidden');
|
||
|
});
|
||
|
|
||
|
$(window).on('action:topic.loaded', activateEditedTooltips);
|
||
|
$(window).on('action:posts.loaded', activateEditedTooltips);
|
||
|
}
|
||
|
|
||
10 years ago
|
function setupMobileMenu() {
|
||
9 years ago
|
if (!window.addEventListener) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
9 years ago
|
|
||
7 years ago
|
require(['pulling', 'storage'], function (Pulling, Storage) {
|
||
5 years ago
|
if (!Pulling) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
7 years ago
|
// initialization
|
||
9 years ago
|
|
||
7 years ago
|
var chatMenuVisible = !config.disableChat && app.user && parseInt(app.user.uid, 10);
|
||
7 years ago
|
var swapped = !!Storage.getItem('persona:menus:legacy-layout');
|
||
7 years ago
|
var margin = window.innerWidth;
|
||
10 years ago
|
|
||
7 years ago
|
if (swapped) {
|
||
7 years ago
|
$('#mobile-menu').removeClass('pull-left');
|
||
|
$('#mobile-chats').addClass('pull-left');
|
||
|
}
|
||
10 years ago
|
|
||
7 years ago
|
if (document.documentElement.getAttribute('data-dir') === 'rtl') {
|
||
|
swapped = !swapped;
|
||
|
}
|
||
|
|
||
7 years ago
|
var navSlideout = Pulling.create({
|
||
|
panel: document.getElementById('panel'),
|
||
|
menu: document.getElementById('menu'),
|
||
|
width: 256,
|
||
|
margin: margin,
|
||
7 years ago
|
side: swapped ? 'right' : 'left',
|
||
7 years ago
|
});
|
||
|
$('#menu').removeClass('hidden');
|
||
|
|
||
|
var chatsSlideout;
|
||
7 years ago
|
if (chatMenuVisible) {
|
||
7 years ago
|
chatsSlideout = Pulling.create({
|
||
|
panel: document.getElementById('panel'),
|
||
|
menu: document.getElementById('chats-menu'),
|
||
|
width: 256,
|
||
|
margin: margin,
|
||
7 years ago
|
side: swapped ? 'left' : 'right',
|
||
7 years ago
|
});
|
||
|
$('#chats-menu').removeClass('hidden');
|
||
|
}
|
||
10 years ago
|
|
||
7 years ago
|
// all menus
|
||
10 years ago
|
|
||
7 years ago
|
function closeOnClick() {
|
||
|
navSlideout.close();
|
||
|
if (chatsSlideout) { chatsSlideout.close(); }
|
||
|
}
|
||
9 years ago
|
|
||
7 years ago
|
function onBeforeOpen() {
|
||
|
document.documentElement.classList.add('slideout-open');
|
||
|
}
|
||
|
|
||
|
function onClose() {
|
||
6 years ago
|
$('#mobile-menu').blur();
|
||
7 years ago
|
document.documentElement.classList.remove('slideout-open');
|
||
|
$('#panel').off('click', closeOnClick);
|
||
|
}
|
||
|
|
||
|
$(window).on('resize action:ajaxify.start', function () {
|
||
|
navSlideout.close();
|
||
7 years ago
|
if (chatsSlideout) { chatsSlideout.close(); }
|
||
7 years ago
|
$('.account .cover').css('top', $('[component="navbar"]').height());
|
||
10 years ago
|
});
|
||
9 years ago
|
|
||
7 years ago
|
navSlideout
|
||
|
.ignore('code, code *, .preventSlideout, .preventSlideout *')
|
||
|
.on('closed', onClose)
|
||
|
.on('beforeopen', onBeforeOpen)
|
||
|
.on('opened', function () {
|
||
|
$('#panel').one('click', closeOnClick);
|
||
|
});
|
||
|
|
||
7 years ago
|
if (chatMenuVisible) {
|
||
7 years ago
|
chatsSlideout
|
||
|
.ignore('code, code *, .preventSlideout, .preventSlideout *')
|
||
|
.on('closed', onClose)
|
||
|
.on('beforeopen', onBeforeOpen)
|
||
|
.on('opened', function () {
|
||
|
$('#panel').one('click', closeOnClick);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// left slideout navigation menu
|
||
|
|
||
|
$('#mobile-menu').on('click', function () {
|
||
|
navSlideout.enable().toggle();
|
||
9 years ago
|
});
|
||
10 years ago
|
|
||
6 years ago
|
// Handle sidebar clicks while page still loading
|
||
|
if (document.getElementById('mobile-menu') === document.activeElement) {
|
||
|
navSlideout.enable().toggle();
|
||
|
}
|
||
|
|
||
7 years ago
|
function loadNotifications() {
|
||
|
require(['notifications'], function(notifications) {
|
||
|
notifications.loadNotifications($('#menu [data-section="notifications"] ul'));
|
||
|
});
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
7 years ago
|
navSlideout.on('opened', loadNotifications);
|
||
|
|
||
7 years ago
|
if (chatMenuVisible) {
|
||
7 years ago
|
navSlideout.on('beforeopen', function () {
|
||
|
chatsSlideout.close();
|
||
|
chatsSlideout.disable();
|
||
|
}).on('closed', function () {
|
||
|
chatsSlideout.enable();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
$('#menu [data-section="navigation"] ul').html($('#main-nav').html() + ($('#search-menu').html() || '') + ($('#logged-out-menu').html() || ''));
|
||
|
|
||
|
$('#user-control-list').children().clone(true, true).appendTo($('#menu [data-section="profile"] ul'));
|
||
|
|
||
|
socket.on('event:user_status_change', function(data) {
|
||
|
if (parseInt(data.uid, 10) === app.user.uid) {
|
||
|
app.updateUserStatus($('#menu [component="user/status"]'), data.status);
|
||
|
navSlideout.close();
|
||
|
}
|
||
10 years ago
|
});
|
||
8 years ago
|
|
||
7 years ago
|
// right slideout chats menu
|
||
|
|
||
|
function loadChats() {
|
||
|
require(['chat'], function (chat) {
|
||
|
chat.loadChatsDropdown($('#chats-menu .chat-list'));
|
||
|
});
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
7 years ago
|
if (chatMenuVisible) {
|
||
7 years ago
|
$('#mobile-chats').removeClass('hidden').on('click', function() {
|
||
|
navSlideout.close();
|
||
|
chatsSlideout.enable().toggle();
|
||
|
});
|
||
|
$('#chats-menu').on('click', 'li[data-roomid]', function () {
|
||
|
chatsSlideout.close();
|
||
|
});
|
||
9 years ago
|
|
||
7 years ago
|
chatsSlideout
|
||
|
.on('opened', loadChats)
|
||
|
.on('beforeopen', function () {
|
||
|
navSlideout.close().disable();
|
||
|
})
|
||
|
.on('closed', function () {
|
||
|
navSlideout.enable();
|
||
|
});
|
||
|
}
|
||
9 years ago
|
|
||
7 years ago
|
// add a checkbox in the user settings page
|
||
|
// so users can swap the sides the menus appear on
|
||
|
|
||
|
function setupSetting() {
|
||
|
if (ajaxify.data.template['account/settings'] && !document.getElementById('persona:menus:legacy-layout')) {
|
||
6 years ago
|
require(['translator'], function (translator) {
|
||
|
translator.translate('[[persona:mobile-menu-side]]', function (translated) {
|
||
|
$('<div class="well checkbox"><label><input type="checkbox" id="persona:menus:legacy-layout"/><strong>' + translated + '</strong></label></div>')
|
||
|
.appendTo('#content .account > .row > div:first-child')
|
||
|
.find('input')
|
||
|
.prop('checked', Storage.getItem('persona:menus:legacy-layout', 'true'))
|
||
|
.change(function (e) {
|
||
|
if (e.target.checked) {
|
||
|
Storage.setItem('persona:menus:legacy-layout', 'true');
|
||
|
} else {
|
||
|
Storage.removeItem('persona:menus:legacy-layout');
|
||
|
}
|
||
|
});
|
||
7 years ago
|
});
|
||
6 years ago
|
})
|
||
7 years ago
|
}
|
||
9 years ago
|
}
|
||
7 years ago
|
|
||
|
$(window).on('action:ajaxify.end', setupSetting);
|
||
|
setupSetting();
|
||
9 years ago
|
});
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
|
function setupHoverCards() {
|
||
|
require(['components'], function(components) {
|
||
9 years ago
|
components.get('topic')
|
||
9 years ago
|
.on('click', '[component="user/picture"],[component="user/status"]', generateUserCard);
|
||
9 years ago
|
});
|
||
|
|
||
|
$(window).on('action:posts.loading', function(ev, data) {
|
||
|
for (var i = 0, ii = data.posts.length; i < ii; i++) {
|
||
|
(ajaxify.data.topics || ajaxify.data.posts)[data.posts[i].index] = data.posts[i];
|
||
|
}
|
||
|
});
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
function generateUserCard(ev) {
|
||
6 years ago
|
var avatar = $(this);
|
||
|
var uid = avatar.parents('[data-uid]').attr('data-uid');
|
||
|
var data = (ajaxify.data.topics || ajaxify.data.posts);
|
||
9 years ago
|
|
||
|
for (var i = 0, ii = data.length; i < ii; i++) {
|
||
6 years ago
|
if (parseInt(data[i].uid, 10) === parseInt(uid, 10)) {
|
||
9 years ago
|
data = data[i].user;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
$('.persona-usercard').remove();
|
||
|
|
||
9 years ago
|
if (parseInt(data.uid, 10) === 0) {
|
||
|
return false;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
socket.emit('user.isFollowing', {uid: data.uid}, function(err, isFollowing) {
|
||
9 years ago
|
app.parseAndTranslate('modules/usercard', data, function(html) {
|
||
9 years ago
|
var card = $(html);
|
||
|
avatar.parents('a').after(card.hide());
|
||
9 years ago
|
|
||
9 years ago
|
if (parseInt(app.user.uid, 10) === parseInt(data.uid, 10) || !app.user.uid) {
|
||
9 years ago
|
card.find('.btn-morph').hide();
|
||
9 years ago
|
} else {
|
||
9 years ago
|
setupFavouriteMorph(card, data.uid, data.username);
|
||
9 years ago
|
|
||
|
if (isFollowing) {
|
||
|
$('.btn-morph').addClass('heart');
|
||
|
} else {
|
||
|
$('.btn-morph').addClass('plus');
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
utils.makeNumbersHumanReadable(card.find('.human-readable-number'));
|
||
9 years ago
|
setupCardRemoval(card);
|
||
9 years ago
|
card.fadeIn();
|
||
|
});
|
||
|
});
|
||
9 years ago
|
|
||
|
ev.preventDefault();
|
||
|
return false;
|
||
|
}
|
||
|
|
||
9 years ago
|
function setupFavouriteButtonOnProfile() {
|
||
|
setupFavouriteMorph($('[component="account/cover"]'), ajaxify.data.uid, ajaxify.data.username);
|
||
|
}
|
||
|
|
||
9 years ago
|
function setupCardRemoval(card) {
|
||
|
function removeCard(ev) {
|
||
|
if ($(ev.target).closest('.persona-usercard').length === 0) {
|
||
|
card.fadeOut(function() {
|
||
|
card.remove();
|
||
|
});
|
||
|
|
||
|
$(document).off('click', removeCard);
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
$(document).on('click', removeCard);
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
function setupFavouriteMorph(parent, uid, username) {
|
||
|
parent.find('.btn-morph').click(function(ev) {
|
||
|
var type = $(this).hasClass('plus') ? 'follow' : 'unfollow';
|
||
|
|
||
|
socket.emit('user.' + type, {uid: uid}, function(err) {
|
||
|
if (err) {
|
||
|
return app.alertError(err.message);
|
||
|
}
|
||
|
|
||
|
app.alertSuccess('[[global:alert.' + type + ', ' + username + ']]');
|
||
|
});
|
||
|
|
||
|
$(this).toggleClass('plus').toggleClass('heart');
|
||
9 years ago
|
$(this).translateAttr('title', type === 'follow' ? '[[global:unfollow]]' : '[[global:follow]]');
|
||
9 years ago
|
|
||
|
if ($(this).find('b.drop').length === 0) {
|
||
|
$(this).prepend('<b class="drop"></b>');
|
||
|
}
|
||
|
|
||
|
var drop = $(this).find('b.drop').removeClass('animate'),
|
||
|
x = ev.pageX - drop.width() / 2 - $(this).offset().left,
|
||
|
y = ev.pageY - drop.height() / 2 - $(this).offset().top;
|
||
|
|
||
|
drop.css({top: y + 'px', left: x + 'px'}).addClass('animate');
|
||
|
});
|
||
|
}
|
||
7 years ago
|
|
||
8 years ago
|
function setupQuickReply() {
|
||
8 years ago
|
$(window).on('action:ajaxify.end', function(ev, data) {
|
||
6 years ago
|
if (data.url && data.url.match('^topic/') && config.enableQuickReply) {
|
||
8 years ago
|
require(['persona/quickreply'], function(quickreply) {
|
||
7 years ago
|
quickreply.init();
|
||
8 years ago
|
});
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
});
|