MediaWiki:Gadget-code-snippets.js

From Old School Near-Reality Wiki
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
  1 /**<nowiki>
  2  * Gadget that loads a couple of snippets for easier access to commonly copy/pasted templates and sections
  3  * Authors:
  4  ** Joeytje50
  5 */
  6 
  7 $(function() {
  8 	var today = new Date().toLocaleString('en-GB', {day: 'numeric', month: 'long', year: 'numeric', timeZone: 'UTC'});
  9 	var pagename = mw.config.get('wgTitle');
 10 
 11 	var preloadMsgs = [
 12 		'NPC',
 13 		'equipment',
 14 		'item',
 15 		'location',
 16 		'monster',
 17 		'scenery',
 18 		'shop',
 19 		//'navbox',
 20 		//'navbox-oneline',
 21 		//'navbox-subgroup',
 22 	].map(function(msg) {return 'newarticletext-'+msg;});
 23 
 24 	function snippet(label, parts) {
 25 		if (typeof(parts) == 'string') parts = [parts]; // if string is given, only use pre
 26 		return {
 27 			label: label,
 28 			action: {
 29 				type: 'encapsulate',
 30 				options: {
 31 					pre: parts[0] || '',
 32 					peri: parts[1] || '',
 33 					post: parts[2] || '',
 34 				}
 35 			}
 36 		};
 37 	}
 38 	
 39 	function getLicense(name, chars, param) {
 40 		var label = name;
 41 		if (name instanceof Array) { // if the label shouldn't be the same as the name
 42 			label = name[1];
 43 			name = name[0];
 44 		}
 45 		return snippet(label, [
 46 			'{{'+name+' license|',
 47 			pagename.slice(0, -chars),
 48 			(param || '') + '}}'
 49 		]);
 50 	}
 51 	
 52 	function getPreloads(preloads) {
 53 		var arr = [];
 54 		for (var i=0;i<preloads.length;i++) {
 55 			var msg = mw.message(preloads[i], mw.config.get('wgTitle'), mw.config.get('wgTitle').split(' (')[0]).plain();
 56 			msg = msg.replace(/<\/?includeonly>/g, '').replace(/<noinclude>[\s\S]*<\/noinclude>/g, ''); // remove include-tags
 57 			var obj = {
 58 				label: preloads[i].charAt(15).toUpperCase() + preloads[i].substr(16),
 59 				action: {
 60 					type: 'encapsulate',
 61 					options: {
 62 						pre: msg,
 63 					},
 64 				},
 65 			};
 66 			arr.push(obj);
 67 		}
 68 		return arr;
 69 	}
 70 
 71 	$( '#wpTextbox1' ).on('wikiEditor-toolbar-doneInitialSections', function() {
 72 		if (typeof($('#wpTextbox1').wikiEditor) != 'function') return console.log($('#wpTextbox1'), $('#wpTextbox1').wikiEditor);
 73 		
 74 		var preloads = {
 75 			label: 'Preloads',
 76 			layout: 'characters',
 77 			characters: [], // will be filled after MW messages have loaded.
 78 		}
 79 		var templates = {
 80 			'label': 'Template presets',
 81 			'layout': 'characters',
 82 			'characters': [
 83 				snippet('Single {{Map}}', [
 84 					'{{Map|name='+pagename+'|x=3200|y=3200|plane=0|r=4|mapID=0|mtype=', 
 85 					'square', 
 86 					'}}'
 87 				]),
 88 				snippet('Multi-pin {{Map}}', [
 89 					'{{Map|name='+pagename+'|', 
 90 					'3200,3200|3232,3200', 
 91 					'|plane=0|mapID=0|mtype=pin}}'
 92 				]),
 93 				snippet('{{Synced switch}}', [
 94 					'{{Synced switch\n|version1 = ', 
 95 					'', 
 96 					'\n|version2 = \n|version3 = \n}}\n'
 97 				]),
 98 				snippet('{{DropLogProject}}', [
 99 					'{{DropLogProject|kills=', 
100 					'0',
101 					'}}\n'
102 				]),
103 				snippet('{{DropLogProject|type=}}', [
104 					'{{DropLogProject|kills=0|type=', 
105 					'chests',
106 					'}}\n'
107 				]),
108 				snippet('{{Relative location}}', [
109 					'{{Relative location\n|location = '+pagename+'\n|north = ', 
110 					'', 
111 					'\n|west = \n|south = \n|east = \n}}\n'
112 				]),
113 				snippet('{{External}}', [
114 					'{{External|rs', 
115 					'|rsc', 
116 					'}}\n'
117 				]),
118 				snippet('{{Listen}}', [
119 					'{{Listen\n|title = \n|desc = \n|filename = ', 
120 					'', 
121 					'}}\n'
122 				]),
123 				snippet('{{Otheruses}}', [
124 					'{{Otheruses|', 
125 					'', 
126 					'}}\n'
127 				]),
128 				snippet('{{Redirect}}', [
129 					'{{Redirect|', 
130 					'', 
131 					'}}\n'
132 				]),
133 			]
134 		};
135 		var sections = {
136 			'label': 'Section presets',
137 			'layout': 'characters',
138 			'characters': [
139 				snippet('==References==', [
140 					'==References==\n{{Reflist', 
141 					'', 
142 					'}}\n'
143 				]),
144 				snippet('==NPC Locations==', [
145 					'==Locations==\n{{LocTableHead}}\n{{LocLine\n|name = '+pagename+'\n|location = ', 
146 					'', 
147 					'\n|levels = \n|members = \n|mapID = \n|plane = 0\n|x:3200,y:3200,icon:redPin\n|x:3232,y:3232,icon:redPin\n|mtype = pin\n|version=\n}}\n{{LocTableBottom}}\n'
148 				]),
149 				snippet('==Object Locations==', [
150 					'==Locations==\n{{ObjectTableHead}}\n{{ObjectLocLine\n|name = '+pagename+'\n|location = ', 
151 					'', 
152 					'\n|members = \n|mapID = \n|plane = 0\n|x:3200,y:3200,icon:redPin\n|x:3232,y:3232,icon:redPin\n|mtype = pin\n|version=\n}}\n{{ObjectTableBottom}}\n'
153 				]),
154 				snippet('==Monster Drops==', [
155 					'==Drops==\n\n===100%===\n{{DropsTableHead}}\n{{DropsLine|name=', 
156 					'', 
157 					'|quantity=1|rarity=Always}}\n{{DropsTableBottom}}\n'
158 				]),
159 				snippet('==Item Creation==', [
160 					'== Creation ==\n{{Recipe\n|skill1 = ', 
161 					'', 
162 					'\n|skill1lvl = \n|skill1boostable = \n|skill1exp = \n|ticks = \n|members = \n|mat1 = \n|mat2 = \n|output1 = '+pagename+'\n|output1quantity = 1}}\n'
163 				]),
164 				snippet('==Item Products==', [
165 					'==Products==\n{{Uses material list|',
166 					pagename,
167 					'}}\n'
168 				]),
169 				snippet('==Item sources==', [
170 					'==Item sources==\n{{Drop sources|',
171 					pagename,
172 					'}}\n'
173 				]),
174 				snippet('===Shop locations===', [
175 					'===Shop locations===\n{{Store locations list|',
176 					pagename,
177 					'}}\n'
178 				]),
179 				snippet('===Spawns===', [
180 					'===Spawns===\n{{ItemSpawnTableHead}}\n{{ItemSpawnLine|name='+pagename+'|location=', 
181 					'', 
182 					'|members=|3200,3200}}\n{{ItemSpawnTableBottom}}\n'
183 				]),
184 				snippet('==Changes==', [
185 					'==Changes==\n{{Subject changes header}}\n{{Subject changes\n|date = ',
186 					today,
187 					'\n|update = \n|poll = \n|change = \n}}\n{{Subject changes footer}}\n'
188 				]),
189 				snippet('==Combat styles==', [
190 					'==Combat styles==\n{{CombatStyles|', 
191 					'', 
192 					'}}\n'
193 				]),
194 				snippet('==Dialogue==', [
195 					'==Dialogue==\n{{Hastranscript|', 
196 					'npc', 
197 					'}}\n'
198 				]),
199 			]
200 		};
201 		var references = {
202 			'label': 'References',
203 			'layout': 'characters',
204 			'characters': [
205 				snippet('Ref section', [
206 					'==References==\n{{Reflist', 
207 					'', 
208 					'}}\n'
209 				]),
210 				snippet('Ref list', [
211 					'{{Reflist|group=', 
212 					'', 
213 					'}}\n'
214 				]),
215 				snippet('CiteTwitter', [
216 					'{{CiteTwitter|author=', 
217 					'', 
218 					'|url=|date=|archiveurl=|archivedate=|quote=|notes=|name=|group=}}'
219 				]),
220 				snippet('CiteReddit', [
221 					'{{CiteReddit|author=', 
222 					'', 
223 					'|url=|title=|quote=|date=|archiveurl=|archivedate=|notes=|name=}}'
224 				]),
225 				snippet('CiteNews', [
226 					'{{CiteNews|author=', 
227 					'', 
228 					'|url=|title=|newsdate=|notes=|name=}}'
229 				]),
230 				snippet('CiteNPC', [
231 					'{{CiteNPC|npc=', 
232 					'', 
233 					'|quote=|loc=|notes=|name=}}'
234 				]),
235 				snippet('NamedRef', [
236 					'{{CiteNPC|name=', 
237 					'', 
238 					'|group=}}'
239 				]),
240 			]
241 		};
242 		var transcript = {
243 			'label': 'Transcripts',
244 			'layout': 'characters',
245 			'characters': [
246 				snippet('==Dialogue==', [
247 					'==Dialogue==\n{{Hastranscript|', 
248 					'npc', 
249 					'}}\n'
250 				]),
251 				snippet('{{Transcript}}', [
252 					'{{Transcript|', 
253 					'npc', 
254 					'}}\n\n==Standard dialogue==\n* '
255 				]),
256 				snippet('{{RSChat}}', [
257 					'{{RSChat|',
258 					// demo text to avoid having to preview to know whether to use RSChat or RSFont
259 					'Cor blimey mate, what are ye doing in me pockets?',
260 					'}}'
261 				]),
262 				snippet('{{RSFont}}', [
263 					'{{RSFont|',
264 					// demo text to avoid having to preview to know whether to use RSChat or RSFont
265 					'Nothing interesting happens.',
266 					'}}'
267 				]),
268 				snippet('End of dialogue', '{{tact|end}}'),
269 				snippet('Members-only notice', '{{tact|members}}'),
270 				snippet('Interface opens', [
271 					'{{tact|opens=', 
272 					'Interface', 
273 					'}}'
274 				]),
275 				snippet('Unlinked interface opens', [
276 					'{{tact|opens2=', 
277 					'Interface', 
278 					'}}'
279 				]),
280 				snippet('Receives item', [
281 					'{{tact|receives=', 
282 					'an [[item]]', 
283 					'}}'
284 				]),
285 				snippet('Shows previous', '{{tact|previous}}'),
286 				snippet('Shows prev-to-prev', '{{tact|previous2}}'),
287 				snippet('Shows initial', '{{tact|initial}}'),
288 				snippet('Shows other options', '{{tact|other}}'),
289 				snippet('Same as above', '{{tact|above}}'),
290 				snippet('Same as below', '{{tact|below}}'),
291 				snippet('Continues below', '{{tact|continue}}'),
292 			]
293 		};
294 		var licenses = {
295 			'label': 'File licenses',
296 			'layout': 'characters',
297 			'characters': [
298 				getLicense('Inventory', 4),
299 				getLicense(['DII', 'Detailed item'], 11),
300 				getLicense('Worn equipment', 13),
301 				getLicense('NPC', 4),
302 				getLicense(['Chathead', 'NPC chathead'], 13, '|sub=npc'),
303 				getLicense(['Chathead', 'Pet chathead'], 13, '|sub=pet'),
304 				getLicense(['Chathead', 'player chathead'], 13, '|sub=player'),
305 				getLicense('Monster', 4),
306 				getLicense('Scenery', 4),
307 				getLicense(['POH', 'POH furniture'], 4),
308 				getLicense('Location', 4),
309 				getLicense('Map', 13),
310 				getLicense('Interface', 1e10),
311 				getLicense('Emote', 4),
312 				getLicense(['Prayer', 'Prayer icon'], 4),
313 				getLicense(['Spell', 'Spell icon'], 4),
314 				getLicense(['Quest', 'Quest scroll'], 18, '|sub=scroll'),
315 				getLicense(['Quest', 'Quest infobox'], 4, '|sub=infobox'),
316 				getLicense('Animation', 1e10),
317 				snippet('Audio', '{{Non-free audio}}'),
318 				getLicense('Miscellaneous', 4),
319 				snippet('Fair use', '{{Fair use}}'),
320 				snippet('CC-BY-SA', '{{CC-BY-SA}}'),
321 			]
322 		};
323 		var feedback = {
324 			'label': 'Missing something?',
325 			'layout': 'table',
326 			'headings': [
327 				{ text: 'Are you missing a code snippet that would be useful here?' }
328 			],
329 			'rows': [
330 				{
331 					text: { html: 
332 						'You can <a href="https://oldschool.runescape.wiki/w/RuneScape:Administrator_requests?action=edit&section=new&preloadtitle=Requesting%20new%20code%20snippet%20in%20editor" target="_blank">'+
333 						'request a snippet</a> to be added.'+
334 						(mw.config.get('wgUserGroups').includes('sysop') ? '<br/>Admins may edit the <a href="/w/MediaWiki:Gadget-code-snippets.js" title="MediaWiki:Gadget-code-snippets.js">code snippets script</a>.' : ''),
335 					},
336 				}
337 			],
338 		};
339 		
340 		
341 		var isRedirect = /^\#REDIRECT\s*\[\[[^\]]+\]\](\n|$)/i.test($('#wpTextbox1').val()) && $('#wikiPreview div').text().length == 0;
342 		
343 		var groups = {};
344 		if (isRedirect && $('body').is('.ns-0')) groups['section-preloads'] = preloads; // will be filled after messages have loaded.
345 		groups['section-templates'] = templates;
346 		if ($('body').is('.ns-0')) groups['section-sections'] = sections; // listed sections are only relevant to mainspace
347 		groups['section-refs'] = references;
348 		if ($('body').is('.ns-120')) groups['section-transcript'] = transcript; // only show on Transcript: namespace.
349 		if ($('body').is('.ns-6')) groups['section-licenses'] = licenses; // only show on File: namespace.
350 		groups['section-feedback'] = feedback;
351 
352 		
353 		// Show additional snippets, except for MediaWiki:, Module:, Exchange:, talk pages.
354 		// Other namespaces might not necessarily need very many of these snippets, but there might be snippets for those in the future.
355 		if (!$('body').is('.ns-8, .ns-114, .ns-828, .ns-talk')) {
356 			$('#wpTextbox1').wikiEditor('addToToolbar', {
357 				'sections': {
358 					'snippets': {
359 						'type': 'booklet',
360 						'label': 'Presets/snippets',
361 						'pages': groups,
362 					}
363 				}
364 			});
365 		}
366 		
367 		if (isRedirect && $('body').is('.ns-0')) {
368 			$.when(mw.loader.using(['mediawiki.api', 'mediawiki.jqueryMsg']), $.ready).then(function() {
369 		        return new mw.Api().loadMessagesIfMissing(preloadMsgs);
370 		    }).then(function() {
371 				$('#wpTextbox1').wikiEditor('addToToolbar', {
372 					section: 'snippets',
373 					page: 'section-preloads',
374 					characters: getPreloads(preloadMsgs),
375 				});
376 			});
377 		}
378 	});
379 });
380 //</nowiki>