Skip to content

Commit

Permalink
Allow rewriting date values (and possibly others)
Browse files Browse the repository at this point in the history
  • Loading branch information
Minstel committed Jun 20, 2019
1 parent 8eaeb6c commit 4ae3137
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 55 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ <h1>LegalForm.js demo</h1>
<script src="js/lib/expand-condition.js"></script>
<script src="js/lib/parse-number.js"></script>
<script src="js/lib/cloner.js"></script>
<script src="js/lib/dot-key.js"></script>
<script src="js/model/form-model.js"></script>
<script src="js/model/legalform-model.js"></script>
<script src="js/model/live-contract-form-model.js"></script>
Expand Down
54 changes: 54 additions & 0 deletions js/lib/dot-key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = { setByKeyPath, getByKeyPath };
}

/**
* Set (nested) property of object using dot notation
*
* @param {object} target
* @param {string} key
* @param value
*/
function setByKeyPath(target, key, value) {
var parts = key.split('.');

for (var i = 0; i < parts.length; i++) {
var part = parts[i];

if (i < parts.length -1) {
if (typeof target[part] !== 'object') {
target[part] = {};
}

target = target[part];
} else {
target[part] = value;
}
}
}

/**
* Get (nested) property of object using dot notation
*
* @param {object} target
* @param {string} key
* @param defaultValue
*/
function getByKeyPath(target, key, defaultValue) {
if (!target || !key) return false;

key = key.split('.');
var l = key.length,
i = 0,
p = '';

for (; i < l; ++i) {
p = key[i];

if (target.hasOwnProperty(p)) target = target[p];
else return defaultValue;
}

return target;
}
92 changes: 37 additions & 55 deletions js/ractive-legalform.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,20 +417,51 @@
})
},

/**
* Get values that should replace ractive values
*/
getRewriteValues: function() {
var values = {};

$(this.elWizard).find('[data-picker="date"]').each(function() {
var $inputGroup = $(this);
var $input = $inputGroup.find('input');

var yearly = !!$input.attr('yearly');
if (yearly) return;

var value = $input.val();
var date = moment(value, 'DD-MM-YYYY', true);
var isoDate = date.utc().format('YYYY-MM-DDTHH:mm:ssZ');

var name = $input.attr('name');
values[name] = isoDate;
});

return values;
},

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

$wizard.find('[data-picker="date"]').each(init); //do on page init, to convert date format from ISO
$wizard.on('click', '[data-picker="date"]', init); //do for fields, that were hidden on page init

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

var yearly = $inputGroup.find('input').attr('yearly');
var format = yearly ? 'DD-MM' : 'DD-MM-YYYY';

$inputGroup.datetimepicker({
locale: ractive.getLocale('short'),
format: yearly ? 'DD-MM' : 'DD-MM-YYYY',
format: format,
extraFormats: ['YYYY-MM-DDTHH:mm:ssZ'], //Allow ISO8601 format for input
dayViewHeaderFormat: yearly ? 'MMMM' : 'MMMM YYYY',

//Allow arrow keys navigation inside date text field
Expand All @@ -446,8 +477,10 @@
}
});

$(e.target).closest('.input-group-addon').trigger('click');
});
if (typeof e !== 'undefined') {
$(e.target).closest('.input-group-addon').trigger('click');
}
}
},

/**
Expand Down Expand Up @@ -605,7 +638,6 @@
var validator = $stepForm.data('bs.validator');

if (!validator) {
console.log('Reinitializing validator...');
self.initBootstrapValidation();
self.updateBootstrapValidation();
return;
Expand Down Expand Up @@ -1099,56 +1131,6 @@
callback(key, meta);
}

/**
* Set (nested) property of object using dot notation
*
* @param {object} target
* @param {string} key
* @param value
*/
function setByKeyPath(target, key, value) {
var parts = key.split('.');

for (var i = 0; i < parts.length; i++) {
var part = parts[i];

if (i < parts.length -1) {
if (typeof target[part] !== 'object') {
target[part] = {};
}

target = target[part];
} else {
target[part] = value;
}
}
}

/**
* Get (nested) property of object using dot notation
*
* @param {object} target
* @param {string} key
* @param defaultValue
*/
function getByKeyPath(target, key, defaultValue) {
if (!target || !key) return false;

key = key.split('.');
var l = key.length,
i = 0,
p = '';

for (; i < l; ++i) {
p = key[i];

if (target.hasOwnProperty(p)) target = target[p];
else return defaultValue;
}

return target;
}

/**
* Build object of http headers from headers names and values
* @param {array|string} names Headers names
Expand Down
1 change: 1 addition & 0 deletions live-contract.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ <h1>LegalForm.js demo</h1>
<script src="js/lib/expand-condition.js"></script>
<script src="js/lib/parse-number.js"></script>
<script src="js/lib/cloner.js"></script>
<script src="js/lib/dot-key.js"></script>
<script src="js/model/form-model.js"></script>
<script src="js/model/legalform-model.js"></script>
<script src="js/model/live-contract-form-model.js"></script>
Expand Down
1 change: 1 addition & 0 deletions material.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ <h1>LegalForm.js demo with Material Design</h1>
<script src="js/lib/expand-condition.js"></script>
<script src="js/lib/parse-number.js"></script>
<script src="js/lib/cloner.js"></script>
<script src="js/lib/dot-key.js"></script>
<script src="js/model/form-model.js"></script>
<script src="js/model/legalform-model.js"></script>
<script src="js/model/live-contract-form-model.js"></script>
Expand Down
1 change: 1 addition & 0 deletions nomaterial.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ <h1>LegalForm.js demo with Material Design</h1>
<script src="js/lib/expand-condition.js"></script>
<script src="js/lib/parse-number.js"></script>
<script src="js/lib/cloner.js"></script>
<script src="js/lib/dot-key.js"></script>
<script src="js/model/form-model.js"></script>
<script src="js/model/legalform-model.js"></script>
<script src="js/model/live-contract-form-model.js"></script>
Expand Down

0 comments on commit 4ae3137

Please sign in to comment.