X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=client%2Fsrc%2Fapp.js;h=619507cc7e2569c22b6f35c2cf12d77573571142;hb=f91187cfa693c4093033f1698deb90e6b70a7acc;hp=39121b2f4cf84e6a83faf08c8b6b5dc8c8c0dff4;hpb=c3d988bc1d1fca76e6d1d8360a6a2e8c6b7da505;p=KiwiIRC.git diff --git a/client/src/app.js b/client/src/app.js index 39121b2..619507c 100644 --- a/client/src/app.js +++ b/client/src/app.js @@ -4,9 +4,11 @@ */ var _kiwi = {}; +_kiwi.misc = {}; _kiwi.model = {}; _kiwi.view = {}; _kiwi.applets = {}; +_kiwi.utils = {}; /** @@ -17,13 +19,24 @@ _kiwi.applets = {}; _kiwi.global = { build_version: '', // Kiwi IRC version this is built from (Set from index.html) settings: undefined, // Instance of _kiwi.model.DataStore - plugins: undefined, - utils: undefined, // TODO: Re-usable methods - user: undefined, // TODO: Limited user methods - server: undefined, // TODO: Limited server methods - - // TODO: think of a better term for this as it will also refer to queries - channels: undefined, // TODO: Limited access to panels list + plugins: undefined, // Instance of _kiwi.model.PluginManager + events: undefined, // Instance of PluginInterface + rpc: undefined, // Instance of WebsocketRpc + utils: {}, // References to misc. re-usable helpers / functions + + // Make public some internal utils for plugins to make use of + initUtils: function() { + this.utils.randomString = randomString; + this.utils.secondsToTime = secondsToTime; + this.utils.parseISO8601 = parseISO8601; + this.utils.escapeRegex = escapeRegex; + this.utils.formatIRCMsg = formatIRCMsg; + this.utils.styleText = styleText; + this.utils.hsl2rgb = hsl2rgb; + + this.utils.notifications = _kiwi.utils.notifications; + this.utils.formatDate = _kiwi.utils.formatDate; + }, addMediaMessageType: function(match, buildHtml) { _kiwi.view.MediaMessage.addType(match, buildHtml); @@ -32,8 +45,27 @@ _kiwi.global = { // Event managers for plugins components: { EventComponent: function(event_source, proxy_event_name) { + /* + * proxyEvent() listens for events then re-triggers them on its own + * event emitter. Why? So we can .off() on this emitter without + * effecting the source of events. Handy for plugins that we don't + * trust meddling with the core events. + * + * If listening for 'all' events the arguments are as follows: + * 1. Name of the triggered event + * 2. The event data + * For all other events, we only have one argument: + * 1. The event data + * + * When this is used via `new kiwi.components.Network()`, this listens + * for 'all' events so the first argument is the event name which is + * the connection ID. We don't want to re-trigger this event name so + * we need to juggle the arguments to find the real event name we want + * to emit. + */ function proxyEvent(event_name, event_data) { - if (proxy_event_name !== 'all') { + if (proxy_event_name == 'all') { + } else { event_data = event_name.event_data; event_name = event_name.event_name; } @@ -44,7 +76,6 @@ _kiwi.global = { // The event we are to proxy proxy_event_name = proxy_event_name || 'all'; - _.extend(this, Backbone.Events); this._source = event_source; @@ -62,19 +93,37 @@ _kiwi.global = { Network: function(connection_id) { var connection_event; + // If no connection id given, use all connections if (typeof connection_id !== 'undefined') { connection_event = 'connection:' + connection_id.toString(); + } else { + connection_event = 'connection'; } + // Helper to get the network object + var getNetwork = function() { + var network = typeof connection_id === 'undefined' ? + _kiwi.app.connections.active_connection : + _kiwi.app.connections.getByConnectionId(connection_id); + + return network ? + network : + undefined; + }; + + // Create the return object (events proxy from the gateway) var obj = new this.EventComponent(_kiwi.gateway, connection_event); + + // Proxy several gateway functions onto the return object var funcs = { kiwi: 'kiwi', raw: 'raw', kick: 'kick', topic: 'topic', part: 'part', join: 'join', action: 'action', ctcp: 'ctcp', - notice: 'notice', msg: 'privmsg', changeNick: 'changeNick', - channelInfo: 'channelInfo', mode: 'mode' + ctcpRequest: 'ctcpRequest', ctcpResponse: 'ctcpResponse', + notice: 'notice', msg: 'privmsg', say: 'privmsg', + changeNick: 'changeNick', channelInfo: 'channelInfo', + mode: 'mode', quit: 'quit' }; - // Proxy each gateway method _.each(funcs, function(gateway_fn, func_name) { obj[func_name] = function() { var fn_name = gateway_fn; @@ -88,6 +137,46 @@ _kiwi.global = { }; }); + // Now for some network related functions... + obj.createQuery = function(nick) { + var network, restricted_keys; + + network = getNetwork(); + if (!network) { + return; + } + + return network.createQuery(nick); + }; + + // Add the networks getters/setters + obj.get = function(name) { + var network, restricted_keys; + + network = getNetwork(); + if (!network) { + return; + } + + restricted_keys = [ + 'password' + ]; + if (restricted_keys.indexOf(name) > -1) { + return undefined; + } + + return network.get(name); + }; + + obj.set = function() { + var network = getNetwork(); + if (!network) { + return; + } + + return network.set.apply(network, arguments); + }; + return obj; }, @@ -104,66 +193,67 @@ _kiwi.global = { }; }); + // Give access to the control input textarea + obj.input = _kiwi.app.controlbox.$('.inp'); + return obj; } }, // Entry point to start the kiwi application init: function (opts, callback) { - var jobs, locale, localeLoaded, textThemeLoaded, text_theme; + var locale_promise, theme_promise, + that = this; + opts = opts || {}; - jobs = new JobManager(); - jobs.onFinish(function(locale, s, xhr) { - _kiwi.app = new _kiwi.model.Application(opts); + this.initUtils(); - // Start the client up - _kiwi.app.initializeInterfaces(); + // Set up the settings datastore + _kiwi.global.settings = _kiwi.model.DataStore.instance('kiwi.settings'); + _kiwi.global.settings.load(); - // Now everything has started up, load the plugin manager for third party plugins - _kiwi.global.plugins = new _kiwi.model.PluginManager(); + // Set the window title + window.document.title = opts.server_settings.client.window_title || 'Kiwi IRC'; - callback(); + locale_promise = new Promise(function (resolve) { + var locale = _kiwi.global.settings.get('locale') || 'magic'; + $.getJSON(opts.base_path + '/assets/locales/' + locale + '.json', function (locale) { + if (locale) { + that.i18n = new Jed(locale); + } else { + that.i18n = new Jed(); + } + resolve(); + }); }); - textThemeLoaded = function(text_theme, s, xhr) { - opts.text_theme = text_theme; + theme_promise = new Promise(function (resolve) { + var text_theme = opts.server_settings.client.settings.text_theme || 'default'; + $.getJSON(opts.base_path + '/assets/text_themes/' + text_theme + '.json', function(text_theme) { + opts.text_theme = text_theme; + resolve(); + }); + }); - jobs.finishJob('load_text_theme'); - }; - localeLoaded = function(locale, s, xhr) { - if (locale) { - _kiwi.global.i18n = new Jed(locale); - } else { - _kiwi.global.i18n = new Jed(); - } + Promise.all([locale_promise, theme_promise]).then(function () { + _kiwi.app = new _kiwi.model.Application(opts); - jobs.finishJob('load_locale'); - }; + // Start the client up + _kiwi.app.initializeInterfaces(); - // Set up the settings datastore - _kiwi.global.settings = _kiwi.model.DataStore.instance('kiwi.settings'); - _kiwi.global.settings.load(); + // Event emitter to let plugins interface with parts of kiwi + _kiwi.global.events = new PluginInterface(); - // Set the window title - window.document.title = opts.server_settings.client.window_title || 'Kiwi IRC'; + // Now everything has started up, load the plugin manager for third party plugins + _kiwi.global.plugins = new _kiwi.model.PluginManager(); - jobs.registerJob('load_locale'); - locale = _kiwi.global.settings.get('locale'); - if (!locale) { - $.getJSON(opts.base_path + '/assets/locales/magic.json', localeLoaded); - } else { - $.getJSON(opts.base_path + '/assets/locales/' + locale + '.json', localeLoaded); - } + callback(); - jobs.registerJob('load_text_theme'); - text_theme = opts.text_theme; - if (!text_theme) { - $.getJSON(opts.base_path + '/assets/text_themes/default.json', textThemeLoaded); - } else { - $.getJSON(opts.base_path + '/assets/text_themes/' + text_theme + '.json', textThemeLoaded); - } + }).then(null, function(err) { + console.error(err.stack); + }); }, start: function() { @@ -354,4 +444,4 @@ if (typeof global !== 'undefined') { } else { // Not within a closure so set a var in the current scope var kiwi = _kiwi.global; -} \ No newline at end of file +}