Skip to content

Commit

Permalink
Implement removing unused elements in stand-alone minimap
Browse files Browse the repository at this point in the history
  • Loading branch information
abe33 committed Oct 30, 2015
1 parent b514baf commit 72e7a90
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 46 deletions.
92 changes: 67 additions & 25 deletions lib/minimap-element.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ class MinimapElement extends HTMLElement
@updateMinimapFlexPosition()

'minimap.minimapScrollIndicator': (@minimapScrollIndicator) =>
if @minimapScrollIndicator and not @scrollIndicator?
if @minimapScrollIndicator and not @scrollIndicator? and not @standAlone
@initializeScrollIndicator()
else if @scrollIndicator?
@disposeScrollIndicator()

@requestUpdate() if @attached

'minimap.displayPluginsControls': (@displayPluginsControls) =>
if @displayPluginsControls and not @openQuickSettings?
if @displayPluginsControls and not @openQuickSettings? and not @standAlone
@initializeOpenQuickSettings()
else if @openQuickSettings?
@disposeOpenQuickSettings()
Expand Down Expand Up @@ -162,46 +162,73 @@ class MinimapElement extends HTMLElement

@shadowRoot.appendChild(@canvas)

@createVisibleArea()
@createControls()

@subscriptions.add @subscribeTo this,
'mousewheel': (e) => @relayMousewheelEvent(e)

@subscriptions.add @subscribeTo @canvas,
'mousedown': (e) => @mousePressedOverCanvas(e)

# Initializes the visible area div.
createVisibleArea: ->
return if @visibleArea?

@visibleArea = document.createElement('div')
@visibleArea.classList.add('minimap-visible-area')
@shadowRoot.appendChild(@visibleArea)

@visibleAreaSubscription = @subscribeTo @visibleArea,
'mousedown': (e) => @startDrag(e)
'touchstart': (e) => @startDrag(e)

@subscriptions.add(@visibleAreaSubscription)

# Removes the visible area div.
removeVisibleArea: ->
return unless @visibleArea?

@subscriptions.remove(@visibleAreaSubscription)
@visibleAreaSubscription.dispose()
@shadowRoot.removeChild(@visibleArea)
delete @visibleArea

# Creates the controls container div.
createControls: ->
return if @controls? or @standAlone

@controls = document.createElement('div')
@controls.classList.add('minimap-controls')
@shadowRoot.appendChild(@controls)

elementMousewheel = (e) => @relayMousewheelEvent(e)
canvasMousedown = (e) => @mousePressedOverCanvas(e)
visibleAreaMousedown = (e) => @startDrag(e)
removeControls: ->
return unless @controls?

@addEventListener 'mousewheel', elementMousewheel
@canvas.addEventListener 'mousedown', canvasMousedown
@visibleArea.addEventListener 'mousedown', visibleAreaMousedown
@visibleArea.addEventListener 'touchstart', visibleAreaMousedown

@subscriptions.add new Disposable =>
@removeEventListener 'mousewheel', elementMousewheel
@canvas.removeEventListener 'mousedown', canvasMousedown
@visibleArea.removeEventListener 'mousedown', visibleAreaMousedown
@visibleArea.removeEventListener 'touchstart', visibleAreaMousedown
@shadowRoot.removeChild(@controls)
delete @controls

# Initializes the scroll indicator div when the `minimapScrollIndicator`
# settings is enabled.
initializeScrollIndicator: ->
return if @scrollIndicator? or @standAlone

@scrollIndicator = document.createElement('div')
@scrollIndicator.classList.add 'minimap-scroll-indicator'
@controls.appendChild(@scrollIndicator)

# Disposes the scroll indicator div when the `minimapScrollIndicator`
# settings is disabled.
disposeScrollIndicator: ->
return unless @scrollIndicator?

@controls.removeChild(@scrollIndicator)
@scrollIndicator = undefined
delete @scrollIndicator

# Initializes the quick settings openener div when the
# `displayPluginsControls` setting is enabled.
initializeOpenQuickSettings: ->
return if @openQuickSettings?
return if @openQuickSettings? or @standAlone

@openQuickSettings = document.createElement('div')
@openQuickSettings.classList.add 'open-minimap-quick-settings'
Expand Down Expand Up @@ -291,21 +318,36 @@ class MinimapElement extends HTMLElement
@subscriptions.add @minimap.onDidDestroy => @destroy()
@subscriptions.add @minimap.onDidChangeConfig =>
@requestForcedUpdate() if @attached

@subscriptions.add @minimap.onDidChangeStandAlone =>
if @minimap.isStandAlone()
@setAttribute('stand-alone', true)
else
@removeAttribute('stand-alone')
@setStandAlone(@minimap.isStandAlone())
@requestUpdate()

