Search

Friday, May 21, 2010

Collapsable CRM section

attachCollapsableToSections();

function getElementsByCondition(condition, container) {
    container = container || document;
    var all = container.all || container.getElementsByTagName('*');
    var arr = [];
    for (var k = 0; k < all.length; k++) {
        var elm = all[k];
        if (condition(elm, k)) arr[arr.length] = elm;
    } return arr;
}

function attachCollapsableToSections() { 
    var sections = getElementsByCondition(function(elm) {
        if (elm.className.indexOf("ms-crm-Form-Section") != -1)
            return true;
    }, null); 
    for (var i = 0; i < sections.length; i++) {
        sections[i].innerHTML = '<img  id="' + i + '_imgid" src="/_imgs/navup.gif" alt="Expanded, click to collapse"  style="cursor:hand;"/> <label id="' + i + '_lblid" />' + sections[i].innerHTML;
        sections[i].childNodes[0].attachEvent('onclick', toggleVisibility);
        sections[i].childNodes[2].attachEvent('onclick', toggleVisibility);       
    }
} 
function toggleVisibility(e) { 
    var sectionContainer = e.srcElement.parentNode.parentNode.parentNode;
    var elements = getElementsByCondition(function(elm) {
        if (elm.vAlign == "top") return true;
    }, sectionContainer);
    for (var i = 0; i < elements.length; i++) {
        if (elements[i].style.display == "none") {
            elements[i].style.display = "";
            if (e.srcElement.id.indexOf("_lblid") != -1) {
                var _imgid;
                _imgid = e.srcElement.id.replace(/(^\d+)(.+$)/i, '$1') + "_imgid";
                document.getElementById(_imgid).src = document.getElementById(_imgid).src.replace("navdown", "navup");
            }
            else {
                e.srcElement.src = e.srcElement.src.replace("navdown", "navup");
            }
        } else {
            elements[i].style.display = "none";
            if (e.srcElement.id.indexOf("_lblid") != -1) {
                var _imgid;
                _imgid = e.srcElement.id.replace(/(^\d+)(.+$)/i, '$1') + "_imgid";
                document.getElementById(_imgid).src = document.getElementById(_imgid).src.replace("navup", "navdown");
            }
            else {
                e.srcElement.src = e.srcElement.src.replace("navup", "navdown");
            }
        }
    }
}

No comments:

Post a Comment