Richard Livsey
2005-Aug-21 16:48 UTC
[Rails-spinoffs] [PATCH] in-place-editor, AJAX loading
I sent this on Friday but somehow managed to email it to the wrong email address! ---- Attached is a patch to add external (AJAX) loading of content to the InPlaceEditor. 2 extra options have been added: loadUrl: a URL to load the content from. If this is present, it will be used instead of the innerHTML. fieldId: I had to add this so we know what the text field is when we''ve loaded the content. It defaults to ''inplaceeditor-field'', so if you have 2 on a page this will be problematic. Could append a random value by default? Any ideas on the best implementation for this? Tested in IE and Moz. Not on a mac so can''t test safari. Thanks. Let me know if there are any problems or ideas :o) -- R.Livsey http://livsey.org -------------- next part -------------- Index: controls.js ==================================================================--- controls.js (revision 2032) +++ controls.js (working copy) @@ -482,7 +482,9 @@ return Form.serialize(form); }, hoverClassName: ''inplaceeditor-hover'', - externalControl: null + externalControl: null, + loadUrl: null, + fieldId: ''inplaceeditor-field'' }, options || {}); if(!this.options.formId && this.element.id) { @@ -550,6 +552,7 @@ textField.type = "text"; textField.name = "value"; textField.value = this.getText(); + textField.id = this.options.fieldId; var size = this.options.size || this.options.cols || 0; if (size != 0) textField.size = size; @@ -562,13 +565,34 @@ textArea.value = this.getText(); textArea.rows = this.options.rows; textArea.cols = this.options.cols || 40; + textArea.id = this.options.fieldId; form.appendChild(textArea); this.editField = textArea; } }, getText: function() { - return this.element.innerHTML; + if (this.options.loadUrl == null) + { + return this.element.innerHTML; + } + else + { + this.loadExternalText(); + return ''loading...''; + } }, + loadExternalText: function() { + new Ajax.Request( + this.options.loadUrl, + { + asynchronous: true, + onComplete: this.onLoadedExternalText.bind(this) + } + ); + }, + onLoadedExternalText: function(t) { + $(this.options.fieldId).value = t.responseText; + }, onclickCancel: function() { this.onComplete(); },