From f4aa8daa5219af1b4c883f3d90ebf69aa0ace393 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 16 Jan 2012 20:03:48 -0800 Subject: [PATCH] Add window.requireStylesheet This allows you to synchronously load a stylesheet into the document's head whenever it is needed. --- spec/atom/window-spec.coffee | 19 ++++++++++++++++++- spec/spec-helper.coffee | 2 ++ src/atom/window.coffee | 7 +++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/spec/atom/window-spec.coffee b/spec/atom/window-spec.coffee index bc2282ea6..80c69af90 100644 --- a/spec/atom/window-spec.coffee +++ b/spec/atom/window-spec.coffee @@ -1,5 +1,5 @@ -require 'window' $ = require 'jquery' +fs = require 'fs' describe "Window", -> describe "keybindings", -> @@ -15,6 +15,23 @@ describe "Window", -> $(window).trigger 'close' expect(window.close).toHaveBeenCalled() + describe "requireStylesheet(path)", -> + it "synchronously loads the stylesheet at the given path and installs a style tag for it in the head", -> + $('head style').remove() + expect($('head style').length).toBe 0 + requireStylesheet('atom.css') + expect($('head style').length).toBe 1 + + styleElt = $('head style') + + fullPath = require.resolve('atom.css') + expect(styleElt.attr('path')).toBe fullPath + expect(styleElt.text()).toBe fs.read(fullPath) + + requireStylesheet('atom.css') + expect($('head style').length).toBe 1 + + describe "bindMenuItem(path, keyPattern, action)", -> it "causes the given menu item to be added to the menu when the window is focused and removed when it is blurred", -> addedPaths = [] diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index cde8a86d8..41d9db7fc 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -3,11 +3,13 @@ $ = require 'jquery' _ = require 'underscore' Native = require 'native' BindingSet = require 'binding-set' +require 'window' afterEach -> (new Native).resetMainMenu() atom.globalKeymap.reset() $('#jasmine-content').empty() + $('head style[path]').remove() window.atom = new (require 'app') diff --git a/src/atom/window.coffee b/src/atom/window.coffee index 6ea9180c9..864e8d6c0 100644 --- a/src/atom/window.coffee +++ b/src/atom/window.coffee @@ -1,6 +1,7 @@ fs = require 'fs' _ = require 'underscore' $ = require 'jquery' +fs = require 'fs' RootView = require 'root-view' @@ -25,6 +26,12 @@ windowAdditions = $(window).unbind('focus') $(window).unbind('blur') + requireStylesheet: (path) -> + fullPath = require.resolve(path) + content = fs.read(fullPath) + return if $("head style[path='#{fullPath}']").length + $('head').append "" + bindMenuItems: -> # we want to integrate this better with keybindings # @bindMenuItem "File > Save", "meta+s", => @rootView.editor.save()