Check validity and availability of usernames during signup via AJAX
[lhc/web/wiklou.git] / resources / 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 * @param {Object} [options]
33 * @param {mw.Api} [options.api] if omitted, will just create a standard API
34 * @param {mw.Title} [options.title="Feedback"] The title of the page where you collect
35 * feedback.
36 * @param {string} [options.dialogTitleMessageKey="feedback-submit"] Message key for the
37 * title of the dialog box
38 * @param {string} [options.bugsLink="//bugzilla.wikimedia.org/enter_bug.cgi"] URL where
39 * bugs can be posted
40 * @param {mw.Uri|string} [options.bugsListLink="//bugzilla.wikimedia.org/query.cgi"]
41 * URL where bugs can be listed
42 */
43 mw.Feedback = function ( options ) {
44 if ( options === undefined ) {
45 options = {};
46 }
47
48 if ( options.api === undefined ) {
49 options.api = new mw.Api();
50 }
51
52 if ( options.title === undefined ) {
53 options.title = new mw.Title( 'Feedback' );
54 }
55
56 if ( options.dialogTitleMessageKey === undefined ) {
57 options.dialogTitleMessageKey = 'feedback-submit';
58 }
59
60 if ( options.bugsLink === undefined ) {
61 options.bugsLink = '//bugzilla.wikimedia.org/enter_bug.cgi';
62 }
63
64 if ( options.bugsListLink === undefined ) {
65 options.bugsListLink = '//bugzilla.wikimedia.org/query.cgi';
66 }
67
68 $.extend( this, options );
69 this.setup();
70 };
71
72 mw.Feedback.prototype = {
73 /**
74 * Sets up interface
75 */
76 setup: function () {
77 var $feedbackPageLink,
78 $bugNoteLink,
79 $bugsListLink,
80 fb = this;
81
82 $feedbackPageLink = $( '<a>' )
83 .attr( {
84 href: fb.title.getUrl(),
85 target: '_blank'
86 } )
87 .css( {
88 whiteSpace: 'nowrap'
89 } );
90
91 $bugNoteLink = $( '<a>' ).attr( { href: '#' } ).click( function () {
92 fb.displayBugs();
93 } );
94
95 $bugsListLink = $( '<a>' ).attr( {
96 href: fb.bugsListLink,
97 target: '_blank'
98 } );
99
100 // TODO: Use a stylesheet instead of these inline styles
101 this.$dialog =
102 $( '<div style="position: relative;"></div>' ).append(
103 $( '<div class="feedback-mode feedback-form"></div>' ).append(
104 $( '<small>' ).append(
105 $( '<p>' ).msg(
106 'feedback-bugornote',
107 $bugNoteLink,
108 fb.title.getNameText(),
109 $feedbackPageLink.clone()
110 )
111 ),
112 $( '<div style="margin-top: 1em;"></div>' ).append(
113 mw.msg( 'feedback-subject' ),
114 $( '<br>' ),
115 $( '<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;"/>' )
116 ),
117 $( '<div style="margin-top: 0.4em;"></div>' ).append(
118 mw.msg( 'feedback-message' ),
119 $( '<br>' ),
120 $( '<textarea name="message" class="feedback-message" rows="5" cols="60"></textarea>' )
121 )
122 ),
123 $( '<div class="feedback-mode feedback-bugs"></div>' ).append(
124 $( '<p>' ).msg( 'feedback-bugcheck', $bugsListLink )
125 ),
126 $( '<div class="feedback-mode feedback-submitting" style="text-align: center; margin: 3em 0;"></div>' ).append(
127 mw.msg( 'feedback-adding' ),
128 $( '<br>' ),
129 $( '<span class="feedback-spinner"></span>' )
130 ),
131 $( '<div class="feedback-mode feedback-thanks" style="text-align: center; margin:1em"></div>' ).msg(
132 'feedback-thanks', fb.title.getNameText(), $feedbackPageLink.clone()
133 ),
134 $( '<div class="feedback-mode feedback-error" style="position: relative;"></div>' ).append(
135 $( '<div class="feedback-error-msg style="color: #990000; margin-top: 0.4em;"></div>' )
136 )
137 );
138
139 // undo some damage from dialog css
140 this.$dialog.find( 'a' ).css( {
141 color: '#0645ad'
142 } );
143
144 this.$dialog.dialog({
145 width: 500,
146 autoOpen: false,
147 title: mw.msg( this.dialogTitleMessageKey ),
148 modal: true,
149 buttons: fb.buttons
150 });
151
152 this.subjectInput = this.$dialog.find( 'input.feedback-subject' ).get(0);
153 this.messageInput = this.$dialog.find( 'textarea.feedback-message' ).get(0);
154
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 this.$dialog.dialog( { buttons:{} } ); // hide the buttons
165 this.$dialog.find( '.feedback-mode' ).hide(); // hide everything
166 this.$dialog.find( '.feedback-' + s ).show(); // show the desired div
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 this.api.newSection( this.title, subject, message, ok, err );
295 },
296
297 /**
298 * Modify the display form, and then open it, focusing interface on the subject.
299 * @param {Object} [contents] Prefilled contents for the feedback form.
300 * @param {string} [contents.subject] The subject of the feedback
301 * @param {string} [contents.message] The content of the feedback
302 */
303 launch: function ( contents ) {
304 this.displayForm( contents );
305 this.$dialog.dialog( 'open' );
306 this.subjectInput.focus();
307 }
308
309 };
310
311 }( mediaWiki, jQuery ) );