Made App a fake singleton. It only has class methods and variables.

This commit is contained in:
Corey Johnson
2011-09-20 11:06:03 -07:00
parent c22587ab66
commit e60fa30daa
5 changed files with 54 additions and 72 deletions

View File

@@ -1,43 +1,24 @@
_ = require 'underscore'
Window = require 'window'
Plugins = require 'plugins'
module.exports = App =
windows: []
root: OSX.NSBundle.mainBundle.resourcePath
module.exports =
class App
@windows: []
@root: OSX.NSBundle.mainBundle.resourcePath
activeWindow: null
@activeWindow: null
setActiveWindow: (window) ->
@setup: ->
@setActiveWindow new Window controller : WindowController
# Move this someone more approriate
if localStorage.lastOpenedFilename
@activeWindow.open(localStorage.lastOpenedFilename)
Plugins.load()
@activeWindow.document.ace._emit "loaded"
@setActiveWindow: (window) ->
@activeWindow = window
@windows.push window if window not in @windows
# path - Optional. The String path to the file to base it on.
newWindow: (path) ->
c = OSX.AtomWindowController.alloc.initWithWindowNibName "AtomWindow"
c.window
c.window.makeKeyAndOrderFront null
# Returns null or a file path.
openPanel: ->
panel = OSX.NSOpenPanel.openPanel
panel.setCanChooseDirectories true
if panel.runModal isnt OSX.NSFileHandlingPanelOKButton
return null
filename = panel.filenames.lastObject
localStorage.lastOpenedFilename = filename
filename
openURL: (url) ->
window.location = url
@activeWindow.setTitle _.last url.replace(/\/$/,'').split '/'
# Returns null or a file path.
savePanel: ->
panel = OSX.NSSavePanel.savePanel
if panel.runModal isnt OSX.NSFileHandlingPanelOKButton
return null
panel.filenames.lastObject
writeToPasteboard: (text) ->
pb = OSX.NSPasteboard.generalPasteboard
pb.declareTypes_owner [OSX.NSStringPboardType], null
pb.setString_forType text, OSX.NSStringPboardType

View File

@@ -1,9 +1,8 @@
_ = require 'underscore'
File = require 'fs'
App = require 'app'
Chrome = require 'chrome'
Pane = require 'pane'
activeWindow = App.activeWindow
ace = require 'ace/ace'
@@ -36,6 +35,8 @@ class Editor extends Pane
sessions: {}
initialize: ->
window.r = require 'app'
@ace = ace.edit "editor"
@ace.setTheme require "ace/theme/twilight"
@ace.getSession().setUseSoftTabs true
@@ -45,7 +46,8 @@ class Editor extends Pane
@ace.setPrintMarginColumn 78
@ace.getSession().on 'change', ->
activeWindow.setDirty true
App = require 'app' # Get rid of this!
App.activeWindow.setDirty true
el = document.body
el.addEventListener 'DOMNodeInsertedIntoDocument', =>
@@ -59,27 +61,29 @@ class Editor extends Pane
@removeTrailingWhitespace()
File.write @filename, @code()
@sessions[@filename] = @ace.getSession()
activeWindow.setDirty false
.setDirty false
@ace._emit 'save', { @filename }
open: (path) ->
path = App.openPanel() if not path
App = require 'app' # Get rid of this!
path = Chrome.openPanel() if not path
return if not path
@filename = path
if File.isDirectory @filename
File.changeWorkingDirectory @filename
activeWindow.setTitle _.last @filename.split '/'
window.x = App
App.activeWindow.setTitle _.last @filename.split '/'
@ace.setSession @newSession()
activeWindow.setDirty false
App.activeWindow.setDirty false
else
if /png|jpe?g|gif/i.test @filename
App.openURL @filename
Chrome.openURL @filename
else
activeWindow.setTitle _.last @filename.split '/'
App.activeWindow.setTitle _.last @filename.split '/'
@sessions[@filename] or= @newSession File.read @filename
@ace.setSession @sessions[@filename]
activeWindow.setDirty false
App.activeWindow.setDirty false
@ace._emit 'open', { @filename }
close: (path) ->
@@ -87,9 +91,9 @@ class Editor extends Pane
@ace._emit 'close', { filename : path }
saveAs: ->
if file = App.savePanel()
if file = Chrome.savePanel()
@filename = file
activeWindow.setTitle _.last @filename.split '/'
App.activeWindow.setTitle _.last @filename.split '/'
@save()
code: ->
@@ -137,12 +141,12 @@ class Editor extends Pane
copy: ->
editor = @ace
text = editor.getSession().doc.getTextRange editor.getSelectionRange()
App.writeToPasteboard text
Chrome.writeToPasteboard text
cut: ->
editor = @ace
text = editor.getSession().doc.getTextRange editor.getSelectionRange()
App.writeToPasteboard text
Chrome.writeToPasteboard text
editor.session.remove editor.getSelectionRange()
eval: ->

View File

@@ -2,10 +2,11 @@ $ = require 'jquery'
_ = require 'underscore'
File = require 'fs'
App = require 'app'
_.map File.list(App.root + "/plugins"), (plugin) ->
require plugin
exports.load = ->
App = require 'app'
_.map File.list(App.root + "/plugins"), (plugin) ->
require plugin
_.map File.list("~/.atomicity/"), (path) ->
require path
_.map File.list("~/.atomicity/"), (path) ->
require path

View File

@@ -1,14 +1,3 @@
# yay!
App = require 'app'
Window = require 'window'
App.setActiveWindow new Window controller: WindowController
Editor = require 'editor'
App.activeWindow.document = new Editor
if localStorage.lastOpenedFilename
App.activeWindow.open(localStorage.lastOpenedFilename)
require 'plugins'
App.activeWindow.document.ace._emit "loaded"
App.setup()

View File

@@ -1,6 +1,6 @@
$ = require 'jquery'
App = require 'app'
Chrome = require 'chrome'
Pane = require 'pane'
module.exports =
@@ -17,6 +17,12 @@ class Window extends Pane
'Command-Ctrl-K' : 'showConsole'
'Command-Ctrl-R' : 'reload'
constructor: (options={}) ->
super options
Editor = require 'editor'
@document = new Editor
initialize: ->
@nswindow = @controller?.window
@@ -44,7 +50,7 @@ class Window extends Pane
@controller.close()
reload: ->
App.newWindow()
Chrome.newWindow()
@close()
isDirty: ->
@@ -58,14 +64,15 @@ class Window extends Pane
@_inspector ?= WindowController.webView.inspector
new: ->
App.newWindow()
Chrome.newWindow()
open: (path) ->
@document?.open path
openURL: (url) ->
if url = prompt "Enter URL:"
App.openURL url
Chrome = require 'app'
Chrome.openURL url
showConsole: ->
@inspector().showConsole(1)