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