Debian Codegraph @

extensions/CategoryTree/modules/ext.categoryTree/ext.categoryTree.js

raw symbols SCIP
1 /**
2 * JavaScript for the CategoryTree extension.
3 *
4 * © 2006 Daniel Kinzler
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @author Daniel Kinzler, brightbyte.de
23 */
24
25 const config = require( './data.json' );
26
27 /**
28 * Expands a given node (loading it's children if not loaded)
29 *
30 * @param {jQuery} $link
31 */
32 function expandNode( $link ) {
33 // Show the children node
34 const $children = $link.parents( '.CategoryTreeItem' )
35 .siblings( '.CategoryTreeChildren' )
36 .css( 'display', '' );
37
38 $link.attr( {
39 title: mw.msg( 'categorytree-collapse' ),
40 'aria-expanded': 'true'
41 } );
42
43 if ( !$link.data( 'ct-loaded' ) ) {
44 loadChildren( $link, $children );
45 }
46 }
47
48 /**
49 * Collapses a node
50 *
51 * @param {jQuery} $link
52 */
53 function collapseNode( $link ) {
54 // Hide the children node
55 $link.parents( '.CategoryTreeItem' )
56 .siblings( '.CategoryTreeChildren' )
57 .css( 'display', 'none' );
58
59 $link.attr( {
60 title: mw.msg( 'categorytree-expand' ),
61 'aria-expanded': 'false'
62 } );
63 }
64
65 /**
66 * Handles clicks on the expand buttons, and calls the appropriate function
67 *
68 * @param {jQuery.Event} e
69 */
70 function handleNode( e ) {
71 e.preventDefault();
72
73 const $link = $( this );
74 if ( $link.attr( 'aria-expanded' ) === 'false' ) {
75 expandNode( $link );
76 } else {
77 collapseNode( $link );
78 }
79 }
80
81 /**
82 * Attach click handler to buttons
83 *
84 * @param {jQuery} $content
85 */
86 function attachHandler( $content ) {
87 $content.find( '.CategoryTreeToggle' )
88 .on( 'click', handleNode )
89 .attr( {
90 role: 'button',
91 title: function () {
92 return mw.msg(
93 $( this ).attr( 'aria-expanded' ) === 'false' ?
94 'categorytree-expand' :
95 'categorytree-collapse'
96 );
97 }
98 } )
99 .addClass( 'CategoryTreeToggleHandlerAttached' );
100 }
101
102 /**
103 * Loads children for a node via an HTTP call
104 *
105 * @param {jQuery} $link
106 * @param {jQuery} $children
107 */
108 function loadChildren( $link, $children ) {
109 /**
110 * Error callback
111 */
112 function error() {
113 const $retryLink = $( '<a>' )
114 .text( mw.msg( 'categorytree-retry' ) )
115 .attr( {
116 role: 'button',
117 tabindex: 0
118 } )
119 .on( 'click keypress', ( e ) => {
120 if (
121 e.type === 'click' ||
122 e.type === 'keypress' && e.which === 13
123 ) {
124 loadChildren( $link, $children );
125 }
126 } );
127
128 $children
129 .text( mw.msg( 'categorytree-error' ) + ' ' )
130 .append( $retryLink );
131 }
132
133 $link.data( 'ct-loaded', true );
134
135 $children.empty().append(
136 $( '<i>' )
137 .addClass( 'CategoryTreeNotice' )
138 .text( mw.msg( 'categorytree-loading' ) )
139 );
140
141 const $linkParentCTTag = $link.parents( '.CategoryTreeTag' );
142
143 // Element may not have a .CategoryTreeTag parent, fallback to defauls
144 // Probably a CategoryPage (@todo: based on what?)
145 const ctTitle = $link.attr( 'data-ct-title' );
146 const ctMode = $linkParentCTTag.data( 'ct-mode' );
147 const mode = typeof ctMode === 'number' ? ctMode : undefined;
148 const ctOptions = $linkParentCTTag.attr( 'data-ct-options' ) || config.defaultCtOptions;
149
150 // Mode and options have defaults or fallbacks, title does not.
151 // Don't make a request if there is no title.
152 if ( !ctTitle ) {
153 error();
154 return;
155 }
156
157 new mw.Api().get( {
158 action: 'categorytree',
159 category: ctTitle,
160 options: ctOptions,
161 uselang: mw.config.get( 'wgUserLanguage' ),
162 formatversion: 2
163 } ).done( ( data ) => {
164 data = data.categorytree.html;
165
166 let $data;
167 if ( data === '' ) {
168 $data = $( '<i>' ).addClass( 'CategoryTreeNotice' )
169 // eslint-disable-next-line mediawiki/msg-doc
170 .text( mw.msg( {
171 0: 'categorytree-no-subcategories',
172 10: 'categorytree-no-pages',
173 100: 'categorytree-no-parent-categories'
174 }[ mode ] || 'categorytree-nothing-found' ) );
175 } else {
176 $data = $( $.parseHTML( data ) );
177 attachHandler( $data );
178 }
179
180 $children.empty().append( $data );
181 } ).fail( error );
182 }
183
184 // Register click events
185 mw.hook( 'wikipage.content' ).add( attachHandler );
186
187 // Attach click handler for categories.
188 // This is needed when wgCategoryTreeHijackPageCategories is enabled.
189 mw.hook( 'wikipage.categories' ).add( attachHandler );
190
191 $( () => {
192 // Attach click handler for sidebar
193 // eslint-disable-next-line no-jquery/no-global-selector
194 attachHandler( $( '#p-categorytree-portlet' ) );
195 } );