-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppropedia.js
149 lines (138 loc) · 4.43 KB
/
Appropedia.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const Appropedia = {
/**
* Initialization script
*/
init: function () {
const $content = $( '#mw-content-text' );
// Special:Search
$content.find( '.mw-search-profile-form select' ).each( Appropedia.updateSearchFilterWidth );
$content.find( '.mw-search-profile-form select' ).on( 'change', Appropedia.updateSearchQuery );
// Reminders
// @todo Move to gadgets
Appropedia.checkReminder();
$content.find( '.template-set-reminder-set-button' ).click( Appropedia.setReminder );
$content.find( '.template-set-reminder-unset-button' ).click( Appropedia.unsetReminder );
// Enable popups on more namespaces
mw.config.set( 'wgContentNamespaces', [ 0, 2, 4, 12 ] );
},
/**
* In Special:Search, update the filter search width
*/
updateSearchFilterWidth: function () {
const $select = $( this );
const text = $select.find( 'option:selected' ).text();
const $dummy = $( '<div></div>' ).text( text );
$dummy.css( { position: 'absolute', visibility: 'hidden' } );
$( 'body' ).append( $dummy );
const width = $dummy.width();
$dummy.remove();
$select.width( width );
},
/**
* In Special:Search, update the search query when a filter changes
*/
updateSearchQuery: function () {
const $select = $( this );
const value = $select.val();
const $options = $select.find( 'option' );
const params = new URLSearchParams( window.location.search );
let search = params.get( 'search' ) || '';
$options.each( function () {
const value = $( this ).val();
search = search.replace( value, '' );
} );
search = search + ' ' + value;
search = search.replace( / +/g, ' ' ).trim();
params.set( 'search', search );
window.location.search = params.toString();
},
/**
* This interacts with {{Set reminder}}
*/
setReminder: function () {
const $template = $( this ).closest( '.template-set-reminder' );
const text = $template.data( 'text' );
const image = $template.data( 'image' );
const category = $template.data( 'category' );
const categoryIgnore = $template.data( 'category-ignore' );
mw.cookie.set( 'ReminderText', text );
mw.cookie.set( 'ReminderImage', image );
mw.cookie.set( 'ReminderCategory', category );
mw.cookie.set( 'ReminderCategoryIgnore', categoryIgnore );
$template.children().toggle();
Appropedia.showReminder();
},
/**
* This interacts with {{Set reminder}}
*/
unsetReminder: function () {
const text = mw.cookie.get( 'ReminderText' );
const $template = $( '.template-set-reminder[data-text="' + text + '"]' );
if ( $template.length ) {
$template.children().toggle();
}
mw.cookie.set( 'ReminderText', null );
mw.cookie.set( 'ReminderImage', null );
mw.cookie.set( 'ReminderCategory', null );
mw.cookie.set( 'ReminderCategoryIgnore', null );
},
/**
* Check if a reminder needs to be shown
*/
checkReminder: function () {
const text = mw.cookie.get( 'ReminderText' );
if ( !text ) {
return;
}
const $template = $( '.template-set-reminder[data-text="' + text + '"]' );
if ( $template.length ) {
$template.children().toggle();
}
const category = mw.cookie.get( 'ReminderCategory' );
const categories = mw.config.get( 'wgCategories' );
if ( category && categories && !categories.includes( category ) ) {
return;
}
const categoryIgnore = mw.cookie.get( 'ReminderCategoryIgnore' );
if ( categoryIgnore && categories.includes( categoryIgnore ) ) {
return;
}
const action = mw.config.get( 'wgAction' );
if ( action !== 'view' ) {
return;
}
const contentmodel = mw.config.get( 'wgPageContentModel' );
if ( contentmodel !== 'wikitext' ) {
return;
}
const mainpage = mw.config.get( 'wgIsMainPage' );
if ( mainpage ) {
return;
}
Appropedia.showReminder();
},
/**
* Show the reminder
*/
showReminder: function () {
const text = mw.cookie.get( 'ReminderText' );
const image = mw.cookie.get( 'ReminderImage', null, 'Antu appointment-reminder.svg' );
let wikitext = '[[File:{{PAGENAME:' + image + '}}|right|38px|link=]]' + text;
wikitext += '<div class="mw-ui-button">Unset reminder</div>';
new mw.Api().get( {
formatversion: 2,
action: 'parse',
text: wikitext,
title: mw.config.get( 'wgPageName' )
} ).done( function ( data ) {
const html = data.parse.text;
const $html = $( html );
mw.notify( $html, { tag: 'reminder', autoHide: false } );
$html.find( '.mw-ui-button' ).click( Appropedia.unsetReminder );
} );
}
};
mw.loader.using( [
'mediawiki.api',
'mediawiki.cookie'
], Appropedia.init );