r96432 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96431‎ | r96432 | r96433 >
Date:13:46, 7 September 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
work on survey ui
Modified paths:
  • /trunk/extensions/Survey/Survey.i18n.php (modified) (history)
  • /trunk/extensions/Survey/Survey.php (modified) (history)
  • /trunk/extensions/Survey/api/ApiQuerySurveys.php (modified) (history)
  • /trunk/extensions/Survey/includes/SurveyQuestion.php (modified) (history)
  • /trunk/extensions/Survey/includes/SurveyTag.php (modified) (history)
  • /trunk/extensions/Survey/resources/ext.survey.js (modified) (history)
  • /trunk/extensions/Survey/resources/jquery.numeric.js (added) (history)
  • /trunk/extensions/Survey/resources/jquery.survey.css (added) (history)
  • /trunk/extensions/Survey/resources/jquery.survey.js (modified) (history)

Diff [purge]

Index: trunk/extensions/Survey/Survey.php
@@ -105,9 +105,8 @@
106106 $wgGroupPermissions['sysop' ]['surveysubmit'] = true;
107107
108108 $moduleTemplate = array(
109 - 'localBasePath' => dirname( __FILE__ ),
110 - 'remoteBasePath' => ( $wgExtensionAssetsPath === false ? $wgScriptPath . '/extensions' : $wgExtensionAssetsPath )
111 - . '/Survey/resources'
 109+ 'localBasePath' => dirname( __FILE__ ) . '/resources',
 110+ 'remoteExtPath' => 'Survey/resources'
112111 );
113112
114113 $wgResourceModules['ext.survey'] = $moduleTemplate + array(
@@ -132,7 +131,7 @@
133132 'ext.survey.special.survey.js'
134133 ),
135134 'styles' => array(
136 - //'ext.survey.special.survey.css'
 135+ 'ext.survey.special.survey.css'
137136 ),
138137 'dependencies' => array( 'ext.survey', 'jquery.ui.button' ),
139138 'messages' => array(
@@ -140,6 +139,7 @@
141140 'survey-question-type-number',
142141 'survey-question-type-select',
143142 'survey-question-type-radio',
 143+ 'survey-question-type-textarea',
144144 'survey-question-label-nr',
145145 'survey-special-label-required',
146146 'survey-special-label-type',
@@ -151,18 +151,24 @@
152152 )
153153 );
154154
 155+$wgResourceModules['ext.survey.numeric'] = $moduleTemplate + array(
 156+ 'scripts' => array(
 157+ 'jquery.numeric.js'
 158+ )
 159+);
 160+
155161 $wgResourceModules['ext.survey.jquery'] = $moduleTemplate + array(
156162 'scripts' => array(
157163 'fancybox/jquery.fancybox-1.3.4.js',
158164 'jquery.survey.js'
159165 ),
160166 'styles' => array(
161 - // This file makes the RL go mad for some reason, so for now load it the old fashioned way.
162 - //'fancybox/jquery.fancybox-1.3.4.css',
 167+ 'jquery.survey.css',
 168+ 'fancybox/jquery.fancybox-1.3.4.css'
163169 ),
164 - 'dependencies' => array( 'ext.survey' ),
 170+ 'dependencies' => array( 'ext.survey', 'jquery.ui.button', 'ext.survey.numeric' ),
165171 'messages' => array(
166 -
 172+ 'survey-jquery-submit',
167173 )
168174 );
169175
Index: trunk/extensions/Survey/Survey.i18n.php
@@ -65,6 +65,7 @@
6666 'survey-question-type-number' => 'Number',
6767 'survey-question-type-select' => 'Dropdown menu',
6868 'survey-question-type-radio' => 'Radio boxes',
 69+ 'survey-question-type-textarea' => 'Text area',
6970 'survey-question-label-nr' => 'Question #$1',
7071 'survey-special-label-required' => 'Question is required',
7172 'survey-special-label-type' => 'Question type',
@@ -74,4 +75,7 @@
7576 'survey-special-label-header' => 'Text to display above the survey',
7677 'survey-special-label-footer' => 'Text to display below the survey',
7778 'survey-special-label-thanks' => 'Thanks message to display after submission of the survey',
 79+
 80+ // Survey jQuery
 81+ 'survey-jquery-submit' => 'Submit',
7882 );
Index: trunk/extensions/Survey/includes/SurveyTag.php
@@ -67,12 +67,6 @@
6868
6969 if ( !$loadedJs ) {
7070 $parser->getOutput()->addModules( 'ext.survey.jquery' );
71 -
72 - global $wgExtensionAssetsPath, $wgScriptPath;
73 - $parser->getOutput()->addHeadItem( Html::linkedStyle(
74 - ( $wgExtensionAssetsPath === false ? $wgScriptPath . '/extensions' : $wgExtensionAssetsPath )
75 - . '/Survey/resources/fancybox/jquery.fancybox-1.3.4.css'
76 - ) );
7771 }
7872
7973 return Html::element(
Index: trunk/extensions/Survey/includes/SurveyQuestion.php
@@ -17,6 +17,7 @@
1818 public static $TYPE_NUMBER = 1;
1919 public static $TYPE_SELECT = 2;
2020 public static $TYPE_RADIO = 3;
 21+ public static $TYPE_TEXTAREA = 4;
2122
2223 /**
2324 * @see SurveyDBClass::getDBTable()
Index: trunk/extensions/Survey/api/ApiQuerySurveys.php
@@ -39,6 +39,13 @@
4040
4141 $this->addTables( 'surveys' );
4242
 43+ $starPropPosition = array_search( '*', $params['props'] );
 44+
 45+ if ( $starPropPosition !== false ) {
 46+ unset( $params['props'][$starPropPosition] );
 47+ $params['props'] = array_merge( $params['props'], Survey::getFieldNames() );
 48+ }
 49+
4350 $fields = array_merge( array( 'id' ), $params['props'] );
4451
4552 $this->addFields( Survey::getPrefixedFields( $fields ) );
@@ -125,7 +132,7 @@
126133 ApiBase::PARAM_ISMULTI => true,
127134 ),
128135 'props' => array(
129 - ApiBase::PARAM_TYPE => Survey::getFieldNames(),
 136+ ApiBase::PARAM_TYPE => array_merge( Survey::getFieldNames(), array( '*' ) ),
130137 ApiBase::PARAM_ISMULTI => true,
131138 ApiBase::PARAM_DFLT => 'id|name|enabled'
132139 ),
Index: trunk/extensions/Survey/resources/jquery.survey.css
@@ -0,0 +1,5 @@
 2+@CHARSET "UTF-8";
 3+
 4+.survey-textarea {
 5+ width: 80%
 6+}
\ No newline at end of file
Property changes on: trunk/extensions/Survey/resources/jquery.survey.css
___________________________________________________________________
Added: svn:eol-style
17 + native
Index: trunk/extensions/Survey/resources/jquery.survey.js
@@ -19,7 +19,8 @@
2020 'format': 'json',
2121 'sunames': options.names.join( '|' ),
2222 'suincquestions': 1,
23 - 'suenabled': 1
 23+ 'suenabled': 1,
 24+ 'suprops': '*'
2425 },
2526 function( data ) {
2627 if ( data.surveys ) {
@@ -39,9 +40,13 @@
4041
4142 this.submitSurvey = function() {
4243 // TODO
 44+
 45+ // $survey.append( $( '<p />' ).text( surveyData.thanks ) );
4346 };
4447
4548 this.getQuestionInput = function( question ) {
 49+ survey.log( 'getQuestionInput: ' + question.id );
 50+
4651 var type = survey.question.type;
4752
4853 var $input;
@@ -50,9 +55,37 @@
5156 case type.TEXT: default:
5257 $input = $( '<input />' ).attr( {
5358 'id': 'survey-question-' + question.id,
 59+ 'class': 'survey-question survey-text'
 60+ } );
 61+ break;
 62+ case type.NUMBER:
 63+ $input = $( '<input />' ).numeric().attr( {
 64+ 'id': 'survey-question-' + question.id,
 65+ 'class': 'survey-question survey-number',
 66+ 'size': 7
 67+ } );
 68+ break;
 69+ case type.SELECT:
 70+ $input = survey.htmlSelect( question.answers, 0, {
 71+ 'id': 'survey-question-' + question.id,
 72+ 'class': 'survey-question survey-select'
 73+ } );
 74+ break;
 75+ case type.RADIO:
 76+ // TODO
 77+ $input = $( '<input />' ).attr( {
 78+ 'id': 'survey-question-' + question.id,
5479 'class': 'survey-question'
5580 } );
5681 break;
 82+ case type.TEXTAREA:
 83+ $input = $( '<textarea />' ).attr( {
 84+ 'id': 'survey-question-' + question.id,
 85+ 'class': 'survey-question survey-textarea',
 86+ 'cols': 80,
 87+ 'rows': 2
 88+ } );
 89+ break;
5790 }
5891
5992 return $input;
@@ -65,8 +98,6 @@
6699
67100 $q.append( this.getQuestionInput( question ) )
68101
69 - $q.append( '<hr />' );
70 -
71102 return $q;
72103 };
73104
@@ -85,8 +116,14 @@
86117
87118 $survey.append( $( '<h1 />' ).text( surveyData.name ) );
88119
 120+ $survey.append( $( '<p />' ).text( surveyData.header ) );
 121+
89122 $survey.append( this.getSurveyQuestions( surveyData.questions ) );
90123
 124+ $survey.append( $( '<button />' ).button( { label: mw.msg( 'survey-jquery-submit' ) } ) );
 125+
 126+ $survey.append( $( '<p />' ).text( surveyData.footer ) );
 127+
91128 return $survey;
92129 };
93130
@@ -97,11 +134,21 @@
98135
99136 $link = $( '<a />' ).attr( {
100137 'href': '#survey-' + surveyData.id,
101 - 'id': 'inline'
102138 } ).html( $div );
103139
104140 surveyElement.html( $link );
105 - $link.fancybox();
 141+
 142+ $link.fancybox( {
 143+// 'width' : '75%',
 144+// 'height' : '75%',
 145+ 'autoScale' : false,
 146+ 'transitionIn' : 'none',
 147+ 'transitionOut' : 'none',
 148+ 'type' : 'inline',
 149+ 'hideOnOverlayClick': false,
 150+ 'autoDimensions': true
 151+ } );
 152+
106153 $link.click();
107154 };
108155
Index: trunk/extensions/Survey/resources/ext.survey.js
@@ -59,6 +59,7 @@
6060 this.NUMBER = 1;
6161 this.SELECT = 2;
6262 this.RADIO = 3;
 63+ this.TEXTAREA = 4;
6364 } );
6465
6566 this.getTypeSelector = function( value, attributes ) {
@@ -68,7 +69,8 @@
6970 'text': survey.question.type.TEXT,
7071 'number': survey.question.type.NUMBER,
7172 'select': survey.question.type.SELECT,
72 - 'radio': survey.question.type.RADIO
 73+ 'radio': survey.question.type.RADIO,
 74+ 'textarea': survey.question.type.TEXTAREA,
7375 };
7476
7577 for ( msg in types ) {
Index: trunk/extensions/Survey/resources/jquery.numeric.js
@@ -0,0 +1,279 @@
 2+/*
 3+ *
 4+ * Copyright (c) 2006-2011 Sam Collett (http://www.texotela.co.uk)
 5+ * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 6+ * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 7+ *
 8+ * Version 1.3
 9+ * Demo: http://www.texotela.co.uk/code/jquery/numeric/
 10+ *
 11+ */
 12+(function($) {
 13+/*
 14+ * Allows only valid characters to be entered into input boxes.
 15+ * Note: fixes value when pasting via Ctrl+V, but not when using the mouse to paste
 16+ * side-effect: Ctrl+A does not work, though you can still use the mouse to select (or double-click to select all)
 17+ *
 18+ * @name numeric
 19+ * @param config { decimal : "." , negative : true }
 20+ * @param callback A function that runs if the number is not valid (fires onblur)
 21+ * @author Sam Collett (http://www.texotela.co.uk)
 22+ * @example $(".numeric").numeric();
 23+ * @example $(".numeric").numeric(","); // use , as separater
 24+ * @example $(".numeric").numeric({ decimal : "," }); // use , as separator
 25+ * @example $(".numeric").numeric({ negative : false }); // do not allow negative values
 26+ * @example $(".numeric").numeric(null, callback); // use default values, pass on the 'callback' function
 27+ *
 28+ */
 29+$.fn.numeric = function(config, callback)
 30+{
 31+ if(typeof config === 'boolean')
 32+ {
 33+ config = { decimal: config };
 34+ }
 35+ config = config || {};
 36+ // if config.negative undefined, set to true (default is to allow negative numbers)
 37+ if(typeof config.negative == "undefined") config.negative = true;
 38+ // set decimal point
 39+ var decimal = (config.decimal === false) ? "" : config.decimal || ".";
 40+ // allow negatives
 41+ var negative = (config.negative === true) ? true : false;
 42+ // callback function
 43+ var callback = typeof callback == "function" ? callback : function(){};
 44+ // set data and methods
 45+ return this.data("numeric.decimal", decimal).data("numeric.negative", negative).data("numeric.callback", callback).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur);
 46+}
 47+
 48+$.fn.numeric.keypress = function(e)
 49+{
 50+ // get decimal character and determine if negatives are allowed
 51+ var decimal = $.data(this, "numeric.decimal");
 52+ var negative = $.data(this, "numeric.negative");
 53+ // get the key that was pressed
 54+ var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
 55+ // allow enter/return key (only when in an input box)
 56+ if(key == 13 && this.nodeName.toLowerCase() == "input")
 57+ {
 58+ return true;
 59+ }
 60+ else if(key == 13)
 61+ {
 62+ return false;
 63+ }
 64+ var allow = false;
 65+ // allow Ctrl+A
 66+ if((e.ctrlKey && key == 97 /* firefox */) || (e.ctrlKey && key == 65) /* opera */) return true;
 67+ // allow Ctrl+X (cut)
 68+ if((e.ctrlKey && key == 120 /* firefox */) || (e.ctrlKey && key == 88) /* opera */) return true;
 69+ // allow Ctrl+C (copy)
 70+ if((e.ctrlKey && key == 99 /* firefox */) || (e.ctrlKey && key == 67) /* opera */) return true;
 71+ // allow Ctrl+Z (undo)
 72+ if((e.ctrlKey && key == 122 /* firefox */) || (e.ctrlKey && key == 90) /* opera */) return true;
 73+ // allow or deny Ctrl+V (paste), Shift+Ins
 74+ if((e.ctrlKey && key == 118 /* firefox */) || (e.ctrlKey && key == 86) /* opera */
 75+ || (e.shiftKey && key == 45)) return true;
 76+ // if a number was not pressed
 77+ if(key < 48 || key > 57)
 78+ {
 79+ /* '-' only allowed at start and if negative numbers allowed */
 80+ if(this.value.indexOf("-") != 0 && negative && key == 45 && (this.value.length == 0 || ($.fn.getSelectionStart(this)) == 0)) return true;
 81+ /* only one decimal separator allowed */
 82+ if(decimal && key == decimal.charCodeAt(0) && this.value.indexOf(decimal) != -1)
 83+ {
 84+ allow = false;
 85+ }
 86+ // check for other keys that have special purposes
 87+ if(
 88+ key != 8 /* backspace */ &&
 89+ key != 9 /* tab */ &&
 90+ key != 13 /* enter */ &&
 91+ key != 35 /* end */ &&
 92+ key != 36 /* home */ &&
 93+ key != 37 /* left */ &&
 94+ key != 39 /* right */ &&
 95+ key != 46 /* del */
 96+ )
 97+ {
 98+ allow = false;
 99+ }
 100+ else
 101+ {
 102+ // for detecting special keys (listed above)
 103+ // IE does not support 'charCode' and ignores them in keypress anyway
 104+ if(typeof e.charCode != "undefined")
 105+ {
 106+ // special keys have 'keyCode' and 'which' the same (e.g. backspace)
 107+ if(e.keyCode == e.which && e.which != 0)
 108+ {
 109+ allow = true;
 110+ // . and delete share the same code, don't allow . (will be set to true later if it is the decimal point)
 111+ if(e.which == 46) allow = false;
 112+ }
 113+ // or keyCode != 0 and 'charCode'/'which' = 0
 114+ else if(e.keyCode != 0 && e.charCode == 0 && e.which == 0)
 115+ {
 116+ allow = true;
 117+ }
 118+ }
 119+ }
 120+ // if key pressed is the decimal and it is not already in the field
 121+ if(decimal && key == decimal.charCodeAt(0))
 122+ {
 123+ if(this.value.indexOf(decimal) == -1)
 124+ {
 125+ allow = true;
 126+ }
 127+ else
 128+ {
 129+ allow = false;
 130+ }
 131+ }
 132+ }
 133+ else
 134+ {
 135+ allow = true;
 136+ }
 137+ return allow;
 138+}
 139+
 140+$.fn.numeric.keyup = function(e)
 141+{
 142+ var val = this.value;
 143+ if(val.length > 0)
 144+ {
 145+ // get carat (cursor) position
 146+ var carat = $.fn.getSelectionStart(this);
 147+ // get decimal character and determine if negatives are allowed
 148+ var decimal = $.data(this, "numeric.decimal");
 149+ var negative = $.data(this, "numeric.negative");
 150+
 151+ // prepend a 0 if necessary
 152+ if(decimal != "")
 153+ {
 154+ // find decimal point
 155+ var dot = val.indexOf(decimal);
 156+ // if dot at start, add 0 before
 157+ if(dot == 0)
 158+ {
 159+ this.value = "0" + val;
 160+ }
 161+ // if dot at position 1, check if there is a - symbol before it
 162+ if(dot == 1 && val.charAt(0) == "-")
 163+ {
 164+ this.value = "-0" + val.substring(1);
 165+ }
 166+ val = this.value;
 167+ }
 168+
 169+ // if pasted in, only allow the following characters
 170+ var validChars = [0,1,2,3,4,5,6,7,8,9,'-',decimal];
 171+ // get length of the value (to loop through)
 172+ var length = val.length;
 173+ // loop backwards (to prevent going out of bounds)
 174+ for(var i = length - 1; i >= 0; i--)
 175+ {
 176+ var ch = val.charAt(i);
 177+ // remove '-' if it is in the wrong place
 178+ if(i != 0 && ch == "-")
 179+ {
 180+ val = val.substring(0, i) + val.substring(i + 1);
 181+ }
 182+ // remove character if it is at the start, a '-' and negatives aren't allowed
 183+ else if(i == 0 && !negative && ch == "-")
 184+ {
 185+ val = val.substring(1);
 186+ }
 187+ var validChar = false;
 188+ // loop through validChars
 189+ for(var j = 0; j < validChars.length; j++)
 190+ {
 191+ // if it is valid, break out the loop
 192+ if(ch == validChars[j])
 193+ {
 194+ validChar = true;
 195+ break;
 196+ }
 197+ }
 198+ // if not a valid character, or a space, remove
 199+ if(!validChar || ch == " ")
 200+ {
 201+ val = val.substring(0, i) + val.substring(i + 1);
 202+ }
 203+ }
 204+ // remove extra decimal characters
 205+ var firstDecimal = val.indexOf(decimal);
 206+ if(firstDecimal > 0)
 207+ {
 208+ for(var i = length - 1; i > firstDecimal; i--)
 209+ {
 210+ var ch = val.charAt(i);
 211+ // remove decimal character
 212+ if(ch == decimal)
 213+ {
 214+ val = val.substring(0, i) + val.substring(i + 1);
 215+ }
 216+ }
 217+ }
 218+ // set the value and prevent the cursor moving to the end
 219+ this.value = val;
 220+ $.fn.setSelection(this, carat);
 221+ }
 222+}
 223+
 224+$.fn.numeric.blur = function()
 225+{
 226+ var decimal = $.data(this, "numeric.decimal");
 227+ var callback = $.data(this, "numeric.callback");
 228+ var val = this.value;
 229+ if(val != "")
 230+ {
 231+ var re = new RegExp("^\\d+$|\\d*" + decimal + "\\d+");
 232+ if(!re.exec(val))
 233+ {
 234+ callback.apply(this);
 235+ }
 236+ }
 237+}
 238+
 239+$.fn.removeNumeric = function()
 240+{
 241+ return this.data("numeric.decimal", null).data("numeric.negative", null).data("numeric.callback", null).unbind("keypress", $.fn.numeric.keypress).unbind("blur", $.fn.numeric.blur);
 242+}
 243+
 244+// Based on code from http://javascript.nwbox.com/cursor_position/ (Diego Perini <dperini@nwbox.com>)
 245+$.fn.getSelectionStart = function(o)
 246+{
 247+ if (o.createTextRange)
 248+ {
 249+ var r = document.selection.createRange().duplicate();
 250+ r.moveEnd('character', o.value.length);
 251+ if (r.text == '') return o.value.length;
 252+ return o.value.lastIndexOf(r.text);
 253+ } else return o.selectionStart;
 254+}
 255+
 256+// set the selection, o is the object (input), p is the position ([start, end] or just start)
 257+$.fn.setSelection = function(o, p)
 258+{
 259+ // if p is number, start and end are the same
 260+ if(typeof p == "number") p = [p, p];
 261+ // only set if p is an array of length 2
 262+ if(p && p.constructor == Array && p.length == 2)
 263+ {
 264+ if (o.createTextRange)
 265+ {
 266+ var r = o.createTextRange();
 267+ r.collapse(true);
 268+ r.moveStart('character', p[0]);
 269+ r.moveEnd('character', p[1]);
 270+ r.select();
 271+ }
 272+ else if(o.setSelectionRange)
 273+ {
 274+ o.focus();
 275+ o.setSelectionRange(p[0], p[1]);
 276+ }
 277+ }
 278+}
 279+
 280+})(jQuery);
\ No newline at end of file
Property changes on: trunk/extensions/Survey/resources/jquery.numeric.js
___________________________________________________________________
Added: svn:eol-style
1281 + native

Status & tagging log