2018-04-29 13:13:51 +00:00
|
|
|
/**
|
|
|
|
* Account image settings section
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
ComunicWeb.pages.settings.sections.accountImage = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open settings section
|
|
|
|
*
|
|
|
|
* @param {object} args Additionnal arguments
|
|
|
|
* @param {HTMLElement} target The target for the page
|
|
|
|
*/
|
|
|
|
open: function(args, target){
|
|
|
|
|
|
|
|
//Create a box
|
|
|
|
var box = createElem2({
|
|
|
|
appendTo: target,
|
|
|
|
type: "div",
|
|
|
|
class: "box box-primary box-account-image-settings"
|
|
|
|
});
|
|
|
|
|
|
|
|
//Add box header
|
|
|
|
var boxHead = createElem2({
|
|
|
|
appendTo: box,
|
|
|
|
type: "div",
|
|
|
|
class: "box-header",
|
|
|
|
});
|
|
|
|
var boxTitle = createElem2({
|
|
|
|
appendTo: boxHead,
|
|
|
|
type: "h3",
|
|
|
|
class: "box-title",
|
|
|
|
innerHTML: "Account image"
|
|
|
|
});
|
|
|
|
|
|
|
|
//Create box body
|
|
|
|
var boxBody = createElem2({
|
|
|
|
appendTo: box,
|
|
|
|
type: "div",
|
|
|
|
class: "box-body"
|
|
|
|
});
|
|
|
|
|
|
|
|
//Add loading callout
|
|
|
|
var loadMsg = ComunicWeb.common.messages.createLoadingCallout(boxBody);
|
|
|
|
|
|
|
|
//Fetch information about account image on the API
|
|
|
|
ComunicWeb.components.settings.interface.getAccountImage(function(result){
|
|
|
|
|
|
|
|
//Remove loading message
|
|
|
|
loadMsg.remove();
|
|
|
|
|
|
|
|
//Check for errors
|
|
|
|
if(result.error){
|
|
|
|
notify("Could not get account image information !", "danger");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-01 08:57:58 +00:00
|
|
|
//Display the form
|
|
|
|
ComunicWeb.pages.settings.sections.accountImage._show_form(boxBody, result);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display account images form
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} target The target for the form
|
|
|
|
* @param {object} info Information about the form
|
|
|
|
*/
|
|
|
|
_show_form: function(target, info){
|
|
|
|
|
|
|
|
//Apply account image settings
|
|
|
|
var accountImageForm = createElem2({
|
|
|
|
type: "div",
|
|
|
|
appendTo: target
|
|
|
|
});
|
|
|
|
|
2018-05-12 08:06:56 +00:00
|
|
|
//Create top actions contener
|
|
|
|
var actionsTopContener = createElem2({
|
|
|
|
appendTo: accountImageForm,
|
|
|
|
type: "div"
|
|
|
|
});
|
|
|
|
|
2018-05-01 08:57:58 +00:00
|
|
|
//First, offer the user to upload a new account image
|
|
|
|
var newAccountImageLabel = createElem2({
|
2018-05-12 08:06:56 +00:00
|
|
|
appendTo: actionsTopContener,
|
2018-05-01 08:57:58 +00:00
|
|
|
type: "label"
|
|
|
|
});
|
|
|
|
var fileInput = createElem2({
|
|
|
|
appendTo: newAccountImageLabel,
|
|
|
|
type: "input",
|
|
|
|
elemType: "file"
|
|
|
|
});
|
|
|
|
fileInput.style.display = "none";
|
|
|
|
var chooseButton = createElem2({
|
|
|
|
appendTo: newAccountImageLabel,
|
|
|
|
type: "div",
|
|
|
|
class: "btn btn-primary",
|
|
|
|
innerHTML: "Upload a new account image"
|
|
|
|
});
|
|
|
|
|
|
|
|
//Add event listener
|
|
|
|
fileInput.addEventListener("change", function(e){
|
2018-04-29 13:13:51 +00:00
|
|
|
|
2018-05-01 08:57:58 +00:00
|
|
|
//Check if no file have been selected
|
|
|
|
if(fileInput.files.length == 0)
|
|
|
|
return;
|
2018-05-01 06:59:14 +00:00
|
|
|
|
2018-05-01 08:57:58 +00:00
|
|
|
//Upload the new file
|
|
|
|
//Display a callout message
|
|
|
|
var message = ComunicWeb.common.messages.createCalloutElem("", "Please wait while your picture is being uploaded...");
|
|
|
|
target.insertBefore(message, accountImageForm);
|
|
|
|
|
|
|
|
//Upload the image
|
|
|
|
var fd = new FormData();
|
|
|
|
fd.append("picture", fileInput.files[0], fileInput.files[0].name);
|
|
|
|
ComunicWeb.components.settings.interface.uploadAccountImage(fd, function(result){
|
|
|
|
|
|
|
|
//Remove message
|
|
|
|
message.remove();
|
|
|
|
|
|
|
|
//Check for errors
|
|
|
|
if(result.error){
|
|
|
|
notify("An error occured while trying to upload your image !", "danger");
|
2018-05-01 06:59:14 +00:00
|
|
|
return;
|
2018-05-01 08:57:58 +00:00
|
|
|
}
|
2018-05-01 06:59:14 +00:00
|
|
|
|
2018-05-01 08:57:58 +00:00
|
|
|
notify("Your picture has been successfully uploaded !", "success");
|
|
|
|
|
|
|
|
//Reload current page
|
|
|
|
ComunicWeb.common.system.reset();
|
|
|
|
});
|
|
|
|
});
|
2018-05-01 06:59:14 +00:00
|
|
|
|
2018-05-12 08:06:56 +00:00
|
|
|
add_space(actionsTopContener);
|
|
|
|
|
|
|
|
//Offer the user to create a new random account image
|
|
|
|
var generateAccountImageBtn = createElem2({
|
|
|
|
appendTo: actionsTopContener,
|
|
|
|
type: "div",
|
|
|
|
class: "btn btn-success",
|
|
|
|
innerHTML: "Generate random"
|
|
|
|
});
|
|
|
|
|
|
|
|
//Make generate account image buttons lives
|
|
|
|
generateAccountImageBtn.onclick = function(){
|
|
|
|
|
|
|
|
//Lock screen
|
|
|
|
message = ComunicWeb.common.page.showTransparentWaitSplashScreen();
|
|
|
|
|
|
|
|
//Generate image
|
|
|
|
var base64 = generateIdentImage();
|
|
|
|
|
|
|
|
//Upload image
|
|
|
|
var fd = new FormData();
|
|
|
|
fd.append("picture", dataURItoBlob("data:image/png;base64," + base64));
|
|
|
|
ComunicWeb.components.settings.interface.uploadAccountImage(fd, function(result){
|
|
|
|
|
|
|
|
//Remove message
|
|
|
|
message.remove();
|
|
|
|
|
|
|
|
//Check for errors
|
|
|
|
if(result.error){
|
|
|
|
notify("An error occured while trying to upload generated image !", "danger");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
notify("Random generated image has been uploaded !", "success");
|
|
|
|
|
|
|
|
//Reload current page
|
|
|
|
ComunicWeb.common.system.reset();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-01 08:57:58 +00:00
|
|
|
//Stop here if the user does not have any account image
|
|
|
|
if(!info.has_image)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//Display user account image
|
|
|
|
createElem2({
|
|
|
|
appendTo: accountImageForm,
|
|
|
|
type: "img",
|
|
|
|
src: info.image_url,
|
|
|
|
class: "settings-account-image-preview"
|
|
|
|
});
|
2018-05-01 06:59:14 +00:00
|
|
|
|
2018-05-01 08:57:58 +00:00
|
|
|
//Create actions contener
|
|
|
|
var actionsContener = createElem2({
|
|
|
|
appendTo: accountImageForm,
|
|
|
|
type: "div"
|
|
|
|
});
|
2018-05-01 06:59:14 +00:00
|
|
|
|
2018-05-01 08:57:58 +00:00
|
|
|
//Delete account image
|
|
|
|
var deleteImage = createElem2({
|
|
|
|
appendTo: accountImageForm,
|
|
|
|
type: "div",
|
|
|
|
class: "btn btn-danger",
|
|
|
|
innerHTML: "Delete image"
|
|
|
|
});
|
2018-05-01 06:59:14 +00:00
|
|
|
|
2018-05-01 09:42:23 +00:00
|
|
|
//Make delete button lives
|
2018-05-01 08:57:58 +00:00
|
|
|
deleteImage.addEventListener("click", function(e){
|
|
|
|
ComunicWeb.common.messages.confirm("Do you really want to delete your account image ? The operation can not be reverted !", function(res){
|
|
|
|
|
|
|
|
if(!res)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//Make a request on the server
|
|
|
|
ComunicWeb.components.settings.interface.deleteAccountImage(function(callback){
|
|
|
|
|
|
|
|
//Delay action to let the time to the page to restart
|
|
|
|
setTimeout(function(){
|
|
|
|
|
|
|
|
//Check for errors
|
|
|
|
if(callback.error){
|
|
|
|
notify("Could not delete your account image !", "danger");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
notify("Your account image has been successfully deleted!", "success");
|
|
|
|
|
|
|
|
}, 500);
|
|
|
|
|
2018-05-01 06:59:14 +00:00
|
|
|
|
2018-05-01 08:57:58 +00:00
|
|
|
//Reset the page
|
2018-05-01 06:59:14 +00:00
|
|
|
ComunicWeb.common.system.reset();
|
2018-05-01 08:57:58 +00:00
|
|
|
|
2018-05-01 06:59:14 +00:00
|
|
|
});
|
2018-05-01 08:57:58 +00:00
|
|
|
|
2018-05-01 06:59:14 +00:00
|
|
|
});
|
2018-04-29 13:13:51 +00:00
|
|
|
});
|
2018-05-01 09:42:23 +00:00
|
|
|
|
|
|
|
//Add the form to update visibility level
|
|
|
|
add_space(accountImageForm);
|
|
|
|
add_space(accountImageForm);
|
|
|
|
|
|
|
|
var visibilityForm = createElem2({
|
|
|
|
appendTo: accountImageForm,
|
|
|
|
type: "form",
|
|
|
|
class: "account-image-visibility-form"
|
|
|
|
});
|
|
|
|
|
|
|
|
//Title
|
|
|
|
createElem2({
|
|
|
|
appendTo: visibilityForm,
|
|
|
|
type: "h4",
|
|
|
|
innerHTML: "Account image visibility"
|
|
|
|
});
|
|
|
|
|
|
|
|
//Add the options
|
|
|
|
//Open
|
|
|
|
createFormGroup({
|
|
|
|
target: visibilityForm,
|
|
|
|
label: "Everyone",
|
|
|
|
name: "account-image-visibility",
|
|
|
|
type: "radio",
|
|
|
|
value: "open",
|
|
|
|
checked: info.visibility == "open"
|
|
|
|
});
|
|
|
|
|
|
|
|
//Public
|
|
|
|
createFormGroup({
|
|
|
|
target: visibilityForm,
|
|
|
|
label: "All Comunic users",
|
|
|
|
name: "account-image-visibility",
|
|
|
|
type: "radio",
|
|
|
|
value: "public",
|
|
|
|
checked: info.visibility == "public"
|
|
|
|
});
|
|
|
|
|
|
|
|
//Friends
|
|
|
|
createFormGroup({
|
|
|
|
target: visibilityForm,
|
|
|
|
label: "My friends only",
|
|
|
|
name: "account-image-visibility",
|
|
|
|
type: "radio",
|
|
|
|
value: "friends",
|
|
|
|
checked: info.visibility == "friends"
|
|
|
|
});
|
|
|
|
|
|
|
|
//Add update button
|
|
|
|
var updateButton = createElem2({
|
|
|
|
appendTo: visibilityForm,
|
|
|
|
type: "div",
|
|
|
|
class: "btn btn-primary",
|
|
|
|
innerHTML: "Update visibility"
|
|
|
|
});
|
|
|
|
|
|
|
|
updateButton.addEventListener("click", function(e){
|
|
|
|
|
|
|
|
//Get the new visibility level
|
|
|
|
var newVisibility = visibilityForm.elements["account-image-visibility"].value;
|
|
|
|
|
|
|
|
//Make a request over the server
|
|
|
|
ComunicWeb.components.settings.interface.updateAccountImageVisibility(newVisibility, function(callback){
|
|
|
|
|
|
|
|
//Check for errors
|
|
|
|
if(callback.error){
|
|
|
|
notify("Could update the visibility of your account image!", "danger");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
notify("The visibility of your account image have been successfully updated!", "success");
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2018-05-01 08:57:58 +00:00
|
|
|
}
|
2018-04-29 13:13:51 +00:00
|
|
|
}
|