Modified: rave/trunk/rave-portal-resources/src/main/webapp/static/script/portal/rave_person_profile.js URL: http://svn.apache.org/viewvc/rave/trunk/rave-portal-resources/src/main/webapp/static/script/portal/rave_person_profile.js?rev=1503074&r1=1503073&r2=1503074&view=diff ============================================================================== --- rave/trunk/rave-portal-resources/src/main/webapp/static/script/portal/rave_person_profile.js (original) +++ rave/trunk/rave-portal-resources/src/main/webapp/static/script/portal/rave_person_profile.js Sun Jul 14 22:55:06 2013 @@ -17,10 +17,13 @@ * under the License. */ -var rave = rave || {}; -rave.personprofile = rave.personprofile || (function() { +define(["jquery", "portal/rave_portal", "rave", "bootstrap"], function($, ravePortal, rave){ // map of {subpage name, boolean} tracking whether or not a sub page has been viewed at least once var subPagesViewedStatus = {}; + var friends = new Array(); + var requestsSent = new Array(); + var friendRequestsReceived = new Array() + function initSubPages() { var $tabs = $('#personProfileSubPages a[data-toggle="tab"]'); //If the implementation does not use bootstrap tabs / subpages, skip this method @@ -85,6 +88,29 @@ rave.personprofile = rave.personprofile return i18nStr; } + function userProfileEditHandler(isEdit) { + //get the edit element + var profileInfo = document.getElementById("profileInfo"); + //extract hidden fields through their class + var hiddenFields = "." + profileInfo.value + "-hidden"; + //extract labels through their class + var visibleFields = "." + profileInfo.value + "-visible"; + + if (isEdit) { + //make hidden fields visible + $(hiddenFields).show(); + //make visible fields invisible + $(visibleFields).hide(); + } + + else { + //make hidden fields invisible + $(hiddenFields).hide(); + //make visible fields visible + $(visibleFields).show(); + } + } + function dealWithUserResults(userResults){ var currentUser = $("#addRemoveFriend").get(0).value; var searchTerm = $('#searchTerm').get(0).value; @@ -95,9 +121,9 @@ rave.personprofile = rave.personprofile } var legend; if(userResults.result.resultSet.length < 1){ - legend = rave.getClientMessage("no.results.found"); + legend = ravePortal.getClientMessage("no.results.found"); }else{ - legend = updateParamsInString(rave.getClientMessage("search.list.result.x.to.y"), + legend = updateParamsInString(ravePortal.getClientMessage("search.list.result.x.to.y"), new Array(userResults.result.offset + 1, userResults.result.resultSet.length + userResults.result.offset, userResults.result.totalResults)); } @@ -110,93 +136,93 @@ rave.personprofile = rave.personprofile //now build the content $targetDiv .append( - $("<table/>") - .addClass("searchdialogcontent") + $("<table/>") + .addClass("searchdialogcontent") + .append( + $("<tr/>") .append( - $("<tr/>") - .append( - $("<td/>") - .addClass("textcell") - .append( - $("<b/>") - .text(rave.getClientMessage("common.username")) - ) - ) - .append( - $("<td/>") - .addClass("booleancell") - .append( - $("<b/>") - .text(rave.getClientMessage("common.friend.status")) - ) - ) + $("<td/>") + .addClass("textcell") + .append( + $("<b/>") + .text(ravePortal.getClientMessage("common.username")) ) + ) .append( - $("<tbody/>") - .attr("id", "searchResultsBody") + $("<td/>") + .addClass("booleancell") + .append( + $("<b/>") + .text(ravePortal.getClientMessage("common.friend.status")) ) - ); + ) + ) + .append( + $("<tbody/>") + .attr("id", "searchResultsBody") + ) + ); - jQuery.each(userResults.result.resultSet, function() { + $.each(userResults.result.resultSet, function() { $('#searchResultsBody') .append( - $("<tr/>") - .attr("id", "searchResultRecord") - .append( - $("<td/>") - .text(this.username) - ) - .append( - $("<td/>") - .attr("id", "friendStatusButtonHolder" + this.id) - ) - ); + $("<tr/>") + .attr("id", "searchResultRecord") + .append( + $("<td/>") + .text(this.username) + ) + .append( + $("<td/>") + .attr("id", "friendStatusButtonHolder" + this.id) + ) + ); if(this.username != currentUser){ // check if already added - if(rave.personprofile.isUserAlreadyFriend(this.username)){ + if(isUserAlreadyFriend(this.username)){ $('#friendStatusButtonHolder'+this.id) .append( - $("<a/>") - .attr("href", "#") - .attr("id", this.entityId) - .attr("onclick", "rave.personprofile.removeFriend("+this.id+", '"+this.username+"');") - .text(rave.getClientMessage("common.remove")) - ); + $("<a/>") + .attr("href", "#") + .attr("id", this.entityId) + .click(function(){removeFriend(this.id, this.username)}) + .text(ravePortal.getClientMessage("common.remove")) + ); // check if already sent friend request - }else if(rave.personprofile.isFriendRequestSent(this.username)){ + }else if(isFriendRequestSent(this.username)){ $('#friendStatusButtonHolder'+this.id) .append( - $("<a/>") - .attr("href", "#") - .attr("id", this.entityId) - .attr("onclick", "rave.personprofile.removeFriendRequestSent("+this.id+", '"+this.username+"');") - .text(rave.getClientMessage("common.cancel.request")) - ); - }else if(rave.personprofile.isFriendRequestReceived(this.username)){ + $("<a/>") + .attr("href", "#") + .attr("id", this.entityId) + .click(function(){removeFriendRequestSent(this.id, this.username)}) + .text(ravePortal.getClientMessage("common.cancel.request")) + ); + }else if(isFriendRequestReceived(this.username)){ $('#friendStatusButtonHolder'+this.id) .append( - $("<a/>") - .attr("href", "#") - .attr("id", this.entityId) - .attr("onclick", "rave.personprofile.acceptFriendRequest("+this.entityId+", '"+this.username+"');") - .text(rave.getClientMessage("common.accept")), - ' / ', - $("<a/>") - .attr("href", "#") - .attr("id", this.id) - .attr("onclick", "rave.personprofile.declineFriendRequest("+this.id+", '"+this.username+"');") - .text(rave.getClientMessage("common.decline")) - ); + $("<a/>") + .attr("href", "#") + .attr("id", this.entityId) + .click(function(){acceptFriendRequest(this.entityId, this.username)}) + .text(ravePortal.getClientMessage("common.accept")), + ' / ', + $("<a/>") + .attr("href", "#") + .attr("id", this.id) + .click(function(){declineFriendRequest(this.id, this.username)}) + .text(ravePortal.getClientMessage("common.decline")) + ); }else { $('#friendStatusButtonHolder'+this.id) .append( - $("<a/>") - .attr("href", "#") - .attr("id", this.id) - .attr("onclick", "rave.personprofile.addFriend("+this.id+", '"+this.username+"');") - .text(rave.getClientMessage("common.add")) - ); + $("<a/>") + .attr("href", "#") + .attr("id", this.id) + .click(function(){addFriend(this.id, this.username)}) + .text(ravePortal.getClientMessage("common.add")) + ); } } @@ -210,28 +236,31 @@ rave.personprofile = rave.personprofile $pagingDiv.append('<div class="pagination"><ul id="pagingul" >'); if(userResults.result.currentPage > 1){ offset = (userResults.result.currentPage - 2) * userResults.result.pageSize; - $('#pagingul').append('<li><a href="#" onclick="rave.api.rpc.getUsers({offset: ' + - offset+', successCallback: function(result)' + - ' {rave.personprofile.dealWithUserResults(result);}});"><</a></li>'); + $('#pagingul').append('<li><a href="#" class="paginationLink" data-offset="' + offset + '">' + '<</a></li>'); } for(var i=1;i<=userResults.result.numberOfPages;i++){ if(i == userResults.result.currentPage){ $('#pagingul').append('<li class="active"><a href="#">'+i+'</a></li>'); }else{ offset = (i - 1) * userResults.result.pageSize; - $('#pagingul').append('<li><a href="#" onclick="rave.api.rpc.getUsers({offset: ' + - offset + ', successCallback: function(result)' + - ' {rave.personprofile.dealWithUserResults(result);}});">' + i + '</a></li>'); + $('#pagingul').append('<li><a href="#" class="paginationLink" data-offset="' + offset + '">' + i + '</a></li>'); } } if (userResults.result.currentPage < userResults.result.numberOfPages){ offset = (userResults.result.currentPage) * userResults.result.pageSize; - $('#pagingul').append('<li><a href="#" onclick="rave.api.rpc.getUsers({offset: ' + - offset + ', successCallback: function(result)' + - ' {rave.personprofile.dealWithUserResults(result);}});">></a></li>'); + $('#pagingul').append('<li><a href="#" class="paginationLink" data-offset="' + offset + '">' + '></a></li>'); } $pagingDiv.append('</ul></div>'); } + + $('.paginationLink').click(function(){ + rave.api.rpc.getUsers({ + offset: $(this).data('offset'), + successCallback: function(result){ + dealWithUserResults(result); + } + }) + }) } // Add a friend to the current user @@ -239,17 +268,16 @@ rave.personprofile = rave.personprofile $('#friendStatusButtonHolder'+userId).hide(); rave.api.rpc.addFriend({friendUsername : username, successCallback: function(result) { - rave.personprofile.addFriendRequestUI(username); + addFriendRequestUI(username); $('#friendStatusButtonHolder'+userId).empty(); $('#friendStatusButtonHolder'+userId) .append( - $("<a/>") - .attr("href", "#") - .attr("id", userId) - .attr("onclick", "rave.personprofile.removeFriendRequestSent(" + - userId+", '" + username+"');") - .text(rave.getClientMessage("common.cancel.request")) - ); + $("<a/>") + .attr("href", "#") + .attr("id", userId) + .click(function(){removeFriendRequestSent(userId, username)}) + .text(ravePortal.getClientMessage("common.cancel.request")) + ); $('#friendStatusButtonHolder'+userId).show(); } }); @@ -257,23 +285,22 @@ rave.personprofile = rave.personprofile // Remove a friend of the current user function removeFriend(userId, username){ - var message = updateParamsInString(rave.getClientMessage("remove.friend.confirm"), + var message = updateParamsInString(ravePortal.getClientMessage("remove.friend.confirm"), new Array(username)); if(confirm(message)){ $('#friendStatusButtonHolder'+userId).hide(); rave.api.rpc.removeFriend({friendUsername : username, successCallback: function(result) { - rave.personprofile.removeFriendUI(username); + removeFriendUI(username); $('#friendStatusButtonHolder'+userId).empty(); $('#friendStatusButtonHolder'+userId) .append( - $("<a/>") - .attr("href", "#") - .attr("id", userId) - .attr("onclick", "rave.personprofile.addFriend(" + - userId+", '" + username+"');") - .text(rave.getClientMessage("common.add")) - ); + $("<a/>") + .attr("href", "#") + .attr("id", userId) + .click(function(){addFriend(userId, username)}) + .text(ravePortal.getClientMessage("common.add")) + ); $('#friendStatusButtonHolder'+userId).show(); } }); @@ -282,23 +309,22 @@ rave.personprofile = rave.personprofile // Cancel the friend request already sent to a user function removeFriendRequestSent(userId, username){ - var message = updateParamsInString(rave.getClientMessage("remove.friend.request.confirm"), + var message = updateParamsInString(ravePortal.getClientMessage("remove.friend.request.confirm"), new Array(username)); if(confirm(message)){ $('#friendStatusButtonHolder'+userId).hide(); rave.api.rpc.removeFriend({friendUsername : username, successCallback: function(result) { - rave.personprofile.removeFriendRequestSentUI(username); + removeFriendRequestSentUI(username); $('#friendStatusButtonHolder'+userId).empty(); $('#friendStatusButtonHolder'+userId) .append( - $("<a/>") - .attr("href", "#") - .attr("id", userId) - .attr("onclick", "rave.personprofile.addFriend(" + - userId+", '" + username+"');") - .text(rave.getClientMessage("common.add")) - ); + $("<a/>") + .attr("href", "#") + .attr("id", userId) + .click(function(){addFriend(userId, username)}) + .text(ravePortal.getClientMessage("common.add")) + ); $('#friendStatusButtonHolder'+userId).show(); } }); @@ -310,17 +336,16 @@ rave.personprofile = rave.personprofile $('#friendStatusButtonHolder'+userId).hide(); rave.api.rpc.acceptFriendRequest({friendUsername : username, successCallback: function(result) { - rave.personprofile.removeFriendRequestReceivedUI(username); + removeFriendRequestReceivedUI(username); $('#friendStatusButtonHolder'+userId).empty(); $('#friendStatusButtonHolder'+userId) .append( - $("<a/>") - .attr("href", "#") - .attr("id", userId) - .attr("onclick", "rave.personprofile.removeFriend(" + - userId+", '" + username+"');") - .text(rave.getClientMessage("common.remove")) - ); + $("<a/>") + .attr("href", "#") + .attr("id", userId) + .click(function(){removeFriend(userId, username)}) + .text(ravePortal.getClientMessage("common.remove")) + ); $('#friendStatusButtonHolder'+userId).show(); } }); @@ -331,44 +356,43 @@ rave.personprofile = rave.personprofile $('#friendStatusButtonHolder'+userId).hide(); rave.api.rpc.removeFriend({friendUsername : username, successCallback: function(result) { - rave.personprofile.removeFriendRequestReceivedUI(username); + removeFriendRequestReceivedUI(username); $('#friendStatusButtonHolder'+userId).empty(); $('#friendStatusButtonHolder'+userId) .append( - $("<a/>") - .attr("href", "#") - .attr("id", userId) - .attr("onclick", "rave.personprofile.addFriend(" + - userId+", '" + username+"');") - .text(rave.getClientMessage("common.add")) - ); + $("<a/>") + .attr("href", "#") + .attr("id", userId) + .click(function(){addFriend(userId, username)}) + .text(ravePortal.getClientMessage("common.add")) + ); $('#friendStatusButtonHolder'+userId).show(); } }); } // Add an item to the List of friend requests sent(maintained for the UI) function addFriendRequestUI(username){ - rave.personprofile.requestsSent.push(username); + requestsSent.push(username); } // Remove a friend from the list of friends(maintained for the UI) function removeFriendUI(friendUsername){ - rave.personprofile.friends.splice(rave.personprofile.friends.indexOf(friendUsername),1); + friends.splice(friends.indexOf(friendUsername),1); } // Remove a friend request from the list of friend requests sent(maintained for the UI) function removeFriendRequestSentUI(friendUsername){ - rave.personprofile.requestsSent.splice(rave.personprofile.requestsSent.indexOf(friendUsername),1); + requestsSent.splice(requestsSent.indexOf(friendUsername),1); } // Remove a friend request from the list of friend requests received(maintained for the UI) function removeFriendRequestReceivedUI(friendUsername){ - rave.personprofile.friendRequestsReceived.splice(rave.personprofile.friendRequestsReceived.indexOf(friendUsername),1); + friendRequestsReceived.splice(friendRequestsReceived.indexOf(friendUsername),1); } // Check if the user is already a friend function isUserAlreadyFriend(username){ - if(rave.personprofile.friends.indexOf(username)>=0){ + if(friends.indexOf(username)>=0){ return true; } else { return false; @@ -377,7 +401,7 @@ rave.personprofile = rave.personprofile // Check if a friend request is already sent to a particular user function isFriendRequestSent(username){ - if(rave.personprofile.requestsSent.indexOf(username)>=0){ + if(requestsSent.indexOf(username)>=0){ return true; } else { return false; @@ -386,7 +410,7 @@ rave.personprofile = rave.personprofile // Check if a friend request is received from a particular user function isFriendRequestReceived(username){ - if(rave.personprofile.friendRequestsReceived.indexOf(username)>=0){ + if(friendRequestsReceived.indexOf(username)>=0){ return true; } else { return false; @@ -398,14 +422,14 @@ rave.personprofile = rave.personprofile var $editButton = $("#profileEdit"); if ($editButton) { $editButton.click(function() { - rave.api.handler.userProfileEditHandler(true); + userProfileEditHandler(true); }); } //user clicks add/remove friend button in the profile page var $friendButton = $("#addRemoveFriend"); if ($friendButton) { $friendButton.click(function() { - rave.personprofile.getFriends({successCallback : function() { + getFriends({successCallback : function() { rave.api.rpc.getUsers({offset: 0, successCallback: function(result) { dealWithUserResults(result); @@ -421,8 +445,9 @@ rave.personprofile = rave.personprofile $('#userSearchResults').empty(); rave.api.rpc.searchUsers({searchTerm: $('#searchTerm').get(0).value, offset: 0, successCallback: function(result) { - rave.personprofile.dealWithUserResults(result); - } + dealWithUserResults(result); + }, + alertEmptySearch: function(){alert(ravePortal.getClientMessage("api.rpc.empty.search.term"));} }); }); @@ -432,7 +457,7 @@ rave.personprofile = rave.personprofile $('#userSearchResults').empty(); rave.api.rpc.getUsers({offset: 0, successCallback: function(result) { - rave.personprofile.dealWithUserResults(result); + dealWithUserResults(result); } }); }); @@ -441,7 +466,7 @@ rave.personprofile = rave.personprofile var $cancelButton = $("#cancelEdit"); if ($cancelButton) { $cancelButton.click(function() { - rave.api.handler.userProfileEditHandler(false); + userProfileEditHandler(false); }); } @@ -453,13 +478,13 @@ rave.personprofile = rave.personprofile var listRequestItem = $(this).parents('.requestItem'); var friendRequestMenu = $(listRequestItem).parent(); $(listRequestItem).remove(); - $('.friendRequestDropdown').append('<li class="message">'+rave.getClientMessage("common.accepted")+'</li>'); + $('.friendRequestDropdown').append('<li class="message">'+ravePortal.getClientMessage("common.accepted")+'</li>'); $('.message').fadeOut(2000, function() { $('.message').remove(); var childItems = $(friendRequestMenu).children('li'); - $('.friendRequestDropdownLink').html(''+rave.getClientMessage("person.profile.friend.requests")+' ('+childItems.size()+')'); + $('.friendRequestDropdownLink').html(''+ravePortal.getClientMessage("person.profile.friend.requests")+' ('+childItems.size()+')'); if(childItems.size()==0) - $('.friendRequestDropdown').append('<li>'+rave.getClientMessage("person.profile.friend.requests.none")+'</li>'); + $('.friendRequestDropdown').append('<li>'+ravePortal.getClientMessage("person.profile.friend.requests.none")+'</li>'); }); e.stopPropagation(); }); @@ -473,13 +498,13 @@ rave.personprofile = rave.personprofile var listRequestItem = $(this).parents('.requestItem'); var friendRequestMenu = $(listRequestItem).parent(); $(listRequestItem).remove(); - $('.friendRequestDropdown').append('<li class="message">'+rave.getClientMessage("common.declined")+'</li>'); + $('.friendRequestDropdown').append('<li class="message">'+ravePortal.getClientMessage("common.declined")+'</li>'); $('.message').fadeOut(2000, function() { $('.message').remove(); var childItems = $(friendRequestMenu).children('li'); - $('.friendRequestDropdownLink').html(''+rave.getClientMessage("person.profile.friend.requests")+' ('+childItems.size()+')'); + $('.friendRequestDropdownLink').html(''+ravePortal.getClientMessage("person.profile.friend.requests")+' ('+childItems.size()+')'); if(childItems.size()==0) - $('.friendRequestDropdown').append('<li>'+rave.getClientMessage("person.profile.friend.requests.none")+'</li>'); + $('.friendRequestDropdown').append('<li>'+ravePortal.getClientMessage("person.profile.friend.requests.none")+'</li>'); }); e.stopPropagation(); }); @@ -488,22 +513,19 @@ rave.personprofile = rave.personprofile // Gets the list of friends from the DB function getFriends(args) { - rave.personprofile.friends = new Array(); - rave.personprofile.requestsSent = new Array(); - rave.personprofile.friendRequestsReceived = new Array(); rave.api.rpc.getFriends({ successCallback: function(result) { - jQuery.each(result.result.accepted, function() { - if(!rave.personprofile.isUserAlreadyFriend(this.username)) - rave.personprofile.friends.push(this.username); + $.each(result.result.accepted, function() { + if(!isUserAlreadyFriend(this.username)) + friends.push(this.username); }); - jQuery.each(result.result.sent, function() { - if(!rave.personprofile.isFriendRequestSent(this.username)) - rave.personprofile.requestsSent.push(this.username); + $.each(result.result.sent, function() { + if(!isFriendRequestSent(this.username)) + requestsSent.push(this.username); }); - jQuery.each(result.result.received, function() { - if(!rave.personprofile.isFriendRequestReceived(this.username)) - rave.personprofile.friendRequestsReceived.push(this.username); + $.each(result.result.received, function() { + if(!isFriendRequestReceived(this.username)) + friendRequestsReceived.push(this.username); }); if(result !=null && typeof args.successCallback == 'function') { args.successCallback(); @@ -517,8 +539,9 @@ rave.personprofile = rave.personprofile initButtons(); } + rave.registerOnInitHandler(init); + return { - init : init, dealWithUserResults : dealWithUserResults, addFriend : addFriend, removeFriend : removeFriend, @@ -534,4 +557,4 @@ rave.personprofile = rave.personprofile acceptFriendRequest : acceptFriendRequest, declineFriendRequest : declineFriendRequest }; -}()); +}) \ No newline at end of file
Modified: rave/trunk/rave-portal-resources/src/main/webapp/static/script/portal/rave_portal.js URL: http://svn.apache.org/viewvc/rave/trunk/rave-portal-resources/src/main/webapp/static/script/portal/rave_portal.js?rev=1503074&r1=1503073&r2=1503074&view=diff ============================================================================== --- rave/trunk/rave-portal-resources/src/main/webapp/static/script/portal/rave_portal.js (original) +++ rave/trunk/rave-portal-resources/src/main/webapp/static/script/portal/rave_portal.js Sun Jul 14 22:55:06 2013 @@ -16,334 +16,151 @@ * specific language governing permissions and limitations * under the License. */ -var rave = _.extend(rave, (function () { - var clientMessages = {}; - var context=""; - - var pageEditor = true; - var pageViewer = { - username: "Unknown", - id: -1 - }; - var pageOwner = { - username: "Unknown", - id: -1 - }; - // JS debug mode is off by default - var javaScriptDebugMode = 0; - //Assigning a default value of 250 if unable to get value from the DB - var defaultWidgetHeight = 250; - var onWidgetsInitializedHandlers = []; - var onProvidersInitializedHandlers = []; - var onUIInitializedHandlers = []; - var onPageInitializedHandlers = []; - - function getClientMessage(key) { - return clientMessages[key]; - } - - function addClientMessage(key, message) { - return clientMessages[key] = message; - } - - function renderErrorWidget(id, message) { - $("#widget-" + id + "-body").html(message); - } - - - function setJavaScriptDebugMode(debugMode) { - javaScriptDebugMode = debugMode; - } - - function getJavaScriptDebugMode() { - return javaScriptDebugMode; - } - - function setPageViewer(viewer) { - pageViewer = viewer; - } - - function getPageViewer() { - return pageViewer; - } - - function setPageOwner(owner) { - pageOwner = owner; - } - - function getPageOwner() { - return pageOwner; - } - - function viewPage(pageId) { - var fragment = (pageId != null) ? ("/" + pageId) : ""; - window.location.href = rave.getContext() + "page/view" + fragment; - } - - function viewWidgetDetail(widgetId, referringPageId, jumpToId) { - if (jumpToId) { - jumpToId = '#' + jumpToId; - } - else { - jumpToId = ''; - } - window.location.href = rave.getContext() + "store/widget/" + widgetId + "?referringPageId=" + referringPageId + jumpToId; - } - - /** - * Determines if a page is empty (has zero widgets) - */ - function isPageEmpty() { - return $.isEmptyObject(widgetByIdMap); - } - - /** - * Removes a regionWidgetId from the internal widget map - * @param regionWidgetId the region widget id to remove - */ - function removeWidgetFromMap(regionWidgetId) { - delete widgetByIdMap[regionWidgetId]; - } - - function initPageEditorStatus(status) { - if (status != "undefined") { - pageEditor = status; - } - } - - function isPageEditor() { - return pageEditor; - } - - function registerOnWidgetsInitizalizedHandler(callback) { - if (onWidgetsInitializedHandlers !== null) { - onWidgetsInitializedHandlers.push(callback); - } else { - callback(); - } - }; - - function registerOnProvidersInitizalizedHandler(callback) { - if (onProvidersInitializedHandlers !== null) { - onProvidersInitializedHandlers.push(callback); - } else { - callback(); - } - }; - - function registerOnUIInitizalizedHandler(callback) { - if (onUIInitializedHandlers !== null) { - onUIInitializedHandlers.push(callback); - } else { - callback(); - } - }; - - function registerOnPageInitizalizedHandler(callback) { - if (onPageInitializedHandlers !== null) { - onPageInitializedHandlers.push(callback); - } else { - callback(); - } - }; - - - /** - * Internal method should only be called from the page.jsp - */ - function runOnPageInitializedHandlers() { - if (onPageInitializedHandlers !== null && onPageInitializedHandlers.length > 0) { - for (var i = 0, j = onPageInitializedHandlers.length; i < j; ++i) { - try { - onPageInitializedHandlers[i](); - } catch (ex) { - gadgets.warn("Could not fire onPageInitializedHandler " + ex.message); - } - } - } - onPageInitializedHandlers = null; // No need to hold these references anymore. - } - - function setContext(contextPath) { - context = contextPath; - rave.api.setContext(contextPath); - } - - function getContext() { - return context; - } - - function extractObjectIdFromElementId(elementId) { - var tokens = elementId.split("-"); - return tokens.length > 2 && tokens[0] == "widget" || tokens[0] == "region" ? tokens[1] : null; - } - - function showInfoMessage(message) { - - var markup = rave.ui.templates['info-message']({ - message: message - }); - - $(markup).hide() - .prependTo("body") - //TODO: add a class for flash box and attach these styles - .css({ position: 'fixed', top: 0, left: 0, width: 'auto', 'max-width': '60%', 'font-size': '1.25em', padding: '.6em 1em', 'z-index': 9999, 'border-radius': '0 0 4px 0'}) - .fadeIn('fast').delay(8000) - .fadeOut(function () { - $(this).remove(); - }); - } - - function displayUsersOfWidget(widgetId) { - rave.api.rest.getUsersForWidget({widgetId: widgetId, successCallback: function (data) { - - //format data for display - _.each(data, function (person) { - person.name = person.displayName || person.preferredName || (person.givenName + " " + person.familyName); - }); - - var markup = rave.ui.templates['users-of-widget']({ - users: data, - //TODO: data from dom evil! should be using gadget object to get name - widgetName: $("#widget-" + widgetId + "-title").text().trim() - }); - - //TODO: don't use jquery ui dialogs? - $(markup).dialog({ - modal: true, - buttons: [ - {text: "Close", click: function () { - $(this).dialog("close"); - }} - ] - }); - }}); - } - - /** - * Public API - */ - return { - displayUsersOfWidget: displayUsersOfWidget, +define(["underscore", "jquery", "rave", "portal/rave_templates", "clientMessages"], + function (_, $, rave, raveTemplates, clientMessages) { - showInfoMessage: showInfoMessage, + // JS debug mode is off by default + var javaScriptDebugMode = 0; - getObjectIdFromDomId: extractObjectIdFromElementId, - /** - * Sets the context path for the Rave web application - * - * @param contextPath the context path of the rave webapp - */ - setContext: setContext, - - /** - * Gets the current context - */ - getContext: getContext, - /** - * Renders an error in place of the widget - * - * @param id the RegionWidgetId of the widget to render in error mode - * @param message The message to display to the user - */ - errorWidget: renderErrorWidget, - - /** - * Gets the value of the JavaScriptDebugMode flag - * 0 = off - * 1 = on - */ - getJavaScriptDebugMode: getJavaScriptDebugMode, - - /** - * Sets the value of the JavaScriptDebugMode flag - * 0 = off - * 1 = on - */ - setJavaScriptDebugMode: setJavaScriptDebugMode, + function getClientMessage(key) { + return clientMessages[key]; + } - /** - * Sets the authenticated page viewer for the Rave web application - * - * @param viewer an object representing the authenticated user viewing the page {username:"bob", id:"1"} - */ - setPageViewer: setPageViewer, + function addClientMessage(key, message) { + return clientMessages[key] = message; + } - /** - * Gets the current viewer - */ - getPageViewer: getPageViewer, + function renderErrorWidget(id, message) { + $("#widget-" + id + "-body").html(message); + } - /** - * Sets the owner of the current page - * - * @param owner an object representing the owner of the page - */ - setPageOwner: setPageOwner, + function setJavaScriptDebugMode(debugMode) { + javaScriptDebugMode = debugMode; + } - /** - * Gets the page owner - */ - getPageOwner: getPageOwner, + function getJavaScriptDebugMode() { + return javaScriptDebugMode; + } - /** - * View a page - * - * @param pageId the pageId to view, or if null, the user's default page - */ - viewPage: viewPage, + function viewPage(pageId) { + var fragment = (pageId != null) ? ("/" + pageId) : ""; + window.location.href = rave.getContext() + "page/view" + fragment; + } - /** - * View the widget detail page of a widget - * - * @param widgetId to widgetId to view - * @param referringPageId the id of the page the call is coming from - */ - viewWidgetDetail: viewWidgetDetail, + function viewWidgetDetail(widgetId, referringPageId, jumpToId) { + if (jumpToId) { + jumpToId = '#' + jumpToId; + } + else { + jumpToId = ''; + } + window.location.href = rave.getContext() + "store/widget/" + widgetId + "?referringPageId=" + referringPageId + jumpToId; + } /** * Determines if a page is empty (has zero widgets) - * @param widgetByIdMap the map of widgets on the page */ - isPageEmpty: isPageEmpty, + function isPageEmpty() { + return _.isEmpty(rave.getWidgets()); + } - /** - * Returns a language specific message based on the supplied key - * - * @param key the key of the message - */ - getClientMessage: getClientMessage, + function isPageEditor() { + return rave.getViewer().editor; + } - /** - * Adds a message to the internal client message map - * - * @param key - * @param message - */ - addClientMessage: addClientMessage, + function extractObjectIdFromElementId(elementId) { + var tokens = elementId.split("-"); + return tokens.length > 2 && tokens[0] == "widget" || tokens[0] == "region" ? tokens[1] : null; + } - /** - * Set if user of a page has editing permission - * Used to stop sending UI events back to the server, rather - * than actually implementing any permission rules - * (which are set on the server) - */ - initPageEditorStatus: initPageEditorStatus, + function showInfoMessage(message) { - /** - * Returns a boolean indicating if the user - * should be treated as an page editor or not - */ - isPageEditor: isPageEditor, + var markup = raveTemplates.templates['info-message']({ + message: message + }); - /** - * Registration methods for initialization events - */ - registerOnWidgetsInitizalizedHandler: registerOnWidgetsInitizalizedHandler, - registerOnProvidersInitizalizedHandler: registerOnProvidersInitizalizedHandler, - registerOnUIInitizalizedHandler: registerOnUIInitizalizedHandler, - registerOnPageInitizalizedHandler: registerOnPageInitizalizedHandler, - runOnPageInitializedHandlers: runOnPageInitializedHandlers - } -})() -); + $(markup).hide() + .prependTo("body") + //TODO: add a class for flash box and attach these styles + .css({ position: 'fixed', top: 0, left: 0, width: 'auto', 'max-width': '60%', 'font-size': '1.25em', padding: '.6em 1em', 'z-index': 9999, 'border-radius': '0 0 4px 0'}) + .fadeIn('fast').delay(8000) + .fadeOut(function () { + $(this).remove(); + }); + } + + + /** + * Public API + */ + return { + + showInfoMessage: showInfoMessage, + + getObjectIdFromDomId: extractObjectIdFromElementId, + /** + * Renders an error in place of the widget + * + * @param id the RegionWidgetId of the widget to render in error mode + * @param message The message to display to the user + */ + errorWidget: renderErrorWidget, + + /** + * Gets the value of the JavaScriptDebugMode flag + * 0 = off + * 1 = on + */ + getJavaScriptDebugMode: getJavaScriptDebugMode, + + /** + * Sets the value of the JavaScriptDebugMode flag + * 0 = off + * 1 = on + */ + setJavaScriptDebugMode: setJavaScriptDebugMode, + + /** + * View a page + * + * @param pageId the pageId to view, or if null, the user's default page + */ + viewPage: viewPage, + + /** + * View the widget detail page of a widget + * + * @param widgetId to widgetId to view + * @param referringPageId the id of the page the call is coming from + */ + viewWidgetDetail: viewWidgetDetail, + + /** + * Determines if a page is empty (has zero widgets) + * @param widgetByIdMap the map of widgets on the page + */ + isPageEmpty: isPageEmpty, + + + /** + * Returns a language specific message based on the supplied key + * + * @param key the key of the message + */ + getClientMessage: getClientMessage, + + /** + * Adds a message to the internal client message map + * + * @param key + * @param message + */ + addClientMessage: addClientMessage, + + /** + * Returns a boolean indicating if the user + * should be treated as an page editor or not + */ + isPageEditor: isPageEditor + } + }) Modified: rave/trunk/rave-portal-resources/src/main/webapp/static/script/portal/rave_store.js URL: http://svn.apache.org/viewvc/rave/trunk/rave-portal-resources/src/main/webapp/static/script/portal/rave_store.js?rev=1503074&r1=1503073&r2=1503074&view=diff ============================================================================== --- rave/trunk/rave-portal-resources/src/main/webapp/static/script/portal/rave_store.js (original) +++ rave/trunk/rave-portal-resources/src/main/webapp/static/script/portal/rave_store.js Sun Jul 14 22:55:06 2013 @@ -16,13 +16,11 @@ * specific language governing permissions and limitations * under the License. */ -var rave = rave || {}; -rave.store = rave.store || (function() { +define(["jquery", "rave", "portal/rave_portal"], function($, rave, ravePortal){ function initRatings() { - $('.ratingButtons').button(); - //Adjust width's + //Adjust width's $('.widgetRating').each(function(){ var $likeBtn = $(this).find(".widgetLikeButton"), $likeCount = $(this).find(".widgetLikeCount"), @@ -59,7 +57,7 @@ rave.store = rave.store || (function() { }; //update the widget ratings on web page - rave.api.handler.widgetRatingHandler(widgetRating); + widgetRatingHandler(widgetRating); $(this).addClass('btn-success'); $(this).siblings('.btn').removeClass('btn-danger'); @@ -84,7 +82,7 @@ rave.store = rave.store || (function() { }; //update the widget ratings on web page - rave.api.handler.widgetRatingHandler(widgetRating); + widgetRatingHandler(widgetRating); $(this).addClass('btn-danger'); $(this).siblings('.btn').removeClass('btn-success'); @@ -127,7 +125,7 @@ rave.store = rave.store || (function() { text: $("#editComment").get(0).value, successCallback: function() { window.location.reload(); } }); - }).html(rave.getClientMessage("common.update")); + }).html(ravePortal.getClientMessage("common.update")); }); } @@ -137,11 +135,9 @@ rave.store = rave.store || (function() { if($(this).attr("data-target")){ target = $(this).attr("data-target"); - console.log($(this).attr("data-target")); } else{ target = $(this).attr("href"); - console.log("else"); } if($(this).attr('data-toggle-text')){ @@ -217,22 +213,143 @@ rave.store = rave.store || (function() { } function confirmAddFromMarketplace(uri, pType){ - var answer = confirm(rave.getClientMessage("confirm.add.from.marketplace")); + var answer = confirm(ravePortal.getClientMessage("confirm.add.from.marketplace")); if(answer){ rave.api.rpc.addWidgetFromMarketplace({ url: uri, providerType: pType, successCallback: function(widget){ if(widget.result == null){ - alert(rave.getClientMessage("failed.add.from.marketplace")); + alert(ravePortal.getClientMessage("failed.add.from.marketplace")); }else{ - alert("(" + widget.result.title + ") " + rave.getClientMessage("success.add.from.marketplace")); + alert("(" + widget.result.title + ") " + ravePortal.getClientMessage("success.add.from.marketplace")); } } }); } } + function widgetRatingHandler(widgetRating) { + + //retrieving the current total likes + var likeTotalLabel = document.getElementById("totalLikes-" + widgetRating.widgetId); + var likeTotal = likeTotalLabel.getAttribute("data-rave-widget-likes"); + + //retrieving the current total dislikes + var dislikeTotalLabel = document.getElementById("totalDislikes-" + widgetRating.widgetId); + var dislikeTotal = dislikeTotalLabel.getAttribute("data-rave-widget-dislikes"); + + //initializing temporary variables + var incrementingTotal = -1; + var incrementingTotalLabel; + var decrementingTotal = -1; + var decrementingTotalLabel; + var curButton = ""; + var prevButton = ""; + var prevRating = -1; + + //check if like rating needs to be updated + if (widgetRating.isLike) { + + //set incrementing total to like total + incrementingTotal = likeTotal; + + //set the incrementing total label to like total label + incrementingTotalLabel = likeTotalLabel; + + //set decrementing total to dislike total + decrementingTotal = dislikeTotal; + + //set the decrementing total label to dislike total label + decrementingTotalLabel = dislikeTotalLabel; + + //set the current clicked button to like button + curButton = widgetRating.widgetLikeButton; + + //set the previous clicked button to dislike button + prevButton = widgetRating.widgetDislikeButton; + + //set the previous rating to 0 to check if dislike was clicked earlier + prevRating = 0; + } + + //check if dislike rating needs to be updated + else { + + //set incrementing total to dislike total + incrementingTotal = dislikeTotal; + + //set the incrementing total label to dislike total label + incrementingTotalLabel = dislikeTotalLabel; + + //set decrementing total to like total + decrementingTotal = likeTotal; + + //set the decrementing total label to like total label + decrementingTotalLabel = likeTotalLabel; + + //set the current clicked button to dislike button + curButton = widgetRating.widgetDislikeButton; + + //set the previous clicked button to like button + prevButton = widgetRating.widgetLikeButton; + + //set the previous rating to 10 to check if like was clicked earlier + prevRating = 10; + } + + //update incrementing total + incrementingTotal = parseInt(incrementingTotal) + 1; + if (incrementingTotalLabel == likeTotalLabel) { + incrementingTotalLabel.setAttribute("data-rave-widget-likes", incrementingTotal); + incrementingTotalLabel.innerHTML = incrementingTotal; + } + else { + incrementingTotalLabel.setAttribute("data-rave-widget-dislikes", incrementingTotal); + incrementingTotalLabel.innerHTML = incrementingTotal; + } + + //get the value of hidden user rating + var hiddenButton = document.getElementById("rate-" + widgetRating.widgetId); + var userPrevRate = hiddenButton.value; + + //if the other button in this pair was checked then ajdust its total, except in IE where + //the button has already toggled BEFORE the 'change' event in which case we have to assume + //that the user had a contrary selection prior to the change event + if (prevButton.get(0).getAttribute("checked") == "true" || curButton.checked == true) { + prevButton.get(0).setAttribute("checked", "false"); + + //remove the previous rating made by the user if any by checking change in userRating + if (parseInt(userPrevRate) == prevRating) { + + //update decrementing total + if (parseInt(decrementingTotal) - 1 > -1) { + decrementingTotal = parseInt(decrementingTotal) - 1; + if (decrementingTotalLabel == likeTotalLabel) { + decrementingTotalLabel.setAttribute("data-rave-widget-likes", decrementingTotal); + decrementingTotalLabel.innerHTML = decrementingTotal; + } + else { + decrementingTotalLabel.setAttribute("data-rave-widget-dislikes", decrementingTotal); + decrementingTotalLabel.innerHTML = decrementingTotal; + } + } + } + + } + + //flag this element as the currently checked one + curButton.setAttribute("checked", "true"); + + //set the user rating of the hidden field + if (widgetRating.isLike) { + hiddenButton.value = "10"; + } + else { + hiddenButton.value = "0"; + } + } + function init(referringPageId){ initRatings(); initComments(); @@ -246,5 +363,4 @@ rave.store = rave.store || (function() { initTags: initTags, confirmAddFromMarketplace : confirmAddFromMarketplace }; - -}()); \ No newline at end of file +}) \ No newline at end of file