@subscriptions.add @minimap.onDidChange (change) =>
@pendingChanges.push(change)
@requestUpdate()

@setAttribute('stand-alone', true) if @minimap.isStandAlone()
@setStandAlone(@minimap.isStandAlone())

@minimap.setScreenHeightAndWidth(@height, @width) if @width? and @height?

@minimap

setStandAlone: (@standAlone) ->
if @standAlone
@setAttribute('stand-alone', true)
@disposeScrollIndicator()
@disposeOpenQuickSettings()
@removeControls()
@removeVisibleArea()

else
@removeAttribute('stand-alone')
@createVisibleArea()
@createControls()
@initializeScrollIndicator() if @minimapScrollIndicator
@initializeOpenQuickSettings() if @displayPluginsControls

# ## ## ######## ######## ### ######## ########
# ## ## ## ## ## ## ## ## ## ##
# ## ## ## ## ## ## ## ## ## ##
Expand Down Expand Up @@ -596,10 +638,10 @@ class MinimapElement extends HTMLElement
# styles - An {Object} where the keys are the properties name and the values
# are the CSS values for theses properties.
applyStyles: (element, styles) ->
cssText = ''
return unless element?

for property,value of styles
cssText += "#{property}: #{value}; "
cssText = ''
cssText += "#{property}: #{value}; " for property,value of styles

element.style.cssText = cssText

Expand Down
52 changes: 31 additions & 21 deletions spec/minimap-element-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -658,36 +658,33 @@ describe 'MinimapElement', ->
expect(minimap.width).toEqual(minimapElement.clientWidth)
expect(minimap.height).toEqual(minimapElement.clientHeight)

it 'does not display the visible area', ->
waitsFor -> nextAnimationFrame isnt noAnimationFrame
runs ->
nextAnimationFrame()
expect(isVisible(minimapElement.visibleArea)).toBeFalsy()
it 'removes the controls div', ->
expect(minimapElement.shadowRoot.querySelector('.minimap-controls')).toBeNull()

it 'removes the visible area', ->
expect(minimapElement.visibleArea).toBeUndefined()

it 'does not display the quick settings button', ->
it 'removes the quick settings button', ->
atom.config.set 'minimap.displayPluginsControls', true

waitsFor -> nextAnimationFrame isnt noAnimationFrame
runs ->
nextAnimationFrame()
expect(isVisible(minimapElement.openQuickSettings)).toBeFalsy()

describe 'when minimap.minimapScrollIndicator setting is true', ->
beforeEach ->
editor.setText(mediumSample)
editorElement.setScrollTop(50)
expect(minimapElement.openQuickSettings).toBeUndefined()

waitsFor -> minimapElement.frameRequested
runs ->
nextAnimationFrame()
atom.config.set 'minimap.minimapScrollIndicator', true
it 'removes the scroll indicator', ->
editor.setText(mediumSample)
editorElement.setScrollTop(50)

waitsFor -> minimapElement.frameRequested
runs -> nextAnimationFrame()
waitsFor -> minimapElement.frameRequested
runs ->
nextAnimationFrame()
atom.config.set 'minimap.minimapScrollIndicator', true

it 'offsets the scroll indicator by the difference', ->
indicator = minimapElement.shadowRoot.querySelector('.minimap-scroll-indicator')
expect(realOffsetLeft(indicator)).toBeCloseTo(16, -1)
waitsFor -> minimapElement.frameRequested
runs ->
nextAnimationFrame()
expect(minimapElement.shadowRoot.querySelector('.minimap-scroll-indicator')).toBeNull()

describe 'pressing the mouse on the minimap canvas', ->
beforeEach ->
Expand All @@ -705,6 +702,19 @@ describe 'MinimapElement', ->
it 'does not scroll the editor to the line below the mouse', ->
expect(editorElement.getScrollTop()).toEqual(1000)

describe 'and is changed to be a classical minimap again', ->
beforeEach ->
atom.config.set 'minimap.displayPluginsControls', true
atom.config.set 'minimap.minimapScrollIndicator', true

minimap.setStandAlone(false)

it 'recreates the destroyed elements', ->
expect(minimapElement.shadowRoot.querySelector('.minimap-controls')).toExist()
expect(minimapElement.shadowRoot.querySelector('.minimap-visible-area')).toExist()
expect(minimapElement.shadowRoot.querySelector('.minimap-scroll-indicator')).toExist()
expect(minimapElement.shadowRoot.querySelector('.open-minimap-quick-settings')).toExist()

# ######## ######## ###### ######## ######## ####### ## ##
# ## ## ## ## ## ## ## ## ## ## ## ##
# ## ## ## ## ## ## ## ## ## ####
Expand Down

0 comments on commit 72e7a90

Please sign in to comment.