http://www.mediawiki.org/wiki/Special:Code/MediaWiki/91173

Revision: 91173
Author:   ashley
Date:     2011-06-30 14:52:47 +0000 (Thu, 30 Jun 2011)
Log Message:
-----------
SocialProfile: rewrite UserBoard's JS to be more object-oriented. Tested, but 
not very extensively.

Modified Paths:
--------------
    trunk/extensions/SocialProfile/UserBoard/BoardBlast.js
    trunk/extensions/SocialProfile/UserBoard/SpecialSendBoardBlast.php
    trunk/extensions/SocialProfile/UserBoard/SpecialUserBoard.php

Added Paths:
-----------
    trunk/extensions/SocialProfile/UserBoard/UserBoard.js

Modified: trunk/extensions/SocialProfile/UserBoard/BoardBlast.js
===================================================================
--- trunk/extensions/SocialProfile/UserBoard/BoardBlast.js      2011-06-30 
14:49:12 UTC (rev 91172)
+++ trunk/extensions/SocialProfile/UserBoard/BoardBlast.js      2011-06-30 
14:52:47 UTC (rev 91173)
@@ -1,91 +1,109 @@
-function toggle_user( user_id ) {
-       if( jQuery( '#user-' + user_id ).hasClass( 'blast-friend-selected' ) ) {
-               jQuery( '#user-' + user_id ).removeClass( 
'blast-friend-selected' ).addClass( 'blast-friend-unselected' );
-       } else if( jQuery( '#user-' + user_id ).hasClass( 
'blast-friend-unselected' ) ) {
-               jQuery( '#user-' + user_id ).removeClass( 
'blast-friend-unselected' ).addClass( 'blast-friend-selected' );
-       }
+var BoardBlast = {
+       submitted: 0,
 
-       if( jQuery( '#user-' + user_id ).hasClass( 'blast-foe-selected' ) ) {
-               jQuery( '#user-' + user_id ).removeClass( 'blast-foe-selected' 
).addClass( 'blast-foe-unselected' );
-       } else if( jQuery( '#user-' + user_id ).hasClass( 
'blast-foe-unselected' ) ) {
-               jQuery( '#user-' + user_id ).removeClass( 
'blast-foe-unselected' ).addClass( 'blast-foe-selected' );
-       }
-}
+       toggleUser: function( user_id ) {
+               var elem = jQuery( '#user-' + user_id );
+               if( elem.hasClass( 'blast-friend-selected' ) ) {
+                       elem.removeClass( 'blast-friend-selected' )
+                               .addClass( 'blast-friend-unselected' );
+               } else if( elem.hasClass( 'blast-friend-unselected' ) ) {
+                       elem.removeClass( 'blast-friend-unselected' )
+                               .addClass( 'blast-friend-selected' );
+               }
 
-function toggle_type( method, on, off ) {
-       list = jQuery( '#blast-friends-list div.' + ( ( method == 1 ) ? off : 
on ) );
+               if( elem.hasClass( 'blast-foe-selected' ) ) {
+                       elem.removeClass( 'blast-foe-selected' )
+                               .addClass( 'blast-foe-unselected' );
+               } else if( elem.hasClass( 'blast-foe-unselected' ) ) {
+                       elem.removeClass( 'blast-foe-unselected' )
+                               .addClass( 'blast-foe-selected' );
+               }
+       },
 
-       for( x = 0; x <= list.length - 1; x++ ) {
-               el = list[x];
-               if( jQuery( el ).hasClass( on ) || jQuery( el ).hasClass( off ) 
) {
-                       if( method == 1 ) {
-                               jQuery( el ).removeClass( off ).addClass( on );
-                       } else {
-                               jQuery( el ).removeClass( on ).addClass( off );
+       toggleType: function( method, on, off ) {
+               var list = jQuery( '#blast-friends-list div.' + ( ( method == 1 
) ? off : on ) );
+
+               for( var x = 0; x <= list.length - 1; x++ ) {
+                       var el = list[x];
+                       if( jQuery( el ).hasClass( on ) || jQuery( el 
).hasClass( off ) ) {
+                               if( method == 1 ) {
+                                       jQuery( el ).removeClass( off 
).addClass( on );
+                               } else {
+                                       jQuery( el ).removeClass( on 
).addClass( off );
+                               }
                        }
                }
-       }
-}
+       },
 
-function toggle_friends( method ) {
-       toggle_type( method, 'blast-friend-selected', 'blast-friend-unselected' 
);
-}
+       toggleFriends: function( method ) {
+               BoardBlast.toggleType(
+                       method,
+                       'blast-friend-selected',
+                       'blast-friend-unselected'
+               );
+       },
 
-function toggle_foes( method ) {
-       toggle_type( method, 'blast-foe-selected', 'blast-foe-unselected' );
-}
+       toggleFoes: function( method ) {
+               BoardBlast.toggleType(
+                       method,
+                       'blast-foe-selected',
+                       'blast-foe-unselected'
+               );
+       },
 
-function select_all() {
-       toggle_friends( 1 );
-       toggle_foes( 1 );
-}
-function unselect_all() {
-       toggle_friends( 0 );
-       toggle_foes( 0 );
-}
+       selectAll: function() {
+               BoardBlast.toggleFriends( 1 );
+               BoardBlast.toggleFoes( 1 );
+       },
 
-submitted = 0;
-function send_messages() {
-       if( submitted == 1 ) {
-               return 0;
-       }
+       unselectAll: function() {
+               BoardBlast.toggleFriends( 0 );
+               BoardBlast.toggleFoes( 0 );
+       },
 
-       submitted = 1;
-       selected = 0;
-       user_ids_to = '';
+       sendMessages: function() {
+               if( BoardBlast.submitted == 1 ) {
+                       return 0;
+               }
 
-       list = jQuery( '#blast-friends-list div.blast-friend-selected' );
-       for( x = 0; x <= list.length - 1; x++ ) {
-               el = list[x];
-               selected++;
-               user_id = el.id.replace( 'user-', '' );
-               user_ids_to += ( ( user_ids_to ) ? ',' : '' ) + user_id;
-       }
+               BoardBlast.submitted = 1;
+               var selected = 0;
+               var user_ids_to = '';
 
-       list = jQuery( '#blast-friends-list div.blast-foe-selected' );
-       for( x = 0; x <= list.length - 1; x++ ) {
-               el = list[x];
-               selected++;
-               user_id = el.id.replace( 'user-', '' );
-               user_ids_to += ( ( user_ids_to ) ? ',' : '' ) + user_id;
-       }
+               var list = jQuery( '#blast-friends-list 
div.blast-friend-selected' );
+               var el, user_id;
+               for( var x = 0; x <= list.length - 1; x++ ) {
+                       el = list[x];
+                       selected++;
+                       user_id = el.id.replace( 'user-', '' );
+                       user_ids_to += ( ( user_ids_to ) ? ',' : '' ) + user_id;
+               }
 
-       if( selected === 0 ) {
-               alert( 'Please select at least one person' );
-               submitted = 0;
-               return 0;
-       }
+               list = jQuery( '#blast-friends-list div.blast-foe-selected' );
+               for( x = 0; x <= list.length - 1; x++ ) {
+                       el = list[x];
+                       selected++;
+                       user_id = el.id.replace( 'user-', '' );
+                       user_ids_to += ( ( user_ids_to ) ? ',' : '' ) + user_id;
+               }
 
-       if( !document.getElementById( 'message' ).value ) {
-               alert( 'Please enter a message' );
-               submitted = 0;
-               return 0;
-       }
+               if( selected === 0 ) {
+                       alert( 'Please select at least one person' );
+                       BoardBlast.submitted = 0;
+                       return 0;
+               }
 
-       document.getElementById( 'ids' ).value = user_ids_to;
+               if( !document.getElementById( 'message' ).value ) {
+                       alert( 'Please enter a message' );
+                       BoardBlast.submitted = 0;
+                       return 0;
+               }
 
-       document.blast.message.style.color = '#ccc';
-       document.blast.message.readOnly = true;
-       document.getElementById( 'blast-friends-list' ).innerHTML = 'Sending 
messages...';
-       document.blast.submit();
-}
+               document.getElementById( 'ids' ).value = user_ids_to;
+
+               document.blast.message.style.color = '#ccc';
+               document.blast.message.readOnly = true;
+               document.getElementById( 'blast-friends-list' ).innerHTML = 
'Sending messages...';
+               document.blast.submit();
+       }
+};

Modified: trunk/extensions/SocialProfile/UserBoard/SpecialSendBoardBlast.php
===================================================================
--- trunk/extensions/SocialProfile/UserBoard/SpecialSendBoardBlast.php  
2011-06-30 14:49:12 UTC (rev 91172)
+++ trunk/extensions/SocialProfile/UserBoard/SpecialSendBoardBlast.php  
2011-06-30 14:52:47 UTC (rev 91173)
@@ -93,17 +93,23 @@
                <div class="blast-nav">
                                <h2>' . wfMsg( 'boardblaststep2' ) . '</h2>
                                <div class="blast-nav-links">
-                                       <a href="javascript:void(0);" 
onclick="javascript:select_all()">' . wfMsg( 'boardlinkselectall' ) . '</a> -
-                                       <a href="javascript:void(0);" 
onclick="javascript:unselect_all()">' . wfMsg( 'boardlinkunselectall' ) . '</a> 
';
+                                       <a href="javascript:void(0);" 
onclick="javascript:BoardBlast.selectAll()">' .
+                                               wfMsg( 'boardlinkselectall' ) . 
'</a> -
+                                       <a href="javascript:void(0);" 
onclick="javascript:BoardBlast.unselectAll()">' .
+                                               wfMsg( 'boardlinkunselectall' ) 
. '</a> ';
 
                if ( $friendCount > 0 && $foeCount > 0 ) {
-                       $output .= '- <a href="javascript:void(0);" 
onclick="javascript:toggle_friends(1)">' . wfMsg( 'boardlinkselectfriends' ) . 
'</a> -';
-                       $output .= '<a href="javascript:void(0);" 
onclick="javascript:toggle_friends(0)">' . wfMsg( 'boardlinkunselectfriends' ) 
. '</a>';
+                       $output .= '- <a href="javascript:void(0);" 
onclick="javascript:BoardBlast.toggleFriends(1)">' .
+                               wfMsg( 'boardlinkselectfriends' ) . '</a> -';
+                       $output .= '<a href="javascript:void(0);" 
onclick="javascript:BoardBlast.toggleFriends(0)">' .
+                               wfMsg( 'boardlinkunselectfriends' ) . '</a>';
                }
 
                if ( $foeCount > 0 && $friendCount > 0 ) {
-                       $output .= '- <a href="javascript:void(0);" 
onclick="javascript:toggle_foes(1)">' . wfMsg( 'boardlinkselectfoes' ) . '</a> 
-';
-                       $output .= '<a href="javascript:void(0);" 
onclick="javascript:toggle_foes(0)">' . wfMsg( 'boardlinkunselectfoes' ) . 
'</a>';
+                       $output .= '- <a href="javascript:void(0);" 
onclick="javascript:BoardBlast.toggleFoes(1)">' .
+                               wfMsg( 'boardlinkselectfoes' ) . '</a> -';
+                       $output .= '<a href="javascript:void(0);" 
onclick="javascript:BoardBlast.toggleFoes(0)">' .
+                               wfMsg( 'boardlinkunselectfoes' ) . '</a>';
                }
                $output .= '</div>
                </div>';
@@ -123,7 +129,7 @@
                                        $class = 'foe';
                                }
                                $id = $relationship['user_id'];
-                               $output .= '<div class="blast-' . $class . 
"-unselected\" id=\"user-{$id}\" onclick=\"javascript:toggle_user({$id})\">
+                               $output .= '<div class="blast-' . $class . 
"-unselected\" id=\"user-{$id}\" 
onclick=\"javascript:BoardBlast.toggleUser({$id})\">
                                                {$relationship['user_name']}
                                        </div>";
                                if ( $x == count( $relationships ) || $x != 1 
&& $x % $per_row == 0 ) {
@@ -140,7 +146,7 @@
                        <div class="cleared"></div>';
 
                $output .= '<div class="blast-message-box-button">
-                       <input type="button" value="' . wfMsg( 
'boardsendbutton' ) . '" class="site-button" 
onclick="javascript:send_messages();" />
+                       <input type="button" value="' . wfMsg( 
'boardsendbutton' ) . '" class="site-button" 
onclick="javascript:BoardBlast.sendMessages();" />
                </div>';
 
                return $output;

Modified: trunk/extensions/SocialProfile/UserBoard/SpecialUserBoard.php
===================================================================
--- trunk/extensions/SocialProfile/UserBoard/SpecialUserBoard.php       
2011-06-30 14:49:12 UTC (rev 91172)
+++ trunk/extensions/SocialProfile/UserBoard/SpecialUserBoard.php       
2011-06-30 14:52:47 UTC (rev 91173)
@@ -27,16 +27,21 @@
         * @param $params Mixed: parameter(s) passed to the page or null
         */
        public function execute( $params ) {
-               global $wgUser, $wgOut, $wgRequest, $wgScriptPath, 
$wgUserBoardScripts;
+               global $wgUser, $wgOut, $wgRequest, $wgScriptPath, $wgHooks, 
$wgUserBoardScripts;
 
-               // Add CSS
+               // This hooked function adds a global JS variable that 
UserBoard.js
+               // uses to the HTML
+               $wgHooks['MakeGlobalVariablesScript'][] = 
'SpecialViewUserBoard::addJSGlobals';
+
+               // Add CSS & JS
                $wgOut->addExtensionStyle( $wgUserBoardScripts . 
'/UserBoard.css' );
+               $wgOut->addScriptFile( $wgUserBoardScripts . '/UserBoard.js' );
 
                $ub_messages_show = 25;
                $user_name = $wgRequest->getVal( 'user' );
                $user_name_2 = $wgRequest->getVal( 'conv' );
                $user_id_2 = ''; // Prevent E_NOTICE
-               $page = $wgRequest->getVal( 'page' );
+               $page = $wgRequest->getInt( 'page', 1 );
 
                /**
                 * Redirect Non-logged in users to Login Page
@@ -76,12 +81,14 @@
                 * Config for the page
                 */
                $per_page = $ub_messages_show;
-               if ( !$page || !is_numeric( $page ) ) {
-                       $page = 1;
-               }
 
                $b = new UserBoard();
-               $ub_messages = $b->getUserBoardMessages( $user_id, $user_id_2, 
$ub_messages_show, $page );
+               $ub_messages = $b->getUserBoardMessages(
+                       $user_id,
+                       $user_id_2,
+                       $ub_messages_show,
+                       $page
+               );
 
                if ( !$user_id_2 ) {
                        $stats = new UserStats( $user_id, $user_name );
@@ -90,7 +97,11 @@
                        // If user is viewing their own board or is allowed to 
delete
                        // others' board messages, show the total count of 
board messages
                        // to them (public + private messages)
-                       if ( $wgUser->getName() == $user_name || 
$wgUser->isAllowed( 'userboard-delete' ) ) {
+                       if (
+                               $wgUser->getName() == $user_name ||
+                               $wgUser->isAllowed( 'userboard-delete' )
+                       )
+                       {
                                $total = $total + 
$stats_data['user_board_priv'];
                        }
                } else {
@@ -113,45 +124,10 @@
                }
 
                $output = '<div class="user-board-top-links">';
-               $output .= '<a href="' . $user->escapeFullURL() . '">&lt; ' . 
wfMsg( 'userboard_backprofile', $user_name ) . '</a>';
+               $output .= '<a href="' . $user->escapeFullURL() . '">&lt; ' .
+                       wfMsg( 'userboard_backprofile', $user_name ) . '</a>';
                $output .= '</div>';
-               $output .= "<script type=\"text/javascript\">/*<![CDATA[*/
-                       var _DELETE_CONFIRM = \"" . wfMsg( 
'userboard_confirmdelete' ) . "\";
-                       var posted = 0;
-                       function send_message() {
-                               if( document.getElementById('message').value && 
!posted ) {
-                                       posted = 1;
-                                       encodedName = encodeURIComponent( 
document.getElementById('user_name_to').value );
-                                       encodedMsg = encodeURIComponent( 
document.getElementById('message').value );
-                                       messageType = 
document.getElementById('message_type').value;
-                                       sajax_request_type = 'POST';
-                                       sajax_do_call( 'wfSendBoardMessage', [ 
encodedName, encodedMsg, messageType, {$per_page} ], function( originalRequest 
) {
-                                                       posted = 0;
-                                                       if( 
document.getElementById('user_name_from').value ) { // its a board to board
-                                                               user_1 = 
document.getElementById('user_name_from').value;
-                                                               user_2 = 
document.getElementById('user_name_to').value;
-                                                       } else {
-                                                               user_1 = 
document.getElementById('user_name_to').value;
-                                                               user_2 = '';
-                                                       }
-                                                       var params = ( user_2 ) 
? '&conv=' + user_2 : '';
-                                                       var url = wgScriptPath 
+ '/index.php?title=Special:UserBoard&user=' + user_1 + params;
-                                                       window.location = url;
-                                               }
-                                       );
-                               }
-                       }
 
-                       function delete_message( id ) {
-                               if( confirm( _DELETE_CONFIRM ) ) {
-                                       sajax_request_type = 'POST';
-                                       sajax_do_call( 'wfDeleteBoardMessage', 
[ id ], function( originalRequest ) {
-                                               window.location.reload();
-                                       });
-                               }
-                       }
-               /*]]>*/</script>";
-
                $board_to_board = ''; // Prevent E_NOTICE
 
                if ( $page == 1 ) {
@@ -162,7 +138,8 @@
                $end = $start + ( count( $ub_messages ) ) - 1;
 
                if ( $wgUser->getName() != $user_name ) {
-                       $board_to_board = '<a href="' . 
UserBoard::getUserBoardToBoardURL( $wgUser->getName(), $user_name ) . '">' . 
wfMsg( 'userboard_boardtoboard' ) . '</a>';
+                       $board_to_board = '<a href="' . 
UserBoard::getUserBoardToBoardURL( $wgUser->getName(), $user_name ) . '">' .
+                               wfMsg( 'userboard_boardtoboard' ) . '</a>';
                }
 
                if ( $total ) {
@@ -176,6 +153,7 @@
                /**
                 * Build next/prev nav
                 */
+               $qs = '';
                if ( $user_id_2 ) {
                        $qs = "&conv={$user_safe_2}";
                }
@@ -229,6 +207,7 @@
                                $user_name_from = htmlspecialchars( $user_name, 
ENT_QUOTES );
                        }
                }
+
                if ( $wgUser->isBlocked() ) {
                        // only let them post to admins
                        $user_to = User::newFromId( $user_id );
@@ -252,7 +231,7 @@
                                        <textarea name="message" id="message" 
cols="63" rows="4"></textarea>
 
                                        <div 
class="user-page-message-box-button">
-                                               <input type="button" value="' . 
wfMsg( 'userboard_sendbutton' ) . '" class="site-button" 
onclick="javascript:send_message();" />
+                                               <input type="button" value="' . 
wfMsg( 'userboard_sendbutton' ) . '" class="site-button" 
onclick="javascript:UserBoard.sendMessage(' . $per_page . ');" />
                                        </div>
 
                                </div>';
@@ -274,17 +253,31 @@
                                $board_link = '';
                                $ub_message_type_label = '';
                                $delete_link = '';
+
                                if ( $wgUser->getName() != 
$ub_message['user_name_from'] ) {
-                                       $board_to_board = '<a href="' . 
UserBoard::getUserBoardToBoardURL( $user_name, $ub_message['user_name_from'] ) 
. '">' . wfMsg( 'userboard_boardtoboard' ) . '</a>';
-                                       $board_link = '<a href="' . 
UserBoard::getUserBoardURL( $ub_message['user_name_from'] ) . '">' . wfMsg( 
'userboard_sendmessage', $ub_message['user_name_from'] ) . '</a>';
+                                       $board_to_board = '<a href="' . 
UserBoard::getUserBoardToBoardURL( $user_name, $ub_message['user_name_from'] ) 
. '">' .
+                                               wfMsg( 'userboard_boardtoboard' 
) . '</a>';
+                                       $board_link = '<a href="' . 
UserBoard::getUserBoardURL( $ub_message['user_name_from'] ) . '">' .
+                                               wfMsg( 'userboard_sendmessage', 
$ub_message['user_name_from'] ) . '</a>';
                                } else {
-                                       $board_link = '<a href="' . 
UserBoard::getUserBoardURL( $ub_message['user_name_from'] ) . '">' . wfMsg( 
'userboard_myboard' ) . '</a>';
+                                       $board_link = '<a href="' . 
UserBoard::getUserBoardURL( $ub_message['user_name_from'] ) . '">' .
+                                               wfMsg( 'userboard_myboard' ) . 
'</a>';
                                }
-                               if ( $wgUser->getName() == 
$ub_message['user_name'] || $wgUser->isAllowed( 'userboard-delete' ) ) {
+
+                               // If the user owns this private message or 
they are allowed to
+                               // delete board messages, show the "delete" 
link to them
+                               if (
+                                       $wgUser->getName() == 
$ub_message['user_name'] ||
+                                       $wgUser->isAllowed( 'userboard-delete' )
+                               )
+                               {
                                        $delete_link = "<span 
class=\"user-board-red\">
-                                               <a href=\"javascript:void(0);\" 
onclick=\"javascript:delete_message({$ub_message['id']})\">" . wfMsg( 
'userboard_delete' ) . '</a>
+                                               <a href=\"javascript:void(0);\" 
onclick=\"javascript:UserBoard.deleteMessage({$ub_message['id']})\">" .
+                                                       wfMsg( 
'userboard_delete' ) . '</a>
                                        </span>';
                                }
+
+                               // Mark private messages as such
                                if ( $ub_message['type'] == 1 ) {
                                        $ub_message_type_label = '(' . wfMsg( 
'userboard_private' ) . ')';
                                }
@@ -325,4 +318,17 @@
 
                $wgOut->addHTML( $output );
        }
+
+       /**
+        * Add a new JS global variable for UserBoard.js to allow localization.
+        * In the future, when we require ResourceLoader, this function can go
+        * away. Until that...
+        *
+        * @param $vars Array: array of pre-existing JS global variables
+        * @return Boolean: true
+        */
+       public static function addJSGlobals( $vars ) {
+               $vars['_DELETE_CONFIRM'] = wfMsg( 'userboard_confirmdelete' );
+               return true;
+       }
 }

Added: trunk/extensions/SocialProfile/UserBoard/UserBoard.js
===================================================================
--- trunk/extensions/SocialProfile/UserBoard/UserBoard.js                       
        (rev 0)
+++ trunk/extensions/SocialProfile/UserBoard/UserBoard.js       2011-06-30 
14:52:47 UTC (rev 91173)
@@ -0,0 +1,46 @@
+var UserBoard = {
+       posted: 0,
+
+       sendMessage: function( perPage ) {
+               if( !perPage ) {
+                       perPage = 25;
+               }
+               var message = document.getElementById( 'message' ).value;
+               var recipient = document.getElementById( 'user_name_to' ).value;
+               var sender = document.getElementById( 'user_name_from' ).value;
+               if( message && !UserBoard.posted ) {
+                       UserBoard.posted = 1;
+                       var encodedName = encodeURIComponent( recipient );
+                       var encodedMsg = encodeURIComponent( message );
+                       var messageType = document.getElementById( 
'message_type' ).value;
+                       sajax_request_type = 'POST';
+                       sajax_do_call(
+                               'wfSendBoardMessage',
+                               [ encodedName, encodedMsg, messageType, perPage 
],
+                               function( request ) {
+                                       UserBoard.posted = 0;
+                                       var user_1, user_2;
+                                       if( sender ) { // it's a board to board
+                                               user_1 = sender;
+                                               user_2 = recipient;
+                                       } else {
+                                               user_1 = recipient;
+                                               user_2 = '';
+                                       }
+                                       var params = ( user_2 ) ? '&conv=' + 
user_2 : '';
+                                       var url = wgScriptPath + 
'/index.php?title=Special:UserBoard&user=' + user_1 + params;
+                                       window.location = url;
+                               }
+                       );
+               }
+       },
+
+       deleteMessage: function( id ) {
+               if( confirm( _DELETE_CONFIRM ) ) {
+                       sajax_request_type = 'POST';
+                       sajax_do_call( 'wfDeleteBoardMessage', [ id ], 
function( request ) {
+                               window.location.reload();
+                       });
+               }
+       }
+};


Property changes on: trunk/extensions/SocialProfile/UserBoard/UserBoard.js
___________________________________________________________________
Added: svn:eol-style
   + native


_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to