change args for feedback and api -- all optional, in array.
[lhc/web/wiklou.git] / resources / mediawiki / mediawiki.feedback.js
1 /**
2 * mediawiki.Feedback
3 *
4 * @author Ryan Kaldari, 2010
5 * @author Neil Kandalgaonkar, 2010-11
6 * @since 1.19
7 *
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.
15 *
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.
19 *
20 * Not compatible with LiquidThreads.
21 *
22 * How to use it:
23 *
24 * var feedback = new mw.Feedback( api, myFeedbackPageTitle );
25 * $( '#myButton' ).click( function() { feedback.launch(); } );
26 *
27 * You can also launch the feedback form with a prefilled subject and body.
28 * See the docs for the launch() method.
29 */
30 ( function( mw, $, undefined ) {
31
32 /**
33 * Thingy for collecting user feedback on a wiki page
34 * @param {Array} options -- optional, all properties optional.
35 * api: {mw.Api} if omitted, will just create a standard API
36 * title: {mw.Title} the title of the page where you collect feedback. Defaults to "Feedback".
37 * dialogTitleMessageKey: {String} message key for the title of the dialog box
38 */
39 mw.Feedback = function( options ) {
40
41 if ( options === undefined ) {
42 options = {};
43 }
44
45 if ( options.api === undefined ) {
46 options.api = new mw.Api();
47 }
48
49 if ( options.title === undefined ) {
50 options.title = new mw.Title( 'Feedback' );
51 }
52
53 if ( options.dialogTitleMessageKey === undefined ) {
54 options.dialogTitleMessageKey = 'feedback-submit';
55 }
56
57 this.api = options.api;
58 this.feedbackTitle = options.title;
59 this.dialogTitleMessageKey = options.dialogTitleMessageKey;
60 this.setup();
61 };
62
63 mw.Feedback.prototype = {
64 setup: function() {
65 var _this = this;
66
67 // Set up buttons for dialog box. We have to do it the hard way since the json keys are localized
68 _this.buttons = {};
69 _this.buttons[ mw.msg( 'feedback-cancel' ) ] = function() { _this.cancel(); };
70 _this.buttons[ mw.msg( 'feedback-submit' ) ] = function() { _this.submit(); };
71
72 var $feedbackPageLink = $j( '<a></a>' ).attr( { 'href': _this.feedbackTitle.getUrl(), 'target': '_blank' } );
73 this.$dialog =
74 $( '<div style="position:relative;"></div>' ).append(
75 $( '<div class="feedback-mode feedback-form"></div>' ).append(
76 $( '<div style="margin-top:0.4em;"></div>' ).append(
77 $( '<small></small>' ).msg( 'feedback-note',
78 _this.feedbackTitle.getNameText(),
79 $feedbackPageLink )
80 ),
81 $( '<div style="margin-top:1em;"></div>' ).append(
82 mw.msg( 'feedback-subject' ),
83 $( '<br/>' ),
84 $( '<input type="text" class="feedback-subject" name="subject" maxlength="60" style="width:99%;"/>' )
85 ),
86 $( '<div style="margin-top:0.4em;"></div>' ).append(
87 mw.msg( 'feedback-message' ),
88 $( '<br/>' ),
89 $( '<textarea name="message" class="feedback-message" style="width:99%;" rows="5" cols="60"></textarea>' )
90 )
91 ),
92 $( '<div class="feedback-mode feedback-submitting" style="text-align:center;margin:3em 0;"></div>' ).append(
93 mw.msg( 'feedback-adding' ),
94 $( '<br/>' ),
95 $( '<img src="http://upload.wikimedia.org/wikipedia/commons/4/42/Loading.gif" />' )
96 ),
97 $( '<div class="feedback-mode feedback-error" style="position:relative;"></div>' ).append(
98 $( '<div class="feedback-error-msg style="color:#990000;margin-top:0.4em;"></div>' )
99
100 )
101 ).dialog({
102 width: 500,
103 autoOpen: false,
104 title: mw.msg( this.dialogTitleMessageKey ),
105 modal: true,
106 buttons: _this.buttons
107 });
108
109 this.subjectInput = this.$dialog.find( 'input.feedback-subject' ).get(0);
110 this.messageInput = this.$dialog.find( 'textarea.feedback-message' ).get(0);
111 this.displayForm();
112 },
113
114 display: function( s ) {
115 this.$dialog.dialog( { buttons:{} } ); // hide the buttons
116 this.$dialog.find( '.feedback-mode' ).hide(); // hide everything
117 this.$dialog.find( '.feedback-' + s ).show(); // show the desired div
118 },
119
120 displaySubmitting: function() {
121 this.display( 'submitting' );
122 },
123
124 /**
125 * Display the feedback form
126 * @param {Object} optional prefilled contents for the feedback form. Object with properties:
127 * subject: {String}
128 * message: {String}
129 */
130 displayForm: function( contents ) {
131 this.subjectInput.value = (contents && contents.subject) ? contents.subject : '';
132 this.messageInput.value = (contents && contents.message) ? contents.message : '';
133
134 this.display( 'form' );
135 this.$dialog.dialog( { buttons: this.buttons } ); // put the buttons back
136 },
137
138 displayError: function( message ) {
139 this.display( 'error' );
140 this.$dialog.find( '.feedback-error-msg' ).msg( message );
141 },
142
143 cancel: function() {
144 this.$dialog.dialog( 'close' );
145 },
146
147 submit: function() {
148 var _this = this;
149
150 // get the values to submit
151 var subject = this.subjectInput.value;
152
153 var message = "<small>User agent: " + navigator.userAgent + "</small>\n\n"
154 + this.messageInput.value;
155 if ( message.indexOf( '~~~' ) == -1 ) {
156 message += " ~~~~";
157 }
158
159 this.displaySubmitting();
160
161 var ok = function( result ) {
162 if ( result.edit !== undefined ) {
163 if ( result.edit.result === 'Success' ) {
164 _this.$dialog.dialog( 'close' ); // edit complete, close dialog box
165 } else {
166 _this.displayError( 'feedback-error1' ); // unknown API result
167 }
168 } else {
169 displayError( 'feedback-error2' ); // edit failed
170 }
171 };
172
173 var err = function( code, info ) {
174 displayError( 'feedback-error3' ); // ajax request failed
175 };
176
177 this.api.newSection( this.feedbackTitle, subject, message, ok, err );
178
179 }, // close submit button function
180
181
182 /**
183 * Modify the display form, and then open it, focusing interface on the subject.
184 * @param {Object} optional prefilled contents for the feedback form. Object with properties:
185 * subject: {String}
186 * message: {String}
187 */
188 launch: function( contents ) {
189 this.displayForm( contents );
190 this.$dialog.dialog( 'open' );
191 this.subjectInput.focus();
192 }
193
194 };
195
196
197 } )( window.mediaWiki, jQuery );