Merge pull request #484 from M2Ys4U/dep-update
[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 channelInfo: 'channelInfo', mode: 'mode'
70 };
71
72 // Proxy each gateway method
73 _.each(funcs, function(gateway_fn, func_name) {
74 obj[func_name] = function() {
75 var fn_name = gateway_fn;
76
77 // Add connection_id to the argument list
78 var args = Array.prototype.slice.call(arguments, 0);
79 args.unshift(connection_id);
80
81 // Call the gateway function on behalf of this connection
82 return _kiwi.gateway[fn_name].apply(_kiwi.gateway, args);
83 };
84 });
85
86 return obj;
87 },
88
89 ControlInput: function() {
90 var obj = new this.EventComponent(_kiwi.app.controlbox);
91 var funcs = {
92 run: 'processInput', addPluginIcon: 'addPluginIcon'
93 };
94
95 _.each(funcs, function(controlbox_fn, func_name) {
96 obj[func_name] = function() {
97 var fn_name = controlbox_fn;
98 return _kiwi.app.controlbox[fn_name].apply(_kiwi.app.controlbox, arguments);
99 };
100 });
101
102 return obj;
103 }
104 },
105
106 // Entry point to start the kiwi application
107 start: function (opts, callback) {
108 var continueStart, locale;
109 opts = opts || {};
110
111 continueStart = function (locale, s, xhr) {
112 if (locale) {
113 _kiwi.global.i18n = new Jed(locale);
114 } else {
115 _kiwi.global.i18n = new Jed();
116 }
117
118 _kiwi.app = new _kiwi.model.Application(opts);
119
120 // Start the client up
121 _kiwi.app.start();
122
123 // Now everything has started up, load the plugin manager for third party plugins
124 _kiwi.global.plugins = new _kiwi.model.PluginManager();
125
126 callback && callback();
127 };
128
129 // Set up the settings datastore
130 _kiwi.global.settings = _kiwi.model.DataStore.instance('kiwi.settings');
131 _kiwi.global.settings.load();
132
133 // Set the window title
134 window.document.title = opts.server_settings.client.window_title || 'Kiwi IRC';
135
136 locale = _kiwi.global.settings.get('locale');
137 if (!locale) {
138 $.getJSON(opts.base_path + '/assets/locales/magic.json', continueStart);
139 } else {
140 $.getJSON(opts.base_path + '/assets/locales/' + locale + '.json', continueStart);
141 }
142 }
143 };
144
145
146
147 // If within a closure, expose the kiwi globals
148 if (typeof global !== 'undefined') {
149 global.kiwi = _kiwi.global;
150 } else {
151 // Not within a closure so set a var in the current scope
152 var kiwi = _kiwi.global;
153 }