2017-05-21 15:25:41 +00:00
|
|
|
/**
|
|
|
|
* Utilities functions
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new HTML node
|
|
|
|
*
|
|
|
|
* @param {String} nodeType The type of the HTML node
|
|
|
|
* @param {HTMLElement} appendTo Optionnal, defines node on which the new node will be applied
|
|
|
|
* @return {HTMLElement} The newly created element
|
|
|
|
*/
|
|
|
|
function createElem(nodeType, appendTo){
|
2017-05-21 15:42:04 +00:00
|
|
|
var newElem = document.createElement(nodeType);
|
2017-05-21 15:25:41 +00:00
|
|
|
|
2017-05-21 15:42:04 +00:00
|
|
|
if(appendTo)
|
|
|
|
appendTo.appendChild(newElem);
|
2017-05-21 15:25:41 +00:00
|
|
|
|
2017-05-21 15:42:04 +00:00
|
|
|
//Return result
|
|
|
|
return newElem;
|
2017-05-21 15:25:41 +00:00
|
|
|
}
|
|
|
|
|
2017-06-17 07:51:48 +00:00
|
|
|
/**
|
|
|
|
* Create a new HTML node (version2)
|
|
|
|
*
|
|
|
|
* @param {Object} infos Informations about the HTML node to create
|
|
|
|
* @info {String} type The type of the new node
|
|
|
|
* @info {HTMLElement} appendTo HTML Element that will receive the new node
|
2017-06-25 17:10:26 +00:00
|
|
|
* @info {HTMLElement} insertBefore Insert before specified HTML element
|
|
|
|
* @info {HTMLElement} insertAsFirstChild Insert the new HTML Element as the first child of the specified element
|
2017-06-21 10:10:22 +00:00
|
|
|
* @info {String} class The class of the new element
|
|
|
|
* @info {String} id The ID of the new element
|
|
|
|
* @info {String} title The title of the new element
|
|
|
|
* @info {String} src The src attribute of the new element
|
2017-06-26 13:36:29 +00:00
|
|
|
* @info {String} href href attribute for the src element
|
2018-01-04 18:39:22 +00:00
|
|
|
* @info {string} name The name of the new element
|
2017-06-21 14:44:10 +00:00
|
|
|
* @info {String} elemType The type attribute of the new element
|
2017-06-21 10:10:22 +00:00
|
|
|
* @info {String} value The value of the new element
|
|
|
|
* @info {String} placeholder The placeholder of the new element
|
|
|
|
* @info {String} innerHTML Specify the html content of the newly created element
|
2017-06-17 07:51:48 +00:00
|
|
|
* @return {HTMLElement} The newly created element
|
|
|
|
*/
|
|
|
|
function createElem2(infos){
|
|
|
|
|
|
|
|
var newElem = document.createElement(infos.type);
|
|
|
|
|
|
|
|
//Append to a specific element
|
|
|
|
if(infos.appendTo)
|
|
|
|
infos.appendTo.appendChild(newElem);
|
|
|
|
|
|
|
|
//Append before a specific element
|
|
|
|
if(infos.insertBefore)
|
|
|
|
infos.insertBefore.parentNode.insertBefore(newElem, infos.insertBefore);
|
|
|
|
|
2017-06-25 17:10:26 +00:00
|
|
|
//Append as the first child of an element
|
|
|
|
if(infos.insertAsFirstChild){
|
|
|
|
//Check if the element as already a child or not
|
|
|
|
if(infos.insertAsFirstChild.firstChild)
|
|
|
|
infos.insertAsFirstChild.insertBefore(newElem, infos.insertAsFirstChild.firstChild);
|
|
|
|
//Else we can just append the newly created element
|
|
|
|
else
|
|
|
|
infos.insertAsFirstChild.appendChild(newElem);
|
|
|
|
}
|
|
|
|
|
2017-06-17 07:51:48 +00:00
|
|
|
//Specify the class of the element
|
|
|
|
if(infos.class)
|
|
|
|
newElem.className = infos.class;
|
|
|
|
|
|
|
|
//Specify the ID of the element
|
|
|
|
if(infos.id)
|
|
|
|
newElem.id = infos.id;
|
|
|
|
|
|
|
|
//Specify the title of the new element
|
|
|
|
if(infos.title)
|
|
|
|
newElem.title = infos.title;
|
|
|
|
|
|
|
|
//Specify the source of the element
|
|
|
|
if(infos.src)
|
|
|
|
newElem.src = infos.src;
|
2017-06-26 13:36:29 +00:00
|
|
|
if(infos.href)
|
|
|
|
newElem.href = infos.href;
|
2017-06-17 07:51:48 +00:00
|
|
|
|
2018-01-04 18:39:22 +00:00
|
|
|
//Specify the name of the new element
|
|
|
|
if(infos.name)
|
|
|
|
newElem.name = infos.name;
|
|
|
|
|
2017-06-17 09:20:54 +00:00
|
|
|
//Specify element type
|
2017-06-21 14:44:10 +00:00
|
|
|
if(infos.elemType)
|
|
|
|
newElem.type = infos.elemType;
|
2017-06-17 09:20:54 +00:00
|
|
|
|
2017-06-17 14:37:40 +00:00
|
|
|
//Specify element value
|
|
|
|
if(infos.value)
|
|
|
|
newElem.value = infos.value;
|
|
|
|
|
2017-06-21 10:10:22 +00:00
|
|
|
//Specify element placeholder
|
|
|
|
if(infos.placeholder)
|
|
|
|
newElem.placeholder = infos.placeholder;
|
|
|
|
|
2017-06-17 08:09:37 +00:00
|
|
|
//Specify node content
|
|
|
|
if(infos.innerHTML)
|
|
|
|
newElem.innerHTML = infos.innerHTML;
|
|
|
|
|
2017-06-17 07:51:48 +00:00
|
|
|
//Return newly created element
|
|
|
|
return newElem;
|
|
|
|
}
|
|
|
|
|
2017-05-21 15:25:41 +00:00
|
|
|
/**
|
|
|
|
* Get an HTML element specified by an ID
|
|
|
|
*
|
|
|
|
* @param {String} nodeName The ID of the element
|
2017-05-24 16:48:52 +00:00
|
|
|
* @return {HTMLElement} The element / False for a failure
|
2017-05-21 15:25:41 +00:00
|
|
|
*/
|
|
|
|
function byId(nodeName){
|
2017-05-21 15:42:04 +00:00
|
|
|
return document.getElementById(nodeName);
|
2017-05-21 16:18:29 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 16:48:52 +00:00
|
|
|
/**
|
|
|
|
* Remove all nodes of a specified HTML element
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} container The container to empty
|
|
|
|
* @return {Boolean} True for a success
|
|
|
|
*/
|
|
|
|
function emptyElem(container){
|
|
|
|
//Get children references
|
2017-06-16 09:18:17 +00:00
|
|
|
var children = container.children;
|
2017-05-24 16:48:52 +00:00
|
|
|
|
|
|
|
//Process each child
|
2017-06-16 09:18:17 +00:00
|
|
|
while(container.children.length > 0){
|
2017-05-24 16:48:52 +00:00
|
|
|
|
2017-06-16 09:18:17 +00:00
|
|
|
//Check if the child has subchild
|
|
|
|
if(container.children[0].children)
|
|
|
|
emptyElem(container.children[0]); //Remove them first
|
|
|
|
|
|
|
|
//Remove child
|
|
|
|
container.children[0].remove();
|
|
|
|
}
|
2017-05-24 16:48:52 +00:00
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-16 08:36:40 +00:00
|
|
|
/**
|
|
|
|
* Delete all the content of an object
|
|
|
|
*
|
|
|
|
* @param {Object} object The object to clear
|
|
|
|
* @return {Boolean} True for a success
|
|
|
|
*/
|
|
|
|
function clearObject(object){
|
|
|
|
|
|
|
|
//Variable (for loop) is specific to this local scop
|
|
|
|
var i = 0;
|
|
|
|
|
|
|
|
//Process each node of the object
|
|
|
|
for(i in object){
|
|
|
|
|
2017-06-25 17:14:34 +00:00
|
|
|
if(!object[i])
|
|
|
|
continue;
|
|
|
|
|
2017-06-16 08:36:40 +00:00
|
|
|
//Check if the node is an object
|
|
|
|
if(object[i].toString() === "[object Object]"){
|
|
|
|
clearObject(object[i]); //Delete object content
|
|
|
|
}
|
|
|
|
|
|
|
|
//Delete node
|
|
|
|
delete object[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-24 14:37:41 +00:00
|
|
|
/**
|
|
|
|
* Check a given email address
|
|
|
|
*
|
|
|
|
* @param {String} emailAddress The email address to check
|
|
|
|
* @return {Boolean} True for a valid email address / false else
|
|
|
|
*/
|
|
|
|
function checkMail(emailAddress){
|
|
|
|
return (emailAddress.match(/^[a-zA-Z0-9_.]+@[a-zA-Z0-9-]{1,}[.][a-zA-Z]{2,5}$/) === null ? false : true);
|
2017-06-05 09:02:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a formgroup element
|
|
|
|
*
|
2017-06-07 12:14:16 +00:00
|
|
|
* @param {Object} infos Informations about the formgroup element to create
|
|
|
|
* * @info {HTMLElement} target The target of the field
|
|
|
|
* * @info {String} label The label of the field
|
|
|
|
* * @info {String} placeholder The placeholder of the field
|
|
|
|
* * @info {Boolean} checked Defines if the fields has to be checked or not (checkbox only)
|
|
|
|
* * @info {Boolean} multiple Defines if the fields can accept more than one response
|
|
|
|
* * @info {String} type The type of the field
|
2017-06-05 09:02:10 +00:00
|
|
|
* @return {HTMLElement} The input
|
|
|
|
*/
|
2017-06-07 12:14:16 +00:00
|
|
|
function createFormGroup(infos){
|
2017-06-05 09:02:10 +00:00
|
|
|
//Create formgroup
|
2017-06-07 12:14:16 +00:00
|
|
|
var formGroup = createElem("div", infos.target);
|
2017-06-05 09:02:10 +00:00
|
|
|
formGroup.className = "form-group";
|
|
|
|
|
|
|
|
//Add label
|
|
|
|
var labelElem = createElem("label", formGroup);
|
|
|
|
|
2017-06-05 09:34:43 +00:00
|
|
|
//Treatement differs if it is a checkbox
|
2017-06-07 12:14:16 +00:00
|
|
|
if(infos.type == "checkbox"){
|
2017-06-05 09:02:10 +00:00
|
|
|
|
2017-06-05 09:34:43 +00:00
|
|
|
//Create checkbox
|
|
|
|
var input = createElem("input", labelElem) ;
|
|
|
|
input.type = "checkbox";
|
2017-06-05 09:02:10 +00:00
|
|
|
|
2017-06-05 09:34:43 +00:00
|
|
|
//Check if input has to be checked by default
|
2017-06-07 12:14:16 +00:00
|
|
|
if(infos.checked){
|
|
|
|
if(infos.checked === true){
|
2017-06-05 09:34:43 +00:00
|
|
|
input.checked = "true";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add label value
|
|
|
|
var labelValue = createElem("span", labelElem);
|
2017-06-07 12:14:16 +00:00
|
|
|
labelValue.innerHTML = " "+infos.label;
|
2017-06-05 09:34:43 +00:00
|
|
|
|
|
|
|
//Enable iCheck
|
|
|
|
$(input).iCheck({
|
|
|
|
checkboxClass: 'icheckbox_flat-blue',
|
|
|
|
radioClass: 'iradio_flat-blue'
|
|
|
|
});
|
|
|
|
}
|
2017-06-07 12:14:16 +00:00
|
|
|
else if(infos.type == "select2"){
|
|
|
|
//In case of select2 element
|
|
|
|
//Check for label
|
|
|
|
if(infos.label)
|
|
|
|
labelElem.innerHTML = infos.label;
|
|
|
|
else
|
|
|
|
labelElem.remove(); //Remove useless label element
|
|
|
|
|
|
|
|
//Create input
|
|
|
|
var input = createElem("select", formGroup);
|
|
|
|
input.style.width = "100%";
|
|
|
|
input.className = "form-control select2";
|
|
|
|
if(infos.multiple) //For multiple changes
|
|
|
|
input.setAttribute("multiple", "multiple");
|
|
|
|
if(infos.placeholder) //Placeholder if required
|
|
|
|
input.setAttribute("data-placeholder", infos.placeholder);
|
|
|
|
|
2017-06-21 10:10:22 +00:00
|
|
|
}
|
|
|
|
//In case of textarea
|
2017-06-25 17:10:26 +00:00
|
|
|
else if(infos.type == "textarea"){
|
2017-06-21 10:10:22 +00:00
|
|
|
//Fill label value
|
|
|
|
if(infos.label)
|
|
|
|
labelElem.innerHTML = infos.label;
|
|
|
|
else
|
|
|
|
labelElem.remove(); //Remove useless label element
|
|
|
|
|
|
|
|
//Create textarea element
|
|
|
|
var input = createElem2({
|
|
|
|
appendTo: formGroup,
|
|
|
|
type: "textarea",
|
|
|
|
class: "form-control",
|
|
|
|
placeholder: infos.placeholder,
|
|
|
|
});
|
|
|
|
|
2017-06-07 12:14:16 +00:00
|
|
|
}
|
2017-06-05 09:34:43 +00:00
|
|
|
else {
|
|
|
|
//Else continue the function as a normal input type
|
2017-06-07 12:14:16 +00:00
|
|
|
labelElem.innerHTML = infos.label;
|
2017-06-05 09:34:43 +00:00
|
|
|
|
|
|
|
//Create input group
|
|
|
|
var inputGroup = createElem("div", formGroup);
|
|
|
|
inputGroup.className = "input-group";
|
|
|
|
inputGroup.style.width = "100%";
|
|
|
|
|
|
|
|
//Create input
|
|
|
|
var input = createElem("input", inputGroup);
|
|
|
|
input.className = "form-control";
|
2017-06-07 12:14:16 +00:00
|
|
|
input.type = infos.type;
|
|
|
|
input.placeholder = infos.placeholder;
|
2017-06-05 09:34:43 +00:00
|
|
|
}
|
2017-06-05 09:02:10 +00:00
|
|
|
|
|
|
|
//Return input
|
|
|
|
return input;
|
2017-06-21 16:38:42 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 18:39:22 +00:00
|
|
|
/**
|
|
|
|
* Create a radio element
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} target The target of the radio
|
|
|
|
* @param {string} name The name of the radio group
|
|
|
|
* @param {string} label The label of the radio
|
|
|
|
* @return {HTMLElement} The input element of the radio
|
|
|
|
*/
|
|
|
|
function create_radio(target, name, label){
|
|
|
|
|
|
|
|
//Contener
|
|
|
|
var radioDiv = createElem2({
|
|
|
|
appendTo: target,
|
|
|
|
type: "div",
|
|
|
|
class: "radio icheck"
|
|
|
|
});
|
|
|
|
|
|
|
|
//Label
|
|
|
|
var radioLabel = createElem2({
|
|
|
|
appendTo: radioDiv,
|
|
|
|
type: "label"
|
|
|
|
});
|
|
|
|
|
|
|
|
//Input
|
|
|
|
var radioInput = createElem2({
|
|
|
|
appendTo: radioLabel,
|
|
|
|
type: "input",
|
|
|
|
name: name,
|
|
|
|
elemType: "radio"
|
|
|
|
});
|
|
|
|
|
|
|
|
//Add label
|
|
|
|
var labelElem = createElem2({
|
|
|
|
appendTo: radioLabel,
|
|
|
|
type: "span",
|
|
|
|
innerHTML: " "+ label
|
|
|
|
});
|
|
|
|
|
|
|
|
//Enable input
|
|
|
|
$(radioInput).iCheck({
|
|
|
|
checkboxClass: 'icheckbox_square-blue',
|
|
|
|
radioClass: 'iradio_square-blue'
|
|
|
|
});
|
|
|
|
|
|
|
|
return radioInput;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-06-21 16:38:42 +00:00
|
|
|
/**
|
|
|
|
* Check if a string is valid and ready to be sent to be saved
|
|
|
|
*
|
|
|
|
* @param {String} value The input string to send
|
|
|
|
* @return {Boolean} True if the string is valid, false else
|
|
|
|
*/
|
|
|
|
function checkString(value){
|
|
|
|
|
|
|
|
//First, check string length
|
2017-06-24 13:07:23 +00:00
|
|
|
if(value.length < 3)
|
2017-06-21 16:38:42 +00:00
|
|
|
return false; //Lenght invalid
|
|
|
|
|
|
|
|
//Success, the string seems to be valid
|
|
|
|
return true;
|
|
|
|
|
2017-07-04 19:13:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove HTML carachters : < and >
|
|
|
|
*
|
|
|
|
* @param {String} input The string to change
|
|
|
|
* @return {String} The updated string
|
|
|
|
*/
|
|
|
|
function removeHtmlTags(input){
|
|
|
|
|
|
|
|
//Prepare update
|
|
|
|
var output = input;
|
|
|
|
|
|
|
|
//Replace opening braces
|
|
|
|
while(output.includes("<")){
|
|
|
|
//Replace an occurence
|
|
|
|
output = output.replace("<", "<");
|
|
|
|
}
|
|
|
|
|
|
|
|
//Replace closing braces
|
|
|
|
while(output.includes(">")){
|
|
|
|
//Replace an occurence
|
|
|
|
output = output.replace(">", ">");
|
|
|
|
}
|
|
|
|
|
|
|
|
//Return result
|
|
|
|
return output;
|
2017-05-21 15:25:41 +00:00
|
|
|
}
|