Friday, May 12, 2017

Generic SharePoint Jquery / Javascript functions - 2

This is another part of SharePoint jQuery / Javascript functions.

//Get element from page with partial element id
//elemIdPart is the second half (after GUID) of the client id of hidden input 

function GetElement(elemIdPart) {
var inputElems = document.getElementsByTagName('input');
for (i = 0; i < inputElems.length; i++) {
if (inputElems[i].getAttribute('id').indexOf(elemIdPart) > -1) {
var inputElement = inputElems[i];
return inputElement;
}
}
}

//Disable element from page with partial element id
//elemIdPart is the second half (after GUID) of the client id of hidden input 

function DisableField(elemIdPart) {
    if (elemIdPart.toString().match("Lookup$") == "Lookup" || elemIdPart.toString().match("Choice$") == "Choice") {
        var selectElems = document.getElementsByTagName('select');
        for (i = 0; i < selectElems.length; i++) {
            if (selectElems[i].getAttribute('id').indexOf(elemIdPart) > -1) {
                var selectElement = selectElems[i];
                selectElement.style.display = "none";

                var newElem = document.createElement("nobr");
                if (selectElement.selectedIndex >= 0 && selectElement.options[selectElement.selectedIndex])
                    newElem.innerHTML = selectElement.options[selectElement.selectedIndex].text;

                selectElement.parentNode.insertBefore(newElem, selectElement);
                break;
            }
        }
    }    
    else if (elemIdPart.toString().match("DateTimeFieldDate$") == "DateTimeFieldDate") {
        var inputElems = document.getElementsByTagName('input');
        for (i = 0; i < inputElems.length; i++) {
            if (inputElems[i].getAttribute('id').indexOf(elemIdPart) > -1) {
                var inputElement = inputElems[i];
                inputElement.style.display = "none";
                inputElement.parentNode.parentNode.parentNode.parentNode.style.display = "none"; // hide Table
                var newElem = document.createElement("nobr");
                newElem.innerHTML = inputElement.value;
                inputElement.parentNode.parentNode.parentNode.parentNode.parentNode.appendChild(newElem);
                break;
            }
        }
    }
    else {
        var thisElem = null;
        var inputElems = document.getElementsByTagName('input');        
        for (i = 0; i < inputElems.length; i++) {
            if (inputElems[i].getAttribute('id').indexOf(elemIdPart) > -1) {
                thisElem = inputElems[i];
                var inputElement = inputElems[i];
                inputElement.style.display = "none";

                var newElem = document.createElement("nobr");
                if (inputElement.type == 'checkbox') {
                    if (inputElement.checked)
                        newElem.innerHTML = 'Yes';
                    else
                        newElem.innerHTML = 'No';
                }
                else {
                    newElem.innerHTML = inputElement.value;
                }
                inputElement.parentNode.insertBefore(newElem, inputElement);
                break;
            }
        }

        if (thisElem == null) { // If type input not found, check in textarea
            var inputElems = document.getElementsByTagName('textarea');
            for (i = 0; i < inputElems.length; i++) {
                if (inputElems[i].getAttribute('id').indexOf(elemIdPart) > -1) {
                    thisElem = inputElems[i];
                    var inputElement = inputElems[i];
                    inputElement.style.display = "none";

                    var newElem = document.createElement("nobr");                   
                    newElem.innerHTML = inputElement.value;
                    inputElement.parentNode.insertBefore(newElem, inputElement);
                    break;
                }
            }
        }
    }

    return 0;
}

// Function to disable lookup fields which have large number of items
function DisableLookUpField(elemIdPart) {
    var inputElems = document.getElementsByTagName('input');

    for (i = 0; i < inputElems.length; i++) {
        if (inputElems[i].getAttribute('id').indexOf(elemIdPart) > -1) {
            var inputElement = inputElems[i];
            inputElement.style.display = "none"; // hide the input element

            var newElem = document.createElement("nobr");
            newElem.innerHTML = inputElement.value;

            inputElement.nextSibling.style.display = 'none'; // hide the drop down arrow image

            inputElement.parentNode.insertBefore(newElem, inputElement);

            break;
        }
    }

    return 0;
}

//Function to disable people/group picker field in Sharepoint.
Where, 
//elemIdPart is the second half (after GUID) of the client id of hidden input Element ().
//This input element is the first child node of , the starting tag for the people picker field.

function DisablePeoplePickerField(elemIdPart) {
    var inputElems = document.getElementsByTagName('input');

    for (i = 0; i < inputElems.length; i++) {
        if (inputElems[i].getAttribute('id').indexOf(elemIdPart) > -1) {
            var inputHiddenElement = inputElems[i];

            /*
            Value of the hidden input element is in the format ##.....
            Split the value using # as the separator, the resulting array will have USERACCOUNT(UID) at it's Odd(Even) positions.
            */
            var useraccounts = inputHiddenElement.value.split('#');
            var strUserAccounts = "";
            for (j = 1; j < useraccounts.length; j += 2) {
                strUserAccounts += useraccounts[j] + " ";
            }

            var newElem = document.createElement("nobar");
            newElem.innerHTML = strUserAccounts;

            inputHiddenElement.parentNode.childNodes[2].style.display = 'none';
            inputHiddenElement.parentNode.insertBefore(newElem, inputHiddenElement);

            break;
        }
    }

    return 0;
}


