ComunicWeb/assets/js/components/userSelect/userSelect.js

145 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-06-05 14:03:14 +00:00
/**
* User selector (using select2)
*
* @author Pierre HUBERT
*/
ComunicWeb.components.userSelect = {
/**
* Initialize user selector for an element of the page
*
* @param {HTMLElement} inputSelect The target select input
* @return {Boolean} True for a success
*/
init: function(inputSelect){
//Log action
ComunicWeb.debug.logMessage("INFO : Initialize user selector");
$(inputSelect).select2({
ajax: {
transport: function(params, success, failure){
//Check if some data were passed or not
if(!params.data.term)
return false;
//Retrive users list
ComunicWeb.user.userInfos.search(params.data.term, function(usersInfos){
if(usersInfos.error)
return; // Doesn't do anything failure();
else{
//Prepare results processing
returnData = {
results: []
}
//Processing results
for(i in usersInfos){
returnData.results.push({
id: usersInfos[i].userID,
text: usersInfos[i].firstName + " " + usersInfos[i].lastName,
accountImage: usersInfos[i].accountImage,
});
}
//Return result
success(returnData);
}
});
},
delay: 250,
},
//Format result displaying
templateResult: ComunicWeb.components.userSelect.formatUser,
});
},
/**
* Format the display of a user
*
* @param {Object} infos Informations about the user
* @return {String} The formated informations
*/
formatUser: function(infos){
if(!infos.id)
return infos.id;
return $("<img src='"+infos.accountImage+"' class='user-select-image' /> <span>" + infos.text + "</span>");
},
2017-06-06 17:05:49 +00:00
/**
* Returns the results of a specified select element
*
* @param {HTMLElement} inputSelect The target element
* @return {Array} An array with all the select IDs
2017-06-06 17:05:49 +00:00
*/
getResults: function(inputSelect){
2017-06-07 14:38:06 +00:00
//Get entries
var selectUsersResult = $(inputSelect).select2("data");
2017-06-06 17:05:49 +00:00
//Prepare return
var usersID = [];
2017-06-06 17:05:49 +00:00
2017-06-07 14:38:06 +00:00
for(i in selectUsersResult){
2017-06-06 17:05:49 +00:00
//Check it is really a children
2017-06-07 14:38:06 +00:00
if(selectUsersResult[i].id){
//Add ID to the table
2017-06-07 14:38:06 +00:00
usersID.push(selectUsersResult[i].id);
2017-06-06 17:05:49 +00:00
}
}
//Return result IDs
return usersID;
},
/**
* Push entries to user select element
*
* @param {HTMLElement} inputSelect The target element (select2 initialized)
* @param {array} usersID The ID of the users to push in select2 element
* @return {Boolean} True for a success
*/
2018-04-29 19:21:01 +00:00
pushEntries: function(inputSelect, usersID){
//Get informations about the entries
2019-05-16 13:07:58 +00:00
getMultipleUsersInfo(usersID, function(usersInfos){
//Check for errors
if(usersInfos.error){
//Log error
ComunicWeb.debug.logMessage("Error ! Couldn't fill select2 element because a request on the server failed !");
return false;
}
//In case of success
var i;
for(i in usersInfos){
//Create the new option
var option = createElem2({
type: "option",
value: usersInfos[i].userID,
innerHTML: usersInfos[i].firstName + " " + usersInfos[i].lastName,
});
option.setAttribute("selected", "true");
//Apply the new option
$(inputSelect).append(option);
$(inputSelect).trigger("change");
}
})
//Success
return true;
2017-06-06 17:05:49 +00:00
}
2017-06-05 14:03:14 +00:00
};