Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Updating Createroom #179

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 66 additions & 26 deletions createroom/createroom.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
var CandyShop = (function(self) { return self; }(CandyShop || {}));

CandyShop.CreateRoom = (function(self, Candy, $) {
self._options = {
domain: null
}
/** Object: about
*
* Contains:
Expand All @@ -23,18 +20,16 @@ CandyShop.CreateRoom = (function(self, Candy, $) {
/**
* Initializes the CreateRoom plugin with the default settings.
*/
self.init = function(options){
// apply the supplied options to the defaults specified
$.extend(true, self._options, options);

self.init = function(){
$(Candy).on('candy:view.room.after-add', function() {
self.appendButton();
// self.appendButton();
});
};

self.appendButton = function(){
if ($('#create-group').length === 0) {
$('#chat-tabs').after(self.Template.createButton);
var createRoomHtml = '<div id="create-group"><div class="click">+ Create Room</div></div>';
$('#chat-tabs').after(createRoomHtml);
$('#create-group').click(function () {
self.showModal();
$('#create-group-form').click(function(event) {
Expand All @@ -48,60 +43,105 @@ CandyShop.CreateRoom = (function(self, Candy, $) {
// Add focus to the form element when it's shown.
$('#create-group-form-name').focus();

$('#create-room-link').click(function(){
$('#create-group-form').submit();
});

// When we press the enter/return key in the form, submit the form.
$('#create-group-form input').keypress(function(event) {
if (event.which === 13 || event.keyCode === 13) {
event.preventDefault();
$('#create-group-form').submit();
}
});

$('#create-group-form').submit(function(event) {
event.preventDefault();
if ($('#create-group-form-name').val() === '') {
// Notify that group name cannot be blank.
$('.form-group.group-form-name-group').addClass('has-error');
$('#create-group-form-name').addClass('has-error');
$('#create-room-error-dialog').html('Room name cannot be blank').removeClass('hide');
// Remove classes after user either starts typing or has pasted in a name.
$('#create-group-form-name').focus(function() {
$('.form-group.group-form-name-group').removeClass('has-error');
$('#create-group-form-name').removeClass('has-error');
$('#create-room-error-dialog').html('').addClass('hide');
});
} else {
var roomName = $('#create-group-form-name').val().trim();
var roomTopic = $('#create-group-form-topic').val().trim();
var isPrivate = $('#create-group-form-is_private').prop('checked') ? 1 : 0;

// Create a valid conference domain.
var conferenceDomain = '@' + Candy.Core.getOptions().conferenceDomain + '.' + Candy.Core.getConnection().domain;

// Create a valid roomjid.
if(!self._options.domain) { // TODO: do this earlier. init() is *too* early because that happens before Candy connects, but maybe we could attach this in a post-connect event handler? That will require more research than I care for at the moment, though.
self._options.domain = "conference" + '.' + Candy.Core.getConnection().domain;
}
var roomJid = roomName.replace(/[^A-Z0-9]+/ig, "_").toLowerCase() + '@' + self._options.domain ;
var roomJid = roomName.replace(/[^A-Z0-9]+/ig, '_').toLowerCase() + conferenceDomain;

// Once we've joined the room, send configuration information.
$(Candy).on('candy:view.room.after-add', function(ev, obj) {
if (obj.roomJid.toUpperCase() === roomJid.toUpperCase()) {
// Configuration items for setting room name.
var configFormType = $build('field', { 'var': 'FORM_TYPE' })
.c('value').t('http://jabber.org/protocol/muc#roomconfig');
var configRoomName = $build('field', { 'var': 'muc#roomconfigRoomName' }).c('value').t(roomName);
var config = [configFormType.tree(), configRoomName.tree()];
var configRoomName = $build('field', { 'var': 'muc#roomconfig_roomname' }).c('value').t(roomName);
var configRoomMembersOnly = $build('field', { 'var': 'much#roomconfig_membersonly' }).c('value').t(isPrivate);
var config = [configFormType.tree(), configRoomName.tree(), configRoomMembersOnly.tree()];

// Send the configuration form to the server, and on success update our DOM.
Candy.Core.log('[CandyShop CreateRoom] sending new room configuration for ' + roomJid);
Candy.Core.getConnection().muc.saveConfiguration(roomJid, config, function(stanza) {
var jid = $(stanza).attr('from');

if (jid === roomJid) {
Candy.View.Pane.Chat.getTab(roomJid).find('.label').html(roomName);
Candy.View.Pane.Chat.getTab(roomJid).find('.room-label').html(roomName);
Candy.Core.Action.Jabber.Room.Admin.SetSubject(roomJid, roomTopic);
}

if (isPrivate) {
Candy.View.Pane.Chat.getTab(roomJid).find('i.connect-group-chat-icon').removeClass('connect-group-chat-icon').addClass('fa fa-lock');
}

// Add it to the roomlist in leftpanehead
if (typeof CandyShop.LeftPaneHead === 'object') {
CandyShop.LeftPaneHead.RoomList.AddRoom(roomJid, roomName, isPrivate);
}
});
}
});

Candy.Core.log('[CandyShop CreateRoom] joining room created through form: ' + roomJid);
// Join the room and close the modal.
Candy.Core.Action.Jabber.Room.Join(roomJid, null);
// Candy.Core.Action.Jabber.Room.Join(roomJid, null);
CandyShop.JoinOnResponse.joinRoom({ roomJid: roomJid, isPrivate: !!isPrivate, topic: roomTopic }, true);
Candy.View.Pane.Chat.Modal.hide();
}
});
};

self.showModal = function(){
Candy.View.Pane.Chat.Modal.show(self.Template.modalForm, true, false);
var filterString = $('#candy .room-list-filter').val()
var templateData = {
create_private_room_permission: CONNECT_PERMISSIONS.create_private_room
};
Candy.View.Pane.Chat.Modal.show(Mustache.to_html(self.Template.modalForm, templateData), true, false);
// Default the new room name to whatever they were looking for
$('#create-group-form-name').val(filterString);
self.addFormHandler();
};

self.Template = {
createButton: '<div id="create-group"><div class="click">+ Create Room</div></div>',
modalForm: '<h4>Create Room</h4><form id="create-group-form">' +
'<div class="form-group group-form-name-group">' +
'<label for="create-group-form-name" class="control-label">Name:</label>' +
'<input class="form-control" type="text" name="room-name" id="create-group-form-name" />' +
'</div><button type="submit">Create</button></form>'
modalForm: '<div class="modal-head">Create Room</div>' +
'<div class="modal-body">' +
'<div class="alert alert-danger hide" id="create-room-error-dialog"></div>' +
'<form id="create-group-form">' +
'<div class="form-group group-form-name-group">' +
'<input class="modal-form-input" type="text" name="room-name" placeholder="Room Name" id="create-group-form-name" />' +
'<input class="modal-form-input" type="text" name="room-topic" placeholder="Room Topic" id="create-group-form-topic" />' +
'{{#create_private_room_permission}}Private <input class="modal-form-input" type="checkbox" name="room-is_private" id="create-group-form-is_private" />{{/create_private_room_permission}}' +
'</div>' +
'</form>' +
'</div>' +
'<div class="modal-foot"><button class="btn btn-xs btn-primary" id="create-room-link">Create Room</button></div>'
};

return self;
Expand Down