ComunicWeb/assets/js/common/utils.js

125 lines
2.9 KiB
JavaScript
Raw Normal View History

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
}
/**
* Get an HTML element specified by an ID
*
* @param {String} nodeName The ID of the element
* @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
}
/**
* 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
var children = container.childNodes;
//Process each child
for(i in children){
if(children[i].remove)
children[i].remove();
}
//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
*
* @param {HTMLElement} target The target of the field
* @param {String} label The label of the field
* @param {String} placeholder The placeholder of the field (for checkbox:
* defines if the box has to be checked by default)
2017-06-05 09:02:10 +00:00
* @param {String} type The type of the field
* @return {HTMLElement} The input
*/
function createFormGroup(target, label, placeholder, type){
//Create formgroup
var formGroup = createElem("div", target);
formGroup.className = "form-group";
//Add label
var labelElem = createElem("label", formGroup);
//Treatement differs if it is a checkbox
if(type == "checkbox"){
2017-06-05 09:02:10 +00:00
//Create checkbox
var input = createElem("input", labelElem) ;
input.type = "checkbox";
2017-06-05 09:02:10 +00:00
//Check if input has to be checked by default
if(placeholder){
if(placeholder === "true"){
input.checked = "true";
}
}
//Add label value
var labelValue = createElem("span", labelElem);
labelValue.innerHTML = " "+label;
//Enable iCheck
$(input).iCheck({
checkboxClass: 'icheckbox_flat-blue',
radioClass: 'iradio_flat-blue'
});
}
else {
//Else continue the function as a normal input type
labelElem.innerHTML = label;
//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";
input.type = type;
input.placeholder = placeholder;
}
2017-06-05 09:02:10 +00:00
//Return input
return input;
2017-05-21 15:25:41 +00:00
}