Add sourcePath parameter to StyleManager::addStyleSheet

This commit is contained in:
Nathan Sobo
2014-10-13 12:09:33 -06:00
parent d3371dbcd2
commit 1c97dcd195
2 changed files with 52 additions and 6 deletions

View File

@@ -7,12 +7,18 @@ describe "StyleManager", ->
manager = new StyleManager
describe "::addStyleSheet(source, params)", ->
it "adds a stylesheet based on the given source and returns a disposable allowing it to be removed", ->
[addEvents, removeEvents, updateEvents] = []
beforeEach ->
addEvents = []
removeEvents = []
updateEvents = []
manager.onDidAddStyleSheet (event) -> addEvents.push(event)
manager.onDidRemoveStyleSheet (event) -> removeEvents.push(event)
manager.onDidUpdateStyleSheet (event) -> updateEvents.push(event)
it "adds a stylesheet based on the given source and returns a disposable allowing it to be removed", ->
disposable = manager.addStyleSheet("a {color: red;}")
expect(addEvents.length).toBe 1
@@ -27,3 +33,26 @@ describe "StyleManager", ->
expect(removeEvents.length).toBe 1
expect(removeEvents[0].styleElement.textContent).toBe "a {color: red;}"
expect(manager.getStyleElements().length).toBe 0
describe "when a sourcePath parameter is specified", ->
it "ensures a maximum of one style element for the given source path, updating a previous if it exists", ->
disposable1 = manager.addStyleSheet("a {color: red;}", sourcePath: '/foo/bar')
expect(addEvents.length).toBe 1
expect(addEvents[0].sourcePath).toBe '/foo/bar'
disposable2 = manager.addStyleSheet("a {color: blue;}", sourcePath: '/foo/bar')
expect(addEvents.length).toBe 1
expect(updateEvents.length).toBe 1
expect(updateEvents[0].sourcePath).toBe '/foo/bar'
expect(updateEvents[0].styleElement.textContent).toBe "a {color: blue;}"
disposable2.dispose()
addEvents = []
manager.addStyleSheet("a {color: yellow;}", sourcePath: '/foo/bar')
expect(addEvents.length).toBe 1
expect(addEvents[0].sourcePath).toBe '/foo/bar'
expect(addEvents[0].styleElement.textContent).toBe "a {color: yellow;}"

View File

@@ -5,6 +5,7 @@ class StyleManager
constructor: ->
@emitter = new Emitter
@styleElements = []
@styleElementsBySourcePath = {}
onDidAddStyleSheet: (callback) ->
@emitter.on 'did-add-style-sheet', callback
@@ -12,19 +13,35 @@ class StyleManager
onDidRemoveStyleSheet: (callback) ->
@emitter.on 'did-remove-style-sheet', callback
onDidUpdateStyleSheet: (callback) ->
@emitter.on 'did-update-style-sheet', callback
getStyleElements: ->
@styleElements.slice()
addStyleSheet: (source) ->
styleElement = document.createElement('style')
addStyleSheet: (source, params) ->
sourcePath = params?.sourcePath
if sourcePath? and styleElement = @styleElementsBySourcePath[sourcePath]
updated = true
else
styleElement = document.createElement('style')
styleElement.textContent = source
@styleElements.push(styleElement)
@emitter.emit 'did-add-style-sheet', {styleElement}
@styleElementsBySourcePath[sourcePath] ?= styleElement if sourcePath?
new Disposable => @removeStyleElement(styleElement)
if updated
@emitter.emit 'did-update-style-sheet', {styleElement, sourcePath}
else
@emitter.emit 'did-add-style-sheet', {styleElement, sourcePath}
removeStyleElement: (styleElement) ->
new Disposable => @removeStyleElement(styleElement, params)
removeStyleElement: (styleElement, params) ->
index = @styleElements.indexOf(styleElement)
unless index is -1
@styleElements.splice(index, 1)
sourcePath = params?.sourcePath
delete @styleElementsBySourcePath[sourcePath] if sourcePath?
@emitter.emit 'did-remove-style-sheet', {styleElement}