Skip to content

Commit 27af58c

Browse files
committed
code refactoring
1 parent a055c8f commit 27af58c

File tree

4 files changed

+26
-29
lines changed

4 files changed

+26
-29
lines changed

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,21 @@ $('.rslider__fox').RangeSliderFox({
113113
```javascript
114114
const obj = $(dom).RangeSliderFox({}).data('RangeSliderFox');
115115

116+
// This piece of code will change range values and set the dot on position 350.
116117
obj.update({
117118
min:0,
118119
max:600,
119120
from:350
120121
})
121122

122123
// unsubscribe from events
123-
124124
obj.update({
125125
onStart:null,
126126
onChange:null,
127127
onUpdate:null,
128128
onReset:null
129129
})
130130

131-
// This piece of code will change range values and set the dot on position 350.
132131

133132
obj.reset()
134133

@@ -152,9 +151,9 @@ obj.destroy()
152151
| max | data-max | 10 (-n, n.n..) | number | Maximal value of the range |
153152
| to | data-to | 2 (-n, n.n..) | number | Second dot position |
154153
| from | data-from | 1 (-n, n.n..) | number | First dot position |
155-
| step | data-step | 0 (max-mun) | number | Step of the dot mooving |
156-
| keyStepOne | data-key-step-one | 0 (max-mun) | number | Step of the dot mooving on keyboard key single pressing |
157-
| keyStepHold | data-key-step-hold | 0 (max-mun) | number | Step of the dot mooving on keyboard key holding |
154+
| step | data-step | 0 (n, n.n.. <= max-min) | number | Step of the dot mooving |
155+
| keyStepOne | data-key-step-one | 0 (max-min) | number | Step of the dot mooving on keyboard key single pressing |
156+
| keyStepHold | data-key-step-hold | 0 (max-min) | number | Step of the dot mooving on keyboard key holding |
158157
| bar | data-bar | false | boolean | Progrees-bar view (shown or hidden) |
159158
| tipPrefix | data-tip-prefix | '' (char…) | string | Prefix for hints (15 characters maximum) |
160159
| tipPostfix | data-tip-postfix | '' (char…) | string | Postfix for hints (15 characters maximum) |
@@ -171,6 +170,14 @@ obj.destroy()
171170
| onUpdate | - | null | Function | Call a callback function after “update” method calling and pass an object of current configuration data as an argument |
172171
| onReset | - | null | Function | Call a callback function after “reset” method calling and pass an object of current configuration data as an argument |
173172

173+
## details
174+
175+
* only one of values gridNum or gridStep can be set for scale. If both of them are set, gridStep will be ignored
176+
177+
* gridSnap is ignored if one of parameters step, keyStepOne, keyStepHold is set
178+
179+
* step is ignored while keyboard controlling if one of parameters keyStepOne, keyStepHold is set
180+
174181

175182
## Setup and Scripts ( build the project, start, testing, production)
176183

src/plugins/range-slider-fox/mvc/model/__tests__/model.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,9 @@ describe('------- Test Model API -------', () => {
342342
await model.onHandle();
343343

344344
mas = await [
345-
-1.45, 1.5,
346-
4.5, 7.5,
347-
10.5, 13.5,
345+
-1.45, 1.6,
346+
4.6, 7.6,
347+
10.6, 13.6,
348348
15.234
349349
];
350350

src/plugins/range-slider-fox/mvc/model/model.ts

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class Model extends Observer {
8787
let opKey = Object.keys(op);
8888
for (let key of opKey) {
8989
// использую type assertions так как не нашёл возможности передавать нужный тип
90-
// не могу отказаться от данной конструкции кода так как это сильно уменьшает копипаст
90+
// не могу отказаться от данной конструкции кода, так как это сильно уменьшает копипаст
9191
const val = this.getProperty(op, key as keyof RangeSliderOptions);
9292
this.setProperty(this, key as keyof Model, val as this[keyof Model]);
9393
}
@@ -260,14 +260,14 @@ class Model extends Observer {
260260

261261
const len = this.trimFraction(this.step);
262262
let mas: number[] = [];
263-
let kol = this.fixedNum((this.min + this.step), len);
263+
let kol = +(this.min + this.step).toFixed(len);
264264
mas.push(this.min);
265265
for (let i = this.min; i <= this.max;) {
266-
i = this.fixedNum((i += this.step), len);
266+
i = +(i += this.step).toFixed(len);
267267
if (kol == i && i < this.max) {
268268
mas.push(kol);
269-
kol = this.fixedNum((kol += this.step), len);
270-
}
269+
kol = +(kol += this.step).toFixed(len);
270+
} else break;
271271
}
272272
mas.push(this.max);
273273

@@ -539,10 +539,10 @@ class Model extends Observer {
539539

540540
if (dotF) {
541541
const num = !signF ? from - step : from + step;
542-
from = this.fixedNum(num, len);
542+
from = +num.toFixed(len);
543543
} else {
544544
const num = !signF ? to - step : to + step;
545-
to = this.fixedNum(num, len);
545+
to = +num.toFixed(len);
546546
}
547547
if (this.type == 'double') {
548548
if (from > to && dotF) from = to;
@@ -556,7 +556,7 @@ class Model extends Observer {
556556
}
557557
if (!this.keyStepHold && this.keyStepOne) {
558558
const len = this.trimFraction(this.keyStepOne);
559-
this.keyStepHold = this.fixedNum(this.keyStepOne, len);
559+
this.keyStepHold = +this.keyStepOne.toFixed(len);
560560
}
561561
repeat ? value(this.keyStepHold) : value(this.keyStepOne);
562562
} else if (this.step) {
@@ -595,18 +595,6 @@ class Model extends Observer {
595595
return +(this.min + (this.toP * this.valP)).toFixed(0);
596596
}
597597

598-
private fixedNum(num: number, len: number) {
599-
let temp = '';
600-
let fl = false;
601-
let k = 0;
602-
for (let item of String(num)) {
603-
if (item == '.') fl = true;
604-
temp += item;
605-
if (fl) ++k;
606-
if (k == len + 1) break;
607-
}
608-
return parseFloat(temp);
609-
}
610598

611599
private trimFraction(num: number) {
612600
const integer = Math.trunc(num);
@@ -703,7 +691,7 @@ class Model extends Observer {
703691
private checkValue(data: PROP, str: string) {
704692
const _this = this;
705693
// использую type assertions так как не нашёл возможности передавать нужный тип
706-
// не могу отказаться от данной конструкции кода так как это сильно уменьшает копипаст
694+
// не могу отказаться от данной конструкции кода, так как это сильно уменьшает копипаст
707695
const key = str as keyof typeof _this;
708696
const val = this[key];
709697
const valF = (val ?? null) != null ? true : false;

src/plugins/range-slider-fox/mvc/view/view.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ class View extends Observer {
365365
if (data) {
366366
const key = String(data[0]);
367367
const val = data[1];
368+
// использую type assertions так как не нашёл возможности передавать нужный тип
369+
// не могу отказаться от данной конструкции кода, так как это сильно уменьшает копипаст
368370
this.setProperty(obj, key as keyof RangeSliderOptions, val);
369371
}
370372
}

0 commit comments

Comments
 (0)