Index: trunk/extensions/CategoryTree/modules/ext.categoryTree.js |
— | — | @@ -27,7 +27,7 @@ |
28 | 28 | */ |
29 | 29 | handleNode: function( e ) { |
30 | 30 | var $link = $( this ); |
31 | | - if ( $link.data( 'ctState' ) === 'collapsed' ) { |
| 31 | + if ( $link.data( 'ct-state' ) === 'collapsed' ) { |
32 | 32 | categoryTree.expandNode( $link ); |
33 | 33 | } else { |
34 | 34 | categoryTree.collapseNode( $link ); |
— | — | @@ -48,9 +48,9 @@ |
49 | 49 | $link |
50 | 50 | .html( mw.msg( 'categorytree-collapse-bullet' ) ) |
51 | 51 | .attr( 'title', mw.msg( 'categorytree-collapse' ) ) |
52 | | - .data( 'ctState', 'expanded' ); |
| 52 | + .data( 'ct-state', 'expanded' ); |
53 | 53 | |
54 | | - if ( !$link.data( 'ctLoaded' ) ) { |
| 54 | + if ( !$link.data( 'ct-loaded' ) ) { |
55 | 55 | categoryTree.loadChildren( $link, $children ); |
56 | 56 | } |
57 | 57 | }, |
— | — | @@ -68,7 +68,7 @@ |
69 | 69 | $link |
70 | 70 | .html( mw.msg( 'categorytree-expand-bullet' ) ) |
71 | 71 | .attr( 'title', mw.msg( 'categorytree-expand' ) ) |
72 | | - .data( 'ctState', 'collapsed' ); |
| 72 | + .data( 'ct-state', 'collapsed' ); |
73 | 73 | }, |
74 | 74 | |
75 | 75 | /** |
— | — | @@ -78,26 +78,53 @@ |
79 | 79 | * @param {jQuery} $children |
80 | 80 | */ |
81 | 81 | loadChildren: function( $link, $children ) { |
82 | | - $link.data( 'ctLoaded', true ); |
| 82 | + var $linkParentCTTag, ctTitle, ctMode, ctOptions; |
| 83 | + |
| 84 | + /** |
| 85 | + * Error callback |
| 86 | + */ |
| 87 | + function error() { |
| 88 | + var $retryLink; |
| 89 | + |
| 90 | + $retryLink = $( '<a>' ) |
| 91 | + .text( mw.msg( 'categorytree-retry' ) ) |
| 92 | + .attr( 'href', '#' ) |
| 93 | + .click( function() { |
| 94 | + categoryTree.loadChildren( $link, $children ); |
| 95 | + } ); |
| 96 | + |
| 97 | + $children |
| 98 | + .text( mw.msg( 'categorytree-error' ) ) |
| 99 | + .append( $retryLink ); |
| 100 | + } |
| 101 | + |
| 102 | + $link.data( 'ct-loaded', true ); |
| 103 | + |
83 | 104 | $children.html( |
84 | 105 | $( '<i class="CategoryTreeNotice"></i>' ) |
85 | 106 | .text( mw.msg( 'categorytree-loading' ) ) |
86 | 107 | ); |
87 | 108 | |
88 | | - var $parentTag = $link.parents( '.CategoryTreeTag' ); |
| 109 | + $linkParentCTTag = $link.parents( '.CategoryTreeTag' ); |
89 | 110 | |
90 | | - if ( $parentTag.length === 0 ) { |
91 | | - // Probably a CategoryPage |
92 | | - $parentTag = $( '<div />' ) |
93 | | - .hide() |
94 | | - .data( 'ctOptions', mw.config.get( 'wgCategoryTreePageCategoryOptions' ) ); |
| 111 | + // Element may not have a .CategoryTreeTag parent, fallback to defauls |
| 112 | + // Probably a CategoryPage (@todo: based on what?) |
| 113 | + ctTitle = $link.data( 'ct-title' ); |
| 114 | + ctMode = $linkParentCTTag.data( 'ct-mode' ) || undefined; |
| 115 | + ctOptions = $linkParentCTTag.data( 'ct-options' ) || mw.config.get( 'wgCategoryTreePageCategoryOptions' ); |
| 116 | + |
| 117 | + // Mode and options have defaults or fallbacks, title does not. |
| 118 | + // Don't make a request if there is no title. |
| 119 | + if ( typeof ctTitle !== 'string' ) { |
| 120 | + error(); |
| 121 | + return; |
95 | 122 | } |
96 | 123 | |
97 | 124 | $.get( |
98 | 125 | mw.util.wikiScript(), { |
99 | 126 | action: 'ajax', |
100 | 127 | rs: 'efCategoryTreeAjaxWrapper', |
101 | | - rsargs: [$link.data( 'ctTitle' ), $parentTag.data( 'ctOptions' ), 'json'] // becomes &rsargs[]=arg1&rsargs[]=arg2... |
| 128 | + rsargs: [ctTitle, ctOptions, 'json'] // becomes &rsargs[]=arg1&rsargs[]=arg2... |
102 | 129 | } |
103 | 130 | ) |
104 | 131 | .success( function ( data ) { |
— | — | @@ -105,7 +132,7 @@ |
106 | 133 | data = data.replace(/##LOAD##/g, mw.msg( 'categorytree-expand' ) ); |
107 | 134 | |
108 | 135 | if ( data === '' ) { |
109 | | - switch ( $parentTag.data( 'ctMode' ) ) { |
| 136 | + switch ( ctMode ) { |
110 | 137 | case 0: |
111 | 138 | data = mw.msg( 'categorytree-no-subcategories' ); |
112 | 139 | break; |
— | — | @@ -128,15 +155,7 @@ |
129 | 156 | .click( categoryTree.handleNode ); |
130 | 157 | categoryTree.showToggles(); |
131 | 158 | } ) |
132 | | - .error( function() { |
133 | | - var $retryLink = $( '<a />' ) |
134 | | - .text( mw.msg( 'categorytree-retry' ) ) |
135 | | - .attr( 'href', '#' ) |
136 | | - .click( function() { categoryTree.loadChildren( $link, $children ); } ); |
137 | | - $children |
138 | | - .text( mw.msg( 'categorytree-error' ) ) |
139 | | - .append( $retryLink ); |
140 | | - } ); |
| 159 | + .error( error ); |
141 | 160 | } |
142 | 161 | }; |
143 | 162 | |