diff --git a/available-chats/README.md b/available-chats/README.md new file mode 100644 index 0000000..fd8263f --- /dev/null +++ b/available-chats/README.md @@ -0,0 +1,28 @@ +# Available Chats Plugin +A plugin to join public rooms and start private chats with roster entries. + +## Usage +Include the JavaScript and CSS files: + +```HTML + +``` + +To enable the Available Chats Plugin, just add one of the ´init´ methods to your bootstrap (uses conference.your.domain as MUC domain): + +```JavaScript +CandyShop.AvailableChats.init(); +``` + +To set the MUC domain use +```JavaScript +CandyShop.AvailableChats.init({ domain: "chats.capulet.net" }); +``` + +To set a MUC subdomain use (end with a dot ("."); will use the connection's domain as base. +```JavaScript +CandyShop.AvailableChats.init({ domain: "chats." }); +``` + +## Screenshot +![Screenshot](https://github.com/amiadogroup/candy-plugins/raw/master/available-rooms/screenshot.png) diff --git a/available-chats/candy.js b/available-chats/candy.js new file mode 100644 index 0000000..da44e75 --- /dev/null +++ b/available-chats/candy.js @@ -0,0 +1,231 @@ +/** File: candy.js + * Candy: show available rooms on conference server and add roster items + * for private chats. + * Fork of available-rooms plugin. + * + * Authors: + * - Jonatan Männchen + * - Georg Jansing + * + * Copyright: + * - (c) 2012 Amiado Group AG. All rights reserved. + * - (c) 2016 Georg Jansing. All rights reserved. + */ + +/** + * TODO: Properly translate ("Kontake", "Chatraum") + */ + +/* global Candy, jQuery, Strophe */ + +var CandyShop = (function(self) { return self; }(CandyShop || {})); + +CandyShop.AvailableChats = (function(self, Candy, $) { + self.about = { + name: 'Candy Plugin Available Chats', + version: '1.0', + }; + + self._options = { + domain: null, /** Prefix (e.g. "conference") or complete MUC Domain + * to list rooms from. If domain ends with a dot (".") + * it will be interpreted as a prefix, otherweise as + * full domain. */ + } + + /** Private chat or MUC */ + self.RoomTypes = { + CONFERENCE: 1, + PRIVATE: 2, + }; + + /** Array: rooms + * all rooms + * + * Contains: + * (Object List) rooms + * (String) jid + * (String) name + * (Integer) people (number of participants) + * (Integer) type (private chat or muc) + */ + self.rooms = []; + + /** Function: init + * Initializes the available-rooms plugin with the default settings. + */ + self.init = function(options) { + $.extend(true, self._options, options); + + $(Candy).on('candy:core.chat.connection', function(e, args) { + if (args.status === Strophe.Status.CONNECTED || + args.status === Strophe.Status.ATTACHED) { + // Load rooms + self.loadRooms(); + + // Do it again all 10 seconds + setInterval(self.loadRooms, 10000); + } + }); + + // Add Handler + $(Candy).on('candy:view.message.before-send', function(e, args) { + // (strip colors) + // if it matches '/list', show rooms and don't send anything + if (args.message.replace(/\|c:\d+\|/, '').toLowerCase() === '/list') { + self.showRooms(); + args.message = ''; + } + }); + $(Candy).on('candy:view.room.after-add', self.loadRooms); + }; + + /** Function: loadRooms + * Load all public rooms + */ + self.loadRooms = function () { + if(! self._options.domain) { + self._options.domain = "conference." + Candy.Core.getConnection().domain; + } else if (self._options.domain.charAt(self._options.domain.length - 1) == ".") { + self._options.domain = self._options.domain + + Candy.Core.getConnection().domain; + } + + Candy.Core.getConnection().muc.listRooms(self._options.domain, function(roomsData) { + CandyShop.AvailableChats.rooms = []; + $.each($(roomsData).find('item'), function(item, room) { + var allreadyIn = false; + $.each(Candy.Core.getRooms(), function(item, roomSearch) { + if(roomSearch.getJid() === $(room).attr('jid')) { + allreadyIn = true; + return false; + } + }); + if (! allreadyIn) { + var name = $(room).attr('name'); + var people = 0; + var pos = name.indexOf("("); + if (pos != -1) { + name = name.substr(0, name.indexOf('(') - 1); + people = name.substr(pos + 1, name.length - pos - 2); + } + + CandyShop.AvailableChats.rooms.push({ + jid: $(room).attr('jid'), + name: name, + people: people, + type: CandyShop.AvailableChats.RoomTypes.CONFERENCE, + }); + } + }); + + // Add roster items + $.each(Candy.Core.getRoster().items, function(jid, contact) { + CandyShop.AvailableChats.rooms.push({ + jid: jid, + name: contact.data.name, + people: 2, + type: CandyShop.AvailableChats.RoomTypes.PRIVATE, + }); + }); + /** Sorting: private chats first, then MUC rooms, ordered by number of participants */ + + CandyShop.AvailableChats.rooms = CandyShop.AvailableChats.rooms.sort(function(a, b) { + if (a.people === b.people) { + return a.name < b.name ? -1 : 1; + } else if (a.type == CandyShop.AvailableChats.RoomTypes.PRIVATE) { + return -1; + } else if (b.type == CandyShop.AvailableChats.RoomTypes.PRIVATE) { + return 1; + } else { + return a.people < b.people ? 1 : -1; + } + }); + CandyShop.AvailableChats.placePlusTab(); + }); + }; + + /** Function: placePlusTab + * placeTheTab + */ + self.placePlusTab = function() { + if(self.rooms.length > 0) { + if($('#add-room').length > 0) { + $('#add-room').parent().remove(); + } + $('#chat-tabs').children().last().after('
  • +
  • '); + $('#add-room').click(self.showRooms); + } else { + if($('#add-room').length > 0) { + $('#add-room').parent().remove(); + } + } + }; + + /** Function: showRooms + * Show all public rooms + */ + self.showRooms = function() { + // get the element + var elem = $('#add-room'); + + // blur the field + elem.blur(); + + // get the necessary items + var menu = $('#context-menu'), + content = $('ul', menu); + + // clear the content if needed + content.empty(); + + // add the matches to the list + for(var i in self.rooms) { + var room = self.rooms[i]; + var people = ""; + if (room.type == CandyShop.AvailableChats.RoomTypes.PRIVATE) { + people = ' (Kontakt)'; + } else { + if (room.people > 0) { + people = ' (Chatraum, ' + room.people + ' Personen)'; + } else { + people = ' (Chatraum)'; + } + } + + content.append('
  • ' + self.rooms[i].name + people + '
  • '); + } + + content.find('li').click(self.joinChanel); + + var pos = elem.offset(), + posLeft = Candy.Util.getPosLeftAccordingToWindowBounds(menu, pos.left + 7), + posTop = Candy.Util.getPosTopAccordingToWindowBounds(menu, pos.top); + + menu.css({'left': posLeft.px, 'top': '7px', backgroundPosition: posLeft.backgroundPositionAlignment + ' ' + posTop.backgroundPositionAlignment}); + menu.fadeIn('fast'); + }; + + /** Function: joinChanel + * Show all public rooms + * + * Parameters: + * (Event) e + */ + self.joinChanel = function(e) { + $('#context-menu').hide(); + if ($(e.currentTarget).attr('data-room-type') == CandyShop.AvailableChats.RoomTypes.PRIVATE) { + // Start private chat + Candy.View.Pane.PrivateRoom.open($(e.currentTarget).attr('data-jid'), $(e.currentTarget).attr("data-room-name"), true, true); + } else { + // Start muc + Candy.Core.Action.Jabber.Room.Join($(e.currentTarget).attr('data-jid')); + } + if($('#add-room').length > 0) { + $('#add-room').parent().remove(); + } + e.preventDefault(); + }; + + return self; +}(CandyShop.AvailableChats || {}, Candy, jQuery)); diff --git a/available-chats/screenshot.png b/available-chats/screenshot.png new file mode 100644 index 0000000..68aca54 Binary files /dev/null and b/available-chats/screenshot.png differ diff --git a/available-rooms/README.md b/available-rooms/README.md index 792e972..d5f6aea 100644 --- a/available-rooms/README.md +++ b/available-rooms/README.md @@ -8,7 +8,7 @@ Include the JavaScript and CSS files: ``` -To enable the Paint Plugin, just add one of the ´init´ methods to your bootstrap: +To enable the Available Rooms Plugin, just add one of the ´init´ methods to your bootstrap: ```JavaScript CandyShop.AvailableRooms.init(); diff --git a/available-rooms/candy.js b/available-rooms/candy.js index 2a4ccdd..8c6febc 100644 --- a/available-rooms/candy.js +++ b/available-rooms/candy.js @@ -3,9 +3,11 @@ * * Authors: * - Jonatan Männchen + * - Georg Jansing * * Copyright: * - (c) 2012 Amiado Group AG. All rights reserved. + * - (c) 2016 Georg Jansing. All rights reserved. */ /* global Candy, jQuery, Strophe */ @@ -20,7 +22,7 @@ CandyShop.AvailableRooms = (function(self, Candy, $) { * (Object List) rooms * (String) jid * (String) name - * (Integer) person + * (Integer) people (number of participants) */ self.rooms = []; @@ -28,8 +30,9 @@ CandyShop.AvailableRooms = (function(self, Candy, $) { * Initializes the available-rooms plugin with the default settings. */ self.init = function(){ - $(Candy.Core.Event).on('candy:core.chat.connection', function(e, args) { - if(args.status === Strophe.Status.ATTACHED) { + $(Candy).on('candy:core.chat.connection', function(e, args) { + if(args.status === Strophe.Status.CONNECTED || + args.status === Strophe.Status.ATTACHED) { // Load rooms self.loadRooms(); @@ -39,7 +42,7 @@ CandyShop.AvailableRooms = (function(self, Candy, $) { }); // Add Handler - $(Candy.View.Pane).bind('candy:view.message.beforeSend', function(e, args) { + $(Candy).on('candy:view.message.before-send', function(e, args) { // (strip colors) // if it matches '/list', show rooms and don't send anything if (args.message.replace(/\|c:\d+\|/, '').toLowerCase() === '/list') { @@ -47,7 +50,7 @@ CandyShop.AvailableRooms = (function(self, Candy, $) { args.message = ''; } }); - $(Candy.View.Pane).bind('candy:view.room.afterAdd', self.loadRooms); + $(Candy).on('candy:view.room.after-add', self.loadRooms); }; /** Function: loadRooms @@ -65,10 +68,18 @@ CandyShop.AvailableRooms = (function(self, Candy, $) { } }); if(!allreadyIn) { + var name = $(room).attr('name'); + var people = 0; + var pos = name.indexOf("("); + if (pos != -1) { + name = name.substr(0, name.indexOf('(') - 1); + people = name.substr(pos + 1, name.length - pos - 2); + } + CandyShop.AvailableRooms.rooms.push({ jid: $(room).attr('jid'), - name: $(room).attr('name').substr(0, $(room).attr('name').indexOf('(') - 1), - people: $(room).attr('name').substr($(room).attr('name').indexOf('(') + 1, $(room).attr('name').length - $(room).attr('name').indexOf('(') - 2) + name: name, + people: people, }); } }); @@ -119,7 +130,13 @@ CandyShop.AvailableRooms = (function(self, Candy, $) { // add the matches to the list for(var i in self.rooms) { - content.append('
  • ' + self.rooms[i].name + ' (' + self.rooms[i].people + ' Personen)
  • '); + var room = self.rooms[i]; + var people = ""; + + if (room.people > 0) { + people = " (" + room.people + " Personen)"; + } + content.append('
  • ' + self.rooms[i].name + people + '
  • '); } content.find('li').click(self.joinChanel);