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