summaryrefslogtreecommitdiff
path: root/extensions/WikiEditor/modules/jquery.wikiEditor.previewDialog.js
blob: 5381e60548cdf1242e6ffb4b1646a98395cb5f0a (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* Publish module for wikiEditor */
/*jshint onevar:false */
( function ( $, mw ) { $.wikiEditor.modules.previewDialog = {

/**
 * Compatability map
 */
'browsers': {
	// Left-to-right languages
	'ltr': {
		'msie': [['>=', 7]],
		'firefox': [['>=', 3]],
		'opera': [['>=', 9.6]],
		'safari': [['>=', 4]]
	},
	// Right-to-left languages
	'rtl': {
		'msie': [['>=', 8]],
		'firefox': [['>=', 3]],
		'opera': [['>=', 9.6]],
		'safari': [['>=', 4]]
	}
},
/**
 * Internally used functions
 */
fn: {
	/**
	 * Creates a publish module within a wikiEditor
	 * @param context Context object of editor to create module in
	 * @param config Configuration object to create module from
	 */
	create: function( context ) {
		// Build the dialog behind the Publish button
		var dialogID = 'wikiEditor-' + context.instance + '-preview-dialog';
		$.wikiEditor.modules.dialogs.fn.create(
			context,
			{
				preview: {
					id: dialogID,
					titleMsg: 'wikieditor-preview-tab',
					html: '\
						<div class="wikiEditor-ui-loading"><span></span></div>\
						<div class="wikiEditor-preview-dialog-contents"></div>\
					',
					init: function() {
					},
					dialog: {
						buttons: {
							'wikieditor-publish-dialog-publish': function() {
								var minorChecked = $( '#wikiEditor-' + context.instance +
									'-dialog-minor' ).is( ':checked' ) ?
										'checked' : '';
								var watchChecked = $( '#wikiEditor-' + context.instance +
									'-dialog-watch' ).is( ':checked' ) ?
										'checked' : '';
								$( '#wpMinoredit' ).attr( 'checked', minorChecked );
								$( '#wpWatchthis' ).attr( 'checked', watchChecked );
								$( '#wpSummary' ).val( $( '#wikiEditor-' + context.instance +
									'-dialog-summary' ).val() );
								$( '#editform' ).submit();
							},
							'wikieditor-publish-dialog-goback': function() {
								$(this).dialog( 'close' );
							}
						},
						resizable: false,
						height: $( 'body' ).height() - 100,
						width: $( 'body' ).width() - 300,
						position: ['center', 'top'],
						open: function() {
							// Gets the latest copy of the wikitext
							var wikitext = context.fn.getContents();
							var $dialog = $( '#' + dialogID );
							$dialog
								.css( 'position', 'relative' )
								.css( 'height', $( 'body' ).height() - 200 )
								.parent()
								.css( 'top', '25px' );
							// $dialog.dialog( 'option', 'width', $( 'body' ).width() - 300 );
							// Aborts when nothing has changed since the last preview
							if ( context.modules.preview.previewText === wikitext ) {
								return;
							}

							$dialog.find( '.wikiEditor-preview-dialog-contents' ).empty();
							$dialog.find( '.wikiEditor-ui-loading' ).show();
							$.post(
								mw.util.wikiScript( 'api' ),
								{
									'action': 'parse',
									'title': mw.config.get( 'wgPageName' ),
									'text': wikitext,
									'prop': 'text',
									'pst': '',
									'format': 'json'
								},
								function( data ) {
									if (
										typeof data.parse === 'undefined' ||
										typeof data.parse.text === 'undefined' ||
										typeof data.parse.text['*'] === 'undefined'
									) {
										return;
									}
									context.modules.preview.previewText = wikitext;
									$dialog.find( '.wikiEditor-ui-loading' ).hide();
									$dialog.find( '.wikiEditor-preview-dialog-contents' )
										.html( '<h1 class="firstHeading" id="firstHeading">' +
											mw.config.get( 'wgTitle' ) + '</h1>' +
											data.parse.text['*'] )
										.find( 'a:not([href^=#])' ).click( function() { return false; } );
								},
								'json'
							);
						}
					},
					resizeme: false
				}
			}
		);
		context.fn.addButton( {
			'captionMsg': 'wikieditor-preview-tab',
			'action': function() {
				context.$textarea.wikiEditor( 'openDialog', 'preview');
				return false;
			}
		} );
	}
}

}; } )( jQuery, mediaWiki );