4 * @author Ryan Kaldari, 2010
5 * @author Neil Kandalgaonkar, 2010-11
8 * This is a way of getting simple feedback from users. It's useful
9 * for testing new features -- users can give you feedback without
10 * the difficulty of opening a whole new talk page. For this reason,
11 * it also tends to collect a wider range of both positive and negative
12 * comments. However you do need to tend to the feedback page. It will
13 * get long relatively quickly, and you often get multiple messages
14 * reporting the same issue.
16 * It takes the form of thing on your page which, when clicked, opens a small
17 * dialog box. Submitting that dialog box appends its contents to a
18 * wiki page that you specify, as a new section.
20 * Not compatible with LiquidThreads.
22 * Minimal example in how to use it:
24 * var feedback = new mw.Feedback();
25 * $( '#myButton' ).click( function () { feedback.launch(); } );
27 * You can also launch the feedback form with a prefilled subject and body.
28 * See the docs for the launch() method.
30 ( function ( mw, $ ) {
32 * Thingy for collecting user feedback on a wiki page
33 * @param {Array} options -- optional, all properties optional.
34 * api: {mw.Api} if omitted, will just create a standard API
35 * title: {mw.Title} the title of the page where you collect feedback. Defaults to "Feedback".
36 * dialogTitleMessageKey: {String} message key for the title of the dialog box
37 * bugsLink: {mw.Uri|String} url where bugs can be posted
38 * bugsListLink: {mw.Uri|String} url where bugs can be listed
40 mw.Feedback = function ( options ) {
41 if ( options === undefined ) {
45 if ( options.api === undefined ) {
46 options.api = new mw.Api();
49 if ( options.title === undefined ) {
50 options.title = new mw.Title( 'Feedback' );
53 if ( options.dialogTitleMessageKey === undefined ) {
54 options.dialogTitleMessageKey = 'feedback-submit';
57 if ( options.bugsLink === undefined ) {
58 options.bugsLink = '//bugzilla.wikimedia.org/enter_bug.cgi';
61 if ( options.bugsListLink === undefined ) {
62 options.bugsListLink = '//bugzilla.wikimedia.org/query.cgi';
65 $.extend( this, options );
69 mw.Feedback.prototype = {
71 var $feedbackPageLink,
76 $feedbackPageLink = $( '<a>' )
78 href: fb.title.getUrl(),
85 $bugNoteLink = $( '<a>' ).attr( { href: '#' } ).click( function () {
89 $bugsListLink = $( '<a>' ).attr( {
90 href: fb.bugsListLink,
94 // TODO: Use a stylesheet instead of these inline styles
96 $( '<div style="position: relative;"></div>' ).append(
97 $( '<div class="feedback-mode feedback-form"></div>' ).append(
98 $( '<small>' ).append(
100 'feedback-bugornote',
102 fb.title.getNameText(),
103 $feedbackPageLink.clone()
106 $( '<div style="margin-top: 1em;"></div>' ).append(
107 mw.msg( 'feedback-subject' ),
109 $( '<input type="text" class="feedback-subject" name="subject" maxlength="60" style="width: 99%;"/>' )
111 $( '<div style="margin-top: 0.4em;"></div>' ).append(
112 mw.msg( 'feedback-message' ),
114 $( '<textarea name="message" class="feedback-message" style="width: 99%;" rows="5" cols="60"></textarea>' )
117 $( '<div class="feedback-mode feedback-bugs"></div>' ).append(
118 $( '<p>' ).msg( 'feedback-bugcheck', $bugsListLink )
120 $( '<div class="feedback-mode feedback-submitting" style="text-align: center; margin: 3em 0;"></div>' ).append(
121 mw.msg( 'feedback-adding' ),
123 $( '<span class="feedback-spinner"></span>' )
125 $( '<div class="feedback-mode feedback-thanks" style="text-align: center; margin:1em"></div>' ).msg(
126 'feedback-thanks', fb.title.getNameText(), $feedbackPageLink.clone()
128 $( '<div class="feedback-mode feedback-error" style="position: relative;"></div>' ).append(
129 $( '<div class="feedback-error-msg style="color: #990000; margin-top: 0.4em;"></div>' )
133 // undo some damage from dialog css
134 this.$dialog.find( 'a' ).css( {
138 this.$dialog.dialog({
141 title: mw.msg( this.dialogTitleMessageKey ),
146 this.subjectInput = this.$dialog.find( 'input.feedback-subject' ).get(0);
147 this.messageInput = this.$dialog.find( 'textarea.feedback-message' ).get(0);
151 display: function ( s ) {
152 this.$dialog.dialog( { buttons:{} } ); // hide the buttons
153 this.$dialog.find( '.feedback-mode' ).hide(); // hide everything
154 this.$dialog.find( '.feedback-' + s ).show(); // show the desired div
157 displaySubmitting: function () {
158 this.display( 'submitting' );
161 displayBugs: function () {
164 this.display( 'bugs' );
165 bugsButtons[ mw.msg( 'feedback-bugnew' ) ] = function () {
166 window.open( fb.bugsLink, '_blank' );
168 bugsButtons[ mw.msg( 'feedback-cancel' ) ] = function () {
171 this.$dialog.dialog( {
176 displayThanks: function () {
179 this.display( 'thanks' );
180 closeButton[ mw.msg( 'feedback-close' ) ] = function () {
181 fb.$dialog.dialog( 'close' );
183 this.$dialog.dialog( {
189 * Display the feedback form
190 * @param {Object} optional prefilled contents for the feedback form. Object with properties:
194 displayForm: function ( contents ) {
197 this.subjectInput.value = ( contents && contents.subject ) ? contents.subject : '';
198 this.messageInput.value = ( contents && contents.message ) ? contents.message : '';
200 this.display( 'form' );
202 // Set up buttons for dialog box. We have to do it the hard way since the json keys are localized
203 formButtons[ mw.msg( 'feedback-submit' ) ] = function () {
206 formButtons[ mw.msg( 'feedback-cancel' ) ] = function () {
209 this.$dialog.dialog( { buttons: formButtons } ); // put the buttons back
212 displayError: function ( message ) {
215 this.display( 'error' );
216 this.$dialog.find( '.feedback-error-msg' ).msg( message );
217 closeButton[ mw.msg( 'feedback-close' ) ] = function () {
218 fb.$dialog.dialog( 'close' );
220 this.$dialog.dialog( { buttons: closeButton } );
223 cancel: function () {
224 this.$dialog.dialog( 'close' );
227 submit: function () {
228 var subject, message,
231 function ok( result ) {
232 if ( result.edit !== undefined ) {
233 if ( result.edit.result === 'Success' ) {
236 // unknown API result
237 fb.displayError( 'feedback-error1' );
241 fb.displayError( 'feedback-error2' );
246 // ajax request failed
247 fb.displayError( 'feedback-error3' );
250 // Get the values to submit.
251 subject = this.subjectInput.value;
253 // We used to include "mw.html.escape( navigator.userAgent )" but there are legal issues
254 // with posting this without their explicit consent
255 message = this.messageInput.value;
256 if ( message.indexOf( '~~~' ) === -1 ) {
260 this.displaySubmitting();
262 this.api.newSection( this.title, subject, message, ok, err );
266 * Modify the display form, and then open it, focusing interface on the subject.
267 * @param {Object} optional prefilled contents for the feedback form. Object with properties:
271 launch: function ( contents ) {
272 this.displayForm( contents );
273 this.$dialog.dialog( 'open' );
274 this.subjectInput.focus();
279 }( mediaWiki, jQuery ) );