| 1 | (function () {\r |
| 2 | \r |
| 3 | _kiwi.model.Application = Backbone.Model.extend({\r |
| 4 | /** _kiwi.view.Application */\r |
| 5 | view: null,\r |
| 6 | \r |
| 7 | /** _kiwi.view.StatusMessage */\r |
| 8 | message: null,\r |
| 9 | \r |
| 10 | /* Address for the kiwi server */\r |
| 11 | kiwi_server: null,\r |
| 12 | \r |
| 13 | initialize: function (options) {\r |
| 14 | if (options.container) {\r |
| 15 | this.set('container', options.container);\r |
| 16 | }\r |
| 17 | \r |
| 18 | // The base url to the kiwi server\r |
| 19 | this.set('base_path', options.base_path ? options.base_path : '');\r |
| 20 | \r |
| 21 | // Path for the settings.json file\r |
| 22 | this.set('settings_path', options.settings_path ?\r |
| 23 | options.settings_path :\r |
| 24 | this.get('base_path') + '/assets/settings.json'\r |
| 25 | );\r |
| 26 | \r |
| 27 | // Any options sent down from the server\r |
| 28 | this.server_settings = options.server_settings || {};\r |
| 29 | this.translations = options.translations || {};\r |
| 30 | this.themes = options.themes || [];\r |
| 31 | this.text_theme = options.text_theme || {};\r |
| 32 | \r |
| 33 | // Best guess at where the kiwi server is if not already specified\r |
| 34 | this.kiwi_server = options.kiwi_server || this.detectKiwiServer();\r |
| 35 | \r |
| 36 | // The applet to initially load\r |
| 37 | this.startup_applet_name = options.startup || 'kiwi_startup';\r |
| 38 | \r |
| 39 | // Set any default settings before anything else is applied\r |
| 40 | if (this.server_settings && this.server_settings.client && this.server_settings.client.settings) {\r |
| 41 | this.applyDefaultClientSettings(this.server_settings.client.settings);\r |
| 42 | }\r |
| 43 | },\r |
| 44 | \r |
| 45 | \r |
| 46 | initializeInterfaces: function () {\r |
| 47 | // Set the gateway up\r |
| 48 | _kiwi.gateway = new _kiwi.model.Gateway();\r |
| 49 | this.bindGatewayCommands(_kiwi.gateway);\r |
| 50 | \r |
| 51 | this.initializeClient();\r |
| 52 | this.initializeGlobals();\r |
| 53 | \r |
| 54 | this.view.barsHide(true);\r |
| 55 | },\r |
| 56 | \r |
| 57 | \r |
| 58 | detectKiwiServer: function () {\r |
| 59 | // If running from file, default to localhost:7777 by default\r |
| 60 | if (window.location.protocol === 'file:') {\r |
| 61 | return 'http://localhost:7778';\r |
| 62 | } else {\r |
| 63 | // Assume the kiwi server is on the same server\r |
| 64 | return window.location.protocol + '//' + window.location.host;\r |
| 65 | }\r |
| 66 | },\r |
| 67 | \r |
| 68 | \r |
| 69 | showStartup: function() {\r |
| 70 | this.startup_applet = _kiwi.model.Applet.load(this.startup_applet_name, {no_tab: true});\r |
| 71 | this.startup_applet.tab = this.view.$('.console');\r |
| 72 | this.startup_applet.view.show();\r |
| 73 | },\r |
| 74 | \r |
| 75 | \r |
| 76 | initializeClient: function () {\r |
| 77 | this.view = new _kiwi.view.Application({model: this, el: this.get('container')});\r |
| 78 | \r |
| 79 | // Takes instances of model_network\r |
| 80 | this.connections = new _kiwi.model.NetworkPanelList();\r |
| 81 | \r |
| 82 | // Applets panel list\r |
| 83 | this.applet_panels = new _kiwi.model.PanelList();\r |
| 84 | this.applet_panels.view.$el.addClass('panellist applets');\r |
| 85 | this.view.$el.find('.tabs').append(this.applet_panels.view.$el);\r |
| 86 | \r |
| 87 | /**\r |
| 88 | * Set the UI components up\r |
| 89 | */\r |
| 90 | this.controlbox = (new _kiwi.view.ControlBox({el: $('#kiwi .controlbox')[0]})).render();\r |
| 91 | this.client_ui_commands = new _kiwi.misc.ClientUiCommands(this, this.controlbox);\r |
| 92 | \r |
| 93 | this.rightbar = new _kiwi.view.RightBar({el: this.view.$('.right_bar')[0]});\r |
| 94 | this.topicbar = new _kiwi.view.TopicBar({el: this.view.$el.find('.topic')[0]});\r |
| 95 | \r |
| 96 | new _kiwi.view.AppToolbar({el: _kiwi.app.view.$el.find('.toolbar .app_tools')[0]});\r |
| 97 | new _kiwi.view.ChannelTools({el: _kiwi.app.view.$el.find('.channel_tools')[0]});\r |
| 98 | \r |
| 99 | this.message = new _kiwi.view.StatusMessage({el: this.view.$el.find('.status_message')[0]});\r |
| 100 | \r |
| 101 | this.resize_handle = new _kiwi.view.ResizeHandler({el: this.view.$el.find('.memberlists_resize_handle')[0]});\r |
| 102 | \r |
| 103 | // Rejigg the UI sizes\r |
| 104 | this.view.doLayout();\r |
| 105 | },\r |
| 106 | \r |
| 107 | \r |
| 108 | initializeGlobals: function () {\r |
| 109 | _kiwi.global.connections = this.connections;\r |
| 110 | \r |
| 111 | _kiwi.global.panels = this.panels;\r |
| 112 | _kiwi.global.panels.applets = this.applet_panels;\r |
| 113 | \r |
| 114 | _kiwi.global.components.Applet = _kiwi.model.Applet;\r |
| 115 | _kiwi.global.components.Panel =_kiwi.model.Panel;\r |
| 116 | _kiwi.global.components.MenuBox = _kiwi.view.MenuBox;\r |
| 117 | _kiwi.global.components.DataStore = _kiwi.model.DataStore;\r |
| 118 | },\r |
| 119 | \r |
| 120 | \r |
| 121 | applyDefaultClientSettings: function (settings) {\r |
| 122 | _.each(settings, function (value, setting) {\r |
| 123 | if (typeof _kiwi.global.settings.get(setting) === 'undefined') {\r |
| 124 | _kiwi.global.settings.set(setting, value);\r |
| 125 | }\r |
| 126 | });\r |
| 127 | },\r |
| 128 | \r |
| 129 | \r |
| 130 | panels: (function() {\r |
| 131 | var active_panel;\r |
| 132 | \r |
| 133 | var fn = function(panel_type) {\r |
| 134 | var panels;\r |
| 135 | \r |
| 136 | // Default panel type\r |
| 137 | panel_type = panel_type || 'connections';\r |
| 138 | \r |
| 139 | switch (panel_type) {\r |
| 140 | case 'connections':\r |
| 141 | panels = this.connections.panels();\r |
| 142 | break;\r |
| 143 | case 'applets':\r |
| 144 | panels = this.applet_panels.models;\r |
| 145 | break;\r |
| 146 | }\r |
| 147 | \r |
| 148 | // Active panels / server\r |
| 149 | panels.active = active_panel;\r |
| 150 | panels.server = this.connections.active_connection ?\r |
| 151 | this.connections.active_connection.panels.server :\r |
| 152 | null;\r |
| 153 | \r |
| 154 | return panels;\r |
| 155 | };\r |
| 156 | \r |
| 157 | _.extend(fn, Backbone.Events);\r |
| 158 | \r |
| 159 | // Keep track of the active panel. Channel/query/server or applet\r |
| 160 | fn.bind('active', function (new_active_panel) {\r |
| 161 | var previous_panel = active_panel;\r |
| 162 | active_panel = new_active_panel;\r |
| 163 | \r |
| 164 | _kiwi.global.events.emit('panel:active', {previous: previous_panel, active: active_panel});\r |
| 165 | });\r |
| 166 | \r |
| 167 | return fn;\r |
| 168 | })(),\r |
| 169 | \r |
| 170 | \r |
| 171 | bindGatewayCommands: function (gw) {\r |
| 172 | var that = this;\r |
| 173 | \r |
| 174 | // As soon as an IRC connection is made, show the full client UI\r |
| 175 | gw.on('connection:connect', function (event) {\r |
| 176 | that.view.barsShow();\r |
| 177 | });\r |
| 178 | \r |
| 179 | \r |
| 180 | /**\r |
| 181 | * Handle the reconnections to the kiwi server\r |
| 182 | */\r |
| 183 | (function () {\r |
| 184 | // 0 = non-reconnecting state. 1 = reconnecting state.\r |
| 185 | var gw_stat = 0;\r |
| 186 | \r |
| 187 | gw.on('disconnect', function (event) {\r |
| 188 | that.view.$el.removeClass('connected');\r |
| 189 | \r |
| 190 | // Reconnection phase will start to kick in\r |
| 191 | gw_stat = 1;\r |
| 192 | });\r |
| 193 | \r |
| 194 | \r |
| 195 | gw.on('reconnecting', function (event) {\r |
| 196 | var msg = translateText('client_models_application_reconnect_in_x_seconds', [event.delay/1000]) + '...';\r |
| 197 | \r |
| 198 | // Only need to mention the repeating re-connection messages on server panels\r |
| 199 | _kiwi.app.connections.forEach(function(connection) {\r |
| 200 | connection.panels.server.addMsg('', styleText('quit', {text: msg}), 'action quit');\r |
| 201 | });\r |
| 202 | });\r |
| 203 | \r |
| 204 | \r |
| 205 | // After the socket has connected, kiwi handshakes and then triggers a kiwi:connected event\r |
| 206 | gw.on('kiwi:connected', function (event) {\r |
| 207 | var msg;\r |
| 208 | \r |
| 209 | that.view.$el.addClass('connected');\r |
| 210 | \r |
| 211 | // If we were reconnecting, show some messages we have connected back OK\r |
| 212 | if (gw_stat === 1) {\r |
| 213 | \r |
| 214 | // No longer in the reconnection state\r |
| 215 | gw_stat = 0;\r |
| 216 | \r |
| 217 | msg = translateText('client_models_application_reconnect_successfully') + ' :)';\r |
| 218 | that.message.text(msg, {timeout: 5000});\r |
| 219 | \r |
| 220 | // Mention the re-connection on every channel\r |
| 221 | _kiwi.app.connections.forEach(function(connection) {\r |
| 222 | connection.reconnect();\r |
| 223 | \r |
| 224 | connection.panels.server.addMsg('', styleText('rejoin', {text: msg}), 'action join');\r |
| 225 | \r |
| 226 | connection.panels.forEach(function(panel) {\r |
| 227 | if (!panel.isChannel())\r |
| 228 | return;\r |
| 229 | \r |
| 230 | panel.addMsg('', styleText('rejoin', {text: msg}), 'action join');\r |
| 231 | });\r |
| 232 | });\r |
| 233 | }\r |
| 234 | \r |
| 235 | });\r |
| 236 | })();\r |
| 237 | \r |
| 238 | \r |
| 239 | gw.on('kiwi:reconfig', function () {\r |
| 240 | $.getJSON(that.get('settings_path'), function (data) {\r |
| 241 | that.server_settings = data.server_settings || {};\r |
| 242 | that.translations = data.translations || {};\r |
| 243 | });\r |
| 244 | });\r |
| 245 | \r |
| 246 | \r |
| 247 | gw.on('kiwi:jumpserver', function (data) {\r |
| 248 | var serv;\r |
| 249 | // No server set? Then nowhere to jump to.\r |
| 250 | if (typeof data.kiwi_server === 'undefined')\r |
| 251 | return;\r |
| 252 | \r |
| 253 | serv = data.kiwi_server;\r |
| 254 | \r |
| 255 | // Strip any trailing slash from the end\r |
| 256 | if (serv[serv.length-1] === '/')\r |
| 257 | serv = serv.substring(0, serv.length-1);\r |
| 258 | \r |
| 259 | // Force the jumpserver now?\r |
| 260 | if (data.force) {\r |
| 261 | // Get an interval between 5 and 6 minutes so everyone doesn't reconnect it all at once\r |
| 262 | var jump_server_interval = Math.random() * (360 - 300) + 300;\r |
| 263 | jump_server_interval = 1;\r |
| 264 | \r |
| 265 | // Tell the user we are going to disconnect, wait 5 minutes then do the actual reconnect\r |
| 266 | var msg = _kiwi.global.i18n.translate('client_models_application_jumpserver_prepare').fetch();\r |
| 267 | that.message.text(msg, {timeout: 10000});\r |
| 268 | \r |
| 269 | setTimeout(function forcedReconnect() {\r |
| 270 | var msg = _kiwi.global.i18n.translate('client_models_application_jumpserver_reconnect').fetch();\r |
| 271 | that.message.text(msg, {timeout: 8000});\r |
| 272 | \r |
| 273 | setTimeout(function forcedReconnectPartTwo() {\r |
| 274 | _kiwi.app.kiwi_server = serv;\r |
| 275 | \r |
| 276 | _kiwi.gateway.reconnect(function() {\r |
| 277 | // Reconnect all the IRC connections\r |
| 278 | that.connections.forEach(function(con){ con.reconnect(); });\r |
| 279 | });\r |
| 280 | }, 5000);\r |
| 281 | \r |
| 282 | }, jump_server_interval * 1000);\r |
| 283 | }\r |
| 284 | });\r |
| 285 | }\r |
| 286 | \r |
| 287 | });\r |
| 288 | \r |
| 289 | })();\r |