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