X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=client%2Fsrc%2Fapp.js;h=6fe8172fc6b33f01e1993fecef875f1b5cfe456a;hb=181f3f9c5bd30ab10560559609eff3ee040c1d33;hp=df454bcd24160fbd4a150aa173de1a93975228e9;hpb=9b609554e86f6c084878155b2333366f10e376e0;p=KiwiIRC.git diff --git a/client/src/app.js b/client/src/app.js index df454bc..6fe8172 100644 --- a/client/src/app.js +++ b/client/src/app.js @@ -4,6 +4,7 @@ */ var _kiwi = {}; +_kiwi.misc = {}; _kiwi.model = {}; _kiwi.view = {}; _kiwi.applets = {}; @@ -19,7 +20,18 @@ _kiwi.global = { settings: undefined, // Instance of _kiwi.model.DataStore plugins: undefined, // Instance of _kiwi.model.PluginManager events: undefined, // Instance of PluginInterface - utils: {}, // TODO: Re-usable methods + rpc: undefined, // Instance of WebsocketRpc + utils: {}, // References to misc. re-usable helpers / functions + + 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; + }, addMediaMessageType: function(match, buildHtml) { _kiwi.view.MediaMessage.addType(match, buildHtml); @@ -48,8 +60,6 @@ _kiwi.global = { */ function proxyEvent(event_name, event_data) { if (proxy_event_name == 'all') { - event_name = event_data.event_name; - event_data = event_data.event_data; } else { event_data = event_name.event_data; event_name = event_name.event_name; @@ -61,7 +71,6 @@ _kiwi.global = { // The event we are to proxy proxy_event_name = proxy_event_name || 'all'; - _.extend(this, Backbone.Events); this._source = event_source; @@ -79,20 +88,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', ctcpRequest: 'ctcpRequest', ctcpResponse: 'ctcpResponse', - notice: 'notice', msg: 'privmsg', changeNick: 'changeNick', - channelInfo: 'channelInfo', mode: 'mode' + 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; @@ -106,6 +132,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; }, @@ -122,17 +188,51 @@ _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) { + this.initUtils(); + + // Set up the settings datastore + _kiwi.global.settings = _kiwi.model.DataStore.instance('kiwi.settings'); + _kiwi.global.settings.load(); + + // Set the window title + window.document.title = opts.server_settings.client.window_title || 'Kiwi IRC'; + + 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(); + }); + }); + + 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(); + }); + }); + + + Promise.all([locale_promise, theme_promise]).then(function () { _kiwi.app = new _kiwi.model.Application(opts); // Start the client up @@ -146,41 +246,6 @@ _kiwi.global = { callback(); }); - - textThemeLoaded = function(text_theme, s, xhr) { - opts.text_theme = text_theme; - - jobs.finishJob('load_text_theme'); - }; - - localeLoaded = function(locale, s, xhr) { - if (locale) { - _kiwi.global.i18n = new Jed(locale); - } else { - _kiwi.global.i18n = new Jed(); - } - - jobs.finishJob('load_locale'); - }; - - // Set up the settings datastore - _kiwi.global.settings = _kiwi.model.DataStore.instance('kiwi.settings'); - _kiwi.global.settings.load(); - - // Set the window title - window.document.title = opts.server_settings.client.window_title || 'Kiwi IRC'; - - 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); - } - - jobs.registerJob('load_text_theme'); - text_theme = opts.server_settings.client.settings.text_theme || 'default'; - $.getJSON(opts.base_path + '/assets/text_themes/' + text_theme + '.json', textThemeLoaded); }, start: function() { @@ -371,4 +436,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 +}