User:Psl85/nightmode.js

/* Night mode for Minecraft wiki by violine1101, 2017 */ var i18n = { activate: 'Activate', activateTitle: 'Activate NightMode', activatedLabel: 'NightMode activated.', deactivate: 'Deactivate', deactivateTitle: 'Deactivate NightMode', deactivatedLabel: 'NightMode deactivated.', settings: 'Options', settingsTitle: 'Change NightMode settings', settingsDialogTitle: 'NightMode settings', settingsEnabled: 'Nightmode settings activated', settingsEnableAuto: 'Automatic time-related activation', settingsTime1: 'NightMode will be activated while the clock is between', settingsTime2: 'and', settingsTime3: '.', settingsInfo: 'The Settings will be saved only for this device and web browser', settingsSave: 'Save', settingsCancel: 'Cancel' };

var windowManager = new OO.ui.WindowManager;

function getCookieValue(a) { var b = document.cookie.match('(^|;)\\s*' + a + '\\s*=\\s*([^;]+)'); return b ? b.pop : ''; }

function loadSettings { var cookie = document.cookie.match('(^|;)\\s*nightmode\\s*=\\s*([^;]+)'); s = cookie ? JSON.parse( cookie.pop ) : { enabled: false, auto: false, time: [ 0, 12 ] };	if ( s.enabled == null ) s.enabled = false; if ( s.auto == null ) s.auto = false; if ( s.time == null ) s.time = [ 0, 12 ]; if ( s.auto ) { var d = new Date; var n = d.getHours * 2 + Math.floor( d.getMinutes / 30 ); if ( 			( s.time[0] <= s.time[1] && n >= s.time[0] && n < s.time[1] )			|| ( s.time[0] > s.time[1] && ( n >= s.time[0] || n < s.time[1] ) )		) s.enabled = true; else s.enabled = false; document.cookie = 'nightmode=' + JSON.stringify( s ) + ';path=/'; }	return s; }

var settings = loadSettings;

function NightmodeSettings( config ) { NightmodeSettings.super.call( this, config ); var enabledOption; var autoOption; var beginSelect; var endSelect; } OO.inheritClass( NightmodeSettings, OO.ui.ProcessDialog );

NightmodeSettings.static.name = 'nightmode-settings'; NightmodeSettings.static.title = i18n.settingsTitle; NightmodeSettings.static.actions = [ { action: 'save', label: i18n.settingsSave, flags: [ 'primary', 'constructive' ] }, { label: i18n.settingsCancel, flags: [ 'safe', 'destructive' ] } ];

function makeItems { var arr = []; for ( var i = 0; i < 48; i++ ) arr[i] = Math.floor( i / 2 ) + ':' + ( i % 2 == 0 ? '00' : '30' ); return arr.map( function ( val, i ) {		return new OO.ui.MenuOptionWidget( { data: i,			label: val } );	} ); }

NightmodeSettings.prototype.initialize = function { NightmodeSettings.super.prototype.initialize.apply( this, arguments ); var dialog = this; this.enabledOption = new OO.ui.ToggleSwitchWidget( {		value: settings.enabled,		label: i18n.settingsEnabled	} );

this.autoOption = new OO.ui.ToggleSwitchWidget( {		value: settings.auto,		label: i18n.settingsEnabled	} ).on( 'change', function( v ) {		dialog.beginSelect.setDisabled( !v );		dialog.endSelect.setDisabled( !v );	} ); this.beginSelect = new OO.ui.DropdownWidget( {		menu: {			items: makeItems		},		$overlay: this.$overlay	} ); this.beginSelect.setDisabled( !this.autoOption.getValue ); this.beginSelect.$element.css( { 'width': '15%', 'min-width': '80px', 'vertical-align': 'middle' } ); this.beginSelect.getMenu.selectItemByData( settings.time[0] ); this.beginSelect.getMenu.$element.css( { 'z-index': 10 } );

this.endSelect = new OO.ui.DropdownWidget( {		menu: {			items: makeItems		},		$overlay: this.$overlay	} ); this.endSelect.getMenu.$element.css( { 'z-index': 10 } ); this.endSelect.$element.css( { 'width': '15%', 'min-width': '80px', 'vertical-align': 'middle' } ); this.endSelect.getMenu.selectItemByData( settings.time[1] ); this.endSelect.setDisabled( !this.autoOption.getValue );

var infoLayout = new OO.ui.PanelLayout( {		expanded: false,		padded: true	} ); infoLayout.$element.append(		new OO.ui.IconWidget( { icon: 'info' } ).$element,		new OO.ui.LabelWidget( { label: $( '' ).html( i18n.settingsInfo ) } ).$element	); var fieldset = new OO.ui.FieldsetLayout; fieldset.addItems( [ 		new OO.ui.FieldLayout( this.enabledOption, { label: i18n.settingsEnabled, align: 'inline' } ),		new OO.ui.FieldLayout( this.autoOption, { label: i18n.settingsEnableAuto, align: 'inline' } ),		new OO.ui.HorizontalLayout( { items: [ new OO.ui.LabelWidget( { label: i18n.settingsTime1 } ), this.beginSelect, new OO.ui.LabelWidget( { label: i18n.settingsTime2 } ), this.endSelect, new OO.ui.LabelWidget( { label: i18n.settingsTime3 } ) ]		} ),		infoLayout	] ); this.content = new OO.ui.PanelLayout( {		scrollable: false,		padded: true	} ); this.content.$element.append(		fieldset.$element	); this.$body.append( this.content.$element ); };

NightmodeSettings.prototype.getActionProcess = function ( action ) { var dialog = this; if ( action ) { settings = { enabled: this.enabledOption.getValue, auto: this.autoOption.getValue, time: [ this.beginSelect.getMenu.getSelectedItem.getData, this.endSelect.getMenu.getSelectedItem.getData ] };		document.cookie = 'nightmode=' + JSON.stringify( settings ) + ';path=/'; updateNightmode; return new OO.ui.Process( function {			dialog.close( { action: action } );		} ); }	return NightmodeSettings.super.prototype.getActionProcess.call( this, action ); };

var nightmodeSettings = new NightmodeSettings( {	size: 'large' } );

function updateNightmode { $( 'body' ).toggleClass( 'nightmode', settings.enabled ); if ( settings.enabled ) { $( '#p-nightmode-label' ).html( i18n.activatedLabel ); $( '.nightmode-toggle a' ).attr( 'title', i18n.deactivateTitle ).html( i18n.deactivate ); }	else { $( '#p-nightmode-label' ).html( i18n.deactivatedLabel ); $( '.nightmode-toggle a' ).attr( 'title', i18n.activateTitle ).html( i18n.activate ); } }

windowManager.addWindows( [ nightmodeSettings ] ); $( 'body' ).append( windowManager.$element );

if ( settings.enabled ) $( 'body' ).addClass( 'nightmode' );

var portals = $( '.portal:not(#p-socialProfiles):not(#p-sitePromos)' );

$( portals[portals.length - 1] ).after(	$( ' ', { 'class': 'portal persistent', 'id': 'p-nightmode' } ).append( $( ' ', { 'id': 'p-nightmode-label' } ).html( settings.enabled ? i18n.activatedLabel : i18n.deactivatedLabel ), $( ' ', { 'class': 'body' } ).append(			$( '', { 'id': 'p-nightmode-list' } ).append( $( '', { 'class': 'nightmode-toggle' } ).append(					$( '', {							'title': ( settings.enabled ? i18n.deactivateTitle : i18n.activateTitle), 'href': '' } )					.html( settings.enabled ? i18n.deactivate : i18n.activate )					.click( function( e ) { e.preventDefault; settings.enabled = !settings.enabled; document.cookie = 'nightmode=' + JSON.stringify( settings ) + ';path=/'; updateNightmode; } )				),				$( '', { 'class': 'nightmode-settings' } ).append(					$( '', { 'title': i18n.settingsTitle, 'href': '' } ).html( i18n.settings ).click( function( e ) { e.preventDefault; settings = loadSettings; windowManager.openWindow( nightmodeSettings ); } )				)			)		)	) );