Fixed typo introduced in commit c11bb23
[KiwiIRC.git] / client / assets / dev / 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 };
70
71 // Proxy each gateway method
72 _.each(funcs, function(gateway_fn, func_name) {
73 obj[func_name] = function() {
74 var fn_name = gateway_fn;
75
76 // Add connection_id to the argument list
77 var args = Array.prototype.slice.call(arguments, 0);
78 args.unshift(connection_id);
79
80 // Call the gateway function on behalf of this connection
81 return _kiwi.gateway[fn_name].apply(_kiwi.gateway, args);
82 };
83 });
84
85 return obj;
86 },
87
88 ControlInput: function() {
89 var obj = new this.EventComponent(_kiwi.app.controlbox);
90 var funcs = {
91 processInput: 'run', addPluginIcon: 'addPluginIcon'
92 };
93
94 _.each(funcs, function(controlbox_fn, func_name) {
95 obj[func_name] = function() {
96 var fn_name = controlbox_fn;
97 return _kiwi.app.controlbox[fn_name].apply(_kiwi.app.controlbox, arguments);
98 };
99 });
100
101 return obj;
102 }
103 },
104
105 // Entry point to start the kiwi application
106 start: function (opts) {
107 opts = opts || {};
108
109 // Load the plugin manager
110 _kiwi.global.plugins = new _kiwi.model.PluginManager();
111
112 // Set up the settings datastore
113 _kiwi.global.settings = _kiwi.model.DataStore.instance('kiwi.settings');
114 _kiwi.global.settings.load();
115
116 _kiwi.app = new _kiwi.model.Application(opts);
117
118 if (opts.kiwi_server) {
119 _kiwi.app.kiwi_server = opts.kiwi_server;
120 }
121
122 // Start the client up
123 _kiwi.app.start();
124
125 return true;
126 }
127 };
128
129
130
131 // If within a closure, expose the kiwi globals
132 if (typeof global !== 'undefined') {
133 global.kiwi = _kiwi.global;
134 } else {
135 // Not within a closure so set a var in the current scope
136 var kiwi = _kiwi.global;
137 }