1 // ========================================================================== 2 // Project: SproutCore - JavaScript Application Framework 3 // Copyright: ©2008-2011 Apple Inc. All rights reserved. 4 // License: Licensed under MIT license (see license.js) 5 // ========================================================================== 6 7 /** 8 @namespace 9 10 This is the default InlineEditorDelegate for SC.LabelView. The default editor 11 is an SC.InlineTextFieldView. 12 13 Only one editor is allowed to be active at a time. If another view requests an 14 editor while an editor is already active, the delegate will first attempt to 15 commit the existing editor, then discard it if commit fails, and fail to 16 acquire if the active editor could not be discarded. 17 18 Each time an editor is required, it instantiates it and appends it to the same 19 parentView as the view being edited. The editor is responsible for positioning 20 itself correctly in its beginEditing method. 21 22 @since SproutCore 1.0 23 */ 24 SC.InlineTextFieldDelegate = /** @scope SC.InlineTextFieldDelegate */{ 25 26 /** 27 The current shared inline editor. 28 29 @type SC.InlineTextFieldView 30 */ 31 editor: null, 32 33 /** 34 If an editor is currently active, dismisses it by first attempting to commit 35 and if that fails attempting to dismiss. If that fails, the acquire fails 36 and returns null. 37 38 Otherwise, it creates the editor as a child of the client view's parentView 39 and returns it. 40 41 The default editor is an SC.InlineTextFieldView. The client view may 42 customize this by setting a different inlineEditor as its exampleEditor 43 property. 44 45 @param {SC.InlineEditable} label the label that is requesting an editor 46 @returns {SC.InlineEditor} the editor the label should use to edit 47 */ 48 acquireEditor: function (label) { 49 var editor = this.editor; 50 51 if (editor) { 52 // attempt to end editing on the previous editor and return null if unable 53 // to end editing successfully 54 if (editor.get('isEditing') && !editor.commitEditing() && !editor.discardEditing()) return null; 55 56 // now release it 57 this.releaseEditor(editor); 58 } 59 60 // default to SC.InlineTextFieldView 61 var exampleEditor = label.exampleEditor ? label.exampleEditor : SC.InlineTextFieldView, 62 parentView = label.get('parentView'); 63 64 // set ourself as the delegate for the editor 65 editor = this.editor = parentView.createChildView(exampleEditor, { 66 inlineEditorDelegate: this 67 }); 68 69 parentView.appendChild(editor); 70 71 return editor; 72 }, 73 74 /** 75 Cleans up the given editor by simply destroying it, which removes it from 76 the view hierarchy. The client view should null any references to the editor 77 so it may be garbage collected. 78 79 @params {SC.InlineEditor} editor the editor that should be cleaned up 80 @returns {Boolean} whether the cleanup succeeded 81 */ 82 releaseEditor: function (editor) { 83 editor.destroy(); 84 85 this.editor = null; 86 87 return YES; 88 } 89 }; 90 91