function DisableCascadingLookupField(FieldIdPart) {
    var selectElems = document.getElementsByTagName('select');

    for (i = 0; i < selectElems.length; i++) {
        if (selectElems[i].getAttribute('id').indexOf(FieldIdPart) > -1) {
            var CascadingFieldElem = selectElems[i];
            var strSelectedVals = "";

            for (i = 0; i < CascadingFieldElem.length; i++) {
                if (CascadingFieldElem.options[i].selected)
                    strSelectedVals += CascadingFieldElem.options[i].text + ";";
            }

            var newElem = document.createElement("nobr");
            newElem.innerHTML = strSelectedVals.substr(0, strSelectedVals.length - 1);

            CascadingFieldElem.style.display = 'none';
            CascadingFieldElem.parentNode.insertBefore(newElem, CascadingFieldElem);

            break;
        }
    }

    return 0;
}


//Function to hide fields in forms
//Parameter fieldLabelName is the name of the field label's element.

function HideFieldInDisplayForm(fieldLabelName) {
    var elems = document.getElementsByName(fieldLabelName);
    if (elems.length > 0) {
        var elemToHide = elems[0];
        while (elemToHide.tagName.toLowerCase() != "tr") {
            elemToHide = elemToHide.parentNode //traverse up the TR element
        }
        elemToHide.style.display = 'none';
    }
}

function HideField(elemIdPart) {
    var el = GetField(elemIdPart);
    if (el) {
        while (el.tagName.toLowerCase() != "tr") {
            el = el.parentNode; // breaks if no "tr" in path to root
        }

        el.style.display = "none";
    }
}

function ShowField(elemIdPart) {
    var el = GetField(elemIdPart);
    if (el) {
        while (el.tagName.toLowerCase() != "tr") {
            el = el.parentNode; // breaks if no "tr" in path to root
        }

        el.style.display = "";
    }
}

function HideDateTimeField(elemIdPart) {
    var el = GetField(elemIdPart);
    if (el) {
        while (el.tagName.toLowerCase() != "tr") {
            el = el.parentNode; // breaks if no "tr" in path to root
        }

        el = el.parentNode;

        while (el.tagName.toLowerCase() != "tr") {
            el = el.parentNode; // breaks if no "tr" in path to root
        }

        el.style.display = "none";
    }
}

function ShowDateTimeField(elemIdPart) {
    var el = GetField(elemIdPart);
    if (el) {
        while (el.tagName.toLowerCase() != "tr") {
            el = el.parentNode; // breaks if no "tr" in path to root
        }

        el = el.parentNode;

        while (el.tagName.toLowerCase() != "tr") {
            el = el.parentNode; // breaks if no "tr" in path to root
        }

        el.style.display = "";
    }
}

function GetField(elemIdPart) {
    var ElementField;
    if (elemIdPart.toString().match("Lookup$") == "Lookup" || elemIdPart.toString().match("Choice$") == "Choice") {
        var selectElems = document.getElementsByTagName('select');
        for (i = 0; i < selectElems.length; i++) {
            if (selectElems[i].getAttribute('id').indexOf(elemIdPart) > -1) {
                ElementField = selectElems[i];
                break;
            }
        }
    }
    if (elemIdPart.toString().match("DropDownChoice$") == "DropDownChoice") {
        var selectElems = document.getElementsByTagName('select');
        for (i = 0; i < selectElems.length; i++) {
            if (selectElems[i].getAttribute('id').indexOf(elemIdPart) > -1) {
                ElementField = selectElems[i];
                break;
            }
        }
    }
    else if (elemIdPart.toString().match("DateTimeFieldDate$") == "DateTimeFieldDate") {
        var inputElems = document.getElementsByTagName('input');
        for (i = 0; i < inputElems.length; i++) {
            if (inputElems[i].getAttribute('id').indexOf(elemIdPart) > -1) {
                ElementField = inputElems[i];
                break;
            }
        }
    }
    else {
        var inputElems = document.getElementsByTagName('input');
        for (i = 0; i < inputElems.length; i++) {
            if (inputElems[i].getAttribute('id').indexOf(elemIdPart) > -1) {
                ElementField = inputElems[i];
                break;
            }
        }
        if (ElementField) return ElementField;
        inputElems = document.getElementsByTagName('textarea');
        for (i = 0; i < inputElems.length; i++) {
            if (inputElems[i].getAttribute('id').indexOf(elemIdPart) > -1) {
                ElementField = inputElems[i];
                break;
            }
        }
    }

    return ElementField;
}

// Cookie related functions 

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

You can use above function like below....

$(document).ready(function () {

var inputDateField = GetElement('ID of the element ... you can provide partial ID'); 
inputDateField.onvaluesetfrompicker = function () {
//do something here in funtion.
var titleField = GetElement('ID of Title Field'); //Title Field
title.value = "Nilesh";
title.parentNode.childNodes[0].innerHTML = "Nilesh";
}

inputDateField.onchange = inputDateField.onvaluesetfrompicker;
});
Share:

0 comments:

Post a Comment