From 6b8fbed0c4a08bf883e95cc777977c7acf16e6e6 Mon Sep 17 00:00:00 2001 From: Darren Date: Mon, 31 Mar 2014 21:34:31 +0100 Subject: [PATCH] Show the app after plugins loaded; Allow custom startup applets; --- client/src/app.js | 19 +++- client/src/index.html.tmpl | 16 ++- client/src/models/application.js | 162 ++++++++++++++++++++++++++++- client/src/models/newconnection.js | 156 +-------------------------- client/src/models/pluginmanager.js | 20 +++- 5 files changed, 206 insertions(+), 167 deletions(-) diff --git a/client/src/app.js b/client/src/app.js index 5f19c81..292010f 100644 --- a/client/src/app.js +++ b/client/src/app.js @@ -109,11 +109,11 @@ _kiwi.global = { }, // Entry point to start the kiwi application - start: function (opts, callback) { + init: function (opts, callback) { var continueStart, locale; opts = opts || {}; - continueStart = function (locale, s, xhr) { + continueInit = function (locale, s, xhr) { if (locale) { _kiwi.global.i18n = new Jed(locale); } else { @@ -123,7 +123,7 @@ _kiwi.global = { _kiwi.app = new _kiwi.model.Application(opts); // Start the client up - _kiwi.app.start(); + _kiwi.app.initializeInterfaces(); // Now everything has started up, load the plugin manager for third party plugins _kiwi.global.plugins = new _kiwi.model.PluginManager(); @@ -140,10 +140,19 @@ _kiwi.global = { locale = _kiwi.global.settings.get('locale'); if (!locale) { - $.getJSON(opts.base_path + '/assets/locales/magic.json', continueStart); + $.getJSON(opts.base_path + '/assets/locales/magic.json', continueInit); } else { - $.getJSON(opts.base_path + '/assets/locales/' + locale + '.json', continueStart); + $.getJSON(opts.base_path + '/assets/locales/' + locale + '.json', continueInit); } + }, + + start: function() { + _kiwi.app.showStartup(); + }, + + // Allow plugins to change the startup applet + registerStartupApplet: function(startup_applet_name) { + _kiwi.app.startup_applet_name = startup_applet_name; } }; diff --git a/client/src/index.html.tmpl b/client/src/index.html.tmpl index bf40fba..7a5bdcf 100644 --- a/client/src/index.html.tmpl +++ b/client/src/index.html.tmpl @@ -493,13 +493,23 @@ // Kiwi IRC version this is built from kiwi.build_version = '<%build_version%>'; - // Start the app - kiwi.start(opts, function() { - // Load any plugins + // Start the app after loading plugins + kiwi.init(opts, function() { if (opts.client_plugins && opts.client_plugins.length > 0) { + + // Wait until all plugins are loaded before starting the app + kiwi.plugins.once('loaded', function() { + kiwi.start(); + }); + _.each(opts.client_plugins, function (plugin_url) { kiwi.plugins.load(plugin_url); }); + + } else { + + // No plugins were needed so start the app + kiwi.start(); } }); }); diff --git a/client/src/models/application.js b/client/src/models/application.js index ecd5279..574ce25 100644 --- a/client/src/models/application.js +++ b/client/src/models/application.js @@ -42,7 +42,7 @@ }, - start: function () { + initializeInterfaces: function () { // Set the gateway up _kiwi.gateway = new _kiwi.model.Gateway(); this.bindGatewayCommands(_kiwi.gateway); @@ -51,8 +51,6 @@ this.initializeGlobals(); this.view.barsHide(true); - - this.showStartup(); }, @@ -163,6 +161,164 @@ })(), + defaultServerSettings: function () { + var parts; + var defaults = { + nick: '', + server: '', + port: 6667, + ssl: false, + channel: '', + channel_key: '' + }; + var uricheck; + + + /** + * Get any settings set by the server + * These settings may be changed in the server selection dialog or via URL parameters + */ + if (this.server_settings.client) { + if (this.server_settings.client.nick) + defaults.nick = this.server_settings.client.nick; + + if (this.server_settings.client.server) + defaults.server = this.server_settings.client.server; + + if (this.server_settings.client.port) + defaults.port = this.server_settings.client.port; + + if (this.server_settings.client.ssl) + defaults.ssl = this.server_settings.client.ssl; + + if (this.server_settings.client.channel) + defaults.channel = this.server_settings.client.channel; + + if (this.server_settings.client.channel_key) + defaults.channel_key = this.server_settings.client.channel_key; + } + + + + /** + * Get any settings passed in the URL + * These settings may be changed in the server selection dialog + */ + + // Any query parameters first + if (getQueryVariable('nick')) + defaults.nick = getQueryVariable('nick'); + + if (window.location.hash) + defaults.channel = window.location.hash; + + + // Process the URL part by part, extracting as we go + parts = window.location.pathname.toString().replace(this.get('base_path'), '').split('/'); + + if (parts.length > 0) { + parts.shift(); + + if (parts.length > 0 && parts[0]) { + // Check to see if we're dealing with an irc: uri, or whether we need to extract the server/channel info from the HTTP URL path. + uricheck = parts[0].substr(0, 7).toLowerCase(); + if ((uricheck === 'ircs%3a') || (uricheck.substr(0,6) === 'irc%3a')) { + parts[0] = decodeURIComponent(parts[0]); + // irc[s]://[:]/[[?]] + uricheck = /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts[0]); + /* + uricheck[1] = ssl (optional) + uricheck[2] = host + uricheck[3] = port (optional) + uricheck[4] = channel (optional) + uricheck[5] = channel key (optional, channel must also be set) + */ + if (uricheck) { + if (typeof uricheck[1] !== 'undefined') { + defaults.ssl = true; + if (defaults.port === 6667) { + defaults.port = 6697; + } + } + defaults.server = uricheck[2]; + if (typeof uricheck[3] !== 'undefined') { + defaults.port = uricheck[3]; + } + if (typeof uricheck[4] !== 'undefined') { + defaults.channel = '#' + uricheck[4]; + if (typeof uricheck[5] !== 'undefined') { + defaults.channel_key = uricheck[5]; + } + } + } + parts = []; + } else { + // Extract the port+ssl if we find one + if (parts[0].search(/:/) > 0) { + defaults.port = parts[0].substring(parts[0].search(/:/) + 1); + defaults.server = parts[0].substring(0, parts[0].search(/:/)); + if (defaults.port[0] === '+') { + defaults.port = parseInt(defaults.port.substring(1), 10); + defaults.ssl = true; + } else { + defaults.ssl = false; + } + + } else { + defaults.server = parts[0]; + } + + parts.shift(); + } + } + + if (parts.length > 0 && parts[0]) { + defaults.channel = '#' + parts[0]; + parts.shift(); + } + } + + // If any settings have been given by the server.. override any auto detected settings + /** + * Get any server restrictions as set in the server config + * These settings can not be changed in the server selection dialog + */ + if (this.server_settings && this.server_settings.connection) { + if (this.server_settings.connection.server) { + defaults.server = this.server_settings.connection.server; + } + + if (this.server_settings.connection.port) { + defaults.port = this.server_settings.connection.port; + } + + if (this.server_settings.connection.ssl) { + defaults.ssl = this.server_settings.connection.ssl; + } + + if (this.server_settings.connection.channel) { + defaults.channel = this.server_settings.connection.channel; + } + + if (this.server_settings.connection.channel_key) { + defaults.channel_key = this.server_settings.connection.channel_key; + } + + if (this.server_settings.connection.nick) { + defaults.nick = this.server_settings.connection.nick; + } + } + + // Set any random numbers if needed + defaults.nick = defaults.nick.replace('?', Math.floor(Math.random() * 100000).toString()); + + if (getQueryVariable('encoding')) + defaults.encoding = getQueryVariable('encoding'); + + return defaults; + }, + + bindGatewayCommands: function (gw) { var that = this; diff --git a/client/src/models/newconnection.js b/client/src/models/newconnection.js index a2e16e7..2b77b68 100644 --- a/client/src/models/newconnection.js +++ b/client/src/models/newconnection.js @@ -7,160 +7,8 @@ _kiwi.model.NewConnection = Backbone.Collection.extend({ }, - populateDefaultServerSettings: function () { - var parts; - var defaults = { - nick: '', - server: '', - port: 6667, - ssl: false, - channel: '', - channel_key: '' - }; - var uricheck; - - - /** - * Get any settings set by the server - * These settings may be changed in the server selection dialog or via URL parameters - */ - if (_kiwi.app.server_settings.client) { - if (_kiwi.app.server_settings.client.nick) - defaults.nick = _kiwi.app.server_settings.client.nick; - - if (_kiwi.app.server_settings.client.server) - defaults.server = _kiwi.app.server_settings.client.server; - - if (_kiwi.app.server_settings.client.port) - defaults.port = _kiwi.app.server_settings.client.port; - - if (_kiwi.app.server_settings.client.ssl) - defaults.ssl = _kiwi.app.server_settings.client.ssl; - - if (_kiwi.app.server_settings.client.channel) - defaults.channel = _kiwi.app.server_settings.client.channel; - - if (_kiwi.app.server_settings.client.channel_key) - defaults.channel_key = _kiwi.app.server_settings.client.channel_key; - } - - - - /** - * Get any settings passed in the URL - * These settings may be changed in the server selection dialog - */ - - // Any query parameters first - if (getQueryVariable('nick')) - defaults.nick = getQueryVariable('nick'); - - if (window.location.hash) - defaults.channel = window.location.hash; - - - // Process the URL part by part, extracting as we go - parts = window.location.pathname.toString().replace(_kiwi.app.get('base_path'), '').split('/'); - - if (parts.length > 0) { - parts.shift(); - - if (parts.length > 0 && parts[0]) { - // Check to see if we're dealing with an irc: uri, or whether we need to extract the server/channel info from the HTTP URL path. - uricheck = parts[0].substr(0, 7).toLowerCase(); - if ((uricheck === 'ircs%3a') || (uricheck.substr(0,6) === 'irc%3a')) { - parts[0] = decodeURIComponent(parts[0]); - // irc[s]://[:]/[[?]] - uricheck = /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts[0]); - /* - uricheck[1] = ssl (optional) - uricheck[2] = host - uricheck[3] = port (optional) - uricheck[4] = channel (optional) - uricheck[5] = channel key (optional, channel must also be set) - */ - if (uricheck) { - if (typeof uricheck[1] !== 'undefined') { - defaults.ssl = true; - if (defaults.port === 6667) { - defaults.port = 6697; - } - } - defaults.server = uricheck[2]; - if (typeof uricheck[3] !== 'undefined') { - defaults.port = uricheck[3]; - } - if (typeof uricheck[4] !== 'undefined') { - defaults.channel = '#' + uricheck[4]; - if (typeof uricheck[5] !== 'undefined') { - defaults.channel_key = uricheck[5]; - } - } - } - parts = []; - } else { - // Extract the port+ssl if we find one - if (parts[0].search(/:/) > 0) { - defaults.port = parts[0].substring(parts[0].search(/:/) + 1); - defaults.server = parts[0].substring(0, parts[0].search(/:/)); - if (defaults.port[0] === '+') { - defaults.port = parseInt(defaults.port.substring(1), 10); - defaults.ssl = true; - } else { - defaults.ssl = false; - } - - } else { - defaults.server = parts[0]; - } - - parts.shift(); - } - } - - if (parts.length > 0 && parts[0]) { - defaults.channel = '#' + parts[0]; - parts.shift(); - } - } - - // If any settings have been given by the server.. override any auto detected settings - /** - * Get any server restrictions as set in the server config - * These settings can not be changed in the server selection dialog - */ - if (_kiwi.app.server_settings && _kiwi.app.server_settings.connection) { - if (_kiwi.app.server_settings.connection.server) { - defaults.server = _kiwi.app.server_settings.connection.server; - } - - if (_kiwi.app.server_settings.connection.port) { - defaults.port = _kiwi.app.server_settings.connection.port; - } - - if (_kiwi.app.server_settings.connection.ssl) { - defaults.ssl = _kiwi.app.server_settings.connection.ssl; - } - - if (_kiwi.app.server_settings.connection.channel) { - defaults.channel = _kiwi.app.server_settings.connection.channel; - } - - if (_kiwi.app.server_settings.connection.channel_key) { - defaults.channel_key = _kiwi.app.server_settings.connection.channel_key; - } - - if (_kiwi.app.server_settings.connection.nick) { - defaults.nick = _kiwi.app.server_settings.connection.nick; - } - } - - // Set any random numbers if needed - defaults.nick = defaults.nick.replace('?', Math.floor(Math.random() * 100000).toString()); - - if (getQueryVariable('encoding')) - defaults.encoding = getQueryVariable('encoding'); - + populateDefaultServerSettings: function() { + var defaults = _kiwi.app.defaultServerSettings(); this.view.populateFields(defaults); }, diff --git a/client/src/models/pluginmanager.js b/client/src/models/pluginmanager.js index 7a5181b..bc5f628 100644 --- a/client/src/models/pluginmanager.js +++ b/client/src/models/pluginmanager.js @@ -2,18 +2,24 @@ _kiwi.model.PluginManager = Backbone.Model.extend({ initialize: function () { this.$plugin_holder = $('') .appendTo(_kiwi.app.view.$el); + + this.loading_plugins = 0; this.loaded_plugins = {}; }, // Load an applet within this panel load: function (url) { + var that = this; + if (this.loaded_plugins[url]) { this.unload(url); } + this.loading_plugins++; + this.loaded_plugins[url] = $('
'); this.loaded_plugins[url].appendTo(this.$plugin_holder) - .load(url); + .load(url, _.bind(that.pluginLoaded, that)); }, @@ -24,5 +30,15 @@ _kiwi.model.PluginManager = Backbone.Model.extend({ this.loaded_plugins[url].remove(); delete this.loaded_plugins[url]; - } + }, + + + // Called after each plugin is loaded + pluginLoaded: function() { + this.loading_plugins--; + + if (this.loading_plugins === 0) { + this.trigger('loaded'); + } + }, }); \ No newline at end of file -- 2.25.1