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.
24 * var feedback = new mw.Feedback( api, myFeedbackPageTitle );
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
, $, undefined ) {
33 * Thingy for collecting user feedback on a wiki page
34 * @param {mw.api} api properly configured to talk to this wiki
35 * @param {mw.Title} the title of the page where you collect feedback
36 * @param {String} optional - message key for the title of the dialog box
38 mw
.Feedback = function( api
, feedbackTitle
, dialogTitleMessageKey
) {
41 this.feedbackTitle
= feedbackTitle
;
42 this.dialogTitleMessageKey
= dialogTitleMessageKey
;
43 if ( this.dialogTitleMessageKey
=== undefined ) {
44 this.dialogTitleMessageKey
= 'feedback-submit';
49 mw
.Feedback
.prototype = {
53 // Set up buttons for dialog box. We have to do it the hard way since the json keys are localized
55 _this
.buttons
[ mw
.msg( 'feedback-cancel' ) ] = function() { _this
.cancel(); };
56 _this
.buttons
[ mw
.msg( 'feedback-submit' ) ] = function() { _this
.submit(); };
58 var $feedbackPageLink
= $j( '<a></a>' ).attr( { 'href': _this
.feedbackTitle
.getUrl(), 'target': '_blank' } );
60 $( '<div style="position:relative;"></div>' ).append(
61 $( '<div class="feedback-mode feedback-form"></div>' ).append(
62 $( '<div style="margin-top:0.4em;"></div>' ).append(
63 $( '<small></small>' ).msg( 'feedback-note',
64 _this
.feedbackTitle
.getNameText(),
67 $( '<div style="margin-top:1em;"></div>' ).append(
68 mw
.msg( 'feedback-subject' ),
70 $( '<input type="text" class="feedback-subject" name="subject" maxlength="60" style="width:99%;"/>' )
72 $( '<div style="margin-top:0.4em;"></div>' ).append(
73 mw
.msg( 'feedback-message' ),
75 $( '<textarea name="message" class="feedback-message" style="width:99%;" rows="5" cols="60"></textarea>' )
78 $( '<div class="feedback-mode feedback-submitting" style="text-align:center;margin:3em 0;"></div>' ).append(
79 mw
.msg( 'feedback-adding' ),
81 $( '<img src="http://upload.wikimedia.org/wikipedia/commons/4/42/Loading.gif" />' )
83 $( '<div class="feedback-mode feedback-error" style="position:relative;"></div>' ).append(
84 $( '<div class="feedback-error-msg style="color:#990000;margin-top:0.4em;"></div>' )
90 title
: mw
.msg( this.dialogTitleMessageKey
),
92 buttons
: _this
.buttons
95 this.subjectInput
= this.$dialog
.find( 'input.feedback-subject' ).get(0);
96 this.messageInput
= this.$dialog
.find( 'textarea.feedback-message' ).get(0);
100 display: function( s
) {
101 this.$dialog
.dialog( { buttons
:{} } ); // hide the buttons
102 this.$dialog
.find( '.feedback-mode' ).hide(); // hide everything
103 this.$dialog
.find( '.feedback-' + s
).show(); // show the desired div
106 displaySubmitting: function() {
107 this.display( 'submitting' );
111 * Display the feedback form
112 * @param {Object} optional prefilled contents for the feedback form. Object with properties:
116 displayForm: function( contents
) {
117 this.subjectInput
.value
= (contents
&& contents
.subject
) ? contents
.subject
: '';
118 this.messageInput
.value
= (contents
&& contents
.message
) ? contents
.message
: '';
120 this.display( 'form' );
121 this.$dialog
.dialog( { buttons
: this.buttons
} ); // put the buttons back
124 displayError: function( message
) {
125 this.display( 'error' );
126 this.$dialog
.find( '.feedback-error-msg' ).msg( message
);
130 this.$dialog
.dialog( 'close' );
136 // get the values to submit
137 var subject
= this.subjectInput
.value
;
139 var message
= "<small>User agent: " + navigator
.userAgent
+ "</small>\n\n"
140 + this.messageInput
.value
;
141 if ( message
.indexOf( '~~~' ) == -1 ) {
145 this.displaySubmitting();
147 var ok = function( result
) {
148 if ( result
.edit
!== undefined ) {
149 if ( result
.edit
.result
=== 'Success' ) {
150 _this
.$dialog
.dialog( 'close' ); // edit complete, close dialog box
152 _this
.displayError( 'feedback-error1' ); // unknown API result
155 displayError( 'feedback-error2' ); // edit failed
159 var err = function( code
, info
) {
160 displayError( 'feedback-error3' ); // ajax request failed
163 this.api
.newSection( this.feedbackTitle
, subject
, message
, ok
, err
);
165 }, // close submit button function
169 * Modify the display form, and then open it, focusing interface on the subject.
170 * @param {Object} optional prefilled contents for the feedback form. Object with properties:
174 launch: function( contents
) {
175 this.displayForm( contents
);
176 this.$dialog
.dialog( 'open' );
177 this.subjectInput
.focus();
183 } )( window
.mediaWiki
, jQuery
);