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