diff --git a/src/app/cursor.coffee b/src/app/cursor.coffee index 0ace75a49..25689f589 100644 --- a/src/app/cursor.coffee +++ b/src/app/cursor.coffee @@ -17,9 +17,11 @@ class Cursor extends View @editor = editor @anchor = new Anchor(@editor, screenPosition) @selection = @editor.compositeSelection.addSelectionForCursor(this) - @one 'attach', => - @updateAppearance() - @editor.syncCursorAnimations() + + afterAttach: (onDom) -> + return unless onDom + @updateAppearance() + @editor.syncCursorAnimations() handleBufferChange: (e) -> @anchor.handleBufferChange(e) diff --git a/src/app/editor.coffee b/src/app/editor.coffee index df4a352bb..8b6219d6f 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -208,14 +208,15 @@ class Editor extends View else @gutter.addClass('drop-shadow') - @on 'attach', (e) => - return if @attached - @attached = true - @calculateDimensions() - @hiddenInput.width(@charWidth) - @setMaxLineLength() if @softWrap - @focus() if @isFocused - @trigger 'editor-open', [this] + + afterAttach: (onDom) -> + return if @attached or not onDom + @attached = true + @calculateDimensions() + @hiddenInput.width(@charWidth) + @setMaxLineLength() if @softWrap + @focus() if @isFocused + @trigger 'editor-open', [this] rootView: -> @parents('#root-view').view() diff --git a/src/app/root-view.coffee b/src/app/root-view.coffee index b39a240cd..e77ab3171 100644 --- a/src/app/root-view.coffee +++ b/src/app/root-view.coffee @@ -26,7 +26,6 @@ class RootView extends View initialize: ({ pathToOpen, projectPath, panesViewState }) -> @on 'toggle-file-finder', => @toggleFileFinder() @on 'show-console', => window.showConsole() - @one 'attach', => @focus() @on 'focus', (e) => if @activeEditor() @activeEditor().focus() @@ -50,6 +49,9 @@ class RootView extends View @deserializePanes(panesViewState) if panesViewState + afterAttach: (onDom) -> + @focus() if onDom + serialize: -> projectPath: @project?.path panesViewState: @serializePanes() diff --git a/vendor/space-pen.coffee b/vendor/space-pen.coffee index bcbb3e811..dd2297770 100644 --- a/vendor/space-pen.coffee +++ b/vendor/space-pen.coffee @@ -62,10 +62,11 @@ class View extends jQuery [html, postProcessingSteps] = @constructor.buildHtml -> @content(args...) jQuery.fn.init.call(this, html) @constructor = jQuery # sadly, jQuery assumes this.constructor == jQuery in pushStack + throw new Error("View markup must have a single root element") if this.length != 1 @wireOutlets(this) @bindEventHandlers(this) @find('*').andSelf().data('view', this) - @attr('triggerAttachEvents', true) + @attr('callAttachHooks', true) step(this) for step in postProcessingSteps @initialize?(args...) @@ -165,9 +166,18 @@ class Builder jQuery.fn.view = -> this.data('view') # Trigger attach event when views are added to the DOM -triggerAttachEvent = (element) -> - if element?.attr?('triggerAttachEvents') and element.parents('html').length - element.find('[triggerAttachEvents]').add(element).trigger('attach') +callAttachHook = (element) -> + return unless element + onDom = element.parents?('html').length > 0 + + elementsWithHooks = [] + elementsWithHooks.push(element[0]) if element.attr?('callAttachHooks') + elementsWithHooks = elementsWithHooks.concat(element.find?('[callAttachHooks]').toArray() ? []) if onDom + + parent = element + for element in elementsWithHooks + view = $(element).view() + $(element).view()?.afterAttach?(onDom) for methodName in ['append', 'prepend', 'after', 'before'] do (methodName) -> @@ -175,7 +185,7 @@ for methodName in ['append', 'prepend', 'after', 'before'] jQuery.fn[methodName] = (args...) -> flatArgs = [].concat args... result = originalMethod.apply(this, flatArgs) - triggerAttachEvent arg for arg in flatArgs + callAttachHook arg for arg in flatArgs result for methodName in ['prependTo', 'appendTo', 'insertAfter', 'insertBefore'] @@ -183,10 +193,9 @@ for methodName in ['prependTo', 'appendTo', 'insertAfter', 'insertBefore'] originalMethod = $.fn[methodName] jQuery.fn[methodName] = (args...) -> result = originalMethod.apply(this, args) - triggerAttachEvent(this) + callAttachHook(this) result (exports ? this).View = View (exports ? this).$$ = (fn) -> View.render.call(View, fn) (exports ? this).$$$ = (fn) -> View.buildHtml.call(View, fn)[0] -