3 Jappix - An open social platform
4 These are the privacy JS scripts for Jappix
6 -------------------------------------------------
10 Last revision: 23/06/11
14 // Opens the privacy popup
15 function openPrivacy() {
18 '<div class="top">' + _e("Privacy") + '</div>' +
20 '<div class="content">' +
21 '<div class="privacy-head">' +
22 '<div class="list-left">' +
23 '<span>' + _e("Choose") + '</span>' +
24 '<select disabled=""></select>' +
25 '<a href="#" class="list-remove one-button talk-images" title="' + _e("Remove") + '"></a>' +
28 '<div class="list-center"></div>' +
30 '<div class="list-right">' +
31 '<span>' + _e("Add") + '</span>' +
32 '<input type="text" placeholder="' + _e("List name") + '" />' +
36 '<div class="privacy-item">' +
37 '<span>' + _e("Item") + '</span>' +
38 '<select disabled=""></select>' +
39 '<a href="#" class="item-add one-button talk-images" title="' + _e("Add") + '"></a>' +
40 '<a href="#" class="item-remove one-button talk-images" title="' + _e("Remove") + '"></a>' +
41 '<a href="#" class="item-save one-button talk-images">' + _e("Save") + '</a>' +
43 '<div class="clear"></div>' +
46 '<div class="privacy-form">' +
47 '<div class="privacy-first">' +
48 '<label><input type="radio" name="action" value="allow" disabled="" />' + _e("Allow") + '</label>' +
49 '<label><input type="radio" name="action" value="deny" disabled="" />' + _e("Deny") + '</label>' +
52 '<div class="privacy-second">' +
53 '<label><input type="radio" name="type" value="jid" disabled="" />' + _e("Address") + '</label>' +
54 '<input type="text" name="jid" disabled="" />' +
56 '<label><input type="radio" name="type" value="group" disabled="" />' + _e("Group") + '</label>' +
57 '<select name="group" disabled="">' + groupsToHtmlPrivacy() + '</select>' +
59 '<label><input type="radio" name="type" value="subscription" disabled="" />' + _e("Subscription") + '</label>' +
60 '<select name="subscription" disabled="">' +
61 '<option value="none">' + _e("None") + '</option>' +
62 '<option value="both">' + _e("Both") + '</option>' +
63 '<option value="from">' + _e("From") + '</option>' +
64 '<option value="to">' + _e("To") + '</option>' +
67 '<label><input type="radio" name="type" value="everybody" disabled="" />' + _e("Everybody") + '</label>' +
70 '<div class="privacy-third">' +
71 '<label><input type="checkbox" name="send-messages" disabled="" />' + _e("Send messages") + '</label>' +
72 '<label><input type="checkbox" name="send-queries" disabled="" />' + _e("Send queries") + '</label>' +
73 '<label><input type="checkbox" name="see-status" disabled="" />' + _e("See my status") + '</label>' +
74 '<label><input type="checkbox" name="send-status" disabled="" />' + _e("Send his/her status") + '</label>' +
75 '<label><input type="checkbox" name="everything" disabled="" />' + _e("Everything") + '</label>' +
78 '<div class="clear"></div>' +
81 '<div class="privacy-active">' +
82 '<label>' + _e("Order") + '<input type="text" name="order" value="1" disabled="" /></label>' +
84 '<div class="privacy-active-elements">' +
85 '<label><input type="checkbox" name="active" disabled="" />' + _e("Active for this session") + '</label>' +
86 '<label><input type="checkbox" name="default" disabled="" />' + _e("Always active") + '</label>' +
91 '<div class="bottom">' +
92 '<div class="wait wait-medium"></div>' +
94 '<a href="#" class="finish">' + _e("Close") + '</a>' +
98 createPopup('privacy', html);
100 // Associate the events
103 // Display the available privacy lists
104 displayListsPrivacy();
106 // Get the first list items
107 displayItemsPrivacy();
112 // Quits the privacy popup
113 function closePrivacy() {
115 destroyPopup('privacy');
120 // Sets the received state for privacy block list
121 function receivedPrivacy() {
123 setDB('privacy-marker', 'available', 'true');
125 // Show privacy elements
126 $('.privacy-hidable').show();
129 // Gets available privacy lists
130 function listPrivacy() {
132 var iq = new JSJaCIQ();
135 iq.setQuery(NS_PRIVACY);
137 con.send(iq, handleListPrivacy);
139 logThis('Getting available privacy list(s)...');
142 // Handles available privacy lists
143 function handleListPrivacy(iq) {
145 if(iq.getType() == 'error')
146 return logThis('Privacy lists not supported!', 2);
148 // Get IQ query content
149 var iqQuery = iq.getQuery();
152 setDB('privacy-lists', 'available', xmlToString(iqQuery));
155 if($(iqQuery).find('list[name=block]').size()) {
156 // Not the default one?
157 if(!$(iqQuery).find('default[name=block]').size())
158 changePrivacy('block', 'default');
160 setDB('privacy-marker', 'default', 'block');
162 // Not the active one?
163 if(!$(iqQuery).find('active[name=block]').size())
164 changePrivacy('block', 'active');
166 setDB('privacy-marker', 'active', 'block');
168 // Get the block list rules
172 // Apply the received marker here
176 logThis('Got available privacy list(s).', 3);
179 // Gets privacy lists
180 function getPrivacy(list) {
182 var iq = new JSJaCIQ();
186 var iqQuery = iq.setQuery(NS_PRIVACY);
187 iqQuery.appendChild(iq.buildNode('list', {'xmlns': NS_PRIVACY, 'name': list}));
189 con.send(iq, handleGetPrivacy);
191 // Must show the wait item?
192 if(exists('#privacy'))
193 $('#privacy .wait').show();
195 logThis('Getting privacy list(s): ' + list);
198 // Handles privacy lists
199 function handleGetPrivacy(iq) {
200 // Apply a "received" marker
203 // Store the data for each list
204 $(iq.getQuery()).find('list').each(function() {
206 var list_name = $(this).attr('name');
208 // Store list content
209 setDB('privacy', list_name, xmlToString(this));
211 // Is this a block list?
212 if(list_name == 'block') {
214 $('#buddy-list .buddy').removeClass('blocked');
217 $(this).find('item[action=deny][type=jid]').each(function() {
218 $('#buddy-list .buddy[data-xid=' + escape($(this).attr('value')) + ']').addClass('blocked');
222 $(this).find('item[action=deny][type=group]').each(function() {
223 $('#buddy-list .group' + hex_md5($(this).attr('value')) + ' .buddy').addClass('blocked');
228 // Must display it to the popup?
229 if(exists('#privacy')) {
230 displayItemsPrivacy();
232 $('#privacy .wait').hide();
235 logThis('Got privacy list(s).', 3);
238 // Sets a privacy list
239 function setPrivacy(list, types, values, actions, orders, presence_in, presence_out, msg, iq_p) {
241 var iq = new JSJaCIQ();
245 var iqQuery = iq.setQuery(NS_PRIVACY);
246 var iqList = iqQuery.appendChild(iq.buildNode('list', {'xmlns': NS_PRIVACY, 'name': list}));
248 // Build the item elements
249 if(types && types.length) {
250 for(var i = 0; i < types.length; i++) {
252 var iqItem = iqList.appendChild(iq.buildNode('item', {'xmlns': NS_PRIVACY}));
256 iqItem.setAttribute('type', types[i]);
258 iqItem.setAttribute('value', values[i]);
260 iqItem.setAttribute('action', actions[i]);
262 iqItem.setAttribute('order', orders[i]);
266 iqItem.appendChild(iq.buildNode('presence-in', {'xmlns': NS_PRIVACY}));
268 iqItem.appendChild(iq.buildNode('presence-out', {'xmlns': NS_PRIVACY}));
270 iqItem.appendChild(iq.buildNode('message', {'xmlns': NS_PRIVACY}));
272 iqItem.appendChild(iq.buildNode('iq', {'xmlns': NS_PRIVACY}));
278 logThis('Sending privacy list: ' + list);
281 // Push a privacy list item to a list
282 function pushPrivacy(list, type, value, action, order, presence_in, presence_out, msg, iq_p, hash, special_action) {
283 // Read the stored elements (to add them)
284 var stored = XMLFromString(getDB('privacy', list));
286 // Read the first value
287 var first_val = value[0];
289 // Must remove the given value?
290 if(special_action == 'remove') {
300 // Serialize them to an array
301 $(stored).find('item').each(function() {
303 var c_type = $(this).attr('type');
304 var c_value = $(this).attr('value');
305 var c_action = $(this).attr('action');
306 var c_order = $(this).attr('order');
309 var c_hash = hex_md5(c_type + c_value + c_action + c_order);
311 // Do not push it twice!
312 if(((c_hash != hash) && (special_action != 'roster')) || ((first_val != c_value) && (special_action == 'roster'))) {
324 action.push(c_action);
328 if($(this).find('presence-in').size())
329 presence_in.push(true);
331 presence_in.push(false);
333 if($(this).find('presence-out').size())
334 presence_out.push(true);
336 presence_out.push(false);
338 if($(this).find('message').size())
343 if($(this).find('iq').size())
351 setPrivacy(list, type, value, action, order, presence_in, presence_out, msg, iq_p);
354 // Change a privacy list status
355 function changePrivacy(list, status) {
357 if(getDB('privacy-marker', status) == list)
361 setDB('privacy-marker', status, list);
364 var iq = new JSJaCIQ();
368 var iqQuery = iq.setQuery(NS_PRIVACY);
369 var iqStatus = iqQuery.appendChild(iq.buildNode(status, {'xmlns': NS_PRIVACY}));
371 // Can add a "name" attribute?
373 iqStatus.setAttribute('name', list);
377 logThis('Changing privacy list status: ' + list + ' to: ' + status);
380 // Checks the privacy status (action) of a value
381 function statusPrivacy(list, value) {
382 return $(XMLFromString(getDB('privacy', list))).find('item[value=' + value + ']').attr('action');
385 // Converts the groups array into a <option /> string
386 function groupsToHtmlPrivacy() {
387 var groups = getAllGroups();
392 html += '<option value="' + encodeQuotes(groups[i]) +'">' + groups[i].htmlEnc() + '</option>';
398 // Displays the privacy lists
399 function displayListsPrivacy() {
402 var select = $('#privacy .privacy-head .list-left select');
403 var data = XMLFromString(getDB('privacy-lists', 'available'));
405 // Parse the XML data!
406 $(data).find('list').each(function() {
407 var list_name = $(this).attr('name');
410 code += '<option value="' + encodeQuotes(list_name) + '">' + list_name.htmlEnc() + '</option>';
418 select.removeAttr('disabled');
420 select.attr('disabled', true);
425 // Displays the privacy items for a list
426 function displayItemsPrivacy() {
429 disableFormPrivacy();
433 var select = $('#privacy .privacy-item select');
434 var list = $('#privacy .privacy-head .list-left select').val();
436 // Reset the item select
443 // Reset the list status
444 $('#privacy .privacy-active input[type=checkbox]').removeAttr('checked');
446 // Display the list status
447 var status = ['active', 'default'];
450 if(getDB('privacy-marker', status[s]) == list)
451 $('#privacy .privacy-active input[name=' + status[s] + ']').attr('checked', true);
454 // Try to read the stored items
455 var items = XMLFromString(getDB('privacy', list));
457 // Must retrieve the data?
459 select.attr('disabled', true);
461 return getPrivacy(list);
465 select.removeAttr('disabled');
467 // Parse the XML data!
468 $(items).find('item').each(function() {
470 var item_type = $(this).attr('type');
471 var item_value = $(this).attr('value');
472 var item_action = $(this).attr('action');
473 var item_order = $(this).attr('order');
476 var item_hash = hex_md5(item_type + item_value + item_action + item_order);
479 var item_presencein = $(this).find('presence-in').size();
480 var item_presenceout = $(this).find('presence-out').size();
481 var item_message = $(this).find('message').size();
482 var item_iq = $(this).find('iq').size();
484 // Apply default values (if missing)
490 item_action = 'allow';
494 // Apply sub-elements values
496 item_presencein = 'true';
498 item_presencein = 'false';
501 item_presenceout = 'true';
503 item_presenceout = 'false';
506 item_message = 'true';
508 item_message = 'false';
515 // Generate item description
517 var desc_arr = [item_type, item_value, item_action, item_order];
520 // Nothing to display?
530 // Add the select option
531 code += '<option data-type="' + encodeQuotes(item_type) + '" data-value="' + encodeQuotes(item_value) + '" data-action="' + encodeQuotes(item_action) + '" data-order="' + encodeQuotes(item_order) + '" data-presence_in="' + encodeQuotes(item_presencein) + '" data-presence_out="' + encodeQuotes(item_presenceout) + '" data-message="' + encodeQuotes(item_message) + '" data-iq="' + encodeQuotes(item_iq) + '" data-hash="' + encodeQuotes(item_hash) + '">' +
539 // Display the first item form
540 var first_item = select.find('option:first');
542 first_item.attr('data-type'),
543 first_item.attr('data-value'),
544 first_item.attr('data-action'),
545 first_item.attr('data-order'),
546 first_item.attr('data-presence_in'),
547 first_item.attr('data-presence_out'),
548 first_item.attr('data-message'),
549 first_item.attr('data-iq')
555 // Displays the privacy form for an item
556 function displayFormPrivacy(type, value, action, order, presence_in, presence_out, message, iq) {
561 $('#privacy .privacy-first input[name=action][value=' + action + ']').attr('checked', true);
563 // Apply the type & value
564 var privacy_second = '#privacy .privacy-second';
565 var privacy_type = privacy_second + ' input[name=type]';
566 var type_check, value_input;
570 type_check = privacy_type + '[value=jid]';
571 value_input = privacy_second + ' input[type=text][name=jid]';
576 type_check = privacy_type + '[value=group]';
577 value_input = privacy_second + ' select[name=group]';
582 type_check = privacy_type + '[value=subscription]';
583 value_input = privacy_second + ' select[name=subscription]';
588 type_check = privacy_type + '[value=everybody]';
594 $(type_check).attr('checked', true);
596 // Can apply a value?
598 $(value_input).val(value);
600 // Apply the things to do
601 var privacy_do = '#privacy .privacy-third input[type=checkbox]';
603 if(presence_in == 'true')
604 $(privacy_do + '[name=send-status]').attr('checked', true);
605 if(presence_out == 'true')
606 $(privacy_do + '[name=see-status]').attr('checked', true);
607 if(message == 'true')
608 $(privacy_do + '[name=send-messages]').attr('checked', true);
610 $(privacy_do + '[name=send-queries]').attr('checked', true);
612 if(!$(privacy_do).filter(':checked').size())
613 $(privacy_do + '[name=everything]').attr('checked', true);
616 $('#privacy .privacy-active input[name=order]').val(order);
619 $('#privacy .privacy-form input, #privacy .privacy-form select, #privacy .privacy-active input').removeAttr('disabled');
622 // Clears the privacy list form
623 function clearFormPrivacy() {
624 // Uncheck checkboxes & radio inputs
625 $('#privacy .privacy-form input[type=checkbox], #privacy .privacy-form input[type=radio]').removeAttr('checked');
628 $('#privacy .privacy-form select option').removeAttr('selected');
629 $('#privacy .privacy-form select option:first').attr('selected', true);
632 $('#privacy .privacy-form input[type=text]').val('');
635 $('#privacy .privacy-active input[name=order]').val('1');
638 // Disables the privacy list form
639 function disableFormPrivacy() {
640 $('#privacy .privacy-form input, #privacy .privacy-form select, #privacy .privacy-active input').attr('disabled', true);
643 // Enables the privacy list form
644 function enableFormPrivacy(rank) {
645 $('#privacy .privacy-' + rank + ' input, #privacy .privacy-' + rank + ' select').removeAttr('disabled');
649 function launchPrivacy() {
651 $('#privacy .bottom .finish').click(closePrivacy);
653 // Placeholder events
654 $('#privacy input[placeholder]').placeholder();
657 $('#privacy .privacy-head a.list-remove').click(function() {
659 var list = $('#privacy .privacy-head .list-left select').val();
665 // Remove it from popup
666 $('#privacy .privacy-head .list-left select option[value=' + list + ']').remove();
668 // Nothing remaining?
669 if(!exists('#privacy .privacy-head .list-left select option'))
670 $('#privacy .privacy-head .list-left select option').attr('disabled', true);
672 // Empty the item select
673 $('#privacy .privacy-item select').attr('disabled', true).html('');
675 // Disable this list before removing it
676 var status = ['active', 'default'];
679 if(getDB('privacy-marker', status[s]) == list)
680 changePrivacy('', status[s]);
683 // Remove from server
688 disableFormPrivacy();
693 $('#privacy .privacy-head .list-right input').keyup(function(e) {
699 var list = $('#privacy .privacy-head .list-right input').val();
700 var select = '#privacy .privacy-head .list-left select';
703 // Create the new element
704 if(!exists(select + ' option[value=' + list + ']')) {
708 // Create a new option
709 $(select).append('<option value="' + encodeQuotes(list) + '">' + list.htmlEnc() + '</option>');
711 // Reset the item select
712 $('#privacy .privacy-item select').attr('disabled', true).html('');
715 // Change the select value & enable it
716 $(select).val(list).removeAttr('disabled');
723 disableFormPrivacy();
725 // Must reload the list items?
727 displayItemsPrivacy();
728 $('#privacy .privacy-item select').removeAttr('disabled');
732 $('#privacy .privacy-head .list-left select').change(displayItemsPrivacy);
734 $('#privacy .privacy-item select').change(function() {
735 // Get the selected item
736 var item = $(this).find('option:selected');
740 item.attr('data-type'),
741 item.attr('data-value'),
742 item.attr('data-action'),
743 item.attr('data-order'),
744 item.attr('data-presence_in'),
745 item.attr('data-presence_out'),
746 item.attr('data-message'),
751 $('#privacy .privacy-item a.item-add').click(function() {
752 // Cannot add anything?
753 if(!exists('#privacy .privacy-head .list-left select option:selected'))
756 // Disable item select
757 $('#privacy .privacy-item select').attr('disabled', true);
761 disableFormPrivacy();
763 // Enable first form item
764 enableFormPrivacy('first');
765 enableFormPrivacy('active');
770 $('#privacy .privacy-item a.item-remove').click(function() {
771 // Cannot add anything?
772 if(!exists('#privacy .privacy-head .list-left select option:selected'))
776 var list = $('#privacy .privacy-head .list-left select').val();
777 var selected = $('#privacy .privacy-item select option:selected');
778 var item = selected.attr('data-value');
779 var hash = selected.attr('data-hash');
781 // Remove it from popup
782 $('#privacy .privacy-item select option:selected').remove();
784 // No more items in this list?
785 if(!exists('#privacy .privacy-item select option')) {
786 // Disable this select
787 $('#privacy .privacy-item select').attr('disabled', true);
789 // Remove the privacy list select item
790 $('#privacy .privacy-head .list-left select option[value=' + list + ']').remove();
792 // No more privacy lists?
793 if(!exists('#privacy .privacy-head .list-left select option'))
794 $('#privacy .privacy-head .list-left select').attr('disabled', true);
796 // Disable this list before removing it
797 var status = ['active', 'default'];
800 if(getDB('privacy-marker', status[s]) == list)
801 changePrivacy('', status[s]);
805 // Synchronize it with server
806 pushPrivacy(list, [], [item], [], [], [], [], [], [], hash, 'remove');
810 disableFormPrivacy();
815 $('#privacy .privacy-item a.item-save').click(function() {
817 if(exists('#privacy .privacy-form input:disabled'))
823 if(!$('#privacy .privacy-item select').is(':disabled'))
824 item_hash = $('#privacy .privacy-item select option:selected').attr('data-hash');
827 var privacy_second = '#privacy .privacy-second';
828 var item_list = $('#privacy .privacy-head .list-left select').val();
829 var item_action = $('#privacy .privacy-first input[name=action]').filter(':checked').val();
830 var item_type = $(privacy_second + ' input[name=type]').filter(':checked').val();
831 var item_order = $('#privacy .privacy-active input[name=order]').val();
834 // Switch the type to get the value
837 item_value = $(privacy_second + ' input[type=text][name=jid]').val();
842 item_value = $(privacy_second + ' select[name=group]').val();
847 item_value = $(privacy_second + ' select[name=subscription]').val();
857 // Get the selected things to do
858 var privacy_third_cb = '#privacy .privacy-third input[type=checkbox]';
859 var item_prin = false;
860 var item_prout = false;
861 var item_msg = false;
864 // Individual select?
865 if(!$(privacy_third_cb + '[name=everything]').filter(':checked').size()) {
866 if($(privacy_third_cb + '[name=send-messages]').filter(':checked').size())
868 if($(privacy_third_cb + '[name=send-queries]').filter(':checked').size())
870 if($(privacy_third_cb + '[name=send-queries]').filter(':checked').size())
872 if($(privacy_third_cb + '[name=see-status]').filter(':checked').size())
874 if($(privacy_third_cb + '[name=send-status]').filter(':checked').size())
878 // Push item to the server!
895 $('#privacy .privacy-first input').change(function() {
896 enableFormPrivacy('second');
899 $('#privacy .privacy-second input').change(function() {
900 enableFormPrivacy('third');
903 $('#privacy .privacy-third input[type=checkbox]').change(function() {
905 var target = '#privacy .privacy-third input[type=checkbox]';
907 // Must tick "everything" checkbox?
908 if(!$(target).filter(':checked').size())
909 $(target + '[name=everything]').attr('checked', true);
911 // Must untick the other checkboxes?
912 else if($(this).is('[name=everything]'))
913 $(target + ':not([name=everything])').removeAttr('checked');
915 // Must untick "everything" checkbox?
917 $(target + '[name=everything]').removeAttr('checked');
920 $('#privacy .privacy-active input[name=order]').keyup(function() {
922 var value = $(this).val();
932 value = parseInt(value);
938 // Apply the filtered value
948 $('#privacy .privacy-active .privacy-active-elements input').change(function() {
950 var list_name = $('#privacy .privacy-head .list-left select').val();
951 var state_name = $(this).attr('name');
954 if(!list_name || !state_name)
957 // Change the current list status
958 if($(this).filter(':checked').size())
959 changePrivacy(list_name, state_name);
961 changePrivacy('', state_name);