Always run window.startup when window.coffee is required

When we actually want to attach the root view in window-bootstrap.coffee, we call `window.attachRootView(path)` instead of calling `window.startup(path)`. Having `startup` called automatically means we can be sure any code we add there runs in every environment (including benchmark and specs). This is where we do things like setup the global keymap, parse text mate bundles and themes, and establish the window close handler. Any globals other than the root view that we want to be available in all environments should be established here. Right now that's just the keymap, but soon I want to add a global pasteboard.
This commit is contained in:
Nathan Sobo
2012-10-24 12:34:45 -06:00
parent 906f816fc6
commit b71fa308a3
6 changed files with 22 additions and 28 deletions

View File

@@ -9,10 +9,7 @@ TextMateBundle = require 'text-mate-bundle'
TextMateTheme = require 'text-mate-theme'
require 'window'
requireStylesheet "jasmine.css"
TextMateBundle.loadAll()
TextMateTheme.loadAll()
RootView.prototype.loadUserConfiguration = ->

View File

@@ -10,7 +10,7 @@ describe "editor.", ->
beforeEach ->
window.rootViewParentSelector = '#jasmine-content'
window.startup()
window.attachRootView()
rootView.project.setPath(require.resolve('benchmark/fixtures'))
editor = rootView.getActiveEditor()

View File

@@ -3,7 +3,7 @@ fs = require 'fs'
describe "Window", ->
beforeEach ->
window.startup(require.resolve('fixtures'))
window.attachRootView(require.resolve('fixtures'))
afterEach ->
window.shutdown()

View File

@@ -14,8 +14,6 @@ require 'window'
atom.showDevTools()
requireStylesheet "jasmine.css"
TextMateBundle.loadAll()
TextMateTheme.loadAll()
defaultTitle = document.title
pathsWithSubscriptions = null

View File

@@ -8,6 +8,9 @@ fs = require 'fs'
_ = require 'underscore'
$ = require 'jquery'
{CoffeeScript} = require 'coffee-script'
RootView = require 'root-view'
require 'jquery-extensions'
require 'underscore-extensions'
windowAdditions =
rootViewParentSelector: 'body'
@@ -15,16 +18,28 @@ windowAdditions =
keymap: null
platform: $native.getPlatform()
startup: (path) ->
# This method runs when the file is required. Any code here will run
# in all environments: spec, benchmark, and application
startup: ->
TextMateBundle.loadAll()
TextMateTheme.loadAll()
@setUpKeymap()
# This method is intended only to be run when starting a normal application
# Note: RootView assigns itself on window on initialization so that
# window.rootView is available when loading user configuration
attachRootView: (pathToOpen) ->
if rootViewState = atom.getRootViewStateForPath(pathToOpen)
RootView.deserialize(rootViewState)
else
new RootView(pathToOpen)
@attachRootView(path)
$(window).on 'close', => @close()
$(@rootViewParentSelector).append(@rootView)
$(window).focus()
$(window).on 'beforeunload', =>
@shutdown()
false
$(window).focus()
shutdown: ->
@rootView.deactivate()
@@ -42,16 +57,6 @@ windowAdditions =
@_handleKeyEvent = (e) => @keymap.handleKeyEvent(e)
$(document).on 'keydown', @_handleKeyEvent
# Note: RootView assigns itself on window on initialization so that
# window.rootView is available when loading user configuration
attachRootView: (pathToOpen) ->
if rootViewState = atom.getRootViewStateForPath(pathToOpen)
RootView.deserialize(rootViewState)
else
new RootView(pathToOpen)
$(@rootViewParentSelector).append @rootView
requireStylesheet: (path) ->
unless fullPath = require.resolve(path)
throw new Error("requireStylesheet could not find a file at path '#{path}'")
@@ -91,12 +96,7 @@ windowAdditions =
console.log description, result
window[key] = value for key, value of windowAdditions
window.setUpKeymap()
RootView = require 'root-view'
require 'jquery-extensions'
require 'underscore-extensions'
window.startup()
requireStylesheet 'reset.css'
requireStylesheet 'atom.css'

View File

@@ -1,5 +1,4 @@
# Like sands through the hourglass, so are the days of our lives.
require 'atom'
require 'window'
window.startup window.location.params.pathToOpen
window.attachRootView(window.location.params.pathToOpen)