Merge branch 'chanlist' of https://github.com/CoryChaplin/KiwiIRC into development
[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 addMediaMessageType: function(match, buildHtml) {
28 _kiwi.view.MediaMessage.addType(match, buildHtml);
29 },
30
31 // Event managers for plugins
32 components: {
33 EventComponent: function(event_source, proxy_event_name) {
34 function proxyEvent(event_name, event_data) {
35 if (proxy_event_name !== 'all') {
36 event_data = event_name.event_data;
37 event_name = event_name.event_name;
38 }
39
40 this.trigger(event_name, event_data);
41 }
42
43 // The event we are to proxy
44 proxy_event_name = proxy_event_name || 'all';
45
46
47 _.extend(this, Backbone.Events);
48 this._source = event_source;
49
50 // Proxy the events to this dispatcher
51 event_source.on(proxy_event_name, proxyEvent, this);
52
53 // Clean up this object
54 this.dispose = function () {
55 event_source.off(proxy_event_name, proxyEvent);
56 this.off();
57 delete this.event_source;
58 };
59 },
60
61 Network: function(connection_id) {
62 var connection_event;
63
64 if (typeof connection_id !== 'undefined') {
65 connection_event = 'connection:' + connection_id.toString();
66 }
67
68 var obj = new this.EventComponent(_kiwi.gateway, connection_event);
69 var funcs = {
70 kiwi: 'kiwi', raw: 'raw', kick: 'kick', topic: 'topic',
71 part: 'part', join: 'join', action: 'action', ctcp: 'ctcp',
72 notice: 'notice', msg: 'privmsg', changeNick: 'changeNick',
73 channelInfo: 'channelInfo', mode: 'mode'
74 };
75
76 // Proxy each gateway method
77 _.each(funcs, function(gateway_fn, func_name) {
78 obj[func_name] = function() {
79 var fn_name = gateway_fn;
80
81 // Add connection_id to the argument list
82 var args = Array.prototype.slice.call(arguments, 0);
83 args.unshift(connection_id);
84
85 // Call the gateway function on behalf of this connection
86 return _kiwi.gateway[fn_name].apply(_kiwi.gateway, args);
87 };
88 });
89
90 return obj;
91 },
92
93 ControlInput: function() {
94 var obj = new this.EventComponent(_kiwi.app.controlbox);
95 var funcs = {
96 run: 'processInput', addPluginIcon: 'addPluginIcon'
97 };
98
99 _.each(funcs, function(controlbox_fn, func_name) {
100 obj[func_name] = function() {
101 var fn_name = controlbox_fn;
102 return _kiwi.app.controlbox[fn_name].apply(_kiwi.app.controlbox, arguments);
103 };
104 });
105
106 return obj;
107 }
108 },
109
110 // Entry point to start the kiwi application
111 start: function (opts, callback) {
112 var continueStart, locale;
113 opts = opts || {};
114
115 continueStart = function (locale, s, xhr) {
116 if (locale) {
117 _kiwi.global.i18n = new Jed(locale);
118 } else {
119 _kiwi.global.i18n = new Jed();
120 }
121
122 _kiwi.app = new _kiwi.model.Application(opts);
123
124 // Start the client up
125 _kiwi.app.start();
126
127 // Now everything has started up, load the plugin manager for third party plugins
128 _kiwi.global.plugins = new _kiwi.model.PluginManager();
129
130 callback && callback();
131 };
132
133 // Set up the settings datastore
134 _kiwi.global.settings = _kiwi.model.DataStore.instance('kiwi.settings');
135 _kiwi.global.settings.load();
136
137 // Set the window title
138 window.document.title = opts.server_settings.client.window_title || 'Kiwi IRC';
139
140 locale = _kiwi.global.settings.get('locale');
141 if (!locale) {
142 $.getJSON(opts.base_path + '/assets/locales/magic.json', continueStart);
143 } else {
144 $.getJSON(opts.base_path + '/assets/locales/' + locale + '.json', continueStart);
145 }
146 }
147 };
148
149
150
151 // If within a closure, expose the kiwi globals
152 if (typeof global !== 'undefined') {
153 global.kiwi = _kiwi.global;
154 } else {
155 // Not within a closure so set a var in the current scope
156 var kiwi = _kiwi.global;
157 }