-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCentralNoticeHelper.js
More file actions
103 lines (83 loc) · 3.23 KB
/
CentralNoticeHelper.js
File metadata and controls
103 lines (83 loc) · 3.23 KB
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
// <nowiki>
// Requested by Pharos for metawiki
// Generates some Special:CentralNoticeBanner geolocation code that otherwise has to be copy/pasted and then manually adjusted
class CentralNoticeHelper {
constructor( mw, $ ) {
this.mw = mw;
// eslint-disable-next-line no-jquery/variable-pattern
this.$ = $;
}
async execute() {
this.mw.util.addPortletLink( 'p-cactions', '#', 'CentralNotice Helper', 'central-notice-code-helper' );
this.$( '#central-notice-code-helper' ).on( 'click', () => {
this.loadForm();
} );
}
loadForm() {
const states = [ 'AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY' ];
const provinces = [ 'AB', 'BC', 'MB', 'NB', 'NL', 'NT', 'NS', 'NU', 'ON', 'PE', 'QC', 'SK', 'YT' ];
const statesAndProvinces = states.concat( provinces ).sort();
const statesHtml = statesAndProvinces.map( ( state ) => `<option value="${ state }">${ state }</option>` ).join( '' );
this.$( '#firstHeading' ).html( 'CentralNotice Helper' );
this.$( '#bodyContent' ).html( `
State:
<select>
${ statesHtml }
</select>
<br />
<select name="where">
<option value="north-of">north-of</option>
<option value="south-of">south-of</option>
<option value="east-of">east-of</option>
<option value="west-of">west-of</option>
</select>
<input type="text" name="latitude-or-longitude" placeholder="36.5" /><br />
<input type="button" value="Generate code" id="generate-code-button">
<div id="central-notice-helper-result"></div>
` );
this.$( '#generate-code-button' ).on( 'click', () => {
if ( this.$( '[name="latitude-or-longitude"]' ).val() === '' ) {
this.$( '#central-notice-helper-result' ).html( 'Please enter a latitude or longitude.' );
return;
} else {
this.loadResult();
}
} );
}
loadResult() {
const state = this.$( '#bodyContent select' ).val();
const type = [ 'north-of', 'south-of' ].includes( this.$( '[name="where"]' ).val() ) ? 'lat' : 'lon';
const decimal = this.$( '[name="latitude-or-longitude"]' ).val();
const html = `<div class="cnotice georegion georegion-${ state }-subregion" id="${ state }-subregion" dir="ltr">`;
const javaScript =
`<script type="text/javascript">
(function(){
var region = mw.centralNotice.data.region;
if( region == '${ state }' && Geo.${ type } <= ${ decimal } ) {
region = '${ state }-subregion';
}
var div = $( 'div.georegion.georegion-' + region )
if( div.length ) {
div.addClass('georegionselected');
$( 'div#${ state }-subregion' ).show();
} else {
mw.centralNotice.bannerData.hideResult = true;
mw.centralNotice.bannerData.hideReason = 'outofscope';
}
})()
</script>`;
this.$( '#central-notice-helper-result' ).html( `
<br /><br />
HTML:<br />
<textarea>${ html }</textarea>
JavaScript:<br />
<textarea style="height:20em;">${ javaScript }</textarea>
` );
}
}
$( async () => {
await mw.loader.using( [ 'mediawiki.util' ], async () => {
await ( new CentralNoticeHelper( mw, $ ) ).execute();
} );
} );
// </nowiki>