diff --git a/dev/Component/Abstract.js b/dev/Component/Abstract.js deleted file mode 100644 index 848414571..000000000 --- a/dev/Component/Abstract.js +++ /dev/null @@ -1,14 +0,0 @@ - -export class AbstractComponent { - constructor() { - this.disposable = []; - } - - dispose() { - this.disposable.forEach((funcToDispose) => { - if (funcToDispose && funcToDispose.dispose) { - funcToDispose.dispose(); - } - }); - } -} diff --git a/dev/Component/AbstractCheckbox.js b/dev/Component/AbstractCheckbox.js index 0bf84f65d..812da3108 100644 --- a/dev/Component/AbstractCheckbox.js +++ b/dev/Component/AbstractCheckbox.js @@ -1,13 +1,10 @@ import ko from 'ko'; -import { AbstractComponent } from 'Component/Abstract'; -class AbstractCheckbox extends AbstractComponent { +export class AbstractCheckbox { /** * @param {Object} params = {} */ constructor(params = {}) { - super(); - this.value = ko.isObservable(params.value) ? params.value : ko.observable(!!params.value); @@ -24,5 +21,3 @@ class AbstractCheckbox extends AbstractComponent { this.enable() && this.value(!this.value()); } } - -export { AbstractCheckbox }; diff --git a/dev/Component/AbstractInput.js b/dev/Component/AbstractInput.js index 9fec3ea44..33bae112a 100644 --- a/dev/Component/AbstractInput.js +++ b/dev/Component/AbstractInput.js @@ -1,16 +1,14 @@ import ko from 'ko'; import { pInt } from 'Common/Utils'; import { SaveSettingsStep } from 'Common/Enums'; -import { AbstractComponent } from 'Component/Abstract'; import { koComputable } from 'External/ko'; +import { dispose } from 'External/ko'; -class AbstractInput extends AbstractComponent { +export class AbstractInput { /** * @param {Object} params */ constructor(params) { - super(); - this.value = params.value || ''; this.label = params.label || ''; this.enable = null == params.enable ? true : params.enable; @@ -43,13 +41,17 @@ class AbstractInput extends AbstractComponent { (size + ' settings-saved-trigger-input ' + classForTrigger()).trim() ); - this.disposable.push(this.trigger.subscribe(setTriggerState, this)); + this.disposable = [ + this.trigger.subscribe(setTriggerState, this), + this.className + ]; } else { this.className = size; + this.disposable = []; } + } - this.disposable.push(this.className); + dispose() { + this.disposable.forEach(dispose); } } - -export { AbstractInput }; diff --git a/dev/Component/EmailAddresses.js b/dev/Component/EmailAddresses.js index 87eee5274..829168fe9 100644 --- a/dev/Component/EmailAddresses.js +++ b/dev/Component/EmailAddresses.js @@ -1,5 +1,4 @@ import { doc, createElement } from 'Common/Globals'; -import { isArray } from 'Common/Utils'; import { EmailModel } from 'Model/Email'; const contentType = 'snappymail/emailaddress', @@ -17,7 +16,7 @@ export class EmailAddressesComponent { doc.body.append(datalist); } - var self = this, + const self = this, // In Chrome we have no access to dataTransfer.getData unless it's the 'drop' event // In Chrome Mobile dataTransfer.types.includes(contentType) fails, only text/plain is set validDropzone = () => dragAddress && dragAddress.li.parentNode !== self.ul, @@ -138,20 +137,56 @@ export class EmailAddressesComponent { _parseValue(val) { if (val) { - var self = this, - values = []; - - const v = val.trim(), - hook = (v && [',', ';', '\n'].includes(v.slice(-1))) - ? EmailModel.splitEmailLine(val) - : null; - - values = (hook || [val]).map(value => EmailModel.parseEmailLine(value)) - .flat(Infinity) - .map(item => (item.toLine ? [item.toLine(false), item] : [item, null])); + const self = this, + v = val.trim(), + hook = (v && [',', ';', '\n'].includes(v.slice(-1))) ? EmailModel.splitEmailLine(val) : null, + values = (hook || [val]).map(value => EmailModel.parseEmailLine(value)) + .flat(Infinity) + .map(item => (item.toLine ? [item.toLine(false), item] : [item, null])); if (values.length) { - self._setChosen(values); + values.forEach(a => { + var v = a[0].trim(), + exists = false, + lastIndex = -1, + obj = { + key : '', + obj : null, + value : '' + }; + + self._chosenValues.forEach((vv, kk) => { + if (vv.value === self._lastEdit) { + lastIndex = kk; + } + + vv.value === v && (exists = true); + }); + + if (v !== '' && a[1] && !exists) { + + obj.key = 'mi_' + Math.random().toString( 16 ).slice( 2, 10 ); + obj.value = v; + obj.obj = a[1]; + + if (-1 < lastIndex) { + self._chosenValues.splice(lastIndex, 0, obj); + } else { + self._chosenValues.push(obj); + } + + self._lastEdit = ''; + self._renderTags(); + } + }); + + if (values.length === 1 && values[0] === '' && self._lastEdit !== '') { + self._lastEdit = ''; + self._renderTags(); + } + + self._setValue(self._buildValue()); + return true; } } @@ -202,56 +237,6 @@ export class EmailAddressesComponent { self._resizeInput(ev); } - _setChosen(valArr) { - var self = this; - - if (!isArray(valArr)){ - return false; - } - - valArr.forEach(a => { - var v = a[0].trim(), - exists = false, - lastIndex = -1, - obj = { - key : '', - obj : null, - value : '' - }; - - self._chosenValues.forEach((vv, kk) => { - if (vv.value === self._lastEdit) { - lastIndex = kk; - } - - vv.value === v && (exists = true); - }); - - if (v !== '' && a && a[1] && !exists) { - - obj.key = 'mi_' + Math.random().toString( 16 ).slice( 2, 10 ); - obj.value = v; - obj.obj = a[1]; - - if (-1 < lastIndex) { - self._chosenValues.splice(lastIndex, 0, obj); - } else { - self._chosenValues.push(obj); - } - - self._lastEdit = ''; - self._renderTags(); - } - }); - - if (valArr.length === 1 && valArr[0] === '' && self._lastEdit !== '') { - self._lastEdit = ''; - self._renderTags(); - } - - self._setValue(self._buildValue()); - } - _buildValue() { return this._chosenValues.map(v => v.value).join(','); } diff --git a/dev/External/ko.js b/dev/External/ko.js index ec3bcec10..3a171b37f 100644 --- a/dev/External/ko.js +++ b/dev/External/ko.js @@ -3,12 +3,15 @@ import { doc, createElement } from 'Common/Globals'; import { SaveSettingsStep } from 'Common/Enums'; import { arrayLength, isFunction } from 'Common/Utils'; -/** - * The value of the pureComputed observable shouldn’t vary based on the - * number of evaluations or other “hidden” information. Its value should be - * based solely on the values of other observables in the application - */ -export const koComputable = fn => ko.computed(fn, {'pure':true}); +export const + /** + * The value of the pureComputed observable shouldn’t vary based on the + * number of evaluations or other “hidden” information. Its value should be + * based solely on the values of other observables in the application + */ + koComputable = fn => ko.computed(fn, {'pure':true}), + + dispose = disposable => disposable && isFunction(disposable.dispose) && disposable.dispose(); ko.bindingHandlers.tooltipErrorTip = { init: (element, fValueAccessor) => { diff --git a/dev/Knoin/AbstractModel.js b/dev/Knoin/AbstractModel.js index d36aa2cc2..311cd1ccc 100644 --- a/dev/Knoin/AbstractModel.js +++ b/dev/Knoin/AbstractModel.js @@ -1,10 +1,5 @@ -import { isArray, isFunction, addObservablesTo, addComputablesTo, forEachObjectValue, forEachObjectEntry } from 'Common/Utils'; - -function dispose(disposable) { - if (disposable && isFunction(disposable.dispose)) { - disposable.dispose(); - } -} +import { isArray, addObservablesTo, addComputablesTo, forEachObjectValue, forEachObjectEntry } from 'Common/Utils'; +import { dispose } from 'External/ko'; function typeCast(curValue, newValue) { if (null != curValue) {