Merge "Don't use rclimit preference on Special:Search"
[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 // undo some damage from dialog css
142 this.$dialog.find( 'a' ).css( {
143 color: '#0645ad'
144 } );
145
146 this.$dialog.dialog( {
147 width: 500,
148 autoOpen: false,
149 title: mw.msg( this.dialogTitleMessageKey ),
150 modal: true,
151 buttons: fb.buttons
152 } );
153
154 this.subjectInput = this.$dialog.find( 'input.feedback-subject' ).get( 0 );
155 this.messageInput = this.$dialog.find( 'textarea.feedback-message' ).get( 0 );
156
157 },
158
159 /**
160 * Displays a section of the dialog.
161 *
162 * @param {"form"|"bugs"|"submitting"|"thanks"|"error"} s
163 * The section of the dialog to show.
164 */
165 display: function ( s ) {
166 // Hide the buttons
167 this.$dialog.dialog( { buttons: {} } );
168 // Hide everything
169 this.$dialog.find( '.feedback-mode' ).hide();
170 // Show the desired div
171 this.$dialog.find( '.feedback-' + s ).show();
172 },
173
174 /**
175 * Display the submitting section.
176 */
177 displaySubmitting: function () {
178 this.display( 'submitting' );
179 },
180
181 /**
182 * Display the bugs section.
183 */
184 displayBugs: function () {
185 var fb = this,
186 bugsButtons = {};
187 this.display( 'bugs' );
188 bugsButtons[ mw.msg( 'feedback-bugnew' ) ] = function () {
189 window.open( fb.bugsLink, '_blank' );
190 };
191 bugsButtons[ mw.msg( 'feedback-cancel' ) ] = function () {
192 fb.cancel();
193 };
194 this.$dialog.dialog( {
195 buttons: bugsButtons
196 } );
197 },
198
199 /**
200 * Display the thanks section.
201 */
202 displayThanks: function () {
203 var fb = this,
204 closeButton = {};
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 this.subjectInput.value = ( contents && contents.subject ) ? contents.subject : '';
224 this.messageInput.value = ( contents && contents.message ) ? contents.message : '';
225
226 this.display( 'form' );
227
228 // Set up buttons for dialog box. We have to do it the hard way since the json keys are localized
229 formButtons[ mw.msg( 'feedback-submit' ) ] = function () {
230 fb.submit();
231 };
232 formButtons[ mw.msg( 'feedback-cancel' ) ] = function () {
233 fb.cancel();
234 };
235 this.$dialog.dialog( { buttons: formButtons } ); // put the buttons back
236 },
237
238 /**
239 * Display an error on the form.
240 *
241 * @param {string} message Should be a valid message key.
242 */
243 displayError: function ( message ) {
244 var fb = this,
245 closeButton = {};
246 this.display( 'error' );
247 this.$dialog.find( '.feedback-error-msg' ).msg( message );
248 closeButton[ mw.msg( 'feedback-close' ) ] = function () {
249 fb.$dialog.dialog( 'close' );
250 };
251 this.$dialog.dialog( { buttons: closeButton } );
252 },
253
254 /**
255 * Close the feedback form.
256 */
257 cancel: function () {
258 this.$dialog.dialog( 'close' );
259 },
260
261 /**
262 * Submit the feedback form.
263 */
264 submit: function () {
265 var subject, message,
266 fb = this;
267
268 function ok( result ) {
269 if ( result.edit !== undefined ) {
270 if ( result.edit.result === 'Success' ) {
271 fb.displayThanks();
272 } else {
273 // unknown API result
274 fb.displayError( 'feedback-error1' );
275 }
276 } else {
277 // edit failed
278 fb.displayError( 'feedback-error2' );
279 }
280 }
281
282 function err() {
283 // ajax request failed
284 fb.displayError( 'feedback-error3' );
285 }
286
287 // Get the values to submit.
288 subject = this.subjectInput.value;
289
290 // We used to include "mw.html.escape( navigator.userAgent )" but there are legal issues
291 // with posting this without their explicit consent
292 message = this.messageInput.value;
293 if ( message.indexOf( '~~~' ) === -1 ) {
294 message += ' ~~~~';
295 }
296
297 this.displaySubmitting();
298
299 this.api.newSection( this.title, subject, message ).done( ok ).fail( err );
300 },
301
302 /**
303 * Modify the display form, and then open it, focusing interface on the subject.
304 * @param {Object} [contents] Prefilled contents for the feedback form.
305 * @param {string} [contents.subject] The subject of the feedback
306 * @param {string} [contents.message] The content of the feedback
307 */
308 launch: function ( contents ) {
309 this.displayForm( contents );
310 this.$dialog.dialog( 'open' );
311 this.subjectInput.focus();
312 }
313
314 };
315
316 }( mediaWiki, jQuery ) );