Created
May 18, 2011 00:04
-
-
Save statianzo/977710 to your computer and use it in GitHub Desktop.
Editable regions in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//BEGIN SerializeAnything | |
/* @projectDescription jQuery Serialize Anything - Serialize anything (and not just forms!) | |
* @author Bramus! (Bram Van Damme) | |
* @version 1.0 | |
* @website: http://www.bram.us/ | |
* @license : BSD | |
*/ | |
(function($) { | |
$.fn.serializeAnything = function() { | |
var toReturn = []; | |
var els = $(this).find(':input').get(); | |
$.each(els, function() { | |
if (this.name && !this.disabled && (this.checked || /select|textarea/i.test(this.nodeName) || /text|hidden|password/i.test(this.type))) { | |
var val = $(this).val(); | |
toReturn.push(encodeURIComponent(this.name) + "=" + encodeURIComponent(val)); | |
} | |
}); | |
return toReturn.join("&").replace(/%20/g, "+"); | |
}; | |
})(jQuery); | |
//END SerializeAnything | |
jQuery.expr[':'].ignoreCaseId = function(objNode, intStackIndex, arrProperties) { | |
return objNode.id.toLowerCase() == arrProperties[3].toLowerCase(); | |
}; | |
(function($) { | |
$.fn.editRegion = function(editTemplate, target, options) { | |
var template = $(editTemplate).hide(); | |
var settings = $.extend({}, $.fn.editRegion.defaults, options); | |
this.each(function() { | |
var instanceTemplate = $(template).clone().removeAttr('class'); | |
var self = $(this).after(instanceTemplate); | |
$(settings.submitId, $(instanceTemplate)).click(function() { | |
$(self).trigger('submit'); | |
}); | |
$(settings.cancelId, $(instanceTemplate)).click(function() { | |
$(self).trigger('deactivate'); | |
}); | |
$(settings.editId, $(self)).click(function() { | |
$(self).trigger('activate'); | |
}); | |
this.isActivated = false; | |
this.activate = function() { | |
if (this.isActivated) { | |
return; | |
} | |
this.isActivated = true; | |
$(self).hide(); | |
$(instanceTemplate).show(); | |
$(':input[name]', $(instanceTemplate)).each(function() { | |
var name = $(this).attr('name'); | |
var field = $('#' + name, $(self)); | |
$(this).val(field.html()); | |
}); | |
}; | |
this.deactivate = function() { | |
if (!this.isActivated) { | |
return false; | |
} | |
this.isActivated = false; | |
$(self).show(); | |
$(instanceTemplate).hide(); | |
return false; | |
}; | |
this.submit = function() { | |
$(instanceTemplate).hide() | |
var loadingContent; | |
if (settings.loadingImage) { | |
loadingContent = $('<img src="' + settings.loadingImage + '"/>'); | |
} | |
else if (settings.loadingText) { | |
loadingContent = $('<div>' + settings.loadingText + '</div>'); | |
} | |
if (loadingContent) $(self).after(loadingContent); | |
var data = $(instanceTemplate).serializeAnything(); | |
$.ajax({ | |
type: 'POST', | |
url: target, | |
data: data, | |
dataType: 'json', | |
success: function(data, textStatus) { | |
$(self).trigger('deactivate'); | |
$(loadingContent).remove(); | |
for (var key in data) { | |
if (data.hasOwnProperty(key)) { | |
$(":ignoreCaseId(" + key + ")", $(self)).html(data[key]); | |
} | |
} | |
}, | |
error: function(XmlHttpRequest, textStatus, errorThrown) { | |
$(self).trigger('deactivate'); | |
$(loadingContent).remove(); | |
} | |
}); | |
return false; | |
}; | |
$(self).bind('activate', this.activate).bind('deactivate', this.deactivate).bind('submit', this.submit); | |
}); | |
return this; | |
}; | |
$.fn.editRegion.defaults = { | |
submitId: '#submit', | |
cancelId: '#cancel', | |
editId: '#edit', | |
loadingText: 'Loading...' | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment