r99593 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99592‎ | r99593 | r99594 >
Date:23:02, 11 October 2011
Author:pgehres
Status:ok (Comments)
Tags:fundraising 
Comment:
Making webitects forms compatible with MW1.17 and RL
Modified paths:
  • /trunk/extensions/DonationInterface/payflowpro_gateway/forms/rapidhtml/RapidHtmlResources.php (modified) (history)
  • /trunk/extensions/DonationInterface/payflowpro_gateway/forms/rapidhtml/html/webitects_2_3step.html (modified) (history)
  • /trunk/extensions/DonationInterface/payflowpro_gateway/forms/rapidhtml/js/globalcollect_test_2.js (modified) (history)
  • /trunk/extensions/DonationInterface/payflowpro_gateway/forms/rapidhtml/js/webitects.js (added) (history)
  • /trunk/extensions/DonationInterface/payflowpro_gateway/forms/rapidhtml/js/webitects_2_3step.js (modified) (history)

Diff [purge]

Index: trunk/extensions/DonationInterface/payflowpro_gateway/forms/rapidhtml/html/webitects_2_3step.html
@@ -1,5 +1,13 @@
22 <script type="text/javascript">
3 -mw.loader.load('pfp.form.rapidhtml.webitects');
 3+ mw.loader.load('pfp.form.rapidhtml.webitects_2_3step');
 4+ // these must go through RapidHTML and thus are inline
 5+ var amountErrors = ['#general|escape','#retryMsg|escape','#amount|escape'],
 6+ billingErrors = ['#fname|escape','#lname|escape','#city|escape','#country|escape',
 7+ '#street|escape','#state|escape','#zip|escape','#emailAdd|escape'],
 8+ paymentErrors = ['#card_num|escape','#card|escape','#cvv|escape'],
 9+ actionURL = "@action",
 10+ scriptPath = "@script_path";
 11+
412 </script>
513 <!--[if lt IE 7]><style type="text/css">body{behavior:url("@script_path/skins/vector/csshover.min.htc")}</style><![endif]-->
614 <!-- start Webitects styles -->
@@ -228,252 +236,6 @@
229237 </div>
230238 </div>
231239
232 - <!-- load jquery (newer) -->
233 - <script type="text/javascript">
234 - ( function( $ ){
235 - window.onload = function (){
236 - // ensure all jQuery instances are set to 1.4.2
237 - jQuery = $;
238 - $j = $;
239 -
240 - // check for RapidHtml errors and display, if any
241 - var amountErrors = ['#general|escape','#retryMsg|escape','#amount|escape'],
242 - amountErrorString = "";
243 - var billingErrors = ['#fname|escape','#lname|escape','#city|escape','#country|escape',
244 - '#street|escape','#state|escape','#zip|escape','#emailAdd|escape'],
245 - billingErrorString = "";
246 - var paymentErrors = ['#card_num|escape','#card|escape','#cvv|escape'],
247 - paymentErrorString = "";
248 -
249 - // generate formatted errors to display
250 - var temp = []
251 - for( var e in amountErrors )
252 - if( amountErrors[e] != "" )
253 - temp[temp.length] = amountErrors[e];
254 - amountErrorString = temp.join( "<br />" );
255 -
256 - temp = []
257 - for( var e in billingErrors )
258 - if( billingErrors[e] != "" )
259 - temp[temp.length] = billingErrors[e];
260 - billingErrorString = temp.join( "<br />" );
261 -
262 - temp = []
263 - for( var e in paymentErrors )
264 - if( paymentErrors[e] != "" )
265 - temp[temp.length] = paymentErrors[e];
266 - paymentErrorString = temp.join( "<br />" );
267 -
268 - // show the errors
269 - var prevError = false;
270 - if( amountErrorString != ""){
271 - $("#amtErrorMessages").html( amountErrorString );
272 - prevError = true;
273 - showStep2(); // init the headers
274 - showStep3();
275 - showStep1(); // should be default, but ensure
276 - }
277 - if( billingErrorString != ""){
278 - $("#billingErrorMessages").html( billingErrorString );
279 - if( !prevError ){
280 - showStep1(); // init the headers
281 - showStep3();
282 - showStep2();
283 - prevError = true;
284 - }
285 - showAmount( $('input[name="amount"]') ); // lets go ahead and assume there is something to show
286 - }
287 - if( paymentErrorString != ""){
288 - $("#paymentErrorMessages").html( paymentErrorString );
289 - if( !prevError ){
290 - showStep1(); // init the headers
291 - showStep2();
292 - showStep3();
293 - }
294 - showAmount( $('input[name="amount"]') ); // lets go ahead and assume there is something to show
295 - }
296 -
297 - // Init inline labels
298 - $(".hint").ezpz_hint();
299 -
300 - $("#cc").click(function(){
301 - if ( validateAmount() ){
302 - showAmount( $('input[name="amount"]') );
303 - showStep2();
304 - }
305 - });
306 -
307 - $("#pp").click(function(){
308 - if ( validateAmount() ){
309 - // set the action to go to PayPal
310 - $('input[name="gateway"]').val("paypal");
311 - $('input[name="PaypalRedirect"]').val("1");
312 - $("#loading").html("<img src='@script_path/extensions/DonationInterface/payflowpro_gateway/forms/rapidhtml/images/loading-white.gif' /> Redirecting to PayPal…");
313 - document.paypalcontribution.action = "@action";
314 - document.paypalcontribution.submit();
315 - }
316 - });
317 - $("#paymentContinueBtn").live("click", function() {
318 - if ( validate_personal( document.paypalcontribution ) ){
319 - showStep3();
320 - }
321 - });
322 - // Set the cards to progress to step 3
323 - $(".cardradio").live("click", function() {
324 - if ( validate_personal( document.paypalcontribution ) ){
325 - showStep3();
326 - }
327 - else {
328 - // show the continue button to indicate how to get to step 3 since they
329 - // have already clicked on a card image
330 - $("#paymentContinue").show();
331 - }
332 - });
333 -
334 - $("#submitcreditcard").click(function(){
335 - // set country to US TODO: make this dynamic
336 - $('input[name="country"]').val("US");
337 -
338 - if ( validate_cc() ){
339 - // set the hidden expiration date input from the two selects
340 - $('input[name="expiration"]').val(
341 - $('select[name="mos"]').val() + $('select[name="year"]').val().substring(2,4)
342 - );
343 - document.paypalcontribution.action = "@action";
344 - document.paypalcontribution.submit();
345 - }
346 - })
347 -
348 - // init all of the header actions
349 - $("#step1header").click(function(){
350 - showStep1();
351 - });
352 - $("#step2header").click(function(){
353 - showStep2();
354 - });
355 - $("#step3header").click(function(){
356 - showStep3();
357 - });
358 -
359 - function showStep1(){
360 - // show the correct sections
361 - $("#step1wrapper").slideDown();
362 - $("#step2wrapper").slideUp();
363 - $("#step3wrapper").slideUp();
364 - $("#change-amount").hide();
365 - $("#change-billing").show();
366 - $("#change-payment").show();
367 - $("#step1header").show(); // just in case
368 - }
369 - function showStep2(){
370 - // show the correct sections
371 - $("#step1wrapper").slideUp();
372 - $("#step2wrapper").slideDown();
373 - $("#step3wrapper").slideUp();
374 - $("#change-amount").show();
375 - $("#change-billing").hide();
376 - $("#change-payment").show();
377 - $("#step2header").show(); // just in case
378 - }
379 - function showStep3(){
380 - // show the correct sections
381 - $("#step1wrapper").slideUp();
382 - $("#step2wrapper").slideUp();
383 - $("#step3wrapper").slideDown();
384 - $("#change-amount").show();
385 - $("#change-billing").show();
386 - $("#change-payment").hide();
387 - $("#step3header").show(); // just in case
388 - }
389 -
390 -
391 - // Fix behavior of images in labels
392 - // TODO: check that disabling this is okay in things other than Chrome
393 -// $("label img").live("click", function() { $("#" + $(this).parents( "label" ).attr( "for" )).click(); });
394 -
395 - // Display selected amount
396 - function showAmount(e){
397 - $("#selected-amount").html( "($" + e.val() + ")" );
398 - $("#change-amount").show();
399 - }
400 -
401 - // Set selected amount to amount
402 - $('input[name="amountRadio"]').click( function(){
403 - setAmount( $(this) );
404 - });
405 - // reset the amount field when "other" is changed
406 - $("#other-amount").change(function(){
407 - setAmount( $(this) );
408 - });
409 - // set the hidden amount input to the value of the selected element
410 - function setAmount(e){
411 - $('input[name="amount"]').val( e.val() );
412 - }
413 -
414 - // show the CVV help image on click
415 - $("#where").click(function(){
416 - $("#codes").toggle();
417 - return false;
418 - });
419 -
420 - function validateAmount(){
421 - var minimums = {
422 - 'USD' : 1
423 - };
424 - var error = true;
425 - var amount = $('input[name="amount"]').val(); // get the amount
426 - amount = amount.replace(/[,.](\d)$/, '\:$10');
427 - amount = amount.replace(/[,.](\d)(\d)$/, '\:$1$2');
428 - amount = amount.replace(/[,.]/g, '');
429 - amount = amount.replace(/:/, '.');
430 - $('input[name="amount"]').val( amount ); // set the new amount back into the form
431 -
432 - // Check amount is a real number, sets error as true (good) if no issues
433 - error = ( amount == null || isNaN( amount ) || amount.value <= 0 );
434 -
435 - // Check amount is at least the minimum
436 - var currency_code = 'USD'; // hard-coded for these forms and tests
437 - $('input[name="currency_code"]').val( currency_code );
438 - if ( typeof( minimums[currency_code] ) == 'undefined' ) {
439 - minimums[currency_code] = 1;
440 - }
441 - if ( amount < minimums[currency_code] || error ) {
442 - alert( 'You must contribute at least $1'.replace('$1', minimums[currency_code] + ' ' + currency_code ) );
443 - error = true;
444 - }
445 - return !error;
446 - }
447 - function validate_cc(){
448 - // reset the errors
449 - $("#paymentErrorMessages").html('');
450 - var error = false;
451 - if( $('input[name="card_num"]').val() == '' ){
452 - $("#paymentErrorMessages").append( "Please enter a valid credit card number" );
453 - error = true;
454 - }
455 - if( $('select[name="mos"]').val() == '' ){
456 - if( $("#paymentErrorMessages").html() != "" )
457 - $("#paymentErrorMessages").append( "<br />" );
458 - $("#paymentErrorMessages").append( "Please enter a valid month for the expiration date" );
459 - error = true;
460 - }
461 - if( $('select[name="year"]').val() == '' ){
462 - if( $("#paymentErrorMessages").html() != "" )
463 - $("#paymentErrorMessages").append( "<br />" );
464 - $("#paymentErrorMessages").append( "Please enter a valid year for the expiration date" );
465 - error = true;
466 - }
467 - if( $('input[name="cvv"]').val() == '' ){
468 - if( $("#paymentErrorMessages").html() != "" )
469 - $("#paymentErrorMessages").append( "<br />" );
470 - $("#paymentErrorMessages").append( "Please enter a valid security code" );
471 - error = true;
472 - }
473 - return !error;
474 - }
475 - }
476 - })(jQuery);
477 - </script>
478240 <!-- Wikimedia Project logo
479241 <li id="footer-copyrightico"><a href="https://www.mediawiki.org//wikimediafoundation.org/"><img src="//bits.wikimedia.org/images/wikimedia-button.png" width="88" height="31" alt="Wikimedia Foundation"/></a></li>
480242 -->
Index: trunk/extensions/DonationInterface/payflowpro_gateway/forms/rapidhtml/RapidHtmlResources.php
@@ -61,6 +61,12 @@
6262 'localBasePath' => dirname( __FILE__ ),
6363 'remoteExtPath' => $wgPayflowRapidHtmlRemoteExtPath,
6464 );
 65+$wgResourceModules[ 'pfp.form.rapidhtml.webitects_2_3step' ] = array(
 66+ 'scripts' => 'js/webitects_2_3step.js',
 67+ 'dependencies' => 'pfp.form.rapidhtml.webitects',
 68+ 'localBasePath' => dirname( __FILE__ ),
 69+ 'remoteExtPath' => $wgPayflowRapidHtmlRemoteExtPath,
 70+);
6571
6672 /**
6773 * globalcollect_test
Index: trunk/extensions/DonationInterface/payflowpro_gateway/forms/rapidhtml/js/globalcollect_test_2.js
@@ -1,4 +1,4 @@
2 -//make HTML5 placeholders work in non supportive browsers
 2+// make HTML5 placeholders work in non supportive browsers
33 $("input[placeholder]").each(function() {
44 if($(this).val()=="") {
55 $(this).addClass('hasplaceholder');
@@ -16,6 +16,7 @@
1717 }
1818 });
1919
 20+// clear the placeholder values on form submit
2021 $('form').submit(function(evt){
2122 $('input[placeholder]').each(function(){
2223 if($(this).attr("placeholder") == $(this).val()) {$(this).val('');}
Index: trunk/extensions/DonationInterface/payflowpro_gateway/forms/rapidhtml/js/webitects_2_3step.js
@@ -0,0 +1,236 @@
 2+/*
 3+ * The following variable are declared inline in webitects_2_3step.html:
 4+ * amountErrors, billingErrors, paymentErrors, scriptPath, actionURL
 5+ */
 6+$( document ).ready( function () {
 7+
 8+ // check for RapidHtml errors and display, if any
 9+ var amountErrorString = "",
 10+ billingErrorString = "",
 11+ paymentErrorString = "";
 12+
 13+ // generate formatted errors to display
 14+ var temp = [];
 15+ for ( var e in amountErrors )
 16+ if ( amountErrors[e] != "" )
 17+ temp[temp.length] = amountErrors[e];
 18+ amountErrorString = temp.join( "<br />" );
 19+
 20+ temp = [];
 21+ for ( var f in billingErrors )
 22+ if ( billingErrors[f] != "" )
 23+ temp[temp.length] = billingErrors[f];
 24+ billingErrorString = temp.join( "<br />" );
 25+
 26+ temp = [];
 27+ for ( var g in paymentErrors )
 28+ if ( paymentErrors[g] != "" )
 29+ temp[temp.length] = paymentErrors[g];
 30+ paymentErrorString = temp.join( "<br />" );
 31+
 32+ // show the errors
 33+ var prevError = false;
 34+ if ( amountErrorString != "" ) {
 35+ $( "#amtErrorMessages" ).html( amountErrorString );
 36+ prevError = true;
 37+ showStep2(); // init the headers
 38+ showStep3();
 39+ showStep1(); // should be default, but ensure
 40+ }
 41+ if ( billingErrorString != "" ) {
 42+ $( "#billingErrorMessages" ).html( billingErrorString );
 43+ if ( !prevError ) {
 44+ showStep1(); // init the headers
 45+ showStep3();
 46+ showStep2();
 47+ prevError = true;
 48+ }
 49+ showAmount( $( 'input[name="amount"]' ) ); // lets go ahead and assume there is something to show
 50+ }
 51+ if ( paymentErrorString != "" ) {
 52+ $( "#paymentErrorMessages" ).html( paymentErrorString );
 53+ if ( !prevError ) {
 54+ showStep1(); // init the headers
 55+ showStep2();
 56+ showStep3();
 57+ }
 58+ showAmount( $( 'input[name="amount"]' ) ); // lets go ahead and assume there is something to show
 59+ }
 60+ // Init inline labels
 61+ $( ".hint" ).ezpz_hint();
 62+
 63+ $( "#cc" ).click( function() {
 64+ if ( validateAmount() ) {
 65+ showAmount( $( 'input[name="amount"]' ) );
 66+ showStep2();
 67+ }
 68+ } );
 69+
 70+ $( "#pp" ).click( function() {
 71+ if ( validateAmount() ) {
 72+ // set the action to go to PayPal
 73+ $( 'input[name="gateway"]' ).val( "paypal" );
 74+ $( 'input[name="PaypalRedirect"]' ).val( "1" );
 75+ $( "#loading" ).html( "<img src=" + scriptPath + "'/extensions/DonationInterface/payflowpro_gateway/forms/rapidhtml/images/loading-white.gif' /> Redirecting to PayPal…" );
 76+ document.paypalcontribution.action = actionURL;
 77+ document.paypalcontribution.submit();
 78+ }
 79+ } );
 80+ $( "#paymentContinueBtn" ).live( "click", function() {
 81+ if ( validate_personal( document.paypalcontribution ) ) {
 82+ showStep3();
 83+ }
 84+ } );
 85+ // Set the cards to progress to step 3
 86+ $( ".cardradio" ).live( "click", function() {
 87+ if ( validate_personal( document.paypalcontribution ) ) {
 88+ showStep3();
 89+ }
 90+ else {
 91+ // show the continue button to indicate how to get to step 3 since they
 92+ // have already clicked on a card image
 93+ $( "#paymentContinue" ).show();
 94+ }
 95+ } );
 96+
 97+ $( "#submitcreditcard" ).click( function() {
 98+ // set country to US TODO: make this dynamic
 99+ $( 'input[name="country"]' ).val( "US" );
 100+
 101+ if ( validate_cc() ) {
 102+ // set the hidden expiration date input from the two selects
 103+ $( 'input[name="expiration"]' ).val(
 104+ $( 'select[name="mos"]' ).val() + $( 'select[name="year"]' ).val().substring( 2, 4 )
 105+ );
 106+ document.paypalcontribution.action = actionURL;
 107+ document.paypalcontribution.submit();
 108+ }
 109+ } );
 110+ // init all of the header actions
 111+ $( "#step1header" ).click( function() {
 112+ showStep1();
 113+ } );
 114+ $( "#step2header" ).click( function() {
 115+ showStep2();
 116+ } );
 117+ $( "#step3header" ).click( function() {
 118+ showStep3();
 119+ } );
 120+ // Set selected amount to amount
 121+ $( 'input[name="amountRadio"]' ).click( function() {
 122+ setAmount( $( this ) );
 123+ } );
 124+ // reset the amount field when "other" is changed
 125+ $( "#other-amount" ).change( function() {
 126+ setAmount( $( this ) );
 127+ } );
 128+
 129+ // show the CVV help image on click
 130+ $( "#where" ).click( function() {
 131+ $( "#codes" ).toggle();
 132+ return false;
 133+ } );
 134+
 135+} );
 136+
 137+function showStep1() {
 138+ // show the correct sections
 139+ $( "#step1wrapper" ).slideDown();
 140+ $( "#step2wrapper" ).slideUp();
 141+ $( "#step3wrapper" ).slideUp();
 142+ $( "#change-amount" ).hide();
 143+ $( "#change-billing" ).show();
 144+ $( "#change-payment" ).show();
 145+ $( "#step1header" ).show(); // just in case
 146+}
 147+
 148+function showStep2() {
 149+ // show the correct sections
 150+ $( "#step1wrapper" ).slideUp();
 151+ $( "#step2wrapper" ).slideDown();
 152+ $( "#step3wrapper" ).slideUp();
 153+ $( "#change-amount" ).show();
 154+ $( "#change-billing" ).hide();
 155+ $( "#change-payment" ).show();
 156+ $( "#step2header" ).show(); // just in case
 157+}
 158+
 159+function showStep3() {
 160+ // show the correct sections
 161+ $( "#step1wrapper" ).slideUp();
 162+ $( "#step2wrapper" ).slideUp();
 163+ $( "#step3wrapper" ).slideDown();
 164+ $( "#change-amount" ).show();
 165+ $( "#change-billing" ).show();
 166+ $( "#change-payment" ).hide();
 167+ $( "#step3header" ).show(); // just in case
 168+}
 169+// Fix behavior of images in labels
 170+// TODO: check that disabling this is okay in things other than Chrome
 171+// $("label img").live("click", function() { $("#" + $(this).parents( "label" ).attr( "for" )).click(); });
 172+
 173+// set the hidden amount input to the value of the selected element
 174+function setAmount( e ) {
 175+ $( 'input[name="amount"]' ).val( e.val() );
 176+}
 177+// Display selected amount
 178+function showAmount( e ) {
 179+ $( "#selected-amount" ).html( "($" + e.val() + ")" );
 180+ $( "#change-amount" ).show();
 181+}
 182+function validateAmount() {
 183+ var minimums = {
 184+ 'USD' : 1
 185+ };
 186+ var error = true;
 187+ var amount = $( 'input[name="amount"]' ).val(); // get the amount
 188+ amount = amount.replace( /[,.](\d)$/, '\:$10' );
 189+ amount = amount.replace( /[,.](\d)(\d)$/, '\:$1$2' );
 190+ amount = amount.replace( /[,.]/g, '' );
 191+ amount = amount.replace( /:/, '.' );
 192+ $( 'input[name="amount"]' ).val( amount ); // set the new amount back into the form
 193+
 194+ // Check amount is a real number, sets error as true (good) if no issues
 195+ error = ( amount == null || isNaN( amount ) || amount.value <= 0 );
 196+
 197+ // Check amount is at least the minimum
 198+ var currency_code = 'USD'; // hard-coded for these forms and tests
 199+ $( 'input[name="currency_code"]' ).val( currency_code );
 200+ if ( typeof( minimums[currency_code] ) == 'undefined' ) {
 201+ minimums[currency_code] = 1;
 202+ }
 203+ if ( amount < minimums[currency_code] || error ) {
 204+ alert( 'You must contribute at least $1'.replace( '$1', minimums[currency_code] + ' ' + currency_code ) );
 205+ error = true;
 206+ }
 207+ return !error;
 208+}
 209+
 210+function validate_cc() {
 211+ // reset the errors
 212+ $( "#paymentErrorMessages" ).html( '' );
 213+ var error = false;
 214+ if ( $( 'input[name="card_num"]' ).val() == '' ) {
 215+ $( "#paymentErrorMessages" ).append( "Please enter a valid credit card number" );
 216+ error = true;
 217+ }
 218+ if ( $( 'select[name="mos"]' ).val() == '' ) {
 219+ if ( $( "#paymentErrorMessages" ).html() != "" )
 220+ $( "#paymentErrorMessages" ).append( "<br />" );
 221+ $( "#paymentErrorMessages" ).append( "Please enter a valid month for the expiration date" );
 222+ error = true;
 223+ }
 224+ if ( $( 'select[name="year"]' ).val() == '' ) {
 225+ if ( $( "#paymentErrorMessages" ).html() != "" )
 226+ $( "#paymentErrorMessages" ).append( "<br />" );
 227+ $( "#paymentErrorMessages" ).append( "Please enter a valid year for the expiration date" );
 228+ error = true;
 229+ }
 230+ if ( $( 'input[name="cvv"]' ).val() == '' ) {
 231+ if ( $( "#paymentErrorMessages" ).html() != "" )
 232+ $( "#paymentErrorMessages" ).append( "<br />" );
 233+ $( "#paymentErrorMessages" ).append( "Please enter a valid security code" );
 234+ error = true;
 235+ }
 236+ return !error;
 237+}
\ No newline at end of file
Index: trunk/extensions/DonationInterface/payflowpro_gateway/forms/rapidhtml/js/webitects.js
Property changes on: trunk/extensions/DonationInterface/payflowpro_gateway/forms/rapidhtml/js/webitects.js
___________________________________________________________________
Added: svn:eol-style
1238 + native

Comments

#Comment by Awjrichards (talk | contribs)   00:49, 12 October 2011

Is there any reason not to use validate_input.js in DonationInterface for some of this? There seems to be some duplicated effort her, but perhaps there is a good reason for that.

#Comment by Pgehres (WMF) (talk | contribs)   01:22, 12 October 2011

Yes, at the moment, validate_input.js doesn't do validation for each of the steps. It could easily be re-written, but when the form was originally coded it was an all-or-nothing validation. And, frankly, I just made teh existing code work.

Status & tagging log