Merge branch 'socks' of https://github.com/M2Ys4U/KiwiIRC into development
[KiwiIRC.git] / client / assets / dev / model_panel.js
1 _kiwi.model.Panel = Backbone.Model.extend({
2 initialize: function (attributes) {
3 var name = this.get("name") || "";
4 this.view = new _kiwi.view.Panel({"model": this, "name": name});
5 this.set({
6 "scrollback": [],
7 "name": name
8 }, {"silent": true});
9 },
10
11 addMsg: function (nick, msg, type, opts) {
12 var message_obj, bs, d,
13 scrollback = (parseInt(_kiwi.global.settings.get('scrollback'), 10) || 250);
14
15 opts = opts || {};
16
17 // Time defaults to now
18 if (!opts || typeof opts.time === 'undefined') {
19 d = new Date();
20 opts.time = d.getHours().toString().lpad(2, "0") + ":" + d.getMinutes().toString().lpad(2, "0") + ":" + d.getSeconds().toString().lpad(2, "0");
21 }
22
23 // CSS style defaults to empty string
24 if (!opts || typeof opts.style === 'undefined') {
25 opts.style = '';
26 }
27
28 // Run through the plugins
29 message_obj = {"msg": msg, "time": opts.time, "nick": nick, "chan": this.get("name"), "type": type, "style": opts.style};
30 //tmp = _kiwi.plugs.run('addmsg', message_obj);
31 if (!message_obj) {
32 return;
33 }
34
35 // The CSS class (action, topic, notice, etc)
36 if (typeof message_obj.type !== "string") {
37 message_obj.type = '';
38 }
39
40 // Make sure we don't have NaN or something
41 if (typeof message_obj.msg !== "string") {
42 message_obj.msg = '';
43 }
44
45 // Update the scrollback
46 bs = this.get("scrollback");
47 if (bs) {
48 bs.push(message_obj);
49
50 // Keep the scrolback limited
51 if (bs.length > scrollback) {
52 bs.splice(scrollback);
53 }
54 this.set({"scrollback": bs}, {silent: true});
55 }
56
57 this.trigger("msg", message_obj);
58 },
59
60
61 clearMessages: function () {
62 this.set({'scrollback': []}, {silent: true});
63 this.addMsg('', 'Window cleared');
64
65 this.view.render();
66 },
67
68 closePanel: function () {
69 if (this.view) {
70 this.view.unbind();
71 this.view.remove();
72 this.view = undefined;
73 delete this.view;
74 }
75
76 var members = this.get('members');
77 if (members) {
78 members.reset([]);
79 this.unset('members');
80 }
81
82 _kiwi.app.panels.remove(this);
83
84 this.unbind();
85 this.destroy();
86
87 // If closing the active panel, switch to the server panel
88 if (this.cid === _kiwi.app.panels.active.cid) {
89 _kiwi.app.panels.server.view.show();
90 }
91 },
92
93 // Alias to closePanel() for child objects to override
94 close: function () {
95 return this.closePanel();
96 },
97
98 isChannel: function () {
99 var channel_prefix = _kiwi.gateway.get('channel_prefix'),
100 this_name = this.get('name');
101
102 if (this.isApplet() || !this_name) return false;
103 return (channel_prefix.indexOf(this_name[0]) > -1);
104 },
105
106 isApplet: function () {
107 return this.applet ? true : false;
108 },
109
110 isServer: function () {
111 return this.server ? true : false;
112 },
113
114 isActive: function () {
115 return (_kiwi.app.panels.active === this);
116 }
117 });