Friday, May 12, 2017

Generic SharePoint Jquery / Javascript functions -1

Below are few very generic Jquery / Javascript functions which can be used by SharePoint developer in daily activities.

var getFileName = function () {
    var chkVisible = $('[id*="_onetidIOFile"]').is(':visible');
    var filename;

    if (chkVisible) {
        filename = $('[id*="_onetidIOFile"]').val() + $("span[dir='ltr']").text();
    }
    else {
        filename = $("[id*='_onetidIOFile']:eq(1), span[dir='ltr']").text();
    }
    return filename;
}

var setTitleField = function (strTitleVal) {
    $("input[Title='Title']").val(strTitleVal);
}

var hideTitleRow = function () {
    $("input[Title='Title']").closest('tr').hide();
}

var hideInputFieldRow = function (fldName) {
    $("nobr:contains('" + fldName + "')").closest("tr").hide();
}

var btnTextChange = function () {
    //setButtonAvailability(
    //currentElem ,
    //strCondition
    //btnValue

    setButtonAvailability(this, 'This file can be Published to SP', 'Publish File');
}


var setButtonAvailability = function (currElem, strCondition, btnValue) {

    //currentElem ,
    //strCondition
    //btnValue

    if ((currElem.innerText === strCondition) && ($("input[value$='" + btnValue + "']").is(":disabled"))) {
        $("input[value$='" + btnValue + "']").prop('disabled', false);
    }
    else if ((currElem.innerText != strCondition)) {
        $("input[value$='" + btnValue + "']").attr("disabled", "disabled");
    }
}


var changeButtonValue = function (btnID, strNewValue) {
    var btnVal = document.getElementById(btnID).value;
    $("input[value$='" + btnVal + "']").attr('value', strNewValue);
}


var IsValidFileName = function (strFileName) {
    var fileNameAdditionalDots = new RegExp("[(?!\.\w*$)\.]");
    var fileNameSpecialCharacters = new RegExp("[~#%&*{}<>':;?/+|\"]");

    if (strFileName !== '') {
        //if (fileNameSpecialCharacters.test(strFileName) || fileNameAdditionalDots.test(strFileName)) {
        if (fileNameSpecialCharacters.test(strFileName)) {
            alert("Please remove the special characters or additional dots from the file name.");
            return false;
        }
        else {
            return true;
        }
    }
    else {
        alert("File name cannot be empty.");
        return false;
    }
}

var ifSelectionSelected = function (strDocType,strFldName) {
    if (strDocType === '' || strDocType === '(None)') {
        alert('Please select value for field '+strFldName);
        return false;
    }
    else {
        return true;
    }
}

var stringEncripte = function (strToEncript) {
    var xorKey = 129;
    var EncryptedString = '';
    for (i = 0; i < strToEncript.length; ++i) {
        EncryptedString += String.fromCharCode(xorKey ^ strToEncript.charCodeAt(i));
    }
    return EncryptedString;
}

var DisableBtnIfPublished = function (isPublishedFieldID, btnValue) {
    var isPublished = GetField(isPublishedFieldID);
    if (isPublished.checked) {
        $("input[value$='" + btnValue + "']").attr("disabled", "disabled");
    }
    else {
        $("input[value$='" + btnValue + "']").prop('disabled', false);
    }
}

var isElementVisible = function (strControl, strControlText) {
    return $(strControl).filter(function () {
        return $(this).text() === strControlText;
    }).is(':visible');
}

var ToggleButtonBehavior = function () {

    if (isElementVisible('div', 'Please select a Document Type.')) {
        $("input[value$='Publish File']").attr("disabled", "disabled");
    }
    else if (isElementVisible('table', 'This file has failed validation. Please see below for details:')) {
        $("input[value$='Publish File']").attr("disabled", "disabled");
    }
    else if (isElementVisible('table', 'This file has passed column name validation')) {
        var isPublishedFldId = $("input[Title='Is Published']").attr('id');
        DisableBtnIfPublished(isPublishedFldId, 'Publish File');
    }
    else {
        $("input[value$='Publish File']").attr("disabled", "disabled");
        setTimeout(ToggleButtonBehavior, 1000);
    }
    
    try {
        SP.SOD.executeOrDelayUntilScriptLoaded(SP.UI.ModalDialog.get_childDialog().autoSize(), 'sp.ui.dialog.js');
    }
    catch (error) {
        /*console.log('Waiting for File Validation.');*/
    } 
}
Share:

0 comments:

Post a Comment