[jQuery] [PLUGIN] editInPlace with checkboxes and selectBoxes

[jQuery] [PLUGIN] editInPlace with checkboxes and selectBoxes

<span style="font-family: courier new,monospace;"><span style="font-family: arial,sans-serif;">I will document this later, but suffice it to say that you can call the editInPlace function with an AJAX callback for successful saves (say, to update the page after a save is completed),  a typeOfBox (which can be "text", "checkbox", or "select" but defaults to "text"), as well as a dataUrl, which provides:
</span></span><ul><li>blank or 'checked="checked"' for checkboxes</li><li>a <select> list containing a list of options for select lists</li></ul>You can provide all sorts of params in the dataUrl, so you could pass in the ID of the record in the database, etc. The only constraint is that the server must return an html fragment matching the appropriate type or wonkiness will ensue. Make sure that the server returns an appropriate response on save, because that response will be used to update the editInPlace box.
There's a quick animation (fade to half, fade in to 100%) that takes under a second to complete to indicate success.
The code follows:
---
<span style="font-family: courier new,monospace;">
var typeList = {};
</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">jQuery.fn.editInPlace = function(url, ajaxCallback, typeOfBox, dataUrl) {
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    this.each(function() { typeList[<a href="http://this.id">this.id</a>] = typeOfBox || "text"; })</span>
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    var data;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    if(dataUrl)
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        $.ajax({url: dataUrl, success: function(res) { data = res.responseText } });</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    editInPlaceClick = function() {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        type = typeList[<a href="http://this.id">
this.id</a>] || "text";</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        buttons = "<p class=\"buttons\"><input type=\"button\" value=\"SAVE\" class=\"saveButton\" />" +
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">            " or <input type=\"button\" value=\"CANCEL\" class=\"cancelButton\" />";
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        if(type == "text")</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
            var ta = "

<input type=\"text\" value = \"" + this.innerHTML + "\" />

" + buttons;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
        if(type == "checkbox")</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">            var ta = "

<input class=\"checkbox\" type=\"checkbox\" " + data + " />

" + buttons;
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        if(type == "select")</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
            var ta = data + buttons;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        var revert = this.innerHTML;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        this.innerHTML = ta;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        $(this).removeClickable();</span>
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        var self = this;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
        $(".saveButton").click(function(e){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">            editable = this.parentNode.parentNode;    </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">            if(type == "text")</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">                value = $(editable).find("input[@type='" + type + "']").val();
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">            else if(type == "checkbox")</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
                value = $(editable).find("input[@type='" + type + "']")[0].checked;            </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">            else if(type == "select")
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">                value = $(editable).find("select").val();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">            $.ajax({</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">                url: url,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">                type: "POST",</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">                data: "value=" + value + "&id=" +
<a href="http://editable.id">editable.id</a>,</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">                complete: function(response) {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">                    $(editable).empty().html(response.responseText);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
                    $(editable).addBackClickable(type);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">                    $(editable).animate({opacity: .5}, "normal", function() {$(editable).animate({opacity: 1}, "normal")});
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">                    if(ajaxCallback && ajaxCallback.constructor == Function) {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">                        ajaxCallback(editable, response);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">                    }
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">                    $("script", self).each(function(){</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">                        eval( this.text || this.textContent || this.innerHTML || "");</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
                    }).remove();                    </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">                }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">           })</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">            e.stopPropagation();            </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        });</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        $(".cancelButton").click(function(e){
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">            editable = this.parentNode.parentNode;                                                                               
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">            $(editable).addBackClickable(type); editable.innerHTML = revert;  </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">            e.stopPropagation();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        });</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    }</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    var clickFunction = editInPlaceClick;
</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    jQuery.fn.removeClickable = function() {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        this.removeClass("editableInPlace").removeClass("editable");</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
        this.unclick();        </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        this.unmouseover().unmouseout();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
    jQuery.fn.addBackClickable = function(editType) {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        this.editInPlace(url, ajaxCallback, editType, dataUrl);       
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    </span>
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    return this.click(clickFunction)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
    .hover(function() {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        $(this).addClass("editableInPlace");                                    </span>
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    }, function() {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
        $(this).removeClass("editableInPlace");</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    });    </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">}</span><br clear="all">
--
Yehuda Katz
Web Developer
(ph)  718.877.1325
(fax) 718.686.4288
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/