]> git.gir.st - VimFx.git/blob - extension/bootstrap.coffee
Merge branch 'develop' into issue-213
[VimFx.git] / extension / bootstrap.coffee
1 'use strict'
2
3 { classes: Cc, interfaces: Ci, utils: Cu } = Components
4
5 Cu.import('resource://gre/modules/Services.jsm')
6
7 # Populate the global namespace with console, require, and include
8 do (global = this) ->
9 loader = Cc['@mozilla.org/moz/jssubscript-loader;1'].getService(Ci.mozIJSSubScriptLoader)
10 baseURI = Services.io.newURI(__SCRIPT_URI_SPEC__, null, null)
11
12 # Loaded packages cache
13 packages = {}
14
15 # Load and cache package
16 require = (name) ->
17 if packages[name] is undefined
18 scope = { require, exports: {} }
19 try
20 path = Services.io.newURI("packages/#{ name }.js", null, baseURI).spec
21 loader.loadSubScript(path, scope)
22 packages[name] = scope.exports
23 catch error
24 dump("Failed to load #{ name }: #{ error }\n")
25 dump(error.stack)
26
27 return packages[name]
28
29 # Unload all packages
30 release = ->
31 for path, scope in packages
32 for name, value in scope
33 scope[name] = null
34 packages = {}
35
36 # Load native console API
37 Cu.import("resource://gre/modules/devtools/Console.jsm")
38
39 # Firefox will call this method on startup/enabling
40 global.startup = (data, reason) ->
41 # Requires for startup/install
42 { loadCss } = require 'utils'
43 { addEventListeners
44 , vimBucket } = require 'events'
45 { getPref
46 , initPrefValues } = require 'prefs'
47 { setButtonInstallPosition
48 , addToolbarButton } = require 'button'
49 options = require 'options'
50 { watchWindows } = require 'window-utils'
51 { unload } = require 'unload'
52
53 if reason == ADDON_INSTALL
54 # Position the toolbar button right before the default Bookmarks button
55 # If Bookmarks button is hidden - then VimFx button will be appended to the toolbar
56 setButtonInstallPosition('nav-bar', 'bookmarks-menu-button-container')
57
58 # Write default preference values on install
59 initPrefValues()
60
61 loadCss('style')
62
63 options.observe()
64
65 watchWindows(addEventListeners, 'navigator:browser')
66 watchWindows(addToolbarButton.bind(undefined, vimBucket), 'navigator:browser')
67
68 unload(release)
69
70 # Firefox will call this method on shutdown/disabling
71 global.shutdown = (data, reason) ->
72 # Don't bother to clean up if the browser is shutting down
73 if reason != APP_SHUTDOWN
74 { unload } = require 'unload'
75 unload()
76
77 global.install = (data, reason) ->
78
79 global.uninstall = (data, reason) ->
Imprint / Impressum