1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/jappix_ynh.git synced 2024-09-03 19:26:19 +02:00
jappix_ynh/source/app/javascripts/favorites.js

785 lines
25 KiB
JavaScript
Raw Normal View History

2014-03-12 14:52:47 +01:00
/*
Jappix - An open social platform
These are the favorites JS scripts for Jappix
-------------------------------------------------
License: AGPL
Author: Valérian Saliou
*/
// Bundle
var Favorites = (function () {
/**
* Alias of this
* @private
*/
var self = {};
2014-04-08 20:14:28 +02:00
/**
2014-03-12 14:52:47 +01:00
* Opens the favorites popup
* @public
* @return {undefined}
*/
self.open = function() {
try {
// Popup HTML content
2014-11-25 23:42:38 +01:00
var html =
'<div class="top">' + Common._e("Manage favorite rooms") + '</div>' +
'<div class="content">' +
'<div class="switch-fav">' +
'<div class="room-switcher room-list">' +
'<div class="icon list-icon talk-images"></div>' +
Common._e("Change favorites") +
'</div>' +
'<div class="room-switcher room-search">' +
'<div class="icon search-icon talk-images"></div>' +
Common._e("Search a room") +
'</div>' +
'</div>' +
'<div class="static-fav">' +
'<div class="favorites-edit favorites-content">' +
'<div class="head fedit-head static-fav-head">' +
'<div class="head-text fedit-head-text">' + Common._e("Select a favorite") + '</div>' +
'<select name="fedit-head-select" class="head-select fedit-head-select"></select>' +
'</div>' +
'<div class="results fedit-results static-fav-results">' +
'<div class="fedit-line">' +
'<label>' + Common._e("Name") + '</label>' +
'<input class="fedit-title" type="text" required="" />' +
'</div>' +
'<div class="fedit-line">' +
'<label>' + Common._e("Nickname") + '</label>' +
'<input class="fedit-nick" type="text" value="' + Name.getNick() + '" required="" />' +
'</div>' +
'<div class="fedit-line">' +
'<label>' + Common._e("Room") + '</label>' +
'<input class="fedit-chan" type="text" required="" />' +
'</div>' +
'<div class="fedit-line">' +
'<label>' + Common._e("Server") + '</label>' +
'<input class="fedit-server" type="text" value="' + HOST_MUC + '" required="" />' +
'</div>' +
'<div class="fedit-line">' +
'<label>' + Common._e("Password") + '</label>' +
'<input class="fedit-password" type="password" />' +
'</div>' +
'<div class="fedit-line">' +
'<label>' + Common._e("Automatic") + '</label>' +
'<input type="checkbox" class="fedit-autojoin" />' +
'</div>' +
'<div class="fedit-actions">' +
'<a href="#" class="fedit-terminate fedit-add add one-button talk-images">' + Common._e("Add") + '</a>' +
'<a href="#" class="fedit-terminate fedit-edit one-button talk-images">' + Common._e("Edit") + '</a>' +
'<a href="#" class="fedit-terminate fedit-remove remove one-button talk-images">' + Common._e("Remove") + '</a>' +
'</div>' +
'</div>' +
'</div>' +
'<div class="favorites-search favorites-content">' +
'<div class="head fsearch-head static-fav-head">' +
'<div class="head-text fsearch-head-text">' + Common._e("Search a room on") + '</div>' +
'<input type="text" class="head-input fsearch-head-server" value="' + HOST_MUC + '" />' +
'</div>' +
'<div class="results fsearch-results static-fav-results">' +
'<p class="fsearch-noresults">' + Common._e("No room found on this server.") + '</p>' +
'</div>' +
'</div>' +
'</div>' +
'</div>' +
'<div class="bottom">' +
'<div class="wait wait-medium"></div>' +
'<a href="#" class="finish">' + Common._e("Close") + '</a>' +
2014-03-12 14:52:47 +01:00
'</div>';
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Create the popup
Popup.create('favorites', html);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Load the favorites
self.load();
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Associate the events
self.instance();
} catch(e) {
Console.error('Favorites.open', e);
}
};
/**
* Resets the favorites elements
* @public
* @return {undefined}
*/
self.reset = function() {
try {
2014-11-25 20:12:58 +01:00
var path_sel = $('#favorites');
2014-11-25 23:42:38 +01:00
2014-11-25 20:12:58 +01:00
path_sel.find('.wait');
path_sel.find('.fedit-terminate').hide();
path_sel.find('.fedit-add').show();
path_sel.find('.fsearch-oneresult').remove();
path_sel.find('input').val('');
path_sel.find('.please-complete').removeClass('please-complete');
path_sel.find('.fedit-nick').val(Name.getNick());
path_sel.find('.fsearch-head-server').val(HOST_MUC);
path_sel.find('.fedit-server').val(HOST_MUC);
path_sel.find('.fedit-autojoin').removeAttr('checked');
2014-03-12 14:52:47 +01:00
} catch(e) {
Console.error('Favorites.reset', e);
}
};
/**
* Quits the favorites popup
* @public
* @return {boolean}
*/
self.quit = function() {
try {
// Destroy the popup
Popup.destroy('favorites');
} catch(e) {
Console.error('Favorites.quit', e);
} finally {
return false;
}
};
/**
* Adds a room to the favorites
* @public
2014-11-25 20:12:58 +01:00
* @param {string} room_xid
* @param {string} room_name
2014-03-12 14:52:47 +01:00
* @return {boolean}
*/
2014-11-25 20:12:58 +01:00
self.addThis = function(room_xid, room_name) {
2014-03-12 14:52:47 +01:00
try {
// Button path
2014-11-25 20:12:58 +01:00
var button_sel = $('#favorites .fsearch-results div[data-xid="' + escape(room_xid) + '"] a.one-button');
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Add a remove button instead of the add one
2014-11-25 20:12:58 +01:00
button_sel.filter('.add').replaceWith(
'<a href="#" class="one-button remove talk-images">' + Common._e("Remove") + '</a>'
);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Click event
2014-11-25 20:12:58 +01:00
button_sel.filter('.remove').click(function() {
return self.removeThis(room_xid, room_name);
2014-03-12 14:52:47 +01:00
});
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Hide the add button in the (opened?) groupchat
2014-11-25 20:12:58 +01:00
$('#' + hex_md5(room_xid) + ' .tools-add').hide();
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Add the database entry
2014-11-25 20:12:58 +01:00
self.display(
room_xid,
Common.explodeThis(' (', room_name, 0), Name.getNick(), '0', ''
);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Publish the favorites
self.publish();
} catch(e) {
Console.error('Favorites.addThis', e);
} finally {
return false;
}
};
/**
* Removes a room from the favorites
* @public
2014-11-25 20:12:58 +01:00
* @param {string} room_xid
* @param {string} room_name
2014-03-12 14:52:47 +01:00
* @return {boolean}
*/
2014-11-25 20:12:58 +01:00
self.removeThis = function(room_xid, room_name) {
2014-03-12 14:52:47 +01:00
try {
// Button path
2014-11-25 20:12:58 +01:00
var button_sel = $('#favorites .fsearch-results div[data-xid="' + escape(room_xid) + '"] a.one-button');
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Add a remove button instead of the add one
2014-11-25 20:12:58 +01:00
button_sel.filter('.remove').replaceWith('<a href="#" class="one-button add talk-images">' + Common._e("Add") + '</a>');
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Click event
2014-11-25 20:12:58 +01:00
button_sel.filter('.add').click(function() {
return self.addThis(room_xid, room_name);
2014-03-12 14:52:47 +01:00
});
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Show the add button in the (opened?) groupchat
2014-11-25 20:12:58 +01:00
$('#' + hex_md5(room_xid) + ' .tools-add').show();
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Remove the favorite
2014-11-25 20:12:58 +01:00
self.remove(room_xid, true);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Publish the favorites
self.publish();
} catch(e) {
Console.error('Favorites.removeThis', e);
} finally {
return false;
}
};
2014-04-08 20:14:28 +02:00
/**
2014-03-12 14:52:47 +01:00
* Edits a favorite
* @public
* @return {undefined}
*/
self.edit = function() {
try {
// Path to favorites
2014-11-25 20:12:58 +01:00
var favorites_sel = $('#favorites');
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Reset the favorites
self.reset();
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Show the edit/remove button, hide the others
2014-11-25 20:12:58 +01:00
favorites_sel.find('.fedit-terminate').hide();
favorites_sel.find('.fedit-edit').show();
favorites_sel.find('.fedit-remove').show();
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// We retrieve the values
2014-11-25 20:12:58 +01:00
var xid = favorites_sel.find('.fedit-head-select').val();
var data_sel = $(Common.XMLFromString(
DataStore.getDB(Connection.desktop_hash, 'favorites', xid)
));
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// If this is not the default room
if(xid != 'none') {
// We apply the values
2014-11-25 20:12:58 +01:00
favorites_sel.find('.fedit-title').val(data_sel.find('name').text());
favorites_sel.find('.fedit-nick').val(data_sel.find('nick').text());
favorites_sel.find('.fedit-chan').val(Common.getXIDNick(xid));
favorites_sel.find('.fedit-server').val(Common.getXIDHost(xid));
favorites_sel.find('.fedit-password').val(data_sel.find('password').text());
2014-11-25 23:42:38 +01:00
2014-11-25 20:12:58 +01:00
if(data_sel.find('autojoin').text() == 'true') {
favorites_sel.find('.fedit-autojoin').attr('checked', true);
}
2014-03-12 14:52:47 +01:00
}
} catch(e) {
Console.error('Favorites.edit', e);
}
};
/**
* Terminate a favorite editing
* @public
* @param {string} type
* @return {boolean}
*/
self.terminateThis = function(type) {
try {
// Path to favorites
2014-11-25 20:12:58 +01:00
var favorites_sel = $('#favorites');
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// We get the values of the current edited groupchat
2014-11-25 20:12:58 +01:00
var old_xid = favorites_sel.find('.fedit-head-select').val();
2014-11-25 23:42:38 +01:00
2014-11-25 20:12:58 +01:00
var title = favorites_sel.find('.fedit-title').val();
var nick = favorites_sel.find('.fedit-nick').val();
var room = favorites_sel.find('.fedit-chan').val();
var server = favorites_sel.find('.fedit-server').val();
2014-03-12 14:52:47 +01:00
var xid = room + '@' + server;
2014-11-25 20:12:58 +01:00
var password = favorites_sel.find('.fedit-password').val();
2014-03-12 14:52:47 +01:00
var autojoin = 'false';
2014-11-25 23:42:38 +01:00
2014-11-25 20:12:58 +01:00
if(favorites_sel.find('.fedit-autojoin').filter(':checked').size()) {
2014-03-12 14:52:47 +01:00
autojoin = 'true';
2014-11-25 20:12:58 +01:00
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// We check the missing values and send this if okay
if((type == 'add') || (type == 'edit')) {
if(title && nick && room && server) {
// Remove the edited room
2014-11-25 20:12:58 +01:00
if(type == 'edit') {
2014-03-12 14:52:47 +01:00
self.remove(old_xid, true);
2014-11-25 20:12:58 +01:00
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Display the favorites
self.display(xid, title, nick, autojoin, password);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Reset the inputs
self.reset();
2014-11-25 20:12:58 +01:00
} else {
favorites_sel.find('input[required]').each(function() {
2014-03-12 14:52:47 +01:00
var select = $(this);
2014-11-25 23:42:38 +01:00
2014-11-25 20:12:58 +01:00
if(!select.val()) {
2014-03-12 14:52:47 +01:00
$(document).oneTime(10, function() {
select.addClass('please-complete').focus();
});
2014-11-25 20:12:58 +01:00
} else {
2014-11-25 23:42:38 +01:00
select.removeClass('please-complete');
2014-11-25 20:12:58 +01:00
}
2014-03-12 14:52:47 +01:00
});
}
2014-11-25 20:12:58 +01:00
} else if(type == 'remove') {
2014-03-12 14:52:47 +01:00
self.remove(old_xid, true);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Reset the inputs
self.reset();
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Publish the new favorites
self.publish();
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
Console.info('Action on this bookmark: ' + room + '@' + server + ' / ' + type);
} catch(e) {
Console.error('Favorites.terminateThis', e);
2014-04-08 20:14:28 +02:00
} finally {
return false;
2014-03-12 14:52:47 +01:00
}
};
/**
* Removes a favorite
* @public
* @param {string} xid
* @param {boolean} database
* @return {undefined}
*/
self.remove = function(xid, database) {
try {
// We remove the target favorite everywhere needed
$('.buddy-conf-groupchat-select option[value="' + xid + '"]').remove();
$('.fedit-head-select option[value="' + xid + '"]').remove();
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Must remove it from database?
if(database) {
DataStore.removeDB(Connection.desktop_hash, 'favorites', xid);
}
} catch(e) {
Console.error('Favorites.remove', e);
}
};
/**
* Sends a favorite to the XMPP server
* @public
* @return {undefined}
*/
self.publish = function() {
try {
var iq = new JSJaCIQ();
iq.setType('set');
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
var query = iq.setQuery(NS_PRIVATE);
2014-11-25 20:12:58 +01:00
var storage = query.appendChild(iq.buildNode('storage', {
'xmlns': NS_BOOKMARKS
}));
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// We generate the XML
var db_regex = new RegExp(('^' + Connection.desktop_hash + '_') + 'favorites_(.+)');
for(var i = 0; i < DataStore.storageDB.length; i++) {
// Get the pointer values
var current = DataStore.storageDB.key(i);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// If the pointer is on a stored favorite
if(current.match(db_regex)) {
2014-11-25 20:12:58 +01:00
var data_sel = $(Common.XMLFromString(
DataStore.storageDB.getItem(current)
));
var xid = data_sel.find('xid').text();
var rName = data_sel.find('name').text();
var nick = data_sel.find('nick').text();
var password = data_sel.find('password').text();
var autojoin = data_sel.find('autojoin').text();
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// We create the node for this groupchat
2014-11-25 20:12:58 +01:00
var item = storage.appendChild(
iq.buildNode('conference', {
'name': rName,
'jid': xid,
'autojoin': autojoin,
xmlns: NS_BOOKMARKS
})
);
2014-11-25 23:42:38 +01:00
2014-11-25 20:12:58 +01:00
item.appendChild(iq.buildNode('nick', {
xmlns: NS_BOOKMARKS
}, nick));
2014-11-25 23:42:38 +01:00
2014-11-25 20:12:58 +01:00
if(password) {
item.appendChild(iq.buildNode('password', {
xmlns: NS_BOOKMARKS
}, password));
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
Console.info('Bookmark sent: ' + xid);
}
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
con.send(iq);
} catch(e) {
Console.error('Favorites.publish', e);
}
};
2014-04-08 20:14:28 +02:00
/**
2014-03-12 14:52:47 +01:00
* Gets a list of the MUC items on a given server
* @public
* @return {undefined}
*/
self.getGCList = function() {
try {
2014-11-25 20:12:58 +01:00
var path_sel = $('#favorites');
var groupchat_server = $('.fsearch-head-server').val();
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// We reset some things
2014-11-25 20:12:58 +01:00
path_sel.find('.fsearch-oneresult').remove();
path_sel.find('.fsearch-noresults').hide();
path_sel.find('.wait').show();
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
var iq = new JSJaCIQ();
iq.setType('get');
2014-11-25 20:12:58 +01:00
iq.setTo(groupchat_server);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
iq.setQuery(NS_DISCO_ITEMS);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
con.send(iq, self.handleGCList);
} catch(e) {
Console.error('Favorites.getGCList', e);
}
};
/**
* Handles the MUC items list
* @public
* @param {object} iq
* @return {undefined}
*/
self.handleGCList = function(iq) {
try {
2014-11-25 20:12:58 +01:00
var path_sel = $('#favorites');
2014-03-12 14:52:47 +01:00
var from = Common.fullXID(Common.getStanzaFrom(iq));
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
if(!iq || (iq.getType() != 'result')) {
Board.openThisError(3);
2014-11-25 23:42:38 +01:00
2014-11-25 20:12:58 +01:00
path_sel.find('.wait').hide();
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
Console.error('Error while retrieving the rooms: ' + from);
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
else {
var handleXML = iq.getQuery();
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
if($(handleXML).find('item').size()) {
// Initialize the HTML code
var html = '';
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
$(handleXML).find('item').each(function() {
2014-11-25 20:12:58 +01:00
var this_sel = $(this);
var room_xid = this_sel.attr('jid');
var room_name = this_sel.attr('name');
2014-11-25 23:42:38 +01:00
2014-11-25 20:12:58 +01:00
if(room_xid && room_name) {
2014-03-12 14:52:47 +01:00
// Escaped values
2014-11-25 20:12:58 +01:00
var escaped_xid = Utils.encodeOnclick(room_xid);
var escaped_name = Utils.encodeOnclick(room_name);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Initialize the room HTML
2014-11-25 23:42:38 +01:00
html += '<div class="oneresult fsearch-oneresult" data-xid="' + escape(room_xid) + '">' +
'<div class="room-name">' + room_name.htmlEnc() + '</div>' +
2014-03-12 14:52:47 +01:00
'<a href="#" class="one-button join talk-images" onclick="return Favorites.join(\'' + escaped_xid + '\');">' + Common._e("Join") + '</a>';
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// This room is yet a favorite
2014-11-25 20:12:58 +01:00
if(DataStore.existDB(Connection.desktop_hash, 'favorites', room_xid)) {
2014-11-25 23:42:38 +01:00
html += '<a href="#" ' +
'class="one-button remove talk-images" ' +
'onclick="return Favorites.removeThis(\'' + escaped_xid + '\', \'' + escaped_name + '\');"' +
'>' +
Common._e("Remove") +
2014-11-25 20:12:58 +01:00
'</a>';
} else {
2014-11-25 23:42:38 +01:00
html += '<a href="#" ' +
'class="one-button add talk-images" ' +
'onclick="return Favorites.addThis(\'' + escaped_xid + '\', \'' + escaped_name + '\');"' +
'>' +
Common._e("Add") +
2014-11-25 20:12:58 +01:00
'</a>';
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Close the room HTML
html += '</div>';
}
});
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Append this code to the popup
2014-11-25 20:12:58 +01:00
path_sel.find('.fsearch-results').append(html);
} else {
path_sel.find('.fsearch-noresults').show();
2014-03-12 14:52:47 +01:00
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
Console.info('Rooms retrieved: ' + from);
}
2014-11-25 23:42:38 +01:00
2014-11-25 20:12:58 +01:00
path_sel.find('.wait').hide();
2014-03-12 14:52:47 +01:00
} catch(e) {
Console.error('Favorites.handleGCList', e);
}
};
/**
* Joins a groupchat from favorites
* @public
* @param {string} room
* @return {boolean}
*/
self.join = function(room) {
try {
self.quit();
2014-11-25 20:12:58 +01:00
Chat.checkCreate(
room,
'groupchat',
'',
'',
Common.getXIDNick(room)
);
2014-03-12 14:52:47 +01:00
} catch(e) {
Console.error('Favorites.join', e);
} finally {
return false;
}
};
/**
* Displays a given favorite
* @public
* @param {string} xid
* @param {string} name
* @param {string} nick
* @param {boolean} autojoin
* @param {string} password
* @return {undefined}
*/
self.display = function(xid, name, nick, autojoin, password) {
try {
// Generate the HTML code
var html = '<option value="' + Common.encodeQuotes(xid) + '">' + name.htmlEnc() + '</option>';
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Remove the existing favorite
self.remove(xid, false);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// We complete the select forms
$('#roster .gc-join-first-option, #favorites .fedit-head-select-first-option').after(html);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// We store the informations
2014-11-25 23:42:38 +01:00
var value = '<groupchat>' +
'<xid>' + xid.htmlEnc() + '</xid>' +
'<name>' + name.htmlEnc() + '</name>' +
'<nick>' + nick.htmlEnc() + '</nick>' +
'<autojoin>' + autojoin.htmlEnc() + '</autojoin>' +
'<password>' + password.htmlEnc() + '</password>' +
2014-11-25 20:12:58 +01:00
'</groupchat>';
2014-03-12 14:52:47 +01:00
DataStore.setDB(Connection.desktop_hash, 'favorites', xid, value);
} catch(e) {
Console.error('Favorites.display', e);
}
};
/**
* Loads the favorites for the popup
* @public
* @return {undefined}
*/
self.load = function() {
try {
// Initialize the HTML code
var html = '';
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Read the database
var db_regex = new RegExp(('^' + Connection.desktop_hash + '_') + 'favorites_(.+)');
for(var i = 0; i < DataStore.storageDB.length; i++) {
// Get the pointer values
var current = DataStore.storageDB.key(i);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// If the pointer is on a stored favorite
if(current.match(db_regex)) {
var data = Common.XMLFromString(DataStore.storageDB.getItem(current));
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Add the current favorite to the HTML code
2014-11-25 23:42:38 +01:00
html += '<option value="' + Common.encodeQuotes($(data).find('xid').text()) + '">' +
$(data).find('name').text().htmlEnc() +
2014-11-25 20:12:58 +01:00
'</option>';
2014-03-12 14:52:47 +01:00
}
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Generate specific HTML code
2014-11-25 23:42:38 +01:00
var favorites_bubble = '<option value="none" class="gc-join-first-option" selected="">' +
Common._e("Select a favorite") +
2014-11-25 20:12:58 +01:00
'</option>' + html;
2014-11-25 23:42:38 +01:00
var favorites_popup = '<option value="none" class="fedit-head-select-first-option" selected="">' +
Common._e("Select a favorite") +
2014-11-25 20:12:58 +01:00
'</option>' + html;
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Append the HTML code
$('#roster .buddy-conf-groupchat-select').html(favorites_bubble);
$('#favorites .fedit-head-select').html(favorites_popup);
} catch(e) {
Console.error('Favorites.load', e);
}
};
/**
* Plugin launcher
* @public
* @return {undefined}
*/
self.instance = function() {
try {
2014-11-25 20:12:58 +01:00
var favorites_sel = $('#favorites');
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Keyboard events
2014-11-25 20:12:58 +01:00
favorites_sel.find('.fsearch-head-server').keyup(function(e) {
2014-03-12 14:52:47 +01:00
if(e.keyCode == 13) {
2014-11-25 20:12:58 +01:00
var this_sel = $(this);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// No value?
2014-11-25 20:12:58 +01:00
if(!this_sel.val()) {
this_sel.val(HOST_MUC);
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Get the list
self.getGCList();
}
});
2014-11-25 23:42:38 +01:00
2014-11-25 20:12:58 +01:00
favorites_sel.find('.fedit-line input').keyup(function(e) {
2014-03-12 14:52:47 +01:00
if(e.keyCode == 13) {
// Edit a favorite
2014-11-25 20:12:58 +01:00
if(favorites_sel.find('.fedit-edit').is(':visible')) {
2014-04-08 20:14:28 +02:00
self.terminateThis('edit');
2014-11-25 20:12:58 +01:00
} else {
2014-04-08 20:14:28 +02:00
self.terminateThis('add');
2014-11-25 20:12:58 +01:00
}
2014-03-12 14:52:47 +01:00
}
});
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Change events
$('.fedit-head-select').change(self.edit);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Click events
2014-11-25 20:12:58 +01:00
favorites_sel.find('.room-switcher').click(function() {
favorites_sel.find('.favorites-content').hide();
2014-03-12 14:52:47 +01:00
self.reset();
});
2014-11-25 23:42:38 +01:00
2014-11-25 20:12:58 +01:00
favorites_sel.find('.room-list').click(function() {
favorites_sel.find('.favorites-edit').show();
2014-03-12 14:52:47 +01:00
});
2014-11-25 23:42:38 +01:00
2014-11-25 20:12:58 +01:00
favorites_sel.find('.room-search').click(function() {
favorites_sel.find('.favorites-search').show();
2014-03-12 14:52:47 +01:00
self.getGCList();
});
2014-11-25 23:42:38 +01:00
2014-11-25 20:12:58 +01:00
favorites_sel.find('.fedit-add').click(function() {
2014-04-08 20:14:28 +02:00
return self.terminateThis('add');
2014-03-12 14:52:47 +01:00
});
2014-11-25 23:42:38 +01:00
2014-11-25 20:12:58 +01:00
favorites_sel.find('.fedit-edit').click(function() {
2014-04-08 20:14:28 +02:00
return self.terminateThis('edit');
2014-03-12 14:52:47 +01:00
});
2014-11-25 23:42:38 +01:00
2014-11-25 20:12:58 +01:00
favorites_sel.find('.fedit-remove').click(function() {
2014-04-08 20:14:28 +02:00
return self.terminateThis('remove');
2014-03-12 14:52:47 +01:00
});
2014-11-25 23:42:38 +01:00
2014-11-25 20:12:58 +01:00
favorites_sel.find('.bottom .finish').click(function() {
2014-03-12 14:52:47 +01:00
return self.quit();
});
} catch(e) {
Console.error('Favorites.instance', e);
}
};
/**
* Return class scope
*/
return self;
})();