MediaWiki:common.js

From Derivative
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.
/* Any JavaScript here will be loaded for all users on every page load. */

function findGetParameter(parameterName) {
    var result = null,
        tmp = [];
    location.search
        .substr(1)
        .split("&")
        .forEach(function (item) {
          tmp = item.split("=");
          if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
        });
    return result;
}

/* load TOC collapsed */
window.addEventListener('DOMContentLoaded', function() { try {
  if (document.getElementById('toc').getElementsByTagName('ul')[0].style.display != 'none') { toggleToc(); }
} catch (exception) {} }, false);

/* custom toolbars */
var customizeToolbar = function () {
	$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
		'section': 'main',
		'group': 'format',
		'tools': {
			"TDpythonbutton": {
				label: 'Python code',
				type: 'button',
				icon: 'https://docs.derivative.ca/images/b/bf/Pythonbutton.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: "<syntaxhighlight lang=python>",
						post: "</syntaxhighlight>"
	  				}
	           }
			},
			"TDcodebutton": {
			label: 'Source code',
			type: 'button',
			icon: 'https://docs.derivative.ca/images/0/05/Codebutton.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: "<code>",
						post: "</code>"
	  				}
	            }
			},
			"TDyoutubebutton": {
			label: 'Embed YouTube video',
			type: 'button',
			icon: 'https://docs.derivative.ca/images/a/af/Youtube.png',
			action: {
				type: 'encapsulate',
				options: {
					pre: "{{#widget:YouTube|id=|width=|height=}}",
					post: ""
				}
			}
			}
		}
	} );
	$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
		'sections': {
			'templates': {
				'type': 'booklet', // Can also be 'booklet'
				'label': 'Templates',
				'pages':{
					'section-glossary': {
						'label': 'Glossary',
						'layout': 'characters',
						'characters':[
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{',
										'peri':'Glossary|Title=|Short=|Long=',
										'post':'}}'
									}
								},
								'label': 'Glossary Template'
							},
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '[[',
										'peri':'Category: Touch Glossary',
										'post':']]'
									}
								},
								'label': 'Glossary Category'
							}
						]
					},
					'section-operators': {
						'label': 'Operator Pages',
						'layout': 'characters',
						'characters':[
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{Summary',
										'peri':'|opFamily=|opLabel=|opType=|opClass=|opFilter=|opLicense=|opCategory=|os=|hardware=|short=|long=',
										'post':'}}'
									}
								},
								'label': 'Operator Summary'
							},
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{ParameterPage',
										'peri':'|opFamily=|pageName=|pageSummary=|items=',
										'post':'}}'
									}
								},
								'label': 'Parameter Page'
							},
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{ParameterSubPage',
										'peri':'|opFamily=|pageName=|pageSummary=|items=',
										'post':'}}'
									}
								},
								'label': 'Parameter Sub Page'
							},
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{OPSection',
										'peri':'|opFamily=|sectionName=|sectionSummary=|items=',
										'post':'}}'
									}
								},
								'label': 'Operator Section'
							},
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{OPSubSection',
										'peri':'|opFamily=|sectionName=|sectionSummary=',
										'post':'}}'
									}
								},
								'label': 'Operator Sub Section'
							},
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{InfoCHOPChannels',
										'peri':'|opFamily=|opLabel=|infoChannels=',
										'post':'}}'
									}
								},
								'label': 'Operator Info CHOP Section'
							},
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{InfoChannel',
										'peri':'|chanName=|chanSummary=',
										'post':'}}'
									}
								},
								'label': 'Info CHOP Channel'
							}
						]
					},
					'section-oppars': {
						'label': 'Operator Parameters',
						'layout': 'characters',
						'characters':[
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{Parameter',
										'peri':'|opFamily=|opType=|parName=|parLabel=|parDefault=|parType=|parReadOnly=|parOrder=|parSummary=|parItems=',
										'post':'}}'
									}
								},
								'label': 'Parameter'
							},
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{ParameterItem',
										'peri':'|opFamily=|parName=|itemName=|itemLabel=|itemDefault=|itemSummary=',
										'post':'}}'
									}
								},
								'label': 'Parameter Item'
							}
						]
					},
					'section-class': {
						'label': 'Python Classes',
						'layout': 'characters',
						'characters':[
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{',
										'peri':'OPClassSummary|OPfamily=|OPtype=|OPlabel=',
										'post':'}}'
									}
								},
								'label': 'OPClassSummary'
							},
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{',
										'peri':'TDClassSummary|label=|summary=',
										'post':'}}'
									}
								},
								'label': 'TDClassSummary'
							},
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{',
										'peri':'ClassMemberSection|Sectionsummary=|items=|empty=',
										'post':'}}'
									}
								},
								'label': 'ClassMemberSection'
							},
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{',
										'peri':'ClassMember|class=|name=|type=|set=|text=|deprecated=',
										'post':'}}'
									}
								},
								'label': 'ClassMember'
							},
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{',
										'peri':'ClassMethodSection|SectionSummary=|items=|empty=',
										'post':'}}'
									}
								},
								'label': 'ClassMethodSection'
							},
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{',
										'peri':'ClassMethod|class=|name=|call=|returns=|text=|deprecated=',
										'post':'}}'
									}
								},
								'label': 'ClassMethod'
							},
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{SubSection|title=|text=}}'
									}
								},
								'label': 'Class SubSection'
							},
						]
					},
					'section-page': {
						'label': 'General Page Elements',
						'layout': 'characters',
						'characters':[
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{History}}'
									}
								},
								'label': 'Tag History'
							},
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{SOPNavBox|opFamily=SOP}}'
									}
								},
								'label': 'Category Navigation Box'
							},
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{#invoke:Category|list|COMPs}}'
									}
								},
								'label': 'Category List'
							},
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '{{lowercase}}'
									}
								},
								'label': 'Force pagetitle to lowercase'
							},
							{
								'action': {
									'type': 'encapsulate',
									'options': {
										'pre': '#REDIRECT [[:Experimental:{{FULLPAGENAME}}]]'
									}
								},
								'label': 'Redirect to Experimental'
							}
						]
					},
				}
			}
		}
	} );
};


/* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar … */
if ( [ 'edit', 'submit' ].indexOf( mw.config.get( 'wgAction' ) ) !== -1 ) {
	mw.loader.using( 'user.options' ).then( function () {
		// This can be the string "0" if the user disabled the preference ([[phab:T54542#555387]])
		if ( mw.user.options.get( 'usebetatoolbar' ) == 1 ) {
			$.when(
				mw.loader.using( 'ext.wikiEditor' ), $.ready
			).then( customizeToolbar );
		}
	} );
}

// Redirect anonymous users to login form.
/*
jQuery(document).ready(function() {
  if (jQuery('#pt-anon_oauth_login').length) {
   var titleUrl = findGetParameter('title');
   if (titleUrl) {
     var returnUrl = '/index.php?title=Special:OAuth2Client/redirect&returnto=' + titleUrl;
   }
   else {
     var returnUrl = '/index.php?title=Special:OAuth2Client/redirect&returnto=Main+Page';
   }
   window.location.href = returnUrl;
  }
});
*/