Upgrade to SpacePen 3b85ccfb0ec43, which replace 'attach' events with 'afterAttach' hooks

This commit is contained in:
Nathan Sobo 2012-04-26 11:37:56 -06:00
parent ddc29f294c
commit 64a91148cd
4 changed files with 33 additions and 19 deletions

View File

@ -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)

View File

@ -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()

View File

@ -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()

View File

@ -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]