1
\$\begingroup\$

Is there a better way to write this?

function(content,redirectUrl,customButtons, headerMsg){
    var $popUp = $("<div/>").popup({
        theme:"a",
        dismissible: false,
        overlayTheme: "b",
        transition: "pop"
    }).on("popupafterclose", function (){
        //remove the popup when closing
        $(this).remove();
    }).css({
        'width':  $(window).width()*80/100,
        'font-size':'0px'
    });
    if(headerMsg)
        header = $('<div class="left f-width pad-top-bottom bold-font black-font center-text mid-font">'+headerMsg+'</div>').appendTo($popUp)
    if(!content)
        content = $("#generalErr").val() || 'An error occurred during processing, please try again after some time';            

    //create a message for the popup
    $("<p/>", {
        html: content
    }).addClass("mid-font").appendTo($popUp);

    if(customButtons){
        var popUpWidth = $(window).width()*80/100,
        buttonWidth = popUpWidth/customButtons.length;
        $.each(customButtons, function(i,button){
            addButtons($popUp,button.text,'',button.action, buttonWidth);
        });
        $popUp.find("a:last-child").css({
            'border-right': '0px'
        });
    } else {
        addButtons($popUp,$("#ok").val() || 'Ok',redirectUrl);
    }
    $popUp.popup('open').trigger("create");
}

function addButtons(popUpObj,text,redirectUrl,buttonAction,buttonWidth){
    var button = $("<a>", {
        text: text
    }).on('click',function(){
        if(buttonAction){
            buttonAction(popUpObj);
        } else {
            if(redirectUrl){
               window.location.href = redirectUrl;
            }
            popUpObj.popup("close");
        }
    }).buttonMarkup({
        inline: false,
        mini: false,
        theme: "a"
    });
    if(buttonAction){
        button.css({
           'width':buttonWidth-1,
           'display':'inline-block',
           'margin-bottom':'0px',
           'padding-left': '0px',
           'padding-right':'0px'
        }).addClass("popup_button_border");
    }
    button.appendTo(popUpObj);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.