Handle priority in when inserting style elements in style manager

This commit is contained in:
Nathan Sobo 2015-01-06 19:33:14 -07:00
parent 4c74b07b22
commit 94e12ee886
2 changed files with 22 additions and 1 deletions

View File

@ -64,3 +64,17 @@ describe "StyleManager", ->
"a {color: green}"
"a {color: blue}"
]
describe "when a priority parameter is specified", ->
it "inserts the style sheet based on the priority", ->
manager.addStyleSheet("a {color: red}", priority: 1)
manager.addStyleSheet("a {color: blue}", priority: 0)
manager.addStyleSheet("a {color: green}", priority: 2)
manager.addStyleSheet("a {color: yellow}", priority: 1)
expect(manager.getStyleElements().map (elt) -> elt.textContent).toEqual [
"a {color: blue}"
"a {color: red}"
"a {color: yellow}"
"a {color: green}"
]

View File

@ -125,7 +125,7 @@ class StyleManager
new Disposable => @removeStyleElement(styleElement)
addStyleElement: (styleElement) ->
{sourcePath, group} = styleElement
{sourcePath, group, priority} = styleElement
if group?
for existingElement, index in @styleElements
@ -133,6 +133,13 @@ class StyleManager
insertIndex = index + 1
else
break if insertIndex?
if priority?
for existingElement, index in @styleElements
if existingElement.priority > priority
insertIndex = index
break
insertIndex ?= @styleElements.length
@styleElements.splice(insertIndex, 0, styleElement)