summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/WikiEditor/modules/jquery.wikiEditor.preview.js
blob: d25dbd12c9dfae31e0edbb588ef13b81224682f4 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* Preview module for wikiEditor */
( function ( $, mw ) {
	$.wikiEditor.modules.preview = {

		/**
		 * Compatibility map
		 */
		browsers: {
			// Left-to-right languages
			ltr: {
				msie: [ [ '>=', 9 ] ],
				firefox: [ [ '>=', 4 ] ],
				opera: [ [ '>=', '10.5' ] ],
				safari: [ [ '>=', 5 ] ],
				chrome: [ [ '>=', 5 ] ]
			},
			// Right-to-left languages
			rtl: {
				msie: [ [ '>=', 9 ] ],
				firefox: [ [ '>=', 4 ] ],
				opera: [ [ '>=', '10.5' ] ],
				safari: [ [ '>=', 5 ] ],
				chrome: [ [ '>=', 5 ] ]
			}
		},

		/**
		 * Internally used functions
		 */
		fn: {
			/**
			 * Creates a preview module within a wikiEditor
			 *
			 * @param {Object} context Context object of editor to create module in
			 */
			create: function ( context ) {
				var loadingMsg,
					api = new mw.Api();

				if ( 'initialized' in context.modules.preview ) {
					return;
				}
				context.modules.preview = {
					initialized: true,
					previewText: null,
					changesText: null
				};
				context.modules.preview.$preview = context.fn.addView( {
					name: 'preview',
					titleMsg: 'wikieditor-preview-tab',
					init: function ( context ) {
						// Gets the latest copy of the wikitext
						var wikitext = context.$textarea.textSelection( 'getContents' );
						// Aborts when nothing has changed since the last preview
						if ( context.modules.preview.previewText === wikitext ) {
							return;
						}
						context.modules.preview.$preview.find( '.wikiEditor-preview-contents' ).empty();
						context.modules.preview.$preview.find( '.wikiEditor-preview-loading' ).show();
						api.post( {
							formatversion: 2,
							action: 'parse',
							title: mw.config.get( 'wgPageName' ),
							text: wikitext,
							pst: '',
							prop: 'text|modules|jsconfigvars',
							preview: true,
							disableeditsection: true,
							useskin: mw.config.get( 'skin' ),
							uselang: mw.config.get( 'wgUserLanguage' )
						} ).always( function () {
							context.modules.preview.$preview.find( '.wikiEditor-preview-loading' ).hide();
						} ).done( function ( data ) {
							var loadmodules, $content;
							if ( !data.parse || !data.parse.text ) {
								return;
							}

							if ( data.parse.jsconfigvars ) {
								mw.config.set( data.parse.jsconfigvars );
							}
							loadmodules = data.parse.modules.concat(
								data.parse.modulescripts,
								data.parse.modulestyles
							);
							mw.loader.load( loadmodules );

							context.modules.preview.previewText = wikitext;
							$content = context.modules.preview.$preview.find( '.wikiEditor-preview-contents' )
								.detach()
								.html( data.parse.text );
							$content.append( '<div class="visualClear"></div>' )
								.find( 'a:not([href^="#"])' )
									.click( false );

							mw.hook( 'wikipage.content' ).fire( $content );
							context.modules.preview.$preview.append( $content );
						} );
					}
				} );

				context.$changesTab = context.fn.addView( {
					name: 'changes',
					titleMsg: 'wikieditor-preview-changes-tab',
					init: function ( context ) {
						// Gets the latest copy of the wikitext
						var section, postdata, postPromise,
							wikitext = context.$textarea.textSelection( 'getContents' );
						// Aborts when nothing has changed since the last time
						if ( context.modules.preview.changesText === wikitext ) {
							return;
						}
						context.$changesTab.find( 'table.diff tbody' ).empty();
						context.$changesTab.find( '.wikiEditor-preview-loading' ).show();

						section = $( '[name="wpSection"]' ).val();
						postdata = {
							formatversion: 2,
							action: 'query',
							prop: 'revisions',
							titles: mw.config.get( 'wgPageName' ),
							rvdifftotext: wikitext,
							rvdifftotextpst: true,
							rvprop: '',
							rvsection: section === '' ? undefined : section
						};
						postPromise = api.post( postdata );

						$.when( postPromise, mw.loader.using( 'mediawiki.diff.styles' ) )
						.always( function () {
							context.$changesTab.find( '.wikiEditor-preview-loading' ).hide();
						} )
						.done( function ( postResult ) {
							var diff;
							try {
								diff = postResult[ 0 ].query.pages[ 0 ]
									.revisions[ 0 ].diff.body;

								context.$changesTab.find( 'table.diff tbody' )
									.html( diff )
									.append( '<div class="visualClear"></div>' );
								mw.hook( 'wikipage.diff' )
									.fire( context.$changesTab.find( 'table.diff' ) );
								context.modules.preview.changesText = wikitext;
							} catch ( e ) {
								// "data.blah is undefined" error, ignore
							}
						} );
					}
				} );

				loadingMsg = mw.msg( 'wikieditor-preview-loading' );
				context.modules.preview.$preview
					.add( context.$changesTab )
					.append( $( '<div>' )
						.addClass( 'wikiEditor-preview-loading' )
						.append( $( '<img>' )
							.addClass( 'wikiEditor-preview-spinner' )
							.attr( {
								src: $.wikiEditor.imgPath + 'dialogs/loading.gif',
								valign: 'absmiddle',
								alt: loadingMsg,
								title: loadingMsg
							} )
						)
						.append(
							$( '<span>' ).text( loadingMsg )
						)
					)
					.append( $( '<div>' )
						.addClass( 'wikiEditor-preview-contents' )
					);
				context.$changesTab.find( '.wikiEditor-preview-contents' )
					.html( '<table class="diff"><col class="diff-marker"/><col class="diff-content"/>' +
						'<col class="diff-marker"/><col class="diff-content"/><tbody/></table>' );
			}
		}

	};

}( jQuery, mediaWiki ) );