-
Notifications
You must be signed in to change notification settings - Fork 0
116552605 javascript code improvement #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sebastiandl
wants to merge
4
commits into
master
Choose a base branch
from
116552605-javascript-code-improvement
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d111000
[#116552605] moved javascript code to new files outside views
sebastiandl ee7422f
[#116552605] improved checkout's pages to avoid using unnecessary jav…
sebastiandl 42aab60
[#116552605] fixed several JS code related to checkout and clubs form.
sebastiandl e431ac3
[#116552605] improved Javascript code
sebastiandl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| var Campaign = { | ||
| Preferences: { | ||
| campaign_preferences: [], | ||
| campaign_preferences_backup: [], | ||
| init: function(){ | ||
| $('#table_preferences_groups').dataTable(); | ||
| Campaign.Preferences.get_preferences_from_field(); | ||
| Campaign.Preferences.campaign_preferences_backup = Campaign.Preferences.campaign_preferences; | ||
| Campaign.Preferences.update_table(); | ||
| }, | ||
| assign: function(id, name) { | ||
| var pg = []; | ||
| pg.id = id; | ||
| pg.name = name; | ||
| if (!Campaign.Preferences.already_on_list(pg)) { | ||
| Campaign.Preferences.campaign_preferences.push(pg); | ||
| Campaign.Preferences.set_preferences_to_field(); | ||
| Campaign.Preferences.update_table(); | ||
| } | ||
| else { | ||
| alert(I18n.t('campaigns.group_already_on_list')); | ||
| } | ||
| }, | ||
| remove_preference: function(index) { | ||
| Campaign.Preferences.campaign_preferences.splice(index, 1); | ||
| Campaign.Preferences.set_preferences_to_field(); | ||
| Campaign.Preferences.update_table(); | ||
| }, | ||
| remove_all_preferences: function() { | ||
| Campaign.Preferences.campaign_preferences = []; | ||
| Campaign.Preferences.set_preferences_to_field(); | ||
| Campaign.Preferences.update_table(); | ||
| }, | ||
| reset_preferences: function() { | ||
| Campaign.Preferences.campaign_preferences = Campaign.Preferences.campaign_preferences_backup; | ||
| Campaign.Preferences.update_table(); | ||
| }, | ||
| set_preferences_to_field: function() { | ||
| var prefs = []; | ||
| for (var i = 0; i < Campaign.Preferences.campaign_preferences.length; i++) { | ||
| prefs[i] = Campaign.Preferences.campaign_preferences[i].id; | ||
| } | ||
| $("#preferences_list").val(prefs); | ||
| }, | ||
| get_preferences_from_field: function() { | ||
| Campaign.Preferences.campaign_preferences = []; | ||
| received_preferences = $("#preferences_list").val(); | ||
| if (received_preferences.indexOf('|') > 0) { | ||
| var prefs = received_preferences.split(','); | ||
| for (var i = 0; i < prefs.length; i++) { | ||
| current_pref = prefs[i].split('|'); | ||
| pref = []; | ||
| pref.id = current_pref[0]; | ||
| pref.name = current_pref[1]; | ||
| Campaign.Preferences.campaign_preferences.push(pref); | ||
| } | ||
| } | ||
| }, | ||
| already_on_list: function(preference) { | ||
| for (var i = 0; i < Campaign.Preferences.campaign_preferences.length; i++) { | ||
| if (Campaign.Preferences.campaign_preferences[i].id == preference.id) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| }, | ||
| update_table: function() { | ||
| $('#assigned_preferences_groups tbody').html(''); | ||
| if (Campaign.Preferences.campaign_preferences.length > 0) { | ||
| for (var i = 0; i < Campaign.Preferences.campaign_preferences.length; i++) { | ||
| name_cell = '<td>' + Campaign.Preferences.campaign_preferences[i].name + '</td>'; | ||
| remove_cell = '<td class="text-right"><a href="javascript:Campaign.Preferences.remove_preference(' + i + ');" title="Remove this product">'+I18n.t('remove')+'</a></td>'; | ||
| $("#assigned_preferences_groups").append('<tr>' + name_cell + remove_cell + '</tr>'); | ||
| } | ||
| } | ||
| else { | ||
| $("#assigned_preferences_groups").append('<tr><td colspan="2">'+I18n.t('campaigns.no_groups_assigned')+'</td></tr>'); | ||
| } | ||
| } | ||
| }, | ||
|
|
||
| Products:{ | ||
| campaign_products: [], | ||
| campaign_products_backup: [], | ||
| init: function(){ | ||
| $('#table_products').dataTable(); | ||
| Campaign.Products.get_products_from_field(); | ||
| Campaign.Products.campaign_products_backup = Campaign.Products.campaign_products; | ||
| Campaign.Products.update_table(); | ||
| }, | ||
| assign: function(id, sku, name) { | ||
| var product = []; | ||
| product.id = id; | ||
| product.sku = sku; | ||
| product.name = name; | ||
| if (!Campaign.Products.product_already_on_list(product)) { | ||
| Campaign.Products.campaign_products.push(product); | ||
| Campaign.Products.set_products_to_field(); | ||
| Campaign.Products.update_table(); | ||
| } | ||
| else { | ||
| alert(I18n.t('campaigns.product_already_on_list')); | ||
| } | ||
| }, | ||
| remove_product: function(product_index) { | ||
| Campaign.Products.campaign_products.splice(product_index, 1); | ||
| Campaign.Products.set_products_to_field(); | ||
| Campaign.Products.update_table(); | ||
| }, | ||
| remove_all_products: function() { | ||
| Campaign.Products.campaign_products = []; | ||
| Campaign.Products.set_products_to_field(); | ||
| Campaign.Products.update_table(); | ||
| }, | ||
| reset_products: function() { | ||
| campaign_products = Campaign.Products.campaign_products_backup; | ||
| update_table(); | ||
| }, | ||
| set_products_to_field: function() { | ||
| products = []; | ||
| for (var i = 0; i < Campaign.Products.campaign_products.length; i++) { | ||
| products[i] = Campaign.Products.campaign_products[i].id; | ||
| } | ||
| $("#products_list").val(products); | ||
| }, | ||
| get_products_from_field: function() { | ||
| Campaign.Products.campaign_products = []; | ||
| received_products = $("#products_list").val(); | ||
| if (received_products.indexOf('|') > 0) { | ||
| products = received_products.split(','); | ||
| for (var i = 0; i < products.length; i++) { | ||
| current_product = products[i].split('|'); | ||
| var product = []; | ||
| product.id = current_product[0]; | ||
| product.sku = current_product[1]; | ||
| product.name = current_product[2]; | ||
| Campaign.Products.campaign_products.push(product); | ||
| } | ||
| } | ||
| }, | ||
| product_already_on_list: function(product) { | ||
| for (var i = 0; i < Campaign.Products.campaign_products.length; i++) { | ||
| if (Campaign.Products.campaign_products[i].id == product.id) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| }, | ||
| update_table: function() { | ||
| $('#assigned_products tbody').html(''); | ||
| if (Campaign.Products.campaign_products.length > 0) { | ||
| for (var i = 0; i < Campaign.Products.campaign_products.length; i++) { | ||
| var sku_cell = '<td>' + Campaign.Products.campaign_products[i].sku + '</td>'; | ||
| var name_cell = '<td>' + Campaign.Products.campaign_products[i].name + '</td>'; | ||
| var remove_cell = '<td class="text-right"><a href="#" onclick="javascript:Campaign.Products.remove_product(' + i + '); return false;" title="Remove this product">'+I18n.t('remove')+'</a></td>'; | ||
| $("#assigned_products").append('<tr>' + sku_cell + name_cell + remove_cell + '</tr>'); | ||
| } | ||
| } | ||
| else { | ||
| $("#assigned_products").append('<tr><td colspan="3">'+I18n.t('campaigns.no_products_assigned')+'</td></tr>'); | ||
| } | ||
| } | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| var Checkout = { | ||
| enroll_url: '', | ||
| result_url: '', | ||
| init: function(user_id, enroll_user, enroll_url, result_url){ | ||
| Checkout.enroll_url = enroll_url; | ||
| Checkout.result_url = result_url; | ||
|
|
||
| $('#modal').modal({ | ||
| show: false, | ||
| backdrop: 'static', | ||
| keyboard: false | ||
| }); | ||
|
|
||
| $('form').submit(function(e) { | ||
| $('#error_explanation').slideUp('fast'); | ||
|
|
||
| var validation_result = Checkout.validate_form($('#credit_card_number').val(), $('#credit_card_exp_month').val(), $('#credit_card_exp_year').val()); | ||
|
|
||
| if (validation_result.errors) { | ||
| $('#error_message').html(validation_result.message); | ||
| $('#error_explanation').slideDown('fast'); | ||
| e.preventDefault(); | ||
| return false; | ||
| } | ||
| else { | ||
| if(enroll_user == true){ | ||
| Checkout.enroll(user_id, $('#credit_card_number').val(), $('#credit_card_exp_month').val(), $('#credit_card_exp_year').val()); | ||
| }else{ | ||
| Checkout.toggle_fields(false); | ||
| $('#submit').val(I18n.t('please_wait')); | ||
| location.href = enroll_url | ||
| } | ||
| } | ||
| }); | ||
| }, | ||
| enroll: function(user_id, cc_number, cc_month, cc_year) { | ||
| Checkout.toggle_fields(false); | ||
| $('#submit').val(I18n.t('please_wait')); | ||
| $.ajax({ | ||
| url: Checkout.enroll_url, | ||
| data: { user_id: user_id, cc_number: cc_number, cc_month: cc_month, cc_year: cc_year }, | ||
| dataType: 'jsonp', | ||
| type: "POST", | ||
| async: true, | ||
| success: function(data) { | ||
| console.log(data.message); | ||
| if (data.code >= 200 && data.code <= 202) { | ||
| location.href = Checkout.result_url | ||
| } | ||
| else { | ||
| alert(I18n.t('checkout.problem_creating_account')); | ||
| } | ||
| Checkout.toggle_fields(true); | ||
| $('#submit').val('Submit'); | ||
| }, | ||
| error: function(data) { | ||
| Checkout.toggle_fields(true); | ||
| $('#submit').val('Submit'); | ||
| } | ||
| }); | ||
| }, | ||
| validate_form: function(card_number, exp_month, exp_year) { | ||
| var e = false; | ||
| var msg = ''; | ||
|
|
||
| if (card_number == '') { | ||
| msg = msg + '<li>'+I18n.t('checkout.card_number_missing')+'</li>'; | ||
| e = true; | ||
| } | ||
| else if (!Checkout.validate_cc_number (card_number)) { | ||
| msg = msg + '<li>'+I18n.t('checkout.card_number_invalid')+'</li>'; | ||
| e = true; | ||
| } | ||
|
|
||
| if (exp_month == '') { | ||
| msg = msg + '<li>'+I18n.t('checkout.card_month_missing')+'</li>'; | ||
| e = true; | ||
| } | ||
|
|
||
| if (exp_year == '') { | ||
| msg = msg + '<li>'+I18n.t('checkout.card_year_missing')+'</li>'; | ||
| e = true; | ||
| } | ||
|
|
||
| if (!Checkout.validate_cc_exp_date(exp_month, exp_year)) { | ||
| msg = msg + '<li>'+I18n.t('checkout.card_expired')+'</li>'; | ||
| e = true; | ||
| } | ||
|
|
||
| var result = { errors: e, message: msg }; | ||
| return result; | ||
| }, | ||
| validate_cc_exp_date: function(exp_month, exp_year) { | ||
| var d = new Date (); | ||
| var retval = true; | ||
| exp_month = parseInt(exp_month, 10); | ||
| if ((exp_month < (d.getMonth() + 1)) && (exp_year == d.getFullYear())) { | ||
| retval = false; | ||
| } | ||
| return retval; | ||
| }, | ||
| validate_cc_number: function(card_number) { | ||
| var retval = false; | ||
| var reg_exp = /^\d{15,16}$/; | ||
| if (reg_exp.test(card_number)) { | ||
| retval = true; | ||
| } | ||
| return retval; | ||
| }, | ||
| toggle_fields: function(state) { | ||
| if(state) { | ||
| $('#modal').modal('hide'); | ||
| $('#submit').removeAttr("disabled"); | ||
| $('#credit_card_number').removeAttr("disabled"); | ||
| $('#credit_card_exp_month').removeAttr("disabled"); | ||
| $('#credit_card_exp_year').removeAttr("disabled"); | ||
|
|
||
| $('#submit').removeClass('disabled'); | ||
| $('#credit_card_number').removeClass('disabled'); | ||
| $('#credit_card_exp_month').removeClass('disabled'); | ||
| $('#credit_card_exp_year').removeClass('disabled'); | ||
| } | ||
| else { | ||
| $('#modal').modal('show'); | ||
| $('#submit').attr("disabled", "disabled"); | ||
| $('#credit_card_number').attr("disabled", "disabled"); | ||
| $('#credit_card_exp_month').attr("disabled", "disabled"); | ||
| $('#credit_card_exp_year').attr("disabled", "disabled"); | ||
|
|
||
| $('#submit').addClass('disabled'); | ||
| $('#credit_card_number').addClass('disabled'); | ||
| $('#credit_card_exp_month').addClass('disabled'); | ||
| $('#credit_card_exp_year').addClass('disabled'); | ||
| } | ||
| }, | ||
| thankyou: function(autologin_url){ | ||
| if(autologin_url.length > 0) { | ||
| window.setTimeout(function() { | ||
| document.location = autologin_url; | ||
| }, 15000); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO we should be using JS validation libraries, like BootstrapValidator or similar that provides validation for CreditCard , Email and other sveral types of fields.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let me check that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have created this US to handle this improvement https://www.pivotaltracker.com/story/show/117069191. I have found several CC validators, but none of was nice to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm using http://formvalidation.io/ in another project. The latest version
is paid, however previous version are free, they are available on github.
Also using https://github.com/1000hz/bootstrap-validator might be a good
choice, Since you don't need to care about appending the error messages
using if+else.
On Wed, Apr 6, 2016 at 12:34 PM, Sebastian De Luca <notifications@github.com