1+ // TODO: This keeps around history across "clear history" events. Fix that.
12var switchPlannerMode = true ;
2- var switchPlanner = { } ;
3+ var switchPlannerInfo = { } ;
34 console . log ( "XXX TESTING XXX" ) ;
45
56var all_rules = new RuleSets ( ) ;
@@ -209,14 +210,14 @@ function writeToSwitchPlanner(type, tab_host, resource_host, resource_url, rewri
209210 // TODO: Maybe also count rewritten URLs separately.
210211 if ( rewritten_url != null ) return ;
211212
212- if ( ! switchPlanner [ tab_host ] )
213- switchPlanner [ tab_host ] = { } ;
214- if ( ! switchPlanner [ tab_host ] [ resource_host ] )
215- switchPlanner [ tab_host ] [ resource_host ] = { } ;
216- if ( ! switchPlanner [ tab_host ] [ resource_host ] [ active_content ] )
217- switchPlanner [ tab_host ] [ resource_host ] [ active_content ] = { } ;
213+ if ( ! switchPlannerInfo [ tab_host ] )
214+ switchPlannerInfo [ tab_host ] = { } ;
215+ if ( ! switchPlannerInfo [ tab_host ] [ resource_host ] )
216+ switchPlannerInfo [ tab_host ] [ resource_host ] = { } ;
217+ if ( ! switchPlannerInfo [ tab_host ] [ resource_host ] [ active_content ] )
218+ switchPlannerInfo [ tab_host ] [ resource_host ] [ active_content ] = { } ;
218219
219- switchPlanner [ tab_host ] [ resource_host ] [ active_content ] [ resource_url ] = 1 ;
220+ switchPlannerInfo [ tab_host ] [ resource_host ] [ active_content ] [ resource_url ] = 1 ;
220221}
221222
222223// Return the number of properties in an object. For associative maps, this is
@@ -230,27 +231,36 @@ function objSize(obj) {
230231 return size ;
231232}
232233
233- // Format the switch planner output for presentation to a user.
234+ // Make an array of asset hosts by score so we can sort them,
235+ // presenting the most important ones first.
234236function sortSwitchPlanner ( tab_host ) {
235- var output = "" ;
236-
237237 var asset_host_list = [ ] ;
238- var parentInfo = switchPlanner [ tab_host ] ;
239- // Make an array of asset hosts by score so we can sort them,
240- // presenting the most important ones first.
238+ var parentInfo = switchPlannerInfo [ tab_host ] ;
241239 for ( var asset_host in parentInfo ) {
242240 var ah = parentInfo [ asset_host ] ;
243241 var activeCount = objSize ( ah [ 1 ] ) ;
244242 var passiveCount = objSize ( ah [ 0 ] ) ;
245- asset_host_list . push ( [ activeCount * 100 + passiveCount , activeCount , passiveCount , asset_host ] ) ;
243+ var score = activeCount * 100 + passiveCount ;
244+ asset_host_list . push ( [ score , activeCount , passiveCount , asset_host ] ) ;
246245 }
247246 asset_host_list . sort ( function ( a , b ) { return a [ 0 ] - b [ 0 ] } ) ;
247+ return asset_host_list ;
248+ }
249+
250+ // Format the switch planner output for presentation to a user.
251+ function switchPlannerSmallHtml ( tab_host ) {
252+ var asset_host_list = sortSwitchPlanner ( tab_host ) ;
253+ if ( asset_host_list . length == 0 ) {
254+ return "<b>none</b>" ;
255+ }
256+
257+ var output = "" ;
248258 for ( var i = asset_host_list . length - 1 ; i >= 0 ; i -- ) {
249259 var host = asset_host_list [ i ] [ 3 ] ;
250260 var activeCount = asset_host_list [ i ] [ 1 ] ;
251261 var passiveCount = asset_host_list [ i ] [ 2 ] ;
252262
253- output += host + ": " ;
263+ output += "<b>" + host + "</b> : " ;
254264 if ( activeCount > 0 ) {
255265 output += activeCount + " active" ;
256266 if ( passiveCount > 0 )
@@ -259,7 +269,41 @@ function sortSwitchPlanner(tab_host) {
259269 if ( passiveCount > 0 ) {
260270 output += passiveCount + " passive" ;
261271 }
262- output += "\n" ;
272+ output += "<br/>" ;
273+ }
274+ return output ;
275+ }
276+
277+ function linksFromKeys ( map ) {
278+ if ( typeof map == 'undefined' ) return "" ;
279+ var output = "" ;
280+ for ( var key in map ) {
281+ if ( map . hasOwnProperty ( key ) ) {
282+ output += "<a href='" + key + "'>" + key + "</a><br/>" ;
283+ }
284+ }
285+ return output ;
286+ }
287+
288+ function switchPlannerDetailsHtml ( tab_host ) {
289+ var asset_host_list = sortSwitchPlanner ( tab_host ) ;
290+ var output = "" ;
291+
292+ for ( var i = asset_host_list . length - 1 ; i >= 0 ; i -- ) {
293+ var host = asset_host_list [ i ] [ 3 ] ;
294+ var activeCount = asset_host_list [ i ] [ 1 ] ;
295+ var passiveCount = asset_host_list [ i ] [ 2 ] ;
296+
297+ output += "<b>" + host + "</b>: " ;
298+ if ( activeCount > 0 ) {
299+ output += activeCount + " active<br/>" ;
300+ output += linksFromKeys ( switchPlannerInfo [ tab_host ] [ host ] [ 1 ] ) ;
301+ }
302+ if ( passiveCount > 0 ) {
303+ output += "<br/>" + passiveCount + " passive<br/>" ;
304+ output += linksFromKeys ( switchPlannerInfo [ tab_host ] [ host ] [ 0 ] ) ;
305+ }
306+ output += "<br/>" ;
263307 }
264308 return output ;
265309}
0 commit comments