var photostatus = photostatus || {}; /** * @param data */ photostatus.receivePhotoStatusData = function(data) { var viewer = data.get('viewer').getData(); var owner = data.get('owner').getData(); var ownerData = data.get('owner_data').getData()[owner.getId()]; //var ownerName = owner.getDisplayName(); var ownerImage = ownerData['image']; var ownerStatus = ownerData['text']; //var html = ['', ownerName, ' is ', ownerStatus]; var html = ['
', ownerStatus]; if (viewer.getId() === owner.getId()) { html = photostatus.addSetStatusInputs(html, ownerImage, ownerStatus); } document.getElementById('profile').innerHTML = html.join(''); }; /** * Asks for the photo status image and text for the profile owner. Also * requests viewer data to see if the owner and viewer are the same. */ photostatus.requestPhotoStatusData = function() { var fields = ['image', 'text']; var req = opensocial.newDataRequest(); // Not needed to fetch data, but to get the viewer's ID number req.add(req.newFetchPersonRequest('VIEWER'), 'viewer'); // Not needed to fetch data, but to get the owner's ID number req.add(req.newFetchPersonRequest('OWNER'), 'owner'); // Get the photo status data for the owner. req.add(req.newFetchPersonAppDataRequest('OWNER', fields), 'owner_data'); req.send(photostatus.receivePhotoStatusData); }; /** * If the viewer of the page is the owner, this function is called to add the * HTML elements which allow the user to change the photo and status text. * @param htmlParts {Array} The HTML elements which will be added to the page. * @param imageUrl {String} The URL of the image the user is currently using * for their status. * @param status {String} Current status text for the user. * @return The htmlParts array with new elements appended. */ photostatus.addSetStatusInputs = function(htmlParts, imageUrl, status) { htmlParts.push('
Status: '); htmlParts.push('Image URL: '); htmlParts.push('
'); htmlParts.push('
'); return htmlParts; }; /** * */ photostatus.setPhotoStatus = function() { var req = opensocial.newDataRequest(); var imageInput = document.getElementById('photostatus.statusImage'); var textInput = document.getElementById('photostatus.statusText'); if (imageInput && textInput) { req.add(req.newUpdatePersonAppDataRequest('VIEWER', 'image', imageInput.value), 'image_handle'); req.add(req.newUpdatePersonAppDataRequest('VIEWER', 'text', textInput.value), 'text_handle'); req.send(photostatus.verifyResponse); } }; /** * @param data */ photostatus.verifyResponse = function(data) { var datakey_error = data.get('image_handle').hadError(); var displayDiv = document.getElementById('photostatus.updateResult'); if (datakey_error) { displayDiv .innerHTML = 'Error when setting your status'; } else { displayDiv .innerHTML = 'Your status has been updated.'; // Request the image status again to update the image and text. photostatus.requestPhotoStatusData(); } }; photostatus.getProfileHtml = function() { //photostatus.setTestData(); photostatus.requestPhotoStatusData(); return 'Loading...'; };