Merge "Fix documentation of HistoryPager::lastLink() and make code more readable"
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.feedback.js
1 /*!
2 * mediawiki.feedback
3 *
4 * @author Ryan Kaldari, 2010
5 * @author Neil Kandalgaonkar, 2010-11
6 * @since 1.19
7 */
8 ( function ( mw, $ ) {
9 /**
10 * This is a way of getting simple feedback from users. It's useful
11 * for testing new features -- users can give you feedback without
12 * the difficulty of opening a whole new talk page. For this reason,
13 * it also tends to collect a wider range of both positive and negative
14 * comments. However you do need to tend to the feedback page. It will
15 * get long relatively quickly, and you often get multiple messages
16 * reporting the same issue.
17 *
18 * It takes the form of thing on your page which, when clicked, opens a small
19 * dialog box. Submitting that dialog box appends its contents to a
20 * wiki page that you specify, as a new section.
21 *
22 * This feature works with classic MediaWiki pages
23 * and is not compatible with LiquidThreads or Flow.
24 *
25 * Minimal usage example:
26 *
27 * var feedback = new mw.Feedback();
28 * $( '#myButton' ).click( function () { feedback.launch(); } );
29 *
30 * You can also launch the feedback form with a prefilled subject and body.
31 * See the docs for the #launch() method.
32 *
33 * @class
34 * @constructor
35 * @param {Object} [options]
36 * @param {mw.Api} [options.api] if omitted, will just create a standard API
37 * @param {mw.Title} [options.title="Feedback"] The title of the page where you collect
38 * feedback.
39 * @param {string} [options.dialogTitleMessageKey="feedback-submit"] Message key for the
40 * title of the dialog box
41 * @param {string} [options.bugsLink="//bugzilla.wikimedia.org/enter_bug.cgi"] URL where
42 * bugs can be posted
43 * @param {mw.Uri|string} [options.bugsListLink="//bugzilla.wikimedia.org/query.cgi"]
44 * URL where bugs can be listed
45 */
46 mw.Feedback = function ( options ) {
47 if ( options === undefined ) {
48 options = {};
49 }
50
51 if ( options.api === undefined ) {
52 options.api = new mw.Api();
53 }
54
55 if ( options.title === undefined ) {
56 options.title = new mw.Title( 'Feedback' );
57 }
58
59 if ( options.dialogTitleMessageKey === undefined ) {
60 options.dialogTitleMessageKey = 'feedback-submit';
61 }
62
63 if ( options.bugsLink === undefined ) {
64 options.bugsLink = '//bugzilla.wikimedia.org/enter_bug.cgi';
65 }
66
67 if ( options.bugsListLink === undefined ) {
68 options.bugsListLink = '//bugzilla.wikimedia.org/query.cgi';
69 }
70
71 $.extend( this, options );
72 this.setup();
73 };
74
75 mw.Feedback.prototype = {
76 /**
77 * Sets up interface
78 */
79 setup: function () {
80 var $feedbackPageLink,
81 $bugNoteLink,
82 $bugsListLink,
83 fb = this;
84
85 $feedbackPageLink = $( '<a>' )
86 .attr( {
87 href: fb.title.getUrl(),
88 target: '_blank'
89 } )
90 .css( {
91 whiteSpace: 'nowrap'
92 } );
93
94 $bugNoteLink = $( '<a>' ).attr( { href: '#' } ).click( function () {
95 fb.displayBugs();
96 } );
97
98 $bugsListLink = $( '<a>' ).attr( {
99 href: fb.bugsListLink,
100 target: '_blank'
101 } );
102
103 // TODO: Use a stylesheet instead of these inline styles
104 this.$dialog =
105 $( '<div style="position: relative;"></div>' ).append(
106 $( '<div class="feedback-mode feedback-form"></div>' ).append(
107 $( '<small>' ).append(
108 $( '<p>' ).msg(
109 'feedback-bugornote',
110 $bugNoteLink,
111 fb.title.getNameText(),
112 $feedbackPageLink.clone()
113 )
114 ),
115 $( '<div style="margin-top: 1em;"></div>' )
116 .msg( 'feedback-subject' )
117 .append(
118 $( '<br>' ),
119 $( '<input type="text" class="feedback-subject" name="subject" maxlength="60" style="width: 100%; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box;"/>' )
120 ),
121 $( '<div style="margin-top: 0.4em;"></div>' )
122 .msg( 'feedback-message' )
123 .append(
124 $( '<br>' ),
125 $( '<textarea name="message" class="feedback-message" rows="5" cols="60"></textarea>' )
126 )
127 ),
128 $( '<div class="feedback-mode feedback-bugs"></div>' ).append(
129 $( '<p>' ).msg( 'feedback-bugcheck', $bugsListLink )
130 ),
131 $( '<div class="feedback-mode feedback-submitting" style="text-align: center; margin: 3em 0;"></div>' )
132 .msg( 'feedback-adding' )
133 .append(
134 $( '<br>' ),
135 $( '<span class="feedback-spinner"></span>' )
136 ),
137 $( '<div class="feedback-mode feedback-thanks" style="text-align: center; margin:1em"></div>' ).msg(
138 'feedback-thanks', fb.title.getNameText(), $feedbackPageLink.clone()
139 ),
140 $( '<div class="feedback-mode feedback-error" style="position: relative;"></div>' ).append(
141 $( '<div class="feedback-error-msg style="color: #990000; margin-top: 0.4em;"></div>' )
142 )
143 );
144
145 this.$dialog.dialog( {
146 width: 500,
147 autoOpen: false,
148 title: mw.message( this.dialogTitleMessageKey ).escaped(),
149 modal: true,
150 buttons: fb.buttons
151 } );
152
153 this.subjectInput = this.$dialog.find( 'input.feedback-subject' ).get( 0 );
154 this.messageInput = this.$dialog.find( 'textarea.feedback-message' ).get( 0 );
155 },
156
157 /**
158 * Displays a section of the dialog.
159 *
160 * @param {"form"|"bugs"|"submitting"|"thanks"|"error"} s
161 * The section of the dialog to show.
162 */
163 display: function ( s ) {
164 // Hide the buttons
165 this.$dialog.dialog( { buttons: {} } );
166 // Hide everything
167 this.$dialog.find( '.feedback-mode' ).hide();
168 // Show the desired div
169 this.$dialog.find( '.feedback-' + s ).show();
170 },
171
172 /**
173 * Display the submitting section.
174 */
175 displaySubmitting: function () {
176 this.display( 'submitting' );
177 },
178
179 /**
180 * Display the bugs section.
181 */
182 displayBugs: function () {
183 var fb = this,
184 bugsButtons = {};
185
186 this.display( 'bugs' );
187 bugsButtons[ mw.msg( 'feedback-bugnew' ) ] = function () {
188 window.open( fb.bugsLink, '_blank' );
189 };
190 bugsButtons[ mw.msg( 'feedback-cancel' ) ] = function () {
191 fb.cancel();
192 };
193 this.$dialog.dialog( {
194 buttons: bugsButtons
195 } );
196 },
197
198 /**
199 * Display the thanks section.
200 */
201 displayThanks: function () {
202 var fb = this,
203 closeButton = {};
204
205 this.display( 'thanks' );
206 closeButton[ mw.msg( 'feedback-close' ) ] = function () {
207 fb.$dialog.dialog( 'close' );
208 };
209 this.$dialog.dialog( {
210 buttons: closeButton
211 } );
212 },
213
214 /**
215 * Display the feedback form
216 * @param {Object} [contents] Prefilled contents for the feedback form.
217 * @param {string} [contents.subject] The subject of the feedback
218 * @param {string} [contents.message] The content of the feedback
219 */
220 displayForm: function ( contents ) {
221 var fb = this,
222 formButtons = {};
223
224 this.subjectInput.value = ( contents && contents.subject ) ? contents.subject : '';
225 this.messageInput.value = ( contents && contents.message ) ? contents.message : '';
226
227 this.display( 'form' );
228
229 // Set up buttons for dialog box. We have to do it the hard way since the json keys are localized
230 formButtons[ mw.msg( 'feedback-submit' ) ] = function () {
231 fb.submit();
232 };
233 formButtons[ mw.msg( 'feedback-cancel' ) ] = function () {
234 fb.cancel();
235 };
236 this.$dialog.dialog( { buttons: formButtons } ); // put the buttons back
237 },
238
239 /**
240 * Display an error on the form.
241 *
242 * @param {string} message Should be a valid message key.
243 */
244 displayError: function ( message ) {
245 var fb = this,
246 closeButton = {};
247
248 this.display( 'error' );
249 this.$dialog.find( '.feedback-error-msg' ).msg( message );
250 closeButton[ mw.msg( 'feedback-close' ) ] = function () {
251 fb.$dialog.dialog( 'close' );
252 };
253 this.$dialog.dialog( { buttons: closeButton } );
254 },
255
256 /**
257 * Close the feedback form.
258 */
259 cancel: function () {
260 this.$dialog.dialog( 'close' );
261 },
262
263 /**
264 * Submit the feedback form.
265 */
266 submit: function () {
267 var subject, message,
268 fb = this;
269
270 // Get the values to submit.
271 subject = this.subjectInput.value;
272
273 // We used to include "mw.html.escape( navigator.userAgent )" but there are legal issues
274 // with posting this without their explicit consent
275 message = this.messageInput.value;
276 if ( message.indexOf( '~~~' ) === -1 ) {
277 message += ' ~~~~';
278 }
279
280 this.displaySubmitting();
281
282 // Post the message, resolving redirects
283 this.api.newSection(
284 this.title,
285 subject,
286 message,
287 { redirect: true }
288 )
289 .done( function ( result ) {
290 if ( result.edit !== undefined ) {
291 if ( result.edit.result === 'Success' ) {
292 fb.displayThanks();
293 } else {
294 // unknown API result
295 fb.displayError( 'feedback-error1' );
296 }
297 } else {
298 // edit failed
299 fb.displayError( 'feedback-error2' );
300 }
301 } )
302 .fail( function () {
303 // ajax request failed
304 fb.displayError( 'feedback-error3' );
305 } );
306 },
307
308 /**
309 * Modify the display form, and then open it, focusing interface on the subject.
310 * @param {Object} [contents] Prefilled contents for the feedback form.
311 * @param {string} [contents.subject] The subject of the feedback
312 * @param {string} [contents.message] The content of the feedback
313 */
314 launch: function ( contents ) {
315 this.displayForm( contents );
316 this.$dialog.dialog( 'open' );
317 this.subjectInput.focus();
318 }
319 };
320 }( mediaWiki, jQuery ) );