4 * @author Ryan Kaldari, 2010
5 * @author Neil Kandalgaonkar, 2010-11
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.
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.
22 * This feature works with classic MediaWiki pages
23 * and is not compatible with LiquidThreads or Flow.
25 * Minimal usage example:
27 * var feedback = new mw.Feedback();
28 * $( '#myButton' ).click( function () { feedback.launch(); } );
30 * You can also launch the feedback form with a prefilled subject and body.
31 * See the docs for the #launch() method.
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
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
43 * @param {mw.Uri|string} [options.bugsListLink="//bugzilla.wikimedia.org/query.cgi"]
44 * URL where bugs can be listed
46 mw
.Feedback = function ( options
) {
47 if ( options
=== undefined ) {
51 if ( options
.api
=== undefined ) {
52 options
.api
= new mw
.Api();
55 if ( options
.title
=== undefined ) {
56 options
.title
= new mw
.Title( 'Feedback' );
59 if ( options
.dialogTitleMessageKey
=== undefined ) {
60 options
.dialogTitleMessageKey
= 'feedback-submit';
63 if ( options
.bugsLink
=== undefined ) {
64 options
.bugsLink
= '//bugzilla.wikimedia.org/enter_bug.cgi';
67 if ( options
.bugsListLink
=== undefined ) {
68 options
.bugsListLink
= '//bugzilla.wikimedia.org/query.cgi';
71 $.extend( this, options
);
75 mw
.Feedback
.prototype = {
80 var $feedbackPageLink
,
85 $feedbackPageLink
= $( '<a>' )
87 href
: fb
.title
.getUrl(),
94 $bugNoteLink
= $( '<a>' ).attr( { href
: '#' } ).click( function () {
98 $bugsListLink
= $( '<a>' ).attr( {
99 href
: fb
.bugsListLink
,
103 // TODO: Use a stylesheet instead of these inline styles
105 $( '<div style="position: relative;"></div>' ).append(
106 $( '<div class="feedback-mode feedback-form"></div>' ).append(
107 $( '<small>' ).append(
109 'feedback-bugornote',
111 fb
.title
.getNameText(),
112 $feedbackPageLink
.clone()
115 $( '<div style="margin-top: 1em;"></div>' )
116 .msg( 'feedback-subject' )
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;"/>' )
121 $( '<div style="margin-top: 0.4em;"></div>' )
122 .msg( 'feedback-message' )
125 $( '<textarea name="message" class="feedback-message" rows="5" cols="60"></textarea>' )
128 $( '<div class="feedback-mode feedback-bugs"></div>' ).append(
129 $( '<p>' ).msg( 'feedback-bugcheck', $bugsListLink
)
131 $( '<div class="feedback-mode feedback-submitting" style="text-align: center; margin: 3em 0;"></div>' )
132 .msg( 'feedback-adding' )
135 $( '<span class="feedback-spinner"></span>' )
137 $( '<div class="feedback-mode feedback-thanks" style="text-align: center; margin:1em"></div>' ).msg(
138 'feedback-thanks', fb
.title
.getNameText(), $feedbackPageLink
.clone()
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>' )
145 this.$dialog
.dialog( {
148 title
: mw
.message( this.dialogTitleMessageKey
).escaped(),
153 this.subjectInput
= this.$dialog
.find( 'input.feedback-subject' ).get( 0 );
154 this.messageInput
= this.$dialog
.find( 'textarea.feedback-message' ).get( 0 );
158 * Displays a section of the dialog.
160 * @param {"form"|"bugs"|"submitting"|"thanks"|"error"} s
161 * The section of the dialog to show.
163 display: function ( s
) {
165 this.$dialog
.dialog( { buttons
: {} } );
167 this.$dialog
.find( '.feedback-mode' ).hide();
168 // Show the desired div
169 this.$dialog
.find( '.feedback-' + s
).show();
173 * Display the submitting section.
175 displaySubmitting: function () {
176 this.display( 'submitting' );
180 * Display the bugs section.
182 displayBugs: function () {
186 this.display( 'bugs' );
187 bugsButtons
[ mw
.msg( 'feedback-bugnew' ) ] = function () {
188 window
.open( fb
.bugsLink
, '_blank' );
190 bugsButtons
[ mw
.msg( 'feedback-cancel' ) ] = function () {
193 this.$dialog
.dialog( {
199 * Display the thanks section.
201 displayThanks: function () {
205 this.display( 'thanks' );
206 closeButton
[ mw
.msg( 'feedback-close' ) ] = function () {
207 fb
.$dialog
.dialog( 'close' );
209 this.$dialog
.dialog( {
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
220 displayForm: function ( contents
) {
224 this.subjectInput
.value
= ( contents
&& contents
.subject
) ? contents
.subject
: '';
225 this.messageInput
.value
= ( contents
&& contents
.message
) ? contents
.message
: '';
227 this.display( 'form' );
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 () {
233 formButtons
[ mw
.msg( 'feedback-cancel' ) ] = function () {
236 this.$dialog
.dialog( { buttons
: formButtons
} ); // put the buttons back
240 * Display an error on the form.
242 * @param {string} message Should be a valid message key.
244 displayError: function ( message
) {
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' );
253 this.$dialog
.dialog( { buttons
: closeButton
} );
257 * Close the feedback form.
259 cancel: function () {
260 this.$dialog
.dialog( 'close' );
264 * Submit the feedback form.
266 submit: function () {
267 var subject
, message
,
270 // Get the values to submit.
271 subject
= this.subjectInput
.value
;
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 ) {
280 this.displaySubmitting();
282 // Post the message, resolving redirects
289 .done( function ( result
) {
290 if ( result
.edit
!== undefined ) {
291 if ( result
.edit
.result
=== 'Success' ) {
294 // unknown API result
295 fb
.displayError( 'feedback-error1' );
299 fb
.displayError( 'feedback-error2' );
303 // ajax request failed
304 fb
.displayError( 'feedback-error3' );
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
314 launch: function ( contents
) {
315 this.displayForm( contents
);
316 this.$dialog
.dialog( 'open' );
317 this.subjectInput
.focus();
320 }( mediaWiki
, jQuery
) );