r91173 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91172‎ | r91173 | r91174 >
Date:14:52, 30 June 2011
Author:ashley
Status:deferred
Tags:
Comment:
SocialProfile: rewrite UserBoard's JS to be more object-oriented. Tested, but not very extensively.
Modified paths:
  • /trunk/extensions/SocialProfile/UserBoard/BoardBlast.js (modified) (history)
  • /trunk/extensions/SocialProfile/UserBoard/SpecialSendBoardBlast.php (modified) (history)
  • /trunk/extensions/SocialProfile/UserBoard/SpecialUserBoard.php (modified) (history)
  • /trunk/extensions/SocialProfile/UserBoard/UserBoard.js (added) (history)

Diff [purge]

Index: trunk/extensions/SocialProfile/UserBoard/UserBoard.js
@@ -0,0 +1,46 @@
 2+var UserBoard = {
 3+ posted: 0,
 4+
 5+ sendMessage: function( perPage ) {
 6+ if( !perPage ) {
 7+ perPage = 25;
 8+ }
 9+ var message = document.getElementById( 'message' ).value;
 10+ var recipient = document.getElementById( 'user_name_to' ).value;
 11+ var sender = document.getElementById( 'user_name_from' ).value;
 12+ if( message && !UserBoard.posted ) {
 13+ UserBoard.posted = 1;
 14+ var encodedName = encodeURIComponent( recipient );
 15+ var encodedMsg = encodeURIComponent( message );
 16+ var messageType = document.getElementById( 'message_type' ).value;
 17+ sajax_request_type = 'POST';
 18+ sajax_do_call(
 19+ 'wfSendBoardMessage',
 20+ [ encodedName, encodedMsg, messageType, perPage ],
 21+ function( request ) {
 22+ UserBoard.posted = 0;
 23+ var user_1, user_2;
 24+ if( sender ) { // it's a board to board
 25+ user_1 = sender;
 26+ user_2 = recipient;
 27+ } else {
 28+ user_1 = recipient;
 29+ user_2 = '';
 30+ }
 31+ var params = ( user_2 ) ? '&conv=' + user_2 : '';
 32+ var url = wgScriptPath + '/index.php?title=Special:UserBoard&user=' + user_1 + params;
 33+ window.location = url;
 34+ }
 35+ );
 36+ }
 37+ },
 38+
 39+ deleteMessage: function( id ) {
 40+ if( confirm( _DELETE_CONFIRM ) ) {
 41+ sajax_request_type = 'POST';
 42+ sajax_do_call( 'wfDeleteBoardMessage', [ id ], function( request ) {
 43+ window.location.reload();
 44+ });
 45+ }
 46+ }
 47+};
Property changes on: trunk/extensions/SocialProfile/UserBoard/UserBoard.js
___________________________________________________________________
Added: svn:eol-style
148 + native
Index: trunk/extensions/SocialProfile/UserBoard/BoardBlast.js
@@ -1,91 +1,109 @@
2 -function toggle_user( user_id ) {
3 - if( jQuery( '#user-' + user_id ).hasClass( 'blast-friend-selected' ) ) {
4 - jQuery( '#user-' + user_id ).removeClass( 'blast-friend-selected' ).addClass( 'blast-friend-unselected' );
5 - } else if( jQuery( '#user-' + user_id ).hasClass( 'blast-friend-unselected' ) ) {
6 - jQuery( '#user-' + user_id ).removeClass( 'blast-friend-unselected' ).addClass( 'blast-friend-selected' );
7 - }
 2+var BoardBlast = {
 3+ submitted: 0,
84
9 - if( jQuery( '#user-' + user_id ).hasClass( 'blast-foe-selected' ) ) {
10 - jQuery( '#user-' + user_id ).removeClass( 'blast-foe-selected' ).addClass( 'blast-foe-unselected' );
11 - } else if( jQuery( '#user-' + user_id ).hasClass( 'blast-foe-unselected' ) ) {
12 - jQuery( '#user-' + user_id ).removeClass( 'blast-foe-unselected' ).addClass( 'blast-foe-selected' );
13 - }
14 -}
 5+ toggleUser: function( user_id ) {
 6+ var elem = jQuery( '#user-' + user_id );
 7+ if( elem.hasClass( 'blast-friend-selected' ) ) {
 8+ elem.removeClass( 'blast-friend-selected' )
 9+ .addClass( 'blast-friend-unselected' );
 10+ } else if( elem.hasClass( 'blast-friend-unselected' ) ) {
 11+ elem.removeClass( 'blast-friend-unselected' )
 12+ .addClass( 'blast-friend-selected' );
 13+ }
1514
16 -function toggle_type( method, on, off ) {
17 - list = jQuery( '#blast-friends-list div.' + ( ( method == 1 ) ? off : on ) );
 15+ if( elem.hasClass( 'blast-foe-selected' ) ) {
 16+ elem.removeClass( 'blast-foe-selected' )
 17+ .addClass( 'blast-foe-unselected' );
 18+ } else if( elem.hasClass( 'blast-foe-unselected' ) ) {
 19+ elem.removeClass( 'blast-foe-unselected' )
 20+ .addClass( 'blast-foe-selected' );
 21+ }
 22+ },
1823
19 - for( x = 0; x <= list.length - 1; x++ ) {
20 - el = list[x];
21 - if( jQuery( el ).hasClass( on ) || jQuery( el ).hasClass( off ) ) {
22 - if( method == 1 ) {
23 - jQuery( el ).removeClass( off ).addClass( on );
24 - } else {
25 - jQuery( el ).removeClass( on ).addClass( off );
 24+ toggleType: function( method, on, off ) {
 25+ var list = jQuery( '#blast-friends-list div.' + ( ( method == 1 ) ? off : on ) );
 26+
 27+ for( var x = 0; x <= list.length - 1; x++ ) {
 28+ var el = list[x];
 29+ if( jQuery( el ).hasClass( on ) || jQuery( el ).hasClass( off ) ) {
 30+ if( method == 1 ) {
 31+ jQuery( el ).removeClass( off ).addClass( on );
 32+ } else {
 33+ jQuery( el ).removeClass( on ).addClass( off );
 34+ }
2635 }
2736 }
28 - }
29 -}
 37+ },
3038
31 -function toggle_friends( method ) {
32 - toggle_type( method, 'blast-friend-selected', 'blast-friend-unselected' );
33 -}
 39+ toggleFriends: function( method ) {
 40+ BoardBlast.toggleType(
 41+ method,
 42+ 'blast-friend-selected',
 43+ 'blast-friend-unselected'
 44+ );
 45+ },
3446
35 -function toggle_foes( method ) {
36 - toggle_type( method, 'blast-foe-selected', 'blast-foe-unselected' );
37 -}
 47+ toggleFoes: function( method ) {
 48+ BoardBlast.toggleType(
 49+ method,
 50+ 'blast-foe-selected',
 51+ 'blast-foe-unselected'
 52+ );
 53+ },
3854
39 -function select_all() {
40 - toggle_friends( 1 );
41 - toggle_foes( 1 );
42 -}
43 -function unselect_all() {
44 - toggle_friends( 0 );
45 - toggle_foes( 0 );
46 -}
 55+ selectAll: function() {
 56+ BoardBlast.toggleFriends( 1 );
 57+ BoardBlast.toggleFoes( 1 );
 58+ },
4759
48 -submitted = 0;
49 -function send_messages() {
50 - if( submitted == 1 ) {
51 - return 0;
52 - }
 60+ unselectAll: function() {
 61+ BoardBlast.toggleFriends( 0 );
 62+ BoardBlast.toggleFoes( 0 );
 63+ },
5364
54 - submitted = 1;
55 - selected = 0;
56 - user_ids_to = '';
 65+ sendMessages: function() {
 66+ if( BoardBlast.submitted == 1 ) {
 67+ return 0;
 68+ }
5769
58 - list = jQuery( '#blast-friends-list div.blast-friend-selected' );
59 - for( x = 0; x <= list.length - 1; x++ ) {
60 - el = list[x];
61 - selected++;
62 - user_id = el.id.replace( 'user-', '' );
63 - user_ids_to += ( ( user_ids_to ) ? ',' : '' ) + user_id;
64 - }
 70+ BoardBlast.submitted = 1;
 71+ var selected = 0;
 72+ var user_ids_to = '';
6573
66 - list = jQuery( '#blast-friends-list div.blast-foe-selected' );
67 - for( x = 0; x <= list.length - 1; x++ ) {
68 - el = list[x];
69 - selected++;
70 - user_id = el.id.replace( 'user-', '' );
71 - user_ids_to += ( ( user_ids_to ) ? ',' : '' ) + user_id;
72 - }
 74+ var list = jQuery( '#blast-friends-list div.blast-friend-selected' );
 75+ var el, user_id;
 76+ for( var x = 0; x <= list.length - 1; x++ ) {
 77+ el = list[x];
 78+ selected++;
 79+ user_id = el.id.replace( 'user-', '' );
 80+ user_ids_to += ( ( user_ids_to ) ? ',' : '' ) + user_id;
 81+ }
7382
74 - if( selected === 0 ) {
75 - alert( 'Please select at least one person' );
76 - submitted = 0;
77 - return 0;
78 - }
 83+ list = jQuery( '#blast-friends-list div.blast-foe-selected' );
 84+ for( x = 0; x <= list.length - 1; x++ ) {
 85+ el = list[x];
 86+ selected++;
 87+ user_id = el.id.replace( 'user-', '' );
 88+ user_ids_to += ( ( user_ids_to ) ? ',' : '' ) + user_id;
 89+ }
7990
80 - if( !document.getElementById( 'message' ).value ) {
81 - alert( 'Please enter a message' );
82 - submitted = 0;
83 - return 0;
84 - }
 91+ if( selected === 0 ) {
 92+ alert( 'Please select at least one person' );
 93+ BoardBlast.submitted = 0;
 94+ return 0;
 95+ }
8596
86 - document.getElementById( 'ids' ).value = user_ids_to;
 97+ if( !document.getElementById( 'message' ).value ) {
 98+ alert( 'Please enter a message' );
 99+ BoardBlast.submitted = 0;
 100+ return 0;
 101+ }
87102
88 - document.blast.message.style.color = '#ccc';
89 - document.blast.message.readOnly = true;
90 - document.getElementById( 'blast-friends-list' ).innerHTML = 'Sending messages...';
91 - document.blast.submit();
92 -}
 103+ document.getElementById( 'ids' ).value = user_ids_to;
 104+
 105+ document.blast.message.style.color = '#ccc';
 106+ document.blast.message.readOnly = true;
 107+ document.getElementById( 'blast-friends-list' ).innerHTML = 'Sending messages...';
 108+ document.blast.submit();
 109+ }
 110+};
Index: trunk/extensions/SocialProfile/UserBoard/SpecialUserBoard.php
@@ -27,16 +27,21 @@
2828 * @param $params Mixed: parameter(s) passed to the page or null
2929 */
3030 public function execute( $params ) {
31 - global $wgUser, $wgOut, $wgRequest, $wgScriptPath, $wgUserBoardScripts;
 31+ global $wgUser, $wgOut, $wgRequest, $wgScriptPath, $wgHooks, $wgUserBoardScripts;
3232
33 - // Add CSS
 33+ // This hooked function adds a global JS variable that UserBoard.js
 34+ // uses to the HTML
 35+ $wgHooks['MakeGlobalVariablesScript'][] = 'SpecialViewUserBoard::addJSGlobals';
 36+
 37+ // Add CSS & JS
3438 $wgOut->addExtensionStyle( $wgUserBoardScripts . '/UserBoard.css' );
 39+ $wgOut->addScriptFile( $wgUserBoardScripts . '/UserBoard.js' );
3540
3641 $ub_messages_show = 25;
3742 $user_name = $wgRequest->getVal( 'user' );
3843 $user_name_2 = $wgRequest->getVal( 'conv' );
3944 $user_id_2 = ''; // Prevent E_NOTICE
40 - $page = $wgRequest->getVal( 'page' );
 45+ $page = $wgRequest->getInt( 'page', 1 );
4146
4247 /**
4348 * Redirect Non-logged in users to Login Page
@@ -76,12 +81,14 @@
7782 * Config for the page
7883 */
7984 $per_page = $ub_messages_show;
80 - if ( !$page || !is_numeric( $page ) ) {
81 - $page = 1;
82 - }
8385
8486 $b = new UserBoard();
85 - $ub_messages = $b->getUserBoardMessages( $user_id, $user_id_2, $ub_messages_show, $page );
 87+ $ub_messages = $b->getUserBoardMessages(
 88+ $user_id,
 89+ $user_id_2,
 90+ $ub_messages_show,
 91+ $page
 92+ );
8693
8794 if ( !$user_id_2 ) {
8895 $stats = new UserStats( $user_id, $user_name );
@@ -90,7 +97,11 @@
9198 // If user is viewing their own board or is allowed to delete
9299 // others' board messages, show the total count of board messages
93100 // to them (public + private messages)
94 - if ( $wgUser->getName() == $user_name || $wgUser->isAllowed( 'userboard-delete' ) ) {
 101+ if (
 102+ $wgUser->getName() == $user_name ||
 103+ $wgUser->isAllowed( 'userboard-delete' )
 104+ )
 105+ {
95106 $total = $total + $stats_data['user_board_priv'];
96107 }
97108 } else {
@@ -113,45 +124,10 @@
114125 }
115126
116127 $output = '<div class="user-board-top-links">';
117 - $output .= '<a href="' . $user->escapeFullURL() . '">&lt; ' . wfMsg( 'userboard_backprofile', $user_name ) . '</a>';
 128+ $output .= '<a href="' . $user->escapeFullURL() . '">&lt; ' .
 129+ wfMsg( 'userboard_backprofile', $user_name ) . '</a>';
118130 $output .= '</div>';
119 - $output .= "<script type=\"text/javascript\">/*<![CDATA[*/
120 - var _DELETE_CONFIRM = \"" . wfMsg( 'userboard_confirmdelete' ) . "\";
121 - var posted = 0;
122 - function send_message() {
123 - if( document.getElementById('message').value && !posted ) {
124 - posted = 1;
125 - encodedName = encodeURIComponent( document.getElementById('user_name_to').value );
126 - encodedMsg = encodeURIComponent( document.getElementById('message').value );
127 - messageType = document.getElementById('message_type').value;
128 - sajax_request_type = 'POST';
129 - sajax_do_call( 'wfSendBoardMessage', [ encodedName, encodedMsg, messageType, {$per_page} ], function( originalRequest ) {
130 - posted = 0;
131 - if( document.getElementById('user_name_from').value ) { // its a board to board
132 - user_1 = document.getElementById('user_name_from').value;
133 - user_2 = document.getElementById('user_name_to').value;
134 - } else {
135 - user_1 = document.getElementById('user_name_to').value;
136 - user_2 = '';
137 - }
138 - var params = ( user_2 ) ? '&conv=' + user_2 : '';
139 - var url = wgScriptPath + '/index.php?title=Special:UserBoard&user=' + user_1 + params;
140 - window.location = url;
141 - }
142 - );
143 - }
144 - }
145131
146 - function delete_message( id ) {
147 - if( confirm( _DELETE_CONFIRM ) ) {
148 - sajax_request_type = 'POST';
149 - sajax_do_call( 'wfDeleteBoardMessage', [ id ], function( originalRequest ) {
150 - window.location.reload();
151 - });
152 - }
153 - }
154 - /*]]>*/</script>";
155 -
156132 $board_to_board = ''; // Prevent E_NOTICE
157133
158134 if ( $page == 1 ) {
@@ -162,7 +138,8 @@
163139 $end = $start + ( count( $ub_messages ) ) - 1;
164140
165141 if ( $wgUser->getName() != $user_name ) {
166 - $board_to_board = '<a href="' . UserBoard::getUserBoardToBoardURL( $wgUser->getName(), $user_name ) . '">' . wfMsg( 'userboard_boardtoboard' ) . '</a>';
 142+ $board_to_board = '<a href="' . UserBoard::getUserBoardToBoardURL( $wgUser->getName(), $user_name ) . '">' .
 143+ wfMsg( 'userboard_boardtoboard' ) . '</a>';
167144 }
168145
169146 if ( $total ) {
@@ -176,6 +153,7 @@
177154 /**
178155 * Build next/prev nav
179156 */
 157+ $qs = '';
180158 if ( $user_id_2 ) {
181159 $qs = "&conv={$user_safe_2}";
182160 }
@@ -229,6 +207,7 @@
230208 $user_name_from = htmlspecialchars( $user_name, ENT_QUOTES );
231209 }
232210 }
 211+
233212 if ( $wgUser->isBlocked() ) {
234213 // only let them post to admins
235214 $user_to = User::newFromId( $user_id );
@@ -252,7 +231,7 @@
253232 <textarea name="message" id="message" cols="63" rows="4"></textarea>
254233
255234 <div class="user-page-message-box-button">
256 - <input type="button" value="' . wfMsg( 'userboard_sendbutton' ) . '" class="site-button" onclick="javascript:send_message();" />
 235+ <input type="button" value="' . wfMsg( 'userboard_sendbutton' ) . '" class="site-button" onclick="javascript:UserBoard.sendMessage(' . $per_page . ');" />
257236 </div>
258237
259238 </div>';
@@ -274,17 +253,31 @@
275254 $board_link = '';
276255 $ub_message_type_label = '';
277256 $delete_link = '';
 257+
278258 if ( $wgUser->getName() != $ub_message['user_name_from'] ) {
279 - $board_to_board = '<a href="' . UserBoard::getUserBoardToBoardURL( $user_name, $ub_message['user_name_from'] ) . '">' . wfMsg( 'userboard_boardtoboard' ) . '</a>';
280 - $board_link = '<a href="' . UserBoard::getUserBoardURL( $ub_message['user_name_from'] ) . '">' . wfMsg( 'userboard_sendmessage', $ub_message['user_name_from'] ) . '</a>';
 259+ $board_to_board = '<a href="' . UserBoard::getUserBoardToBoardURL( $user_name, $ub_message['user_name_from'] ) . '">' .
 260+ wfMsg( 'userboard_boardtoboard' ) . '</a>';
 261+ $board_link = '<a href="' . UserBoard::getUserBoardURL( $ub_message['user_name_from'] ) . '">' .
 262+ wfMsg( 'userboard_sendmessage', $ub_message['user_name_from'] ) . '</a>';
281263 } else {
282 - $board_link = '<a href="' . UserBoard::getUserBoardURL( $ub_message['user_name_from'] ) . '">' . wfMsg( 'userboard_myboard' ) . '</a>';
 264+ $board_link = '<a href="' . UserBoard::getUserBoardURL( $ub_message['user_name_from'] ) . '">' .
 265+ wfMsg( 'userboard_myboard' ) . '</a>';
283266 }
284 - if ( $wgUser->getName() == $ub_message['user_name'] || $wgUser->isAllowed( 'userboard-delete' ) ) {
 267+
 268+ // If the user owns this private message or they are allowed to
 269+ // delete board messages, show the "delete" link to them
 270+ if (
 271+ $wgUser->getName() == $ub_message['user_name'] ||
 272+ $wgUser->isAllowed( 'userboard-delete' )
 273+ )
 274+ {
285275 $delete_link = "<span class=\"user-board-red\">
286 - <a href=\"javascript:void(0);\" onclick=\"javascript:delete_message({$ub_message['id']})\">" . wfMsg( 'userboard_delete' ) . '</a>
 276+ <a href=\"javascript:void(0);\" onclick=\"javascript:UserBoard.deleteMessage({$ub_message['id']})\">" .
 277+ wfMsg( 'userboard_delete' ) . '</a>
287278 </span>';
288279 }
 280+
 281+ // Mark private messages as such
289282 if ( $ub_message['type'] == 1 ) {
290283 $ub_message_type_label = '(' . wfMsg( 'userboard_private' ) . ')';
291284 }
@@ -325,4 +318,17 @@
326319
327320 $wgOut->addHTML( $output );
328321 }
 322+
 323+ /**
 324+ * Add a new JS global variable for UserBoard.js to allow localization.
 325+ * In the future, when we require ResourceLoader, this function can go
 326+ * away. Until that...
 327+ *
 328+ * @param $vars Array: array of pre-existing JS global variables
 329+ * @return Boolean: true
 330+ */
 331+ public static function addJSGlobals( $vars ) {
 332+ $vars['_DELETE_CONFIRM'] = wfMsg( 'userboard_confirmdelete' );
 333+ return true;
 334+ }
329335 }
Index: trunk/extensions/SocialProfile/UserBoard/SpecialSendBoardBlast.php
@@ -93,17 +93,23 @@
9494 <div class="blast-nav">
9595 <h2>' . wfMsg( 'boardblaststep2' ) . '</h2>
9696 <div class="blast-nav-links">
97 - <a href="javascript:void(0);" onclick="javascript:select_all()">' . wfMsg( 'boardlinkselectall' ) . '</a> -
98 - <a href="javascript:void(0);" onclick="javascript:unselect_all()">' . wfMsg( 'boardlinkunselectall' ) . '</a> ';
 97+ <a href="javascript:void(0);" onclick="javascript:BoardBlast.selectAll()">' .
 98+ wfMsg( 'boardlinkselectall' ) . '</a> -
 99+ <a href="javascript:void(0);" onclick="javascript:BoardBlast.unselectAll()">' .
 100+ wfMsg( 'boardlinkunselectall' ) . '</a> ';
99101
100102 if ( $friendCount > 0 && $foeCount > 0 ) {
101 - $output .= '- <a href="javascript:void(0);" onclick="javascript:toggle_friends(1)">' . wfMsg( 'boardlinkselectfriends' ) . '</a> -';
102 - $output .= '<a href="javascript:void(0);" onclick="javascript:toggle_friends(0)">' . wfMsg( 'boardlinkunselectfriends' ) . '</a>';
 103+ $output .= '- <a href="javascript:void(0);" onclick="javascript:BoardBlast.toggleFriends(1)">' .
 104+ wfMsg( 'boardlinkselectfriends' ) . '</a> -';
 105+ $output .= '<a href="javascript:void(0);" onclick="javascript:BoardBlast.toggleFriends(0)">' .
 106+ wfMsg( 'boardlinkunselectfriends' ) . '</a>';
103107 }
104108
105109 if ( $foeCount > 0 && $friendCount > 0 ) {
106 - $output .= '- <a href="javascript:void(0);" onclick="javascript:toggle_foes(1)">' . wfMsg( 'boardlinkselectfoes' ) . '</a> -';
107 - $output .= '<a href="javascript:void(0);" onclick="javascript:toggle_foes(0)">' . wfMsg( 'boardlinkunselectfoes' ) . '</a>';
 110+ $output .= '- <a href="javascript:void(0);" onclick="javascript:BoardBlast.toggleFoes(1)">' .
 111+ wfMsg( 'boardlinkselectfoes' ) . '</a> -';
 112+ $output .= '<a href="javascript:void(0);" onclick="javascript:BoardBlast.toggleFoes(0)">' .
 113+ wfMsg( 'boardlinkunselectfoes' ) . '</a>';
108114 }
109115 $output .= '</div>
110116 </div>';
@@ -123,7 +129,7 @@
124130 $class = 'foe';
125131 }
126132 $id = $relationship['user_id'];
127 - $output .= '<div class="blast-' . $class . "-unselected\" id=\"user-{$id}\" onclick=\"javascript:toggle_user({$id})\">
 133+ $output .= '<div class="blast-' . $class . "-unselected\" id=\"user-{$id}\" onclick=\"javascript:BoardBlast.toggleUser({$id})\">
128134 {$relationship['user_name']}
129135 </div>";
130136 if ( $x == count( $relationships ) || $x != 1 && $x % $per_row == 0 ) {
@@ -140,7 +146,7 @@
141147 <div class="cleared"></div>';
142148
143149 $output .= '<div class="blast-message-box-button">
144 - <input type="button" value="' . wfMsg( 'boardsendbutton' ) . '" class="site-button" onclick="javascript:send_messages();" />
 150+ <input type="button" value="' . wfMsg( 'boardsendbutton' ) . '" class="site-button" onclick="javascript:BoardBlast.sendMessages();" />
145151 </div>';
146152
147153 return $output;

Status & tagging log