Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
/**
 * Given an <a href> element, it loads an URL and sets its contents into the target element.
 * If target element is '_blank', sets contents into a jQuery dialog with the width of the
 * element container (if it is bigger than 320px).
 * @param event event that triggers this action.
 * @param element HTMLAnchor element which fires loading and contains href attribute with the desired URL.
 * @param targetId id of element which will hold the result of the request, or '_blank'
 * @param contentId if not null, we will select only a portion of HTML result identified by this id
 **/
function asyncLoad(event, element, targetId, contentId) {
    if (event.preventDefault) {
        event.preventDefault();
    } else {
        event.returnValue = false; // IE
    }
    var url = $(element).prop('href');
    if (contentId && contentId.length > 0) {
        url += ' #' + contentId; // selects an element of the result
    }
    if ('_blank' == targetId) {
        var elementWidth = $(element).parent().width();
        if (elementWidth <= 320) {
            elementWidth = 320;
        }
        // Load URL contents into a new dialog
        var div = $('<div />');
        $(div).load(url, '', function() {
            var dialog = $(div).dialog({
                width: elementWidth,
                height: "400"
            });
            var title = $(div).children().attr('title');
            if (title) {
                $(dialog).dialog('option', {
                    title: title
                });
            }
        });
    } else {
        // Load URL contents into targetId element
        $('#' + targetId).load(url);
    }
}

/**
 * Given a form element inside a jquery-tab widget, switch to the next tab.
 */
function nextTab(event, tabId){
    if (event.keyCode && event.keyCode == 9) { // TAB
        var tabs = $('#' + tabId);
        // show next tab
        var currentTab = tabs.tabs('option', 'selected');
        var tabLength = tabs.tabs('length');
        if (currentTab < tabLength - 1) {
            tabs.tabs("select", currentTab + 1);
            // focus to first element of next tab
            var nextTab = $('#' + tabId).children('div').get(currentTab + 1);
            $(nextTab).find(':input').first().focus();
        }
    }
}

/**
 * Print all object properties, optionally with its values.
 */
function printObject(obj, values) {
    // use console.log
}

/**
 * Given a select element, selects the option provided.
 */
function selectOption(selectId, optionValue) {
    var select = document.getElementById(selectId);
    if (select) {
        for (i = 0; i < select.options.length; i++) {
            if (select.options[i].value == optionValue) {
                select.selectedIndex = i;
            }
        }
        $('#' + selectId).trigger('change');
    }
}

/**
 * Shows a pop-up with a select form. When form is closed, updates result in page.
 * Used in purchaseList.html and invoiceList.html to modify states via AJAX.
 * It must be invoked onclick event like:
 *    <a href="purchaseView.html" onclick="loadFormDialog(event, this, 'purchaseState', 'purchaseState', 'purchaseBox345', 'Varias facturas')">
 *       <span class="PENDING">Pendiente</span>
 *    </a>
 */
function loadFormDialog(event, aElement, targetId, selectName, boxId, severalText) {
    if (event.preventDefault) {
        event.preventDefault();
    } else {
        event.returnValue = false; // IE
    }
    var div = $('<div />');
    var url = $(aElement).attr('href') + ' #' + targetId;
    $(div).load(url, {}, function() {
        var dialog = $(div).dialog({
            width: 600
        });
        $(dialog).find('form').ajaxForm({
            beforeSubmit: function() {
                    if ($(dialog).find('#' + boxId).is(':checked')) {
                        $(aElement).children().text(severalText);
                        $(aElement).prop('onclick', null);
                    } else {
                        var newValue = $(dialog).find('select[name=\"' + selectName + '\"] option:selected').text();
                        $(aElement).children().text(newValue);
                        var newClass = $(dialog).find('select[name=\"' + selectName + '\"] option:selected').val();
                        $(aElement).children().attr('class', newClass);
                    }
                    dialog.dialog('disable');
                },
            success : function() {
                    dialog.dialog('destroy');
                    $(aElement).effect('highlight', {color: '#ffff00'}, 4000);
                }
        });
    });
}

/**
 * Sets a carouFredSel into the element with provided id;
 */
function carousel(carouselId, heightWidthRatio, autoplay) {
    var carouselSelector = '#' + carouselId;
    var carousel = $(carouselSelector).carouFredSel({
        width: "100%",
        responsive: true,
        items: {
            visible: 1,
            height: heightWidthRatio + "%"
        },
        scroll: {
            duration: 500,
            fx: "crossfade" // "none", "scroll", "directscroll", "fade", "crossfade", "cover" or "uncover".
        },
        auto: {
            delay: 10000,
            timeoutDuration: 10000,
            button: "#carousel-play"
        },
        prev: {
            button: "#carousel-prev",
            key: "left"
        },
        next: {
            button: "#carousel-next",
            key: "right"
        },
        swipe: {
            onMouse: true,
            onTouch: true
        },
        pagination: "#carousel-pagination"
    });
    // Start paused
    if(!autoplay) {
        carousel.trigger('pause');
    }
    // Pause with ESC key
    $(document).keydown(function(event) {
        if (event.which == 27) { // ESC key
            carousel.trigger('pause');
        }
    });
}

// FIXME Move to an external file
/** Share on facebook */
function shareOnFacebook(link, pictureURL, name, caption, description) {

    var obj = {
        method: 'feed',
        link: link,
        picture: pictureURL,
        name: name,
        caption: caption,
        description: description
    };

    function callback(response) {
        if (response) {
            if (response.post_id) {
                $( "#facebook-share-done" ).dialog( "open" );
            }
        } else {
            $( "#facebook-share-fail" ).dialog( "open" );
        }
    }

    FB.ui(obj, callback);
}
New to GrepCode? Check out our FAQ X