3 Jappix - An open social platform
4 These are the PEP JS scripts for Jappix
6 -------------------------------------------------
10 Last revision: 26/08/11
14 // Stores the PEP items
15 function storePEP(xid, type, value1, value2, value3, value4) {
16 // Handle the correct values
27 if(value1 || value2 || value3 || value4) {
28 // Define the XML variable
29 var xml = '<pep type="' + type + '">';
31 // Generate the correct XML
33 xml += '<artist>' + value1.htmlEnc() + '</artist><title>' + value2.htmlEnc() + '</title><album>' + value3.htmlEnc() + '</album><uri>' + value4.htmlEnc() + '</uri>';
34 else if(type == 'geoloc')
35 xml += '<lat>' + value1.htmlEnc() + '</lat><lon>' + value2.htmlEnc() + '</lon><human>' + value3.htmlEnc() + '</human>';
37 xml += '<value>' + value1.htmlEnc() + '</value><text>' + value2.htmlEnc() + '</text>';
42 // Update the input with the new value
43 setDB('pep-' + type, xid, xml);
47 removeDB('pep-' + type, xid);
49 // Display the PEP event
50 displayPEP(xid, type);
53 // Displays a PEP item
54 function displayPEP(xid, type) {
55 // Read the target input for values
56 var value = $(XMLFromString(getDB('pep-' + type, xid)));
60 // If the PEP element exists
63 var hash = hex_md5(xid);
69 // Parse the XML for mood and activity
70 if((type == 'mood') || (type == 'activity')) {
72 var pepValue = value.find('value').text();
73 var pepText = value.find('text').text();
79 // Apply the good values
81 fValue = moodIcon(pepValue);
82 else if(type == 'activity')
83 fValue = activityIcon(pepValue);
85 fText = _e("unknown");
92 fValue = moodIcon('undefined');
93 else if(type == 'activity')
94 fValue = activityIcon('exercising');
96 fText = _e("unknown");
100 fText = fText.htmlEnc();
103 else if(type == 'tune') {
104 fValue = 'tune-note';
107 // Parse the tune XML
108 var tArtist = value.find('artist').text();
109 var tTitle = value.find('title').text();
110 var tAlbum = value.find('album').text();
111 var tURI = value.find('uri').text();
112 var fArtist, fTitle, fAlbum, fURI;
114 // Apply the good values
115 if(!tArtist && !tAlbum && !tTitle) {
116 fText = _e("unknown");
123 fURI = 'http://grooveshark.com/search?q=' + encodeURIComponent(tArtist + ' ' + tTitle + ' ' + tAlbum);
129 fArtist = _e("unknown");
135 fTitle = _e("unknown");
141 fAlbum = _e("unknown");
145 // Generate the link to the title
146 aLink = ' href="' + fURI + '" target="_blank"';
148 // Generate the text to be displayed
149 dText = fArtist + ' - ' + fTitle + ' (' + fAlbum + ')';
150 fText = '<a' + aLink + '>' + dText + '</a>';
155 fText = _e("unknown");
160 else if(type == 'geoloc') {
161 fValue = 'location-world';
164 // Parse the geoloc XML
165 var tLat = value.find('lat').text();
166 var tLon = value.find('lon').text();
167 var tHuman = value.find('human').text();
170 // No human location?
172 tHuman = _e("See his/her position on the globe");
174 // Generate the text to be displayed
176 aLink = ' href="http://maps.google.com/?q=' + encodeQuotes(tLat) + ',' + encodeQuotes(tLon) + '" target="_blank"';
177 fText = '<a' + aLink + '>' + tHuman.htmlEnc() + '</a>';
182 dText = tLat + '; ' + tLon;
186 fText = _e("unknown");
192 fText = _e("unknown");
197 // Apply the text to the buddy infos
198 var this_buddy = '#buddy-list .buddy[data-xid=' + escape(xid) + ']';
200 if(exists(this_buddy))
201 $(this_buddy + ' .bi-' + type).replaceWith('<p class="bi-' + type + ' talk-images ' + fValue + '" title="' + encodeQuotes(dText) + '">' + fText + '</p>');
203 // Apply the text to the buddy chat
204 if(exists('#' + hash)) {
206 var bc_pep = $('#' + hash + ' .bc-pep');
208 // We remove the old PEP item
209 bc_pep.find('a.bi-' + type).remove();
211 // If the new PEP item is not null, create a new one
212 if(fText != _e("unknown"))
214 '<a' + aLink + ' class="bi-' + type + ' talk-images ' + fValue + '" title="' + encodeQuotes(dText) + '"></a>'
217 // Process the new status position
218 adaptChatPresence(hash);
221 // If this is the PEP values of the logged in user
222 if(xid == getXID()) {
223 // Change the icon/value of the target element
224 if((type == 'mood') || (type == 'activity')) {
225 // Change the input value
227 var dAttr = pepValue;
229 // Must apply default values?
230 if(pepValue == 'none') {
234 dAttr = 'exercising';
238 if(dText != _e("unknown"))
241 // Store this user event in our database
242 setDB(type + '-value', 1, dAttr);
243 setDB(type + '-text', 1, dVal);
245 // Apply this PEP event
246 $('#my-infos .f-' + type + ' a.picker').attr('data-value', dAttr);
247 $('#my-infos .f-' + type + ' input').val(dVal);
248 $('#my-infos .f-' + type + ' input').placeholder();
251 else if((type == 'tune') || (type == 'geoloc')) {
253 $('#my-infos .f-others a.' + type).remove();
256 if(dText != _e("unknown")) {
258 var href, title, icon_class;
263 icon_class = 'tune-note';
267 href = 'http://maps.google.com/?q=' + encodeQuotes(tLat) + ',' + encodeQuotes(tLon);
268 title = _e("Where are you?") + ' (' + dText + ')';
269 icon_class = 'location-world';
272 // Must create the container?
273 if(!exists('#my-infos .f-others'))
274 $('#my-infos .content').append('<div class="element f-others"></div>');
276 // Create the element
277 $('#my-infos .f-others').prepend(
278 '<a class="icon ' + type + '" href="' + encodeQuotes(href) + '" target="_blank" title="' + encodeQuotes(title) + '">' +
279 '<span class="talk-images ' + icon_class + '"></span>' +
285 else if(!exists('#my-infos .f-others a.icon'))
286 $('#my-infos .f-others').remove();
288 // Process the buddy-list height again
295 // Changes the mood icon
296 function moodIcon(value) {
313 case 'contemplative':
390 // Return the good icon name
394 // Changes the activity icon
395 function activityIcon(value) {
402 icon = 'activity-doing_chores';
406 icon = 'activity-drinking';
410 icon = 'activity-eating';
414 icon = 'activity-grooming';
417 case 'having_appointment':
418 icon = 'activity-having_appointment';
422 icon = 'activity-inactive';
426 icon = 'activity-relaxing';
430 icon = 'activity-talking';
434 icon = 'activity-traveling';
438 icon = 'activity-working';
441 icon = 'activity-exercising';
445 // Return the good icon name
449 // Sends the user's mood
450 function sendMood(value, text) {
451 /* REF: http://xmpp.org/extensions/xep-0107.html */
453 // We propagate the mood on the xmpp network
454 var iq = new JSJaCIQ();
457 // We create the XML document
458 var pubsub = iq.appendNode('pubsub', {'xmlns': NS_PUBSUB});
459 var publish = pubsub.appendChild(iq.buildNode('publish', {'node': NS_MOOD, 'xmlns': NS_PUBSUB}));
460 var item = publish.appendChild(iq.buildNode('item', {'xmlns': NS_PUBSUB}));
461 var mood = item.appendChild(iq.buildNode('mood', {'xmlns': NS_MOOD}));
463 if(value != 'none') {
464 mood.appendChild(iq.buildNode(value, {'xmlns': NS_MOOD}));
465 mood.appendChild(iq.buildNode('text', {'xmlns': NS_MOOD}, text));
468 // And finally we send the mood that is set
471 logThis('New mood sent: ' + value + ' (' + text + ')', 3);
474 // Sends the user's activity
475 function sendActivity(main, sub, text) {
476 // We propagate the mood on the xmpp network
477 var iq = new JSJaCIQ();
480 // We create the XML document
481 var pubsub = iq.appendNode('pubsub', {'xmlns': NS_PUBSUB});
482 var publish = pubsub.appendChild(iq.buildNode('publish', {'node': NS_ACTIVITY, 'xmlns': NS_PUBSUB}));
483 var item = publish.appendChild(iq.buildNode('item', {'xmlns': NS_PUBSUB}));
484 var activity = item.appendChild(iq.buildNode('activity', {'xmlns': NS_ACTIVITY}));
487 var mainType = activity.appendChild(iq.buildNode(main, {'xmlns': NS_ACTIVITY}));
491 mainType.appendChild(iq.buildNode(sub, {'xmlns': NS_ACTIVITY}));
493 activity.appendChild(iq.buildNode('text', {'xmlns': NS_ACTIVITY}, text));
496 // And finally we send the mood that is set
499 logThis('New activity sent: ' + main + ' (' + text + ')', 3);
502 // Sends the user's geographic position
503 function sendPosition(vLat, vLon, vAlt, vCountry, vCountrycode, vRegion, vPostalcode, vLocality, vStreet, vBuilding, vText, vURI) {
504 /* REF: http://xmpp.org/extensions/xep-0080.html */
506 // We propagate the position on pubsub
507 var iq = new JSJaCIQ();
510 // We create the XML document
511 var pubsub = iq.appendNode('pubsub', {'xmlns': NS_PUBSUB});
512 var publish = pubsub.appendChild(iq.buildNode('publish', {'node': NS_GEOLOC, 'xmlns': NS_PUBSUB}));
513 var item = publish.appendChild(iq.buildNode('item', {'xmlns': NS_PUBSUB}));
514 var geoloc = item.appendChild(iq.buildNode('geoloc', {'xmlns': NS_GEOLOC}));
516 // Create two position arrays
517 var pos_names = ['lat', 'lon', 'alt', 'country', 'countrycode', 'region', 'postalcode', 'locality', 'street', 'building', 'text', 'uri', 'timestamp'];
518 var pos_values = [ vLat, vLon, vAlt, vCountry, vCountrycode, vRegion, vPostalcode, vLocality, vStreet, vBuilding, vText, vURI, getXMPPTime('utc')];
520 for(var i = 0; i < pos_names.length; i++) {
521 if(pos_names[i] && pos_values[i])
522 geoloc.appendChild(iq.buildNode(pos_names[i], {'xmlns': NS_GEOLOC}, pos_values[i]));
525 // And finally we send the XML
530 logThis('Geolocated.', 3);
532 logThis('Not geolocated.', 2);
535 // Parses the user's geographic position
536 function parsePosition(data) {
537 var result = $(data).find('result:first');
539 // Get latitude and longitude
540 var lat = result.find('geometry:first location:first lat').text();
541 var lng = result.find('geometry:first location:first lng').text();
546 result.find('address_component:has(type:contains("country")):first long_name').text(),
547 result.find('address_component:has(type:contains("country")):first short_name').text(),
548 result.find('address_component:has(type:contains("administrative_area_level_1")):first long_name').text(),
549 result.find('address_component:has(type:contains("postal_code")):first long_name').text(),
550 result.find('address_component:has(type:contains("locality")):first long_name').text(),
551 result.find('address_component:has(type:contains("route")):first long_name').text(),
552 result.find('address_component:has(type:contains("street_number")):first long_name').text(),
553 result.find('formatted_address:first').text(),
554 'http://maps.google.com/?q=' + encodeQuotes(lat) + ',' + encodeQuotes(lng)
560 // Converts a position into an human-readable one
561 function humanPosition(tLocality, tRegion, tCountry) {
569 tHuman += ', ' + tRegion;
571 tHuman += ', ' + tCountry;
579 tHuman += ', ' + tCountry;
589 // Gets the user's geographic position
590 function getPosition(position) {
591 // Convert integers to strings
592 var vLat = '' + position.coords.latitude;
593 var vLon = '' + position.coords.longitude;
594 var vAlt = '' + position.coords.altitude;
596 // Get full position (from Google Maps API)
597 $.get('./php/geolocation.php', {latitude: vLat, longitude: vLon, language: XML_LANG}, function(data) {
599 var results = parsePosition(data);
618 setDB('geolocation', 'now', xmlToString(data));
620 logThis('Position details got from Google Maps API.');
623 logThis('Position got: latitude > ' + vLat + ' / longitude > ' + vLon + ' / altitude > ' + vAlt);
626 // Geolocates the user
627 function geolocate() {
628 // Don't fire it until options & features are not retrieved!
629 if(!getDB('options', 'geolocation') || (getDB('options', 'geolocation') == '0') || !enabledPEP())
632 // We publish the user location if allowed
633 if(navigator.geolocation) {
634 // Wait a bit... (to fix a bug)
635 $('#my-infos').stopTime().oneTime('1s', function() {
636 navigator.geolocation.getCurrentPosition(getPosition);
639 logThis('Geolocating...', 3);
644 logThis('Not geolocated: browser does not support it.', 1);
647 // Displays all the supported PEP events for a given XID
648 function displayAllPEP(xid) {
649 displayPEP(xid, 'mood');
650 displayPEP(xid, 'activity');
651 displayPEP(xid, 'tune');
652 displayPEP(xid, 'geoloc');
656 function launchPEP() {
657 // Apply empty values to the PEP database
658 setDB('mood-value', 1, '');
659 setDB('mood-text', 1, '');
660 setDB('activity-value', 1, '');
661 setDB('activity-text', 1, '');
663 // Click event for user mood
664 $('#my-infos .f-mood a.picker').click(function() {
665 // Initialize some vars
666 var path = '#my-infos .f-mood div.bubble';
667 var mood_id = ['crazy', 'excited', 'playful', 'happy', 'shocked', 'hot', 'sad', 'amorous', 'confident'];
668 var mood_lang = [_e("Crazy"), _e("Excited"), _e("Playful"), _e("Happy"), _e("Shocked"), _e("Hot"), _e("Sad"), _e("Amorous"), _e("Confident")];
669 var mood_val = $('#my-infos .f-mood a.picker').attr('data-value');
672 var can_append = true;
683 // Generate the HTML code
684 var html = '<div class="bubble removable">';
687 // Yet in use: no need to display it!
688 if(mood_id[i] == mood_val)
691 html += '<a href="#" class="talk-images" data-value="' + mood_id[i] + '" title="' + mood_lang[i] + '"></a>';
696 // Append the HTML code
697 $('#my-infos .f-mood').append(html);
700 $(path + ' a').click(function() {
701 // Update the mood marker
702 $('#my-infos .f-mood a.picker').attr('data-value', $(this).attr('data-value'));
707 // Focus on the status input
708 $(document).oneTime(10, function() {
709 $('#mood-text').focus();
718 // Click event for user activity
719 $('#my-infos .f-activity a.picker').click(function() {
720 // Initialize some vars
721 var path = '#my-infos .f-activity div.bubble';
722 var activity_id = ['doing_chores', 'drinking', 'eating', 'exercising', 'grooming', 'having_appointment', 'inactive', 'relaxing', 'talking', 'traveling', 'working'];
723 var activity_lang = [_e("Chores"), _e("Drinking"), _e("Eating"), _e("Exercising"), _e("Grooming"), _e("Appointment"), _e("Inactive"), _e("Relaxing"), _e("Talking"), _e("Traveling"), _e("Working")];
724 var activity_val = $('#my-infos .f-activity a.picker').attr('data-value');
727 var can_append = true;
738 // Generate the HTML code
739 var html = '<div class="bubble removable">';
741 for(i in activity_id) {
742 // Yet in use: no need to display it!
743 if(activity_id[i] == activity_val)
746 html += '<a href="#" class="talk-images" data-value="' + activity_id[i] + '" title="' + activity_lang[i] + '"></a>';
751 // Append the HTML code
752 $('#my-infos .f-activity').append(html);
755 $(path + ' a').click(function() {
756 // Update the activity marker
757 $('#my-infos .f-activity a.picker').attr('data-value', $(this).attr('data-value'));
762 // Focus on the status input
763 $(document).oneTime(10, function() {
764 $('#activity-text').focus();
773 // Submit events for PEP inputs
774 $('#mood-text, #activity-text').placeholder()
777 if(e.keyCode == 13) {
784 // Input blur handler
785 $('#mood-text').blur(function() {
786 // Read the parameters
787 var value = $('#my-infos .f-mood a.picker').attr('data-value');
788 var text = $(this).val();
790 // Must send the mood?
791 if((value != getDB('mood-value', 1)) || (text != getDB('mood-text', 1))) {
792 // Update the local stored values
793 setDB('mood-value', 1, value);
794 setDB('mood-text', 1, text);
797 sendMood(value, text);
801 // Input focus handler
806 // Input blur handler
807 $('#activity-text').blur(function() {
808 // Read the parameters
809 var value = $('#my-infos .f-activity a.picker').attr('data-value');
810 var text = $(this).val();
812 // Must send the activity?
813 if((value != getDB('activity-value', 1)) || (text != getDB('activity-text', 1))) {
814 // Update the local stored values
815 setDB('activity-value', 1, value);
816 setDB('activity-text', 1, text);
819 sendActivity(value, '', text);
823 // Input focus handler