Commit 72e9698d authored by JC Brand's avatar JC Brand

Refactored Status messaging.

Also ixed but where status wasn't set at initial page load.
parent 09e34353
...@@ -215,7 +215,7 @@ ...@@ -215,7 +215,7 @@
fullname = this.model.get('fullname'), fullname = this.model.get('fullname'),
time, stamp; time, stamp;
if (xmppchat.xmppstatus.getOwnStatus() === 'offline') { if (xmppchat.xmppstatus.getStatus() === 'offline') {
// only update the UI if the user is not offline // only update the UI if the user is not offline
return; return;
} }
...@@ -1182,7 +1182,7 @@ ...@@ -1182,7 +1182,7 @@
presence_type = $(presence).attr('type'), presence_type = $(presence).attr('type'),
show = $(presence).find('show'), show = $(presence).find('show'),
status_message = $(presence).find('status'), status_message = $(presence).find('status'),
item; item, model;
if ((($(presence).find('x').attr('xmlns') || '').indexOf(Strophe.NS.MUC) === 0) || (this.isSelf(bare_jid))) { if ((($(presence).find('x').attr('xmlns') || '').indexOf(Strophe.NS.MUC) === 0) || (this.isSelf(bare_jid))) {
// Ignore MUC or self-addressed stanzas // Ignore MUC or self-addressed stanzas
...@@ -1344,24 +1344,49 @@ ...@@ -1344,24 +1344,49 @@
xmppchat.XMPPStatus = Backbone.Model.extend({ xmppchat.XMPPStatus = Backbone.Model.extend({
sendPresence: function (type) { initialize: function () {
if (type === undefined) { this.set({
type = this.getOwnStatus() || 'online'; 'status' : this.getStatus(),
'status_message' : this.getStatusMessage(),
});
},
initStatus: function () {
/* Called when the page is loaded and we aren't sure what the users
* status is. Will also cause the UI to be updated with the correct
* status.
*/
var stat = this.getStatus();
if (stat === undefined) {
stat = 'online';
this.setStatus(stat);
} else {
this.sendPresence(stat);
} }
},
sendPresence: function (type) {
xmppchat.connection.send($pres({'type':type})); xmppchat.connection.send($pres({'type':type}));
}, },
getOwnStatus: function () { getStatus: function () {
return store.get(xmppchat.connection.bare_jid+'-xmpp-status'); return store.get(xmppchat.connection.bare_jid+'-xmpp-status');
}, },
setOwnStatus: function (value) { setStatus: function (value) {
this.sendPresence(value); this.sendPresence(value);
this.set({'status': value});
store.set(xmppchat.connection.bare_jid+'-xmpp-status', value); store.set(xmppchat.connection.bare_jid+'-xmpp-status', value);
}, },
setCustomStatus: function (presence_type, custom_status) { setStatusMessage: function (status_message) {
xmppchat.connection.send($pres({'type':presence_type}).c('status').t(custom_status)); xmppchat.connection.send($pres({'type':this.getStatus()}).c('status').t(status_message));
this.set({'status_message': status_message});
store.set(xmppchat.connection.bare_jid+'-xmpp-custom-status', status_message);
},
getStatusMessage: function () {
return store.get(xmppchat.connection.bare_jid+'-xmpp-custom-status');
} }
}); });
...@@ -1372,8 +1397,8 @@ ...@@ -1372,8 +1397,8 @@
events: { events: {
"click a.choose-xmpp-status": "toggleOptions", "click a.choose-xmpp-status": "toggleOptions",
"click #fancy-xmpp-status-select a.change-xmpp-status-message": "renderStatusChangeForm", "click #fancy-xmpp-status-select a.change-xmpp-status-message": "renderStatusChangeForm",
"submit #set-custom-xmpp-status": "changeStatusMessage", "submit #set-custom-xmpp-status": "setStatusMessage",
"click .dropdown dd ul li a": "setOwnStatus" "click .dropdown dd ul li a": "setStatus"
}, },
toggleOptions: function (ev) { toggleOptions: function (ev) {
...@@ -1383,52 +1408,52 @@ ...@@ -1383,52 +1408,52 @@
change_status_message_template: _.template( change_status_message_template: _.template(
'<form id="set-custom-xmpp-status">' + '<form id="set-custom-xmpp-status">' +
'<input type="text" class="custom-xmpp-status" {{ chat_status }}" placeholder="Custom status"/>' + '<input type="text" class="custom-xmpp-status" {{ status_message }}" placeholder="Custom status"/>' +
'<button type="submit">Save</button>' + '<button type="submit">Save</button>' +
'</form>'), '</form>'),
status_template: _.template( status_template: _.template(
'<div class="xmpp-status">' + '<div class="xmpp-status">' +
'<a class="choose-xmpp-status {{ presence_type }}" href="#" title="Click to change your chat status">' + '<a class="choose-xmpp-status {{ presence_type }}" href="#" title="Click to change your chat status">' +
'{{ chat_status }} <span class="value">{{ chat_status }}</span>' + '{{ status_message }} <span class="value">{{ status_message }}</span>' +
'</a>' + '</a>' +
'<a class="change-xmpp-status-message" href="#" Title="Click here to write a custom status message"></a>' + '<a class="change-xmpp-status-message" href="#" Title="Click here to write a custom status message"></a>' +
'</div>'), '</div>'),
changeStatusMessage: function (ev) {
ev.preventDefault();
var chat_status = $(ev.target).find('input').attr('value');
var presence_type = this.model.getOwnStatus() || 'I am offline';
$(this.el).find('#fancy-xmpp-status-select').html(
this.status_template({
'chat_status': chat_status,
'presence_type': presence_type
}));
this.model.setCustomStatus(presence_type, chat_status);
},
renderStatusChangeForm: function (ev) { renderStatusChangeForm: function (ev) {
ev.preventDefault(); ev.preventDefault();
var chat_status = this.model.getOwnStatus() || 'offline'; var status_message = this.model.getStatus() || 'offline';
var input = this.change_status_message_template({'chat_status': chat_status}); var input = this.change_status_message_template({'status_message': status_message});
this.$el.find('.xmpp-status').replaceWith(input); this.$el.find('.xmpp-status').replaceWith(input);
this.$el.find('.custom-xmpp-status').focus().focus(); this.$el.find('.custom-xmpp-status').focus().focus();
}, },
setOwnStatus: function (ev) { setStatusMessage: function (ev) {
ev.preventDefault();
var status_message = $(ev.target).find('input').attr('value');
if (status_message === "") {
}
this.model.setStatusMessage(status_message);
},
setStatus: function (ev) {
ev.preventDefault(); ev.preventDefault();
var $el = $(ev.target).find('span'), var $el = $(ev.target).find('span'),
value = $el.text(); value = $el.text();
this.model.setStatus(value);
this.$el.find(".dropdown dd ul").hide();
},
updateStatusUI: function (ev) {
var stat = ev.get('status'),
status_message = ev.get('status_message') || "I am " + stat;
$(this.el).find('#fancy-xmpp-status-select').html( $(this.el).find('#fancy-xmpp-status-select').html(
this.status_template({ this.status_template({
'chat_status': 'I am ' + value, 'presence_type': stat,
'presence_type': value 'status_message': status_message
})); }));
$(this.el).find(".dropdown dd ul").hide();
$(this.el).find("#source").val($($el).find("span.value").html());
this.model.setOwnStatus(value);
}, },
choose_template: _.template( choose_template: _.template(
...@@ -1446,18 +1471,18 @@ ...@@ -1446,18 +1471,18 @@
'</li>'), '</li>'),
initialize: function () { initialize: function () {
// Replace the default dropdown with something nicer
// -------------------------------------------------
var $select = $(this.el).find('select#select-xmpp-status'), var $select = $(this.el).find('select#select-xmpp-status'),
presence_type = this.model.getOwnStatus() || 'offline', presence_type = this.model.getStatus() || 'offline',
options = $('option', $select), options = $('option', $select),
that = this; that = this;
$(this.el).html(this.choose_template()); $(this.el).html(this.choose_template());
$(this.el).find('#fancy-xmpp-status-select') $(this.el).find('#fancy-xmpp-status-select')
.html(this.status_template({ .html(this.status_template({
'chat_status': "I am " + presence_type, 'status_message': "I am " + presence_type,
'presence_type': presence_type 'presence_type': presence_type
})); }));
// iterate through all the <option> elements and create UL // iterate through all the <option> elements and create UL
options.each(function(){ options.each(function(){
$(that.el).find("#target dd ul").append(that.option_template({ $(that.el).find("#target dd ul").append(that.option_template({
...@@ -1466,6 +1491,11 @@ ...@@ -1466,6 +1491,11 @@
})).hide(); })).hide();
}); });
$select.remove(); $select.remove();
// Listen for status change on the model and initialize
// ----------------------------------------------------
this.options.model.on("change", $.proxy(this.updateStatusUI, this));
this.model.initStatus();
} }
}); });
...@@ -1528,8 +1558,6 @@ ...@@ -1528,8 +1558,6 @@
'model': this.xmppstatus 'model': this.xmppstatus
}); });
this.xmppstatus.sendPresence();
// Controlbox toggler // Controlbox toggler
$toggle.bind('click', $.proxy(function (e) { $toggle.bind('click', $.proxy(function (e) {
e.preventDefault(); e.preventDefault();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment