deactivated event on themes + can reload stylesheet

This commit is contained in:
Ben Ogle
2013-09-10 15:21:07 -07:00
parent b48a35fc14
commit d3eec2db05
2 changed files with 38 additions and 2 deletions

View File

@@ -51,3 +51,30 @@ describe "Theme", ->
expect($(".editor").css("padding-top")).toBe "10px"
expect($(".editor").css("padding-right")).toBe "20px"
expect($(".editor").css("padding-bottom")).toBe "30px"
describe "reloading a theme", ->
beforeEach ->
themePath = project.resolve('themes/theme-with-package-file')
theme = new Theme(themePath)
theme.load()
afterEach ->
theme.deactivate()
it "reloads without readding to the stylesheets list", ->
expect(theme.stylesheets.length).toBe 3
theme.loadStylesheet(theme.stylesheets[0])
expect(theme.stylesheets.length).toBe 3
describe "events", ->
beforeEach ->
themePath = project.resolve('themes/theme-with-package-file')
theme = new Theme(themePath)
afterEach ->
theme.deactivate()
it "deactivated event fires on .deactivate()", ->
theme.on 'deactivated', spy = jasmine.createSpy()
theme.deactivate()
expect(spy).toHaveBeenCalled()

View File

@@ -1,10 +1,14 @@
_ = require 'underscore'
fsUtils = require 'fs-utils'
path = require 'path'
EventEmitter = require 'event-emitter'
### Internal ###
module.exports =
class Theme
_.extend @prototype, EventEmitter
stylesheetPath: null
stylesheets: null
@@ -24,7 +28,7 @@ class Theme
# Loads the stylesheets found in a `package.cson` file.
load: ->
if path.extname(@stylesheetPath) in ['.css', '.less']
if @isFile()
@loadStylesheet(@stylesheetPath)
else
@directoryPath = @stylesheetPath
@@ -38,13 +42,18 @@ class Theme
else
@loadStylesheet(stylesheetPath) for stylesheetPath in fsUtils.listSync(@stylesheetPath, ['.css', '.less'])
isFile: ->
path.extname(@stylesheetPath) in ['.css', '.less']
# Given a path, this loads it as a stylesheet.
#
# stylesheetPath - A {String} to a stylesheet
loadStylesheet: (stylesheetPath) ->
@stylesheets.push stylesheetPath
@stylesheets.push(stylesheetPath) if @stylesheets.indexOf(stylesheetPath) < 0
content = window.loadStylesheet(stylesheetPath)
window.applyStylesheet(stylesheetPath, content, 'userTheme')
deactivate: ->
window.removeStylesheet(stylesheetPath) for stylesheetPath in @stylesheets
@trigger('deactivated')