'use strict'; /* globals define, ajaxify, socket, app, config, templates, bootbox */ define('forum/account/edit', ['forum/account/header', 'uploader', 'translator', 'components', 'cropper'], function (header, uploader, translator, components, cropper) { var AccountEdit = {}; AccountEdit.init = function () { header.init(); $('#submitBtn').on('click', updateProfile); app.loadJQueryUI(function () { $('#inputBirthday').datepicker({ changeMonth: true, changeYear: true, yearRange: '1900:-5y', defaultDate: '-13y' }); }); handleImageChange(); handleAccountDelete(); handleEmailConfirm(); updateSignature(); updateAboutMe(); }; function updateProfile() { var userData = { uid: $('#inputUID').val(), fullname: $('#inputFullname').val(), website: $('#inputWebsite').val(), birthday: $('#inputBirthday').val(), location: $('#inputLocation').val(), groupTitle: $('#groupTitle').val(), signature: $('#inputSignature').val(), aboutme: $('#inputAboutMe').val() }; $(window).trigger('action:profile.update', userData); socket.emit('user.updateProfile', userData, function (err, data) { if (err) { return app.alertError(err.message); } app.alertSuccess('[[user:profile_update_success]]'); if (data.picture) { $('#user-current-picture').attr('src', data.picture); } updateHeader(data.picture); }); return false; } function updateHeader(picture) { if (parseInt(ajaxify.data.theirid, 10) !== parseInt(ajaxify.data.yourid, 10)) { return; } components.get('header/userpicture')[picture ? 'show' : 'hide'](); components.get('header/usericon')[!picture ? 'show' : 'hide'](); if (picture) { components.get('header/userpicture').attr('src', picture); } } function handleImageChange() { $('#changePictureBtn').on('click', function () { socket.emit('user.getProfilePictures', {uid: ajaxify.data.uid}, function (err, pictures) { if (err) { return app.alertError(err.message); } // boolean to signify whether an uploaded picture is present in the pictures list var uploaded = pictures.reduce(function (memo, cur) { return memo || cur.type === 'uploaded'; }, false); templates.parse('partials/modals/change_picture_modal', { pictures: pictures, uploaded: uploaded, allowProfileImageUploads: ajaxify.data.allowProfileImageUploads }, function (html) { translator.translate(html, function (html) { var modal = bootbox.dialog({ className: 'picture-switcher', title: '[[user:change_picture]]', message: html, show: true, buttons: { close: { label: '[[global:close]]', callback: onCloseModal, className: 'btn-link' }, update: { label: '[[global:save_changes]]', callback: saveSelection } } }); modal.on('shown.bs.modal', updateImages); modal.on('click', '.list-group-item', function selectImageType() { modal.find('.list-group-item').removeClass('active'); $(this).addClass('active'); }); handleImageUpload(modal); function updateImages() { var userIcon = modal.find('.user-icon'); userIcon .css('background-color', ajaxify.data['icon:bgColor']) .text(ajaxify.data['icon:text']); // Check to see which one is the active picture if (!ajaxify.data.picture) { modal.find('.list-group-item .user-icon').parents('.list-group-item').addClass('active'); } else { modal.find('.list-group-item img').each(function () { if (this.getAttribute('src') === ajaxify.data.picture) { $(this).parents('.list-group-item').addClass('active'); } }); } } function saveSelection() { var type = modal.find('.list-group-item.active').attr('data-type'); changeUserPicture(type, function (err) { if (err) { return app.alertError(err.message); } updateHeader(type === 'default' ? '' : modal.find('.list-group-item.active img').attr('src')); ajaxify.refresh(); }); } function onCloseModal() { modal.modal('hide'); } }); }); }); return false; }); } function handleAccountDelete() { $('#deleteAccountBtn').on('click', function () { translator.translate('[[user:delete_account_confirm]]', function (translated) { var modal = bootbox.confirm(translated + '
', function (confirm) { if (!confirm) { return; } if ($('#confirm-username').val() !== app.user.username) { app.alertError('[[error:invalid-username]]'); return false; } else { socket.emit('user.deleteAccount', {}, function (err) { if (err) { return app.alertError(err.message); } window.location.href = config.relative_path + '/'; }); } }); modal.on('shown.bs.modal', function () { modal.find('input').focus(); }); }); return false; }); } function handleImageUpload(modal) { function onUploadComplete(urlOnServer) { urlOnServer = urlOnServer + '?' + Date.now(); updateHeader(urlOnServer); if (ajaxify.data.picture.length) { $('#user-current-picture, img.avatar').attr('src', urlOnServer); ajaxify.data.uploadedpicture = urlOnServer; } else { ajaxify.refresh(function () { $('#user-current-picture, img.avatar').attr('src', urlOnServer); }); } } function onRemoveComplete() { if (ajaxify.data.uploadedpicture === ajaxify.data.picture) { ajaxify.refresh(); updateHeader(); } } function handleImageCrop(data) { templates.parse('partials/modals/crop_uploaded_picture', {url: data.url}, function (cropperHtml) { translator.translate(cropperHtml, function (translated) { var cropperModal = $(translated); cropperModal.modal('show'); var img = document.getElementById('cropped-image'); var cropperTool = new cropper.default(img, { aspectRatio: 1 / 1, viewMode: 1 }); cropperModal.find('.crop-btn').on('click', function () { $(this).addClass('disabled'); var imageData = data.imageType ? cropperTool.getCroppedCanvas().toDataURL(data.imageType) : cropperTool.getCroppedCanvas().toDataURL(); cropperModal.find('#upload-progress-bar').css('width', '100%'); cropperModal.find('#upload-progress-box').show().removeClass('hide'); socket.emit('user.uploadCroppedPicture', { uid: ajaxify.data.theirid, imageData: imageData }, function (err, imageData) { if (err) { app.alertError(err.message); } onUploadComplete(imageData.url); cropperModal.modal('hide'); }); }); }); }); } modal.find('[data-action="upload"]').on('click', function () { modal.modal('hide'); uploader.show({ route: config.relative_path + '/api/user/' + ajaxify.data.userslug + '/uploadpicture', params: {}, fileSize: ajaxify.data.maximumProfileImageSize, title: '[[user:upload_picture]]', description: '[[user:upload_a_picture]]', accept: '.png,.jpg,.bmp' }, function (data) { handleImageCrop(data); }); return false; }); modal.find('[data-action="upload-url"]').on('click', function () { modal.modal('hide'); templates.parse('partials/modals/upload_picture_from_url_modal', {}, function (html) { translator.translate(html, function (html) { var uploadModal = $(html); uploadModal.modal('show'); uploadModal.find('.upload-btn').on('click', function () { var url = uploadModal.find('#uploadFromUrl').val(); if (!url) { return; } uploadModal.modal('hide'); handleImageCrop({url: url}); return false; }); }); }); return false; }); modal.find('[data-action="remove-uploaded"]').on('click', function () { socket.emit('user.removeUploadedPicture', {uid: ajaxify.data.theirid}, function (err) { modal.modal('hide'); if (err) { return app.alertError(err.message); } onRemoveComplete(); }); }); } function handleEmailConfirm() { $('#confirm-email').on('click', function () { var btn = $(this).attr('disabled', true); socket.emit('user.emailConfirm', {}, function (err) { btn.removeAttr('disabled'); if (err) { return app.alertError(err.message); } app.alertSuccess('[[notifications:email-confirm-sent]]'); }); }); } function changeUserPicture(type, callback) { socket.emit('user.changePicture', { type: type, uid: ajaxify.data.theirid }, callback); } function getCharsLeft(el, max) { return el.length ? '(' + el.val().length + '/' + max + ')' : ''; } function updateSignature() { var el = $('#inputSignature'); $('#signatureCharCountLeft').html(getCharsLeft(el, ajaxify.data.maximumSignatureLength)); el.on('keyup change', function () { $('#signatureCharCountLeft').html(getCharsLeft(el, ajaxify.data.maximumSignatureLength)); }); } function updateAboutMe() { var el = $('#inputAboutMe'); $('#aboutMeCharCountLeft').html(getCharsLeft(el, ajaxify.data.maximumAboutMeLength)); el.on('keyup change', function () { $('#aboutMeCharCountLeft').html(getCharsLeft(el, ajaxify.data.maximumAboutMeLength)); }); } return AccountEdit; });