Index: trunk/extensions/Survey/Survey.php |
— | — | @@ -149,6 +149,7 @@ |
150 | 150 | 'survey-special-label-button', |
151 | 151 | 'survey-special-remove', |
152 | 152 | 'survey-special-remove-confirm', |
| 153 | + 'survey-special-label-answers' |
153 | 154 | ) |
154 | 155 | ); |
155 | 156 | |
Index: trunk/extensions/Survey/Survey.i18n.php |
— | — | @@ -83,6 +83,7 @@ |
84 | 84 | 'survey-special-label-header' => 'Text to display above the survey', |
85 | 85 | 'survey-special-label-footer' => 'Text to display below the survey', |
86 | 86 | 'survey-special-label-thanks' => 'Thanks message to display after submission of the survey', |
| 87 | + 'survey-special-label-answers' => 'Available answers, one per line.', |
87 | 88 | |
88 | 89 | // Survey jQuery |
89 | 90 | 'survey-jquery-submit' => 'Submit', |
Index: trunk/extensions/Survey/specials/SpecialSurvey.php |
— | — | @@ -126,12 +126,20 @@ |
127 | 127 | $questionDbId = $questionId; |
128 | 128 | } |
129 | 129 | |
| 130 | + $answers = array_filter( |
| 131 | + explode( "\n", $wgRequest->getText( "survey-question-answers-$questionId" ) ), |
| 132 | + function( $line ) { |
| 133 | + return trim( $line ) != ''; |
| 134 | + } |
| 135 | + ); |
| 136 | + |
130 | 137 | $question = new SurveyQuestion( array( |
131 | 138 | 'id' => $questionDbId, |
132 | 139 | 'removed' => 0, |
133 | 140 | 'text' => $wgRequest->getText( "survey-question-text-$questionId" ), |
134 | 141 | 'type' => $wgRequest->getInt( "survey-question-type-$questionId" ), |
135 | | - 'required' => $wgRequest->getCheck( "survey-question-required-$questionId" ) |
| 142 | + 'required' => $wgRequest->getCheck( "survey-question-required-$questionId" ), |
| 143 | + 'answers' => $answers |
136 | 144 | ) ); |
137 | 145 | |
138 | 146 | return $question; |
— | — | @@ -224,12 +232,7 @@ |
225 | 233 | foreach ( $survey->getQuestions() as /* SurveyQuestion */ $question ) { |
226 | 234 | $fields[] = array( |
227 | 235 | 'class' => 'SurveyQuestionField', |
228 | | - 'options' => array( |
229 | | - 'required' => $question->getField( 'required' ), |
230 | | - 'text' => $question->getField( 'text' ), |
231 | | - 'type' => $question->getField( 'type' ), |
232 | | - 'id' => $question->getId(), |
233 | | - ) |
| 236 | + 'options' => $question->toArray() |
234 | 237 | ); |
235 | 238 | } |
236 | 239 | |
Index: trunk/extensions/Survey/includes/SurveyQuestion.php |
— | — | @@ -83,6 +83,24 @@ |
84 | 84 | } |
85 | 85 | |
86 | 86 | /** |
| 87 | + * Returns a list of default field values. |
| 88 | + * field name => field value |
| 89 | + * |
| 90 | + * @since 0.1 |
| 91 | + * |
| 92 | + * @return array |
| 93 | + */ |
| 94 | + public static function getDefaults() { |
| 95 | + return array( |
| 96 | + 'text' => '', |
| 97 | + 'type' => self::$TYPE_TEXT, |
| 98 | + 'required' => false, |
| 99 | + 'answers' => array(), |
| 100 | + 'removed' => false, |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
87 | 105 | * Unserialization method for survey question data passed as a multi-value API parameter. |
88 | 106 | * Uses base64 and replaces padding = by !, so the values does not contain any = or |. |
89 | 107 | * |
Index: trunk/extensions/Survey/resources/ext.survey.special.survey.js |
— | — | @@ -5,8 +5,57 @@ |
6 | 6 | * @licence GNU GPL v3 or later |
7 | 7 | * @author Jeroen De Dauw <jeroendedauw at gmail dot com> |
8 | 8 | */ |
9 | | -(function( $, mw ) { $( document ).ready( function() { |
10 | 9 | |
| 10 | +(function( $, mw, survey ) { |
| 11 | + |
| 12 | + survey.answerSelector = function( options ) { |
| 13 | + var _this = this; |
| 14 | + |
| 15 | + var defaults = { |
| 16 | + 'visible': true, |
| 17 | + 'answers': [] |
| 18 | + }; |
| 19 | + |
| 20 | + options = $.extend( defaults, options ); |
| 21 | + |
| 22 | + this.$div = $( '<div />' ).html( '' ); |
| 23 | + |
| 24 | + this.$div.append( $( '<p />' ).text( mw.msg( 'survey-special-label-answers' ) ) ); |
| 25 | + |
| 26 | + this.$div.append( $( '<textarea />' ).attr( options.attr ).val( options.answers.join( '\n' ) ) ); |
| 27 | + |
| 28 | + this.setVisible( options.visible ); |
| 29 | + }; |
| 30 | + |
| 31 | + survey.answerSelector.prototype = { |
| 32 | + |
| 33 | + getHtml: function() { |
| 34 | + return this.$div; |
| 35 | + }, |
| 36 | + |
| 37 | + show: function() { |
| 38 | + this.$div.show(); |
| 39 | + }, |
| 40 | + |
| 41 | + hide: function() { |
| 42 | + this.$div.hide(); |
| 43 | + }, |
| 44 | + |
| 45 | + setVisible: function( visible ) { |
| 46 | + if ( visible ) { |
| 47 | + this.show(); |
| 48 | + } |
| 49 | + else { |
| 50 | + this.hide(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + }; |
| 55 | + |
| 56 | +} )( jQuery, window.mediaWiki, window.survey ); |
| 57 | + |
| 58 | +(function( $, mw, survey ) { $( document ).ready( function() { |
| 59 | + |
11 | 60 | var _this = this; |
12 | 61 | |
13 | 62 | var $table = null; |
— | — | @@ -25,7 +74,7 @@ |
26 | 75 | ) ); |
27 | 76 | |
28 | 77 | $tr.append( $( '<td />' ).attr( { 'class': 'mw-input' } ).html( |
29 | | - getQuestionInput( { 'id': 'new' } ) |
| 78 | + getQuestionInput( { 'id': 'new', 'answers': [], 'type': 0 } ) |
30 | 79 | ).append( $( '<button />' ).button( { 'label': mw.msg( 'survey-special-label-button' ) } ) |
31 | 80 | .click( function() { onAddQuestionRequest(); return false; } ) |
32 | 81 | ) ); |
— | — | @@ -69,6 +118,17 @@ |
70 | 119 | 'border': '1px solid black', |
71 | 120 | 'id': 'survey-question-div-' + question.id |
72 | 121 | } ); |
| 122 | + |
| 123 | + var answerSelector = new survey.answerSelector( { |
| 124 | + 'visible': survey.question.typeHasAnswers( parseInt( question.type ) ), |
| 125 | + 'attr': { |
| 126 | + 'rows': 2, |
| 127 | + 'cols': 80, |
| 128 | + 'id': 'survey-question-answers-' + question.id, |
| 129 | + 'name': 'survey-question-answers-' + question.id |
| 130 | + }, |
| 131 | + 'answers': question.answers |
| 132 | + } ); |
73 | 133 | |
74 | 134 | $input.append( $( '<label />' ).attr( { |
75 | 135 | 'for': 'survey-question-text-' + question.id |
— | — | @@ -89,10 +149,16 @@ |
90 | 150 | 'for': 'survey-question-type-' + question.id |
91 | 151 | } ).text( mw.msg( 'survey-special-label-type' ) ) ); |
92 | 152 | |
93 | | - $input.append( survey.question.getTypeSelector( question.type, { |
94 | | - 'id': 'survey-question-type-' + question.id, |
95 | | - 'name': 'survey-question-type-' + question.id |
96 | | - } ) ); |
| 153 | + $input.append( survey.question.getTypeSelector( |
| 154 | + question.type, |
| 155 | + { |
| 156 | + 'id': 'survey-question-type-' + question.id, |
| 157 | + 'name': 'survey-question-type-' + question.id |
| 158 | + }, |
| 159 | + function( newValue ) { |
| 160 | + answerSelector.setVisible( survey.question.typeHasAnswers( parseInt( newValue ) ) ); |
| 161 | + } |
| 162 | + ) ); |
97 | 163 | |
98 | 164 | $required = $( '<input />' ).attr( { |
99 | 165 | 'id': 'survey-question-required-' + question.id, |
— | — | @@ -110,6 +176,8 @@ |
111 | 177 | 'for': 'survey-question-required-' + question.id |
112 | 178 | } ).text( mw.msg( 'survey-special-label-required' ) ) ); |
113 | 179 | |
| 180 | + $input.append( answerSelector.getHtml() ); |
| 181 | + |
114 | 182 | return $input; |
115 | 183 | }; |
116 | 184 | |
— | — | @@ -122,7 +190,8 @@ |
123 | 191 | 'text': $( '#survey-question-text-new' ).val(), |
124 | 192 | 'required': !!$( '#survey-question-required-new' ).attr( 'checked' ), |
125 | 193 | 'type': $( '#survey-question-type-new' ).val(), |
126 | | - 'id': 'new-' + newQuestionNr++ |
| 194 | + 'id': 'new-' + newQuestionNr++, |
| 195 | + 'answers': $( '#survey-question-answers-new' ).val().split( '\n' ) |
127 | 196 | } ); |
128 | 197 | $( '#survey-question-text-new' ).focus().select(); |
129 | 198 | $( 'html' ).animate( { scrollTop: $( document ).height() }, 'fast' ); |
— | — | @@ -146,6 +215,7 @@ |
147 | 216 | 'required': $this.attr( 'data-required' ) == '1', |
148 | 217 | 'id': $this.attr( 'data-id' ), |
149 | 218 | 'type': $this.attr( 'data-type' ), |
| 219 | + 'answers': eval( $this.attr( 'data-answers' ) ), |
150 | 220 | } ); |
151 | 221 | } ); |
152 | 222 | |
— | — | @@ -156,4 +226,4 @@ |
157 | 227 | |
158 | 228 | setup(); |
159 | 229 | |
160 | | -} ); })( jQuery, window.mediaWiki ); |
\ No newline at end of file |
| 230 | +} ); })( jQuery, window.mediaWiki, window.survey ); |
\ No newline at end of file |
Index: trunk/extensions/Survey/resources/jquery.survey.js |
— | — | @@ -49,61 +49,83 @@ |
50 | 50 | var type = survey.question.type; |
51 | 51 | |
52 | 52 | var $input; |
| 53 | + var id = 'survey-question-' + question.id; |
53 | 54 | |
54 | 55 | switch ( question.type ) { |
55 | 56 | case type.TEXT: default: |
56 | 57 | $input = $( '<input />' ).attr( { |
57 | | - 'id': 'survey-question-' + question.id, |
| 58 | + 'id': id, |
58 | 59 | 'class': 'survey-question survey-text' |
59 | 60 | } ); |
60 | 61 | break; |
61 | 62 | case type.NUMBER: |
62 | 63 | $input = $( '<input />' ).numeric().attr( { |
63 | | - 'id': 'survey-question-' + question.id, |
| 64 | + 'id': id, |
64 | 65 | 'class': 'survey-question survey-number', |
65 | 66 | 'size': 7 |
66 | 67 | } ); |
67 | 68 | break; |
68 | 69 | case type.SELECT: |
69 | 70 | $input = survey.htmlSelect( question.answers, 0, { |
70 | | - 'id': 'survey-question-' + question.id, |
| 71 | + 'id': id, |
71 | 72 | 'class': 'survey-question survey-select' |
72 | 73 | } ); |
73 | 74 | break; |
74 | 75 | case type.RADIO: |
75 | | - // TODO |
76 | | - $input = $( '<input />' ).attr( { |
77 | | - 'id': 'survey-question-' + question.id, |
78 | | - 'class': 'survey-question' |
79 | | - } ); |
| 76 | + $input = survey.htmlRadio( |
| 77 | + question.answers, |
| 78 | + 0, |
| 79 | + id, |
| 80 | + { |
| 81 | + 'id': id, |
| 82 | + 'class': 'survey-question survey-radio' |
| 83 | + } |
| 84 | + ); |
80 | 85 | break; |
81 | 86 | case type.TEXTAREA: |
82 | 87 | $input = $( '<textarea />' ).attr( { |
83 | | - 'id': 'survey-question-' + question.id, |
| 88 | + 'id': id, |
84 | 89 | 'class': 'survey-question survey-textarea', |
85 | 90 | 'cols': 80, |
86 | 91 | 'rows': 2 |
87 | 92 | } ); |
88 | 93 | break; |
| 94 | + case type.CHECK: |
| 95 | + $input = $( '<input />' ).attr( { |
| 96 | + 'id': id, |
| 97 | + 'type': 'checkbox', |
| 98 | + 'class': 'survey-question survey-check', |
| 99 | + } ); |
| 100 | + break; |
89 | 101 | } |
90 | 102 | |
91 | 103 | $input.data( 'question-id', question.id ); |
92 | 104 | |
93 | 105 | this.inputs.push( $input ); |
94 | 106 | |
95 | | - return $input; |
96 | | - }; |
97 | | - |
98 | | - this.getSurveyQuestion = function( question ) { |
99 | | - $q = $( '<div />' ); |
| 107 | + $q = $( '<div />' ).html( $input ); |
100 | 108 | |
101 | | - $q.append( $( '<p />' ).text( question.text ) ); |
| 109 | + if ( question.type == type.CHECK ) { |
| 110 | + $q.append( $( '<label />' ).text( question.text ).attr( 'for', id ) ); |
| 111 | + } |
| 112 | + else { |
| 113 | + $q.prepend( $( '<p />' ).text( question.text ) ); |
| 114 | + } |
102 | 115 | |
103 | | - $q.append( this.getQuestionInput( question ) ) |
104 | | - |
105 | 116 | return $q; |
106 | 117 | }; |
107 | 118 | |
| 119 | + this.getSurveyQuestion = function( question ) { |
| 120 | + if ( survey.question.typeHasAnswers( question.type ) |
| 121 | + && question.answers.length == 0 ) { |
| 122 | + survey.log( 'getSurveyQuestion: no answers for: ' + question.id ); |
| 123 | + return ''; |
| 124 | + } |
| 125 | + else { |
| 126 | + return this.getQuestionInput( question ); |
| 127 | + } |
| 128 | + }; |
| 129 | + |
108 | 130 | this.getSurveyQuestions = function( questions ) { |
109 | 131 | $questions = $( '<div />' ); |
110 | 132 | |
Index: trunk/extensions/Survey/resources/ext.survey.js |
— | — | @@ -34,7 +34,7 @@ |
35 | 35 | } |
36 | 36 | }; |
37 | 37 | |
38 | | - this.htmlSelect = function( options, value, attributes ) { |
| 38 | + this.htmlSelect = function( options, value, attributes, onChangeCallback ) { |
39 | 39 | $select = $( '<select />' ).attr( attributes ); |
40 | 40 | |
41 | 41 | for ( message in options ) { |
— | — | @@ -47,9 +47,40 @@ |
48 | 48 | $select.append( $( '<option />' ).text( message ).attr( attribs ) ); |
49 | 49 | } |
50 | 50 | |
| 51 | + if ( typeof onChangeCallback !== 'undefined' ) { |
| 52 | + $select.change( function() { onChangeCallback( $( this ).val() ) } ); |
| 53 | + } |
| 54 | + |
51 | 55 | return $select; |
52 | 56 | }; |
53 | 57 | |
| 58 | + this.htmlRadio = function( options, value, name, attributes ) { |
| 59 | + var $radio = $( '<div />' ).attr( attributes ); |
| 60 | + $radio.html( '' ); |
| 61 | + |
| 62 | + for ( message in options ) { |
| 63 | + var value = options[message]; |
| 64 | + var id = name + value; |
| 65 | + |
| 66 | + $input = $( '<input />' ).attr( { |
| 67 | + 'id': id, |
| 68 | + 'type': 'radio', |
| 69 | + 'name': name, |
| 70 | + 'value': value |
| 71 | + } ); |
| 72 | + |
| 73 | + if ( value === options[message] ) { |
| 74 | + $input.attr( 'checked', 'checked' ); |
| 75 | + } |
| 76 | + |
| 77 | + $radio.append( $input ); |
| 78 | + $radio.append( $( '<label />' ).attr( 'for', id ).text( message ) ); |
| 79 | + $radio.append( $( '<br />' ) ); |
| 80 | + } |
| 81 | + |
| 82 | + return $radio; |
| 83 | + }; |
| 84 | + |
54 | 85 | this.question = new( function() { |
55 | 86 | |
56 | 87 | this.type = new( function() { |
— | — | @@ -61,7 +92,11 @@ |
62 | 93 | this.CHECK = 5; |
63 | 94 | } ); |
64 | 95 | |
65 | | - this.getTypeSelector = function( value, attributes ) { |
| 96 | + this.typeHasAnswers = function( t ) { |
| 97 | + return $.inArray( t, [ survey.question.type.RADIO, survey.question.type.SELECT ] ) !== -1; |
| 98 | + }; |
| 99 | + |
| 100 | + this.getTypeSelector = function( value, attributes, onChangeCallback ) { |
66 | 101 | var options = []; |
67 | 102 | |
68 | 103 | var types = { |
— | — | @@ -77,7 +112,7 @@ |
78 | 113 | options[survey.msg( 'survey-question-type-' + msg )] = types[msg]; |
79 | 114 | } |
80 | 115 | |
81 | | - return survey.htmlSelect( options, parseInt( value ), attributes ); |
| 116 | + return survey.htmlSelect( options, parseInt( value ), attributes, onChangeCallback ); |
82 | 117 | }; |
83 | 118 | |
84 | 119 | } ); |