2014-06-17 11:08:05 +00:00
|
|
|
( function ( $, mw ) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class mw.popups.settings
|
|
|
|
* @singleton
|
|
|
|
*/
|
|
|
|
var settings = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the navigation popups gadget is enabled by looking for
|
|
|
|
* the `disablePopups` method
|
|
|
|
* @property navPopEnabled
|
|
|
|
*/
|
|
|
|
settings.navPopEnabled = ( typeof disablePopups !== 'undefined' );
|
|
|
|
|
|
|
|
/**
|
2014-08-29 04:22:55 +00:00
|
|
|
* The settings' dialog's section element.
|
|
|
|
* Defined in settings.open
|
2014-06-17 11:08:05 +00:00
|
|
|
* @property $element
|
|
|
|
*/
|
2014-08-29 04:22:55 +00:00
|
|
|
settings.$element = null;
|
2014-06-17 11:08:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the relevant form and labels in the settings dialog
|
|
|
|
*
|
|
|
|
* @method render
|
|
|
|
*/
|
|
|
|
settings.render = function () {
|
|
|
|
var
|
|
|
|
$closeIcon = $( '<a>' ).html( '×' ),
|
|
|
|
$titleText = $( '<h1>' ).text( mw.message( 'popups-settings-title' ).text() ),
|
|
|
|
$title = $( '<header>' ).append( $closeIcon, $titleText ),
|
|
|
|
$description = $( '<p>' ).text( mw.message( 'popups-settings-description' ).text() ),
|
|
|
|
$radioGroup = $( '<radiogroup>' ),
|
2014-08-29 04:22:55 +00:00
|
|
|
$cancelButton = $( '<button>' )
|
|
|
|
.addClass( 'mw-ui-button mw-ui-quiet' )
|
|
|
|
.text( mw.message( 'popups-settings-cancel' ).text() )
|
|
|
|
.attr( 'type', 'button' ),
|
|
|
|
$saveButton = $( '<button>' )
|
|
|
|
.addClass( 'mw-ui-button mw-ui-constructive' )
|
|
|
|
.text( mw.message( 'popups-settings-save' ).text() )
|
|
|
|
.attr( 'type', 'button' ),
|
|
|
|
$actions = $( '<div>' )
|
|
|
|
.addClass( 'mwe-popups-settings-actions' )
|
|
|
|
.append( $cancelButton, $saveButton ),
|
|
|
|
$okayButton = $( '<button>' )
|
|
|
|
.addClass( 'mw-ui-button mw-ui-constructive' )
|
|
|
|
.text( mw.message( 'popups-settings-help-ok' ).text() )
|
|
|
|
.attr( 'type', 'button' ),
|
|
|
|
$help = $( '<div>' )
|
|
|
|
.attr( 'id', 'mwe-popups-settings-help' )
|
|
|
|
.append(
|
|
|
|
$( '<p>' ).text( mw.message( 'popups-settings-help' ).text() ),
|
|
|
|
$( '<div>' ).addClass( 'mwe-popups-settings-help-image' ),
|
|
|
|
$( '<div>' ).addClass( 'mwe-popups-settings-actions' ).append( $okayButton )
|
|
|
|
)
|
|
|
|
.hide(),
|
2014-06-17 11:08:05 +00:00
|
|
|
$form = $( '<form>' ).append( $radioGroup, $actions ),
|
2014-08-29 04:22:55 +00:00
|
|
|
$main = $( '<main>' ).attr( 'id', 'mwe-popups-settings-form' ).append( $description, $form ),
|
|
|
|
options = {
|
|
|
|
'simple': [
|
|
|
|
mw.message( 'popups-settings-option-simple' ).text(),
|
|
|
|
mw.message( 'popups-settings-option-simple-description' ).text(),
|
|
|
|
'hovercard.svg'
|
|
|
|
],
|
|
|
|
'advanced': [
|
|
|
|
mw.message( 'popups-settings-option-advanced' ).text(),
|
|
|
|
mw.message( 'popups-settings-option-advanced-description' ).text(),
|
|
|
|
'navpop.svg'
|
|
|
|
],
|
|
|
|
'off': [
|
|
|
|
mw.message( 'popups-settings-option-off' ).text(),
|
|
|
|
mw.message( 'popups-settings-option-off-description' ).text()
|
|
|
|
]
|
|
|
|
};
|
2014-06-17 11:08:05 +00:00
|
|
|
|
|
|
|
$( $closeIcon ).add( $cancelButton ).click( settings.close );
|
|
|
|
$saveButton.click( settings.save );
|
2014-08-29 04:22:55 +00:00
|
|
|
$okayButton.click( function () {
|
|
|
|
settings.close();
|
|
|
|
location.reload();
|
|
|
|
} );
|
2014-06-17 11:08:05 +00:00
|
|
|
|
2014-08-29 04:22:55 +00:00
|
|
|
$radioGroup.append( settings.renderOption( 'simple', options.simple, true ) );
|
2014-08-12 10:40:57 +00:00
|
|
|
if ( settings.navPopEnabled ) {
|
2014-08-29 04:22:55 +00:00
|
|
|
$radioGroup.append( settings.renderOption( 'advanced', options.advanced ) );
|
2014-06-17 11:08:05 +00:00
|
|
|
}
|
2014-08-29 04:22:55 +00:00
|
|
|
$radioGroup.append( settings.renderOption( 'off', options.off ) );
|
2014-06-17 11:08:05 +00:00
|
|
|
|
2014-08-29 04:22:55 +00:00
|
|
|
settings.$element = $( '<section> ' ).attr( 'id', 'mwe-popups-settings' );
|
2014-06-17 11:08:05 +00:00
|
|
|
settings.$element
|
|
|
|
.hide()
|
2014-08-29 04:22:55 +00:00
|
|
|
.append( $title, $main, $help );
|
2014-06-17 11:08:05 +00:00
|
|
|
|
|
|
|
$( 'body' ).append( settings.$element );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders an option with its input[type='radio'], label and image
|
|
|
|
*
|
|
|
|
* @method renderOption
|
|
|
|
* @param {Array} content
|
|
|
|
* @param {Boolean} checked
|
|
|
|
* @return {jQuery} The paragraph that holds the above mentioned elements
|
|
|
|
*/
|
|
|
|
settings.renderOption = function ( id, content, checked ) {
|
|
|
|
var
|
|
|
|
title = content[ 0 ].toLowerCase().split( ' ' ).join( '_' ),
|
|
|
|
domId = 'mwe-popups-settings-' + title,
|
|
|
|
$input = $( '<input>' )
|
|
|
|
.attr( {
|
|
|
|
name: 'mwe-popups-setting',
|
|
|
|
value: id,
|
|
|
|
type: 'radio',
|
|
|
|
id: domId
|
|
|
|
} ),
|
|
|
|
$label = $( '<label>' )
|
|
|
|
.attr( 'for', domId )
|
2015-03-24 08:06:07 +00:00
|
|
|
.text( content[ 1 ] )
|
2014-06-17 11:08:05 +00:00
|
|
|
.prepend( $( '<span>' ).text( content[ 0 ] ) ),
|
|
|
|
$p = $( '<p>' ).append( $input, $label );
|
|
|
|
|
|
|
|
if ( checked ) {
|
|
|
|
$input.prop( 'checked', true );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( content.length === 3) {
|
|
|
|
$p.append(
|
|
|
|
$( '<img>' ).attr( 'src', mw.config.get( 'wgExtensionAssetsPath' ) + '/Popups/resources/' + content[ 2 ] )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $p;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the setting to localStorage (jStorage) and close the dialog
|
|
|
|
*
|
|
|
|
* @method save
|
|
|
|
*/
|
|
|
|
settings.save = function () {
|
|
|
|
var v = $('input[name=mwe-popups-setting]:checked','#mwe-popups-settings').val();
|
2014-08-29 04:22:55 +00:00
|
|
|
if ( v === 'simple' ) {
|
2014-06-17 11:08:05 +00:00
|
|
|
$.jStorage.set( 'mwe-popups-enabled', 'true' );
|
|
|
|
location.reload();
|
2014-08-29 04:22:55 +00:00
|
|
|
settings.close();
|
2014-06-17 11:08:05 +00:00
|
|
|
} else {
|
|
|
|
$.jStorage.set( 'mwe-popups-enabled', 'false' );
|
2014-08-29 04:22:55 +00:00
|
|
|
$( '#mwe-popups-settings-form' ).hide();
|
|
|
|
$( '#mwe-popups-settings-help' ).show();
|
2014-06-17 11:08:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the settings element and position it correctly
|
|
|
|
*
|
|
|
|
* @method open
|
|
|
|
*/
|
2014-08-18 11:37:54 +00:00
|
|
|
settings.open = function () {
|
2014-06-17 11:08:05 +00:00
|
|
|
var
|
|
|
|
h = $( window ).height(),
|
|
|
|
w = $( window ).width();
|
|
|
|
|
|
|
|
$( 'body' ).append( $( '<div>' ).addClass( 'mwe-popups-overlay' ) );
|
|
|
|
|
2014-08-29 04:22:55 +00:00
|
|
|
if ( !settings.$element ) {
|
|
|
|
settings.render();
|
|
|
|
}
|
|
|
|
|
2014-06-17 11:08:05 +00:00
|
|
|
settings.$element
|
|
|
|
.show()
|
|
|
|
.css( 'left', ( w - 600 ) / 2 )
|
|
|
|
.css( 'top', ( h - settings.$element.outerHeight( true ) ) / 2 );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-08-29 04:22:55 +00:00
|
|
|
* Close the setting dialoag and remove the overlay.
|
|
|
|
* If the close button is clicked on the help dialog
|
|
|
|
* save the setting and reload the page.
|
2014-06-17 11:08:05 +00:00
|
|
|
*
|
|
|
|
* @method close
|
|
|
|
*/
|
|
|
|
settings.close = function () {
|
2014-08-29 04:22:55 +00:00
|
|
|
if ( $( '#mwe-popups-settings-help' ).is( ':visible' ) ) {
|
|
|
|
location.reload();
|
|
|
|
} else {
|
|
|
|
$( '.mwe-popups-overlay' ).remove();
|
|
|
|
settings.$element.hide();
|
|
|
|
}
|
2014-06-17 11:08:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a link to the footer to re-enable hovercards
|
|
|
|
*
|
|
|
|
* @method addFooterLink
|
|
|
|
*/
|
|
|
|
settings.addFooterLink = function () {
|
|
|
|
if ( mw.popups.enabled ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var
|
|
|
|
$setting = $( '<li>' ).append(
|
|
|
|
$( '<a>' )
|
|
|
|
.attr( 'href', '#)' )
|
|
|
|
.text( mw.message( 'popups-settings-enable' ).text() )
|
2014-08-18 11:37:54 +00:00
|
|
|
.click( function ( e ) {
|
|
|
|
settings.open();
|
|
|
|
e.preventDefault();
|
|
|
|
} )
|
2014-06-17 11:08:05 +00:00
|
|
|
),
|
|
|
|
$footer = $( '#footer-places, #f-list' );
|
|
|
|
|
|
|
|
// From https://en.wikipedia.org/wiki/MediaWiki:Gadget-ReferenceTooltips.js
|
|
|
|
if ( $footer.length === 0 ) {
|
|
|
|
$footer = $( '#footer li' ).parent();
|
|
|
|
}
|
|
|
|
$footer.append( $setting );
|
|
|
|
};
|
|
|
|
|
|
|
|
$( function () {
|
|
|
|
if( !mw.popups.enabled ) {
|
|
|
|
settings.addFooterLink();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
mw.popups.settings = settings;
|
|
|
|
|
|
|
|
} ) ( jQuery, mediaWiki );
|