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 * Not compatible with LiquidThreads.
24 * Minimal example in how to use it:
26 * var feedback = new mw.Feedback();
27 * $( '#myButton' ).click( function () { feedback.launch(); } );
29 * You can also launch the feedback form with a prefilled subject and body.
30 * See the docs for the #launch() method.
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
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
42 * @param {mw.Uri|string} [options.bugsListLink="//bugzilla.wikimedia.org/query.cgi"]
43 * URL where bugs can be listed
45 mw
.Feedback = function ( options
) {
46 if ( options
=== undefined ) {
50 if ( options
.api
=== undefined ) {
51 options
.api
= new mw
.Api();
54 if ( options
.title
=== undefined ) {
55 options
.title
= new mw
.Title( 'Feedback' );
58 if ( options
.dialogTitleMessageKey
=== undefined ) {
59 options
.dialogTitleMessageKey
= 'feedback-submit';
62 if ( options
.bugsLink
=== undefined ) {
63 options
.bugsLink
= '//bugzilla.wikimedia.org/enter_bug.cgi';
66 if ( options
.bugsListLink
=== undefined ) {
67 options
.bugsListLink
= '//bugzilla.wikimedia.org/query.cgi';
70 $.extend( this, options
);
74 mw
.Feedback
.prototype = {
79 var $feedbackPageLink
,
84 $feedbackPageLink
= $( '<a>' )
86 href
: fb
.title
.getUrl(),
93 $bugNoteLink
= $( '<a>' ).attr( { href
: '#' } ).click( function () {
97 $bugsListLink
= $( '<a>' ).attr( {
98 href
: fb
.bugsListLink
,
102 // TODO: Use a stylesheet instead of these inline styles
104 $( '<div style="position: relative;"></div>' ).append(
105 $( '<div class="feedback-mode feedback-form"></div>' ).append(
106 $( '<small>' ).append(
108 'feedback-bugornote',
110 fb
.title
.getNameText(),
111 $feedbackPageLink
.clone()
114 $( '<div style="margin-top: 1em;"></div>' ).append(
115 mw
.msg( 'feedback-subject' ),
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;"/>' )
119 $( '<div style="margin-top: 0.4em;"></div>' ).append(
120 mw
.msg( 'feedback-message' ),
122 $( '<textarea name="message" class="feedback-message" rows="5" cols="60"></textarea>' )
125 $( '<div class="feedback-mode feedback-bugs"></div>' ).append(
126 $( '<p>' ).msg( 'feedback-bugcheck', $bugsListLink
)
128 $( '<div class="feedback-mode feedback-submitting" style="text-align: center; margin: 3em 0;"></div>' ).append(
129 mw
.msg( 'feedback-adding' ),
131 $( '<span class="feedback-spinner"></span>' )
133 $( '<div class="feedback-mode feedback-thanks" style="text-align: center; margin:1em"></div>' ).msg(
134 'feedback-thanks', fb
.title
.getNameText(), $feedbackPageLink
.clone()
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>' )
141 // undo some damage from dialog css
142 this.$dialog
.find( 'a' ).css( {
146 this.$dialog
.dialog( {
149 title
: mw
.msg( this.dialogTitleMessageKey
),
154 this.subjectInput
= this.$dialog
.find( 'input.feedback-subject' ).get( 0 );
155 this.messageInput
= this.$dialog
.find( 'textarea.feedback-message' ).get( 0 );
160 * Displays a section of the dialog.
162 * @param {"form"|"bugs"|"submitting"|"thanks"|"error"} s
163 * The section of the dialog to show.
165 display: function ( s
) {
167 this.$dialog
.dialog( { buttons
: {} } );
169 this.$dialog
.find( '.feedback-mode' ).hide();
170 // Show the desired div
171 this.$dialog
.find( '.feedback-' + s
).show();
175 * Display the submitting section.
177 displaySubmitting: function () {
178 this.display( 'submitting' );
182 * Display the bugs section.
184 displayBugs: function () {
187 this.display( 'bugs' );
188 bugsButtons
[ mw
.msg( 'feedback-bugnew' ) ] = function () {
189 window
.open( fb
.bugsLink
, '_blank' );
191 bugsButtons
[ mw
.msg( 'feedback-cancel' ) ] = function () {
194 this.$dialog
.dialog( {
200 * Display the thanks section.
202 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
) {
223 this.subjectInput
.value
= ( contents
&& contents
.subject
) ? contents
.subject
: '';
224 this.messageInput
.value
= ( contents
&& contents
.message
) ? contents
.message
: '';
226 this.display( 'form' );
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 () {
232 formButtons
[ mw
.msg( 'feedback-cancel' ) ] = function () {
235 this.$dialog
.dialog( { buttons
: formButtons
} ); // put the buttons back
239 * Display an error on the form.
241 * @param {string} message Should be a valid message key.
243 displayError: function ( message
) {
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' );
251 this.$dialog
.dialog( { buttons
: closeButton
} );
255 * Close the feedback form.
257 cancel: function () {
258 this.$dialog
.dialog( 'close' );
262 * Submit the feedback form.
264 submit: function () {
265 var subject
, message
,
268 function ok( result
) {
269 if ( result
.edit
!== undefined ) {
270 if ( result
.edit
.result
=== 'Success' ) {
273 // unknown API result
274 fb
.displayError( 'feedback-error1' );
278 fb
.displayError( 'feedback-error2' );
283 // ajax request failed
284 fb
.displayError( 'feedback-error3' );
287 // Get the values to submit.
288 subject
= this.subjectInput
.value
;
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 ) {
297 this.displaySubmitting();
299 this.api
.newSection( this.title
, subject
, message
, ok
, err
);
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
308 launch: function ( contents
) {
309 this.displayForm( contents
);
310 this.$dialog
.dialog( 'open' );
311 this.subjectInput
.focus();
316 }( mediaWiki
, jQuery
) );