Skip to content

Commit

Permalink
$mol_number removed format
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Nov 25, 2024
1 parent 8203550 commit 5971986
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 43 deletions.
14 changes: 0 additions & 14 deletions format/format.view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,8 @@ namespace $.$$ {
++ to
}

// Если по-сравнению с this.mask_prev в mask добавились/удалились не подчервивания слева от from
// То надо увеличить/уменьшить from и to, на кол-во добавленных/удаленных символов
const before = mask.slice(0, from).replace(/_/g, '').length
const before_prev = this.mask_prev.slice(0, from).replace(/_/g, '').length
let delta = before - before_prev

from += delta
to += delta


this.mask_prev = mask

return [ from, to ]
}

protected mask_prev = ''

@ $mol_mem
value_changed( next?: string ) {
Expand Down
4 changes: 1 addition & 3 deletions number/number.view.tree
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ $mol_number $mol_view
value? +NaN
enabled true
sub /
<= String $mol_format
allow <= allow <= allow_default \0123456789,.-
mask* <= mask* \
<= String $mol_string
- \ we don't want native number controls in mobile devices
- \ https://stackoverflow.com/questions/3790935/can-i-hide-the-html5-number-input-s-spin-box
type <= type \tel
Expand Down
32 changes: 6 additions & 26 deletions number/number.view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,6 @@ namespace $.$$ {
return val.toFixed( Math.ceil( fixed_number ) )
}
}

override allow() {
let next = this.allow_default()

const precision = this.precision_view()

// Точку в конце поставить нельзя, если precision_view целое число > 0
if ( precision - Math.floor(precision) === 0 ) next = next.replace(/[.,]/g, '')

// Минус нельзя ввести, если минимальное значение >=0
if (this.value_min() >= 0) next = next.replace('-', '')

return next
}

@ $mol_mem
override value_string( next? : string ): string {
Expand All @@ -77,9 +63,14 @@ namespace $.$$ {
const current = this.round( this.value_limited() )
if (next === undefined) return current

const precision = this.precision_view()

// Точку в конце поставить нельзя, если precision_view целое число > 0
if ( precision - Math.floor(precision) === 0 ) next = next.replace(/[.,]/g, '')

// Запятые меняем на точки, удаляем не-цифры и не-точки и лишние ноли в начале целой части.
// Минус получится ввести только в начале.
next = (next.startsWith('-') ? '-' : '')
next = (this.value_min() >= 0 && next.startsWith('-') ? '-' : '')
+ next.replace(/,/g, '.').replace(/[^\d\.]/g, '').replace(/^0{2,}/, '0')

let dot_pos = next.indexOf('.')
Expand Down Expand Up @@ -108,17 +99,6 @@ namespace $.$$ {
return this.round( this.value_limited(Number(next || Number.NaN)) )
}

override mask(val: string) {
const [_, minus, main = '', frac] = val.match(/(\-)?(\d+)(?:\.?(\d+))?/) ?? []

const prefix = (minus ? '_' : '') + format(main)
if (! frac) return prefix

const suffix = format(frac).split('').reverse().join('')

return prefix + '_' + suffix
}

@ $mol_mem
override dec_enabled() : boolean {
return this.enabled() && (
Expand Down

0 comments on commit 5971986

Please sign in to comment.