Skip to content

Commit

Permalink
Merge branch 'master' into price_fields_should_be_stored_as_float_com…
Browse files Browse the repository at this point in the history
…ma_fix
  • Loading branch information
svenstm committed Jan 22, 2019
2 parents 17b02aa + afa2c67 commit 3ff2e39
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 11 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ <h1>LegalForm.js demo</h1>
<script src="js/lib/calculation-vars.js"></script>
<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/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
208 changes: 208 additions & 0 deletions js/lib/cloner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@

/**
* We use this lib in LT for deep cloning, in order to preserve getter methods, like toString.
* Method $.extend() does not preserve such methods.
*/

/*!
Copyright (C) 2015 by Andrea Giammarchi - @WebReflection
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
var cloner = (function (O) {'use strict';

// (C) Andrea Giammarchi - Mit Style

var

// constants
VALUE = 'value',
PROTO = '__proto__', // to avoid jshint complains

// shortcuts
isArray = Array.isArray,
create = O.create,
dP = O.defineProperty,
dPs = O.defineProperties,
gOPD = O.getOwnPropertyDescriptor,
gOPN = O.getOwnPropertyNames,
gOPS = O.getOwnPropertySymbols ||
function (o) { return Array.prototype; },
gPO = O.getPrototypeOf ||
function (o) { return o[PROTO]; },
hOP = O.prototype.hasOwnProperty,
oKs = (typeof Reflect !== typeof oK) &&
Reflect.ownKeys ||
function (o) { return gOPS(o).concat(gOPN(o)); },
set = function (descriptors, key, descriptor) {
if (key in descriptors) dP(descriptors, key, {
configurable: true,
enumerable: true,
value: descriptor
});
else descriptors[key] = descriptor;
},

// used to avoid recursions in deep copy
index = -1,
known = null,
blown = null,
clean = function () { known = blown = null; },

// utilities
New = function (source, descriptors) {
var out = isArray(source) ? [] : create(gPO(source));
return descriptors ? Object.defineProperties(out, descriptors) : out;
},

// deep copy and merge
deepCopy = function deepCopy(source) {
var result = New(source);
known = [source];
blown = [result];
deepDefine(result, source);
clean();
return result;
},
deepMerge = function (target) {
known = [];
blown = [];
for (var i = 1; i < arguments.length; i++) {
known[i - 1] = arguments[i];
blown[i - 1] = target;
}
merge.apply(true, arguments);
clean();
return target;
},

// shallow copy and merge
shallowCopy = function shallowCopy(source) {
clean();
for (var
key,
descriptors = {},
keys = oKs(source),
i = keys.length; i--;
set(descriptors, key, gOPD(source, key))
) key = keys[i];
return New(source, descriptors);
},
shallowMerge = function () {
clean();
return merge.apply(false, arguments);
},

// internal methods
isObject = function isObject(value) {
/*jshint eqnull: true */
return value != null && typeof value === 'object';
},
shouldCopy = function shouldCopy(value) {
/*jshint eqnull: true */
index = -1;
if (isObject(value)) {
if (known == null) return true;
index = known.indexOf(value);
if (index < 0) return 0 < known.push(value);
}
return false;
},
deepDefine = function deepDefine(target, source) {
for (var
key, descriptor,
descriptors = {},
keys = oKs(source),
i = keys.length; i--;
) {
key = keys[i];
descriptor = gOPD(source, key);
if (VALUE in descriptor) deepValue(descriptor);
set(descriptors, key, descriptor);
}
dPs(target, descriptors);
},
deepValue = function deepValue(descriptor) {
var value = descriptor[VALUE];
if (shouldCopy(value)) {
descriptor[VALUE] = New(value);
deepDefine(descriptor[VALUE], value);
blown[known.indexOf(value)] = descriptor[VALUE];
} else if (-1 < index && index in blown) {
descriptor[VALUE] = blown[index];
}
},
merge = function merge(target) {
for (var
source,
keys, key,
value, tvalue,
descriptor,
deep = this.valueOf(),
descriptors = {},
i, a = 1;
a < arguments.length; a++
) {
source = arguments[a];
keys = oKs(source);
for (i = 0; i < keys.length; i++) {
key = keys[i];
descriptor = gOPD(source, key);
if (hOP.call(target, key)) {
if (VALUE in descriptor) {
value = descriptor[VALUE];
if (shouldCopy(value)) {
descriptor = gOPD(target, key);
if (VALUE in descriptor) {
tvalue = descriptor[VALUE];
if (isObject(tvalue)) {
merge.call(deep, tvalue, value);
}
}
}
}
} else {
if (deep && VALUE in descriptor) {
deepValue(descriptor);
}
}
set(descriptors, key, descriptor);
}
}
return dPs(target, descriptors);
}
;

return {
deep: {
copy: deepCopy,
merge: deepMerge
},
shallow: {
copy: shallowCopy,
merge: shallowMerge
}
};

}(Object));

if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = cloner;
}
36 changes: 25 additions & 11 deletions js/ractive-legalform.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@
} else if (repeater > stepCount) {
var addLength = repeater - stepCount;
for (var i = 0; i < addLength; i++) {
value.push($.extend(true, {}, tmpl));
var newItem = cloner.deep.copy(tmpl);
value.push(newItem);
}
}

Expand Down Expand Up @@ -409,7 +410,7 @@
this.initPreviewSwitch();
this.refreshLikerts();

metaRecursive(this.meta, $.proxy(this.initField, this));
metaRecursive(this.get('meta'), $.proxy(this.initField, this));

this.on('complete', function() {
$('#doc').trigger('shown.preview');
Expand Down Expand Up @@ -460,6 +461,12 @@
*/
initAmountField: function (key, meta) {
var value = this.get(key);

var defaultObj = getByKeyPath(this.defaults, key, undefined);
if (typeof defaultObj !== 'undefined') {
setAmountToStringMethod(defaultObj);
}

if (!value) return;

if (value.amount === '') {
Expand All @@ -481,15 +488,7 @@
value.unit = newUnits[0];
}

if (!value.hasOwnProperty('toString')) {
//Set toString method
var toString = function() {
return (this.amount !== '' && this.amount !== null) ? this.amount + ' ' + this.unit : '';
};

defineProperty(value, 'toString', toString);
}

setAmountToStringMethod(value);
this.update(key);
},

Expand Down Expand Up @@ -1187,6 +1186,21 @@
selectize.onSearchChange(selectedText);
}

/**
* Set toString method for number with unit field
* @param {object} value
*/
function setAmountToStringMethod(value) {
if (!value.hasOwnProperty('toString')) {
//Set toString method
var toString = function() {
return (this.amount !== '' && this.amount !== null) ? this.amount + ' ' + this.unit : '';
};

defineProperty(value, 'toString', toString);
}
}

/**
* Determine if keypath ends with string
* @param {string} keypath
Expand Down
1 change: 1 addition & 0 deletions live-contract.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ <h1>LegalForm.js demo</h1>
<script src="js/lib/calculation-vars.js"></script>
<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/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 @@ -65,6 +65,7 @@ <h1>LegalForm.js demo with Material Design</h1>
<script src="js/lib/calculation-vars.js"></script>
<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/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 @@ -76,6 +76,7 @@ <h1>LegalForm.js demo with Material Design</h1>
<script src="js/lib/calculation-vars.js"></script>
<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/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 3ff2e39

Please sign in to comment.