Merge pull request #423 from M2Ys4U/title
[KiwiIRC.git] / client / src / app.js
1 // Holds anything kiwi client specific (ie. front, gateway, _kiwi.plugs..)
2 /**
3 * @namespace
4 */
5 var _kiwi = {};
6
7 _kiwi.model = {};
8 _kiwi.view = {};
9 _kiwi.applets = {};
10
11
12 /**
13 * A global container for third party access
14 * Will be used to access a limited subset of kiwi functionality
15 * and data (think: plugins)
16 */
17 _kiwi.global = {
18 settings: undefined, // Instance of _kiwi.model.DataStore
19 plugins: undefined,
20 utils: undefined, // TODO: Re-usable methods
21 user: undefined, // TODO: Limited user methods
22 server: undefined, // TODO: Limited server methods
23
24 // TODO: think of a better term for this as it will also refer to queries
25 channels: undefined, // TODO: Limited access to panels list
26
27 // Event managers for plugins
28 components: {
29 EventComponent: function(event_source, proxy_event_name) {
30 function proxyEvent(event_name, event_data) {
31 if (proxy_event_name !== 'all') {
32 event_data = event_name.event_data;
33 event_name = event_name.event_name
34 }
35 //console.log(proxy_event_name, event_name, event_data);
36 this.trigger(event_name, event_data);
37 }
38
39 // The event we are to proxy
40 proxy_event_name = proxy_event_name || 'all';
41
42
43 _.extend(this, Backbone.Events);
44 this._source = event_source;
45
46 // Proxy the events to this dispatcher
47 event_source.on(proxy_event_name, proxyEvent, this);
48
49 // Clean up this object
50 this.dispose = function () {
51 event_source.off(proxy_event_name, proxyEvent);
52 this.off();
53 delete this.event_source;
54 };
55 },
56
57 Network: function(connection_id) {
58 var connection_event;
59
60 if (typeof connection_id !== 'undefined') {
61 connection_event = 'connection:' + connection_id.toString();
62 }
63
64 var obj = new this.EventComponent(_kiwi.gateway, connection_event);
65 var funcs = {
66 kiwi: 'kiwi', raw: 'raw', kick: 'kick', topic: 'topic',
67 part: 'part', join: 'join', action: 'action', ctcp: 'ctcp',
68 notice: 'notice', msg: 'privmsg', changeNick: 'changeNick'
69 };
70
71 // Proxy each gateway method
72 _.each(funcs, function(gateway_fn, func_name) {
73 obj[func_name] = function() {
74 var fn_name = gateway_fn;
75
76 // Add connection_id to the argument list
77 var args = Array.prototype.slice.call(arguments, 0);
78 args.unshift(connection_id);
79
80 // Call the gateway function on behalf of this connection
81 return _kiwi.gateway[fn_name].apply(_kiwi.gateway, args);
82 };
83 });
84
85 return obj;
86 },
87
88 ControlInput: function() {
89 var obj = new this.EventComponent(_kiwi.app.controlbox);
90 var funcs = {
91 processInput: 'run', addPluginIcon: 'addPluginIcon'
92 };
93
94 _.each(funcs, function(controlbox_fn, func_name) {
95 obj[func_name] = function() {
96 var fn_name = controlbox_fn;
97 return _kiwi.app.controlbox[fn_name].apply(_kiwi.app.controlbox, arguments);
98 };
99 });
100
101 return obj;
102 }
103 },
104
105 // Entry point to start the kiwi application
106 start: function (opts, callback) {
107 var continueStart, locale;
108 opts = opts || {};
109
110 continueStart = function (locale, s, xhr) {
111 if (locale) {
112 _kiwi.global.i18n = new Jed({locale_data: locale, domain: xhr.getResponseHeader('Content-Language')});
113 } else {
114 _kiwi.global.i18n = new Jed();
115 }
116
117 _kiwi.app = new _kiwi.model.Application(opts);
118
119 if (opts.kiwi_server) {
120 _kiwi.app.kiwi_server = opts.kiwi_server;
121 }
122
123 // Start the client up
124 _kiwi.app.start();
125
126 // Now everything has started up, load the plugin manager for third party plugins
127 _kiwi.global.plugins = new _kiwi.model.PluginManager();
128
129 callback && callback();
130 };
131
132 // Set up the settings datastore
133 _kiwi.global.settings = _kiwi.model.DataStore.instance('kiwi.settings');
134 _kiwi.global.settings.load();
135
136 // Set the window title
137 window.document.title = opts.server_settings.client.window_title || 'Kiwi IRC';
138
139 locale = _kiwi.global.settings.get('locale');
140 if (!locale) {
141 $.getJSON(opts.base_path + '/assets/locales/magic.json', continueStart);
142 } else {
143 $.getJSON(opts.base_path + '/assets/locales/' + locale + '.json', continueStart);
144 }
145 }
146 };
147
148
149
150 // If within a closure, expose the kiwi globals
151 if (typeof global !== 'undefined') {
152 global.kiwi = _kiwi.global;
153 } else {
154 // Not within a closure so set a var in the current scope
155 var kiwi = _kiwi.global;
156 }