|
| 1 | +/* eslint no-console:0 */ |
| 2 | +var Environment = require('../environment'); |
| 3 | +Environment.init(); |
| 4 | + |
| 5 | +var path = require('path'); |
| 6 | +var resourcePath = path.join(__dirname, '..', '..'); |
| 7 | + |
| 8 | +var ModuleCache = require('hadron-module-cache'); |
| 9 | +ModuleCache.register(resourcePath); |
| 10 | +ModuleCache.add(resourcePath); |
| 11 | + |
| 12 | +var pkg = require('../../package.json'); |
| 13 | +var CompileCache = require('hadron-compile-cache'); |
| 14 | +CompileCache.setHomeDirectory(resourcePath); |
| 15 | +CompileCache.digestMappings = pkg._compileCacheMappings || {}; |
| 16 | + |
| 17 | +var StyleManager = require('./style-manager'); |
| 18 | +StyleManager.writeStyles(); |
| 19 | + |
| 20 | +/** |
| 21 | + * The main entrypoint for the application! |
| 22 | + */ |
| 23 | +var electron = require('electron'); |
| 24 | +var shell = electron.shell; |
| 25 | +var app = require('ampersand-app'); |
| 26 | +var APP_VERSION = electron.remote.app.getVersion(); |
| 27 | + |
| 28 | +var _ = require('lodash'); |
| 29 | +var qs = require('qs'); |
| 30 | +var ViewSwitcher = require('ampersand-view-switcher'); |
| 31 | +var View = require('ampersand-view'); |
| 32 | +var localLinks = require('local-links'); |
| 33 | +var ipc = require('hadron-ipc'); |
| 34 | +var Router = require('./help/router'); |
| 35 | +var metrics = require('mongodb-js-metrics')(); |
| 36 | + |
| 37 | +var addInspectElementMenu = require('debug-menu').install; |
| 38 | + |
| 39 | +ipc.once('app:launched', function() { |
| 40 | + console.log('in app:launched'); |
| 41 | + if (process.env.NODE_ENV !== 'production') { |
| 42 | + require('debug').enable('mon*,had*'); |
| 43 | + require('debug/browser'); |
| 44 | + } |
| 45 | +}); |
| 46 | + |
| 47 | +var debug = require('debug')('mongodb-compass:help'); |
| 48 | + |
| 49 | +var Application = View.extend({ |
| 50 | + template: function() { |
| 51 | + return [ |
| 52 | + '<div id="application">', |
| 53 | + ' <div data-hook="layout-container"></div>', |
| 54 | + '</div>' |
| 55 | + ].join('\n'); |
| 56 | + }, |
| 57 | + props: { |
| 58 | + version: { |
| 59 | + type: 'string', |
| 60 | + default: APP_VERSION |
| 61 | + } |
| 62 | + }, |
| 63 | + session: { |
| 64 | + /** |
| 65 | + * Details of the MongoDB Instance we're currently connected to. |
| 66 | + */ |
| 67 | + instance: 'state', |
| 68 | + /** |
| 69 | + * @see http://learn.humanjavascript.com/react-ampersand/creating-a-router-and-pages |
| 70 | + */ |
| 71 | + router: 'object' |
| 72 | + }, |
| 73 | + events: { |
| 74 | + 'click a': 'onLinkClick' |
| 75 | + }, |
| 76 | + onClientReady: function() { |
| 77 | + this.startRouter(); |
| 78 | + }, |
| 79 | + startRouter: function() { |
| 80 | + this.router = new Router(); |
| 81 | + debug('Listening for page changes from the router...'); |
| 82 | + this.listenTo(this.router, 'page', this.onPageChange); |
| 83 | + |
| 84 | + debug('Starting router...'); |
| 85 | + this.router.history.start({ |
| 86 | + pushState: false, |
| 87 | + root: '/' |
| 88 | + }); |
| 89 | + }, |
| 90 | + /** |
| 91 | + * When you want to go to a different page in the app or just save |
| 92 | + * state via the URL. |
| 93 | + * @param {String} fragment - To update the location bar with. |
| 94 | + * @param {Object} [options] - `silent` and `params` |
| 95 | + */ |
| 96 | + navigate: function(fragment, options) { |
| 97 | + options = _.defaults(options || {}, { |
| 98 | + silent: false, |
| 99 | + params: null |
| 100 | + }); |
| 101 | + if (options.params) { |
| 102 | + fragment += '?' + qs.stringify(options.params); |
| 103 | + } |
| 104 | + |
| 105 | + var hash = fragment.charAt(0) === '/' ? fragment.slice(1) : fragment; |
| 106 | + this.router.history.navigate(hash, { |
| 107 | + trigger: !options.silent |
| 108 | + }); |
| 109 | + }, |
| 110 | + /** |
| 111 | + * Called a soon as the DOM is ready so we can |
| 112 | + * start showing status indicators as |
| 113 | + * quickly as possible. |
| 114 | + */ |
| 115 | + render: function() { |
| 116 | + debug('Rendering app container...'); |
| 117 | + |
| 118 | + this.el = document.querySelector('#application'); |
| 119 | + this.renderWithTemplate(this); |
| 120 | + this.pageSwitcher = new ViewSwitcher(this.queryByHook('layout-container'), { |
| 121 | + show: function() { |
| 122 | + document.scrollTop = 0; |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + if (process.env.NODE_ENV !== 'production') { |
| 127 | + debug('Installing "Inspect Element" context menu'); |
| 128 | + addInspectElementMenu(); |
| 129 | + } |
| 130 | + }, |
| 131 | + onPageChange: function(view) { |
| 132 | + metrics.track('App', 'viewed', view.screenName); |
| 133 | + this.pageSwitcher.set(view); |
| 134 | + }, |
| 135 | + onLinkClick: function(event) { |
| 136 | + // ignore help links, they're handled in `onHelpClicked` |
| 137 | + if (event.target.className === 'help') { |
| 138 | + return; |
| 139 | + } |
| 140 | + var pathname = localLinks.getLocalPathname(event); |
| 141 | + if (pathname) { |
| 142 | + event.preventDefault(); |
| 143 | + this.router.history.navigate(pathname); |
| 144 | + return; |
| 145 | + } else if (event.currentTarget.getAttribute('href') !== '#') { |
| 146 | + event.preventDefault(); |
| 147 | + event.stopPropagation(); |
| 148 | + shell.openExternal(event.target.href); |
| 149 | + } |
| 150 | + } |
| 151 | +}); |
| 152 | + |
| 153 | +var state = new Application({}); |
| 154 | + |
| 155 | +app.extend({ |
| 156 | + client: null, |
| 157 | + navigate: state.navigate.bind(state), |
| 158 | + onDomReady: function() { |
| 159 | + state.render(); |
| 160 | + // Not serving a part of the app which uses the client, |
| 161 | + // so we can just start everything up now. |
| 162 | + state.startRouter(); |
| 163 | + return; |
| 164 | + }, |
| 165 | + init: function() { |
| 166 | + var self = this; |
| 167 | + // signal to main process that app is ready |
| 168 | + ipc.call('window:renderer-ready'); |
| 169 | + // as soon as dom is ready, render and set up the rest |
| 170 | + self.onDomReady(); |
| 171 | + } |
| 172 | +}); |
| 173 | + |
| 174 | +Object.defineProperty(app, 'instance', { |
| 175 | + get: function() { |
| 176 | + return state.instance; |
| 177 | + } |
| 178 | +}); |
| 179 | + |
| 180 | +Object.defineProperty(app, 'router', { |
| 181 | + get: function() { |
| 182 | + return state.router; |
| 183 | + } |
| 184 | +}); |
| 185 | + |
| 186 | +Object.defineProperty(app, 'state', { |
| 187 | + get: function() { |
| 188 | + return state; |
| 189 | + } |
| 190 | +}); |
| 191 | + |
| 192 | +app.init(); |
| 193 | + |
| 194 | +window.app = app; |
0 commit comments