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>' ).append(
116 mw
.msg( 'feedback-subject' ),
118 $( '<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 $( '<div style="margin-top: 0.4em;"></div>' ).append(
121 mw
.msg( 'feedback-message' ),
123 $( '<textarea name="message" class="feedback-message" rows="5" cols="60"></textarea>' )
126 $( '<div class="feedback-mode feedback-bugs"></div>' ).append(
127 $( '<p>' ).msg( 'feedback-bugcheck', $bugsListLink
)
129 $( '<div class="feedback-mode feedback-submitting" style="text-align: center; margin: 3em 0;"></div>' ).append(
130 mw
.msg( 'feedback-adding' ),
132 $( '<span class="feedback-spinner"></span>' )
134 $( '<div class="feedback-mode feedback-thanks" style="text-align: center; margin:1em"></div>' ).msg(
135 'feedback-thanks', fb
.title
.getNameText(), $feedbackPageLink
.clone()
137 $( '<div class="feedback-mode feedback-error" style="position: relative;"></div>' ).append(
138 $( '<div class="feedback-error-msg style="color: #990000; margin-top: 0.4em;"></div>' )
142 this.$dialog
.dialog( {
145 title
: mw
.msg( this.dialogTitleMessageKey
),
150 this.subjectInput
= this.$dialog
.find( 'input.feedback-subject' ).get( 0 );
151 this.messageInput
= this.$dialog
.find( 'textarea.feedback-message' ).get( 0 );
155 * Displays a section of the dialog.
157 * @param {"form"|"bugs"|"submitting"|"thanks"|"error"} s
158 * The section of the dialog to show.
160 display: function ( s
) {
162 this.$dialog
.dialog( { buttons
: {} } );
164 this.$dialog
.find( '.feedback-mode' ).hide();
165 // Show the desired div
166 this.$dialog
.find( '.feedback-' + s
).show();
170 * Display the submitting section.
172 displaySubmitting: function () {
173 this.display( 'submitting' );
177 * Display the bugs section.
179 displayBugs: function () {
183 this.display( 'bugs' );
184 bugsButtons
[ mw
.msg( 'feedback-bugnew' ) ] = function () {
185 window
.open( fb
.bugsLink
, '_blank' );
187 bugsButtons
[ mw
.msg( 'feedback-cancel' ) ] = function () {
190 this.$dialog
.dialog( {
196 * Display the thanks section.
198 displayThanks: function () {
202 this.display( 'thanks' );
203 closeButton
[ mw
.msg( 'feedback-close' ) ] = function () {
204 fb
.$dialog
.dialog( 'close' );
206 this.$dialog
.dialog( {
212 * Display the feedback form
213 * @param {Object} [contents] Prefilled contents for the feedback form.
214 * @param {string} [contents.subject] The subject of the feedback
215 * @param {string} [contents.message] The content of the feedback
217 displayForm: function ( contents
) {
221 this.subjectInput
.value
= ( contents
&& contents
.subject
) ? contents
.subject
: '';
222 this.messageInput
.value
= ( contents
&& contents
.message
) ? contents
.message
: '';
224 this.display( 'form' );
226 // Set up buttons for dialog box. We have to do it the hard way since the json keys are localized
227 formButtons
[ mw
.msg( 'feedback-submit' ) ] = function () {
230 formButtons
[ mw
.msg( 'feedback-cancel' ) ] = function () {
233 this.$dialog
.dialog( { buttons
: formButtons
} ); // put the buttons back
237 * Display an error on the form.
239 * @param {string} message Should be a valid message key.
241 displayError: function ( message
) {
245 this.display( 'error' );
246 this.$dialog
.find( '.feedback-error-msg' ).msg( message
);
247 closeButton
[ mw
.msg( 'feedback-close' ) ] = function () {
248 fb
.$dialog
.dialog( 'close' );
250 this.$dialog
.dialog( { buttons
: closeButton
} );
254 * Close the feedback form.
256 cancel: function () {
257 this.$dialog
.dialog( 'close' );
261 * Submit the feedback form.
263 submit: function () {
264 var subject
, message
,
267 function ok( result
) {
268 if ( result
.edit
!== undefined ) {
269 if ( result
.edit
.result
=== 'Success' ) {
272 // unknown API result
273 fb
.displayError( 'feedback-error1' );
277 fb
.displayError( 'feedback-error2' );
282 // ajax request failed
283 fb
.displayError( 'feedback-error3' );
286 // Get the values to submit.
287 subject
= this.subjectInput
.value
;
289 // We used to include "mw.html.escape( navigator.userAgent )" but there are legal issues
290 // with posting this without their explicit consent
291 message
= this.messageInput
.value
;
292 if ( message
.indexOf( '~~~' ) === -1 ) {
296 this.displaySubmitting();
298 // Post the message, resolving redirects
304 ).done( ok
).fail( err
);
308 * Modify the display form, and then open it, focusing interface on the subject.
309 * @param {Object} [contents] Prefilled contents for the feedback form.
310 * @param {string} [contents.subject] The subject of the feedback
311 * @param {string} [contents.message] The content of the feedback
313 launch: function ( contents
) {
314 this.displayForm( contents
);
315 this.$dialog
.dialog( 'open' );
316 this.subjectInput
.focus();
319 }( mediaWiki
, jQuery
) );