Merge branch '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 bs.push(message_obj);
48
49 // Keep the scrolback limited
50 if (bs.length > scrollback) {
51 bs.splice(scrollback);
52 }
53 this.set({"scrollback": bs}, {silent: true});
54
55 this.trigger("msg", message_obj);
56 },
57
58
59 clearMessages: function () {
60 this.set({'scrollback': []}, {silent: true});
61 this.addMsg('', 'Window cleared');
62
63 this.view.render();
64 },
65
66 closePanel: function () {
67 if (this.view) {
68 this.view.unbind();
69 this.view.remove();
70 this.view = undefined;
71 delete this.view;
72 }
73
74 var members = this.get('members');
75 if (members) {
76 members.reset([]);
77 this.unset('members');
78 }
79
80 _kiwi.app.panels.remove(this);
81
82 this.unbind();
83 this.destroy();
84
85 // If closing the active panel, switch to the server panel
86 if (this.cid === _kiwi.app.panels.active.cid) {
87 _kiwi.app.panels.server.view.show();
88 }
89 },
90
91 // Alias to closePanel() for child objects to override
92 close: function () {
93 return this.closePanel();
94 },
95
96 isChannel: function () {
97 var channel_prefix = _kiwi.gateway.get('channel_prefix'),
98 this_name = this.get('name');
99
100 if (this.isApplet() || !this_name) return false;
101 return (channel_prefix.indexOf(this_name[0]) > -1);
102 },
103
104 isApplet: function () {
105 return this.applet ? true : false;
106 },
107
108 isServer: function () {
109 return this.server ? true : false;
110 },
111
112 isActive: function () {
113 return (_kiwi.app.panels.active === this);
114 }
115 });