Skip to content

Commit

Permalink
Merge pull request #52 from legalthings/Hardcoded_locale_for_datepicker
Browse files Browse the repository at this point in the history
Get template locale to use in datepicker. Remove datepicker init from validation
  • Loading branch information
jasny authored May 12, 2017
2 parents c418e7e + 0a2f04d commit b009768
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 35 deletions.
43 changes: 9 additions & 34 deletions js/legalform-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,7 @@
/**
* Init validation for date picker
*/
this.initDatePicker = function ()
{
$(this.elWizard).on('click', '[data-picker="date"]', function(e) {
if ($(this).data('DateTimePicker')) return;

$(this).datetimepicker({ locale: 'nl', format: 'DD-MM-YYYY' });
$(e.target).closest('.input-group-addon').trigger('click');

//Fix material label
$(this).find(':input').on('focusout', function(e) {
if (e.target.value !== '') {
$(e.target).parent().parent().removeClass('is-empty');
} else {
$(e.target).parent().parent().addClass('is-empty');
}
});
});

this.initDatePicker = function () {
$(this.elWizard).on('dp.change', $.proxy(function(e) {
var input = $(e.target).find(':input').get(0);
var name = $(input).attr('name');
Expand All @@ -67,8 +50,7 @@
/**
* Init custom validation
*/
this.initCustomValidation = function ()
{
this.initCustomValidation = function () {
$(this.elWizard).on('change', ':input', $.proxy(function(e) {
this.validateField(e.target);
}, this));
Expand All @@ -77,8 +59,7 @@
/**
* Launch validation when interacting with text field
*/
this.initTextFields = function ()
{
this.initTextFields = function () {
$(this.elWizard).on('focus keyup', textFields, $.proxy(function(e) {
this.handleValidation(e.target);
}, this));
Expand All @@ -87,8 +68,7 @@
/**
* Launch validation when interacting with "state" field
*/
this.initStateFields = function ()
{
this.initStateFields = function () {
$(this.elWizard).on('click', stateFields, $.proxy(function(e) {
this.handleValidation(e.target);
}, this));
Expand All @@ -97,8 +77,7 @@
/**
* Init and show tooltips
*/
this.initShowTooltip = function ()
{
this.initShowTooltip = function () {
$(this.elWizard).on('mouseover click', '[rel=tooltip]', $.proxy(function(e) {
this.initTooltip(e.target);
}, this));
Expand All @@ -107,8 +86,7 @@
/**
* Close programaticaly opened tooltip when leaving field
*/
this.initHideTooltipOnBlur = function()
{
this.initHideTooltipOnBlur = function() {
$(this.elWizard).on('blur', textFields + ', ' + stateFields, $.proxy(function(e) {
var help = $(e.target).closest('.form-group').find('[rel="tooltip"]');
var tooltip = $(help).data('bs.tooltip');
Expand All @@ -119,8 +97,7 @@
/**
* Close programaticaly opened tooltips on form scroll
*/
this.initHideTooltipOnScroll = function ()
{
this.initHideTooltipOnScroll = function () {
$(this.elWizard).on('scroll', function(e) {
$('[rel="tooltip"]').each(function() {
var tooltip = $(e.target).data('bs.tooltip');
Expand All @@ -135,16 +112,14 @@
/**
* Initialize the bootrap vaidation for the forms
*/
this.initBootstrapValidation = function ()
{
this.initBootstrapValidation = function () {
$(this.elWizard).find('form').validator();
}

/**
* Initialize validation on step event
*/
this.initOnStep = function ()
{
this.initOnStep = function () {
var ractive = this.ractive;
var self = this;

Expand Down
51 changes: 50 additions & 1 deletion js/ractive-legalform.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* Current locale
*/
locale: null,
locale: 'en_US',

/**
* Number of steps in the wizard
Expand Down Expand Up @@ -195,6 +195,7 @@
this.initWizard();
$('.form-scrollable').perfectScrollbar();

this.initDatePicker();
this.initInputmask();
this.initPreviewSwitch();
this.refreshLikerts();
Expand All @@ -203,6 +204,29 @@
$('#doc').trigger('shown.preview');
},

/**
* Init date picker
*/
initDatePicker: function () {
var ractive = this;

$(this.elWizard).on('click', '[data-picker="date"]', function(e) {
if ($(this).data('DateTimePicker')) return;

$(this).datetimepicker({ locale: ractive.getLocale('short'), format: 'DD-MM-YYYY' });
$(e.target).closest('.input-group-addon').trigger('click');

//Fix material label
$(this).find(':input').on('focusout', function(e) {
if (e.target.value !== '') {
$(e.target).parent().parent().removeClass('is-empty');
} else {
$(e.target).parent().parent().addClass('is-empty');
}
});
});
},

/**
* Initialize special field types
*/
Expand Down Expand Up @@ -709,6 +733,31 @@
return $.extend(true, {}, this.defaults, this.values, globals, {meta: this.meta});
},

/**
* Get locale of template or document
* @param {string} format
* @return {string}
*/
getLocale: function(format) {
var locale = this.locale;
var delimiter = '_';
var pos = locale.indexOf(delimiter);

if (format === 'short') {
if (pos !== -1) locale = locale.substr(0, pos);
} else if (format === 'momentjs') {
locale = locale.toLowerCase();
if (pos !== -1) {
parts = locale.split(delimiter);
locale = parts[0] === parts[1] ? parts[0] : parts.join('-');
}
} else if (format) {
throw 'Unknown format "' + format + '" for getting document locale';
}

return locale;
},

/**
* Show alert message
* @param {string} status Message status (danger, warning, success)
Expand Down

0 comments on commit b009768

Please sign in to comment.