diff --git a/spec/theme-spec.coffee b/spec/theme-spec.coffee index 4e1d40a55..f4d313945 100644 --- a/spec/theme-spec.coffee +++ b/spec/theme-spec.coffee @@ -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() diff --git a/src/theme.coffee b/src/theme.coffee index a4af012d9..325ab29e2 100644 --- a/src/theme.coffee +++ b/src/theme.coffee @@ -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') +