YUI.add('textedit', function(Y) { var Node = Y.Node; function TextEdit(config) { TextEdit.superclass.constructor.apply(this, arguments); } TextEdit.NAME = "TextEdit"; TextEdit.NS = "TextEdit"; TextEdit.ATTRS = { text: { value: null }, inputLabel: { value: "Title" }, infoText: { value: "click to edit title" }, target: { value: null }, field: { value: null }, annotation: { value: null }, store: { value: null } }; Y.extend(TextEdit, Y.Plugin.Base, { initializer: function(args) { var parentNode = this.get("host"), text = this.get("text"); parentNode.addClass("textedit"); var textNode = parentNode.appendChild(Node.create('
')), infoNode = parentNode.appendChild(Node.create('
'+ this.get("infoText")+'
')) inputNode = parentNode.appendChild(Node.create('
')), inputLabel = inputNode.appendChild(Node.create('
' +this.get('inputLabel')+'
')), inputField = inputNode.appendChild(Node.create('')), cancelButton = inputNode.appendChild(Node.create('')); saveButton = inputNode.appendChild(Node.create('')); this._setText({newVal:text}); this.on("textChange", this._setText, this); textNode.on("click", this._enableEdit, this); cancelButton.on("click", function() {this._setText({newVal:this.get("text")})}, this); saveButton.on("click", this._submitText, this); }, _setText : function(e) { var host = this.get("host"), text = e.newVal; if(text) { host.one('.text').setContent(text); host.removeClass("edit"); } else { this._enableEdit(); } }, _enableEdit : function(e) { var host = this.get("host"), text = this.get("text"), input = host.one('textarea'); input.set("value", text); host.addClass("edit"); input.focus(); input.select(); }, _submitText : function(e) { var oSelf = this, oldText = this.get("text"), newText = this.get("host").one('textarea').get("value"), updateService = this.get("store").update; var data = { target:this.get("target"), field:this.get("field"), body:Y.JSON.stringify({value:newText, type:"literal"}) }; if(this.get("annotation")) { data.annotation = this.get("annotation"); } if(oldText!==newText) { Y.io(updateService, { data:data, on:{success: function(e,o) { var r = Y.JSON.parse(o.responseText); oSelf.set("annotation", r.annotation); oSelf.set("text", newText) } } }); } else { this.get("host").removeClass("edit"); } } }); Y.Plugin.TextEdit = TextEdit; }, '0.0.1', { requires: [ 'node','event','plugin','io-base','json','querystring-stringify-simple' ] });