Skip to content

Commit 7e96117

Browse files
committed
issues #11 - moved the constants to a separate file and removed unnecessary initialization of properties
1 parent 975c55e commit 7e96117

File tree

18 files changed

+115
-137
lines changed

18 files changed

+115
-137
lines changed

src/components/code/CopyCodeButton.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { boundMethod } from 'autobind-decorator';
22

33
import RangeSliderOptions from '@shared/interface/globInterface';
4+
import { NAME_PLUGIN } from '@shared/constants';
45

56
class CopyCodeButton {
67
private className: string;
@@ -70,7 +71,7 @@ class CopyCodeButton {
7071
private handleCopyClick() {
7172
if (!this.list) return false;
7273

73-
let text = '$(\'.demo\').RangeSliderFox({\n';
74+
let text = `$('.demo').${NAME_PLUGIN}({\n`;
7475

7576
this.list.childNodes.forEach((item) => {
7677
const element = item as HTMLElement;

src/components/input-data/InputData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class InputData {
33

44
private input: HTMLInputElement | null = null;
55

6-
private className: string = '';
6+
private className: string;
77

88
private element: Element;
99

src/components/keyboard-control/KeyboardControl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class KeyboardControl {
1616

1717
private keyStepHoldCache: number = 0;
1818

19-
private nameClass: string = '';
19+
private nameClass: string;
2020

2121
private objRangeSlider: any;
2222

src/components/miscellaneous/Miscellaneous.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Different {
2727

2828
private reset: HTMLButtonElement | null = null;
2929

30-
private panel: Element | null = null;
30+
private panel: Element;
3131

3232
private modifier: string = '';
3333

@@ -87,7 +87,7 @@ class Different {
8787
this.orientation.checked = orientFlag;
8888
this.orientationCache = orientation ?? '';
8989

90-
const { classList } = this.panel as Element;
90+
const { classList } = this.panel;
9191

9292
if (orientFlag) {
9393
classList.add(this.modifier);
@@ -177,7 +177,7 @@ class Different {
177177
@boundMethod
178178
private handleOrientationClick(event: Event) {
179179
const element = event.target as HTMLInputElement;
180-
const { classList } = this.panel as Element;
180+
const { classList } = this.panel;
181181

182182
if (element.checked) {
183183
classList.add(this.modifier);
@@ -197,9 +197,7 @@ class Different {
197197
}
198198

199199
private setDomElement() {
200-
if (this.panel) {
201-
this.modifier = `${this.panel.classList[0]}_vertical`;
202-
}
200+
this.modifier = `${this.panel.classList[0]}_vertical`;
203201

204202
this.type = this.getDomElement('double');
205203
this.disabled = this.getDomElement('disabled');

src/components/panel/Panel.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import Hints from '@com/hints/Hints';
66
import Different from '@com/miscellaneous/Miscellaneous';
77
import CopyCodeButton from '@com/code/CopyCodeButton';
88
import KeyboardControl from '@com/keyboard-control/KeyboardControl';
9+
import { NAME_PLUGIN } from '@shared/constants';
910

1011
interface Actions {
1112
bindEvent: <T>(object: T) => boolean | void;
1213
}
1314

1415
class Panel {
15-
private element: Element | null = null;
16+
private element: Element;
1617

1718
private objectValues: Values | null = null;
1819

@@ -26,7 +27,7 @@ class Panel {
2627

2728
private objectCopyCode: CopyCodeButton | null = null;
2829

29-
private className: string = '';
30+
private className: string;
3031

3132
private objectKeyboardControl: KeyboardControl | null = null;
3233

@@ -37,8 +38,6 @@ class Panel {
3738
}
3839

3940
createRangeSlider(options: RangeSliderOptions) {
40-
if (!this.element) return false;
41-
4241
const element = this.element.querySelector(
4342
`${this.className}__slider-wrapper`,
4443
);
@@ -69,7 +68,7 @@ class Panel {
6968
onChange,
7069
onUpdate,
7170
onReset,
72-
}).data('RangeSliderFox'); // will return an object for one item
71+
}).data(NAME_PLUGIN); // will return an object for one item
7372

7473
const bindEvent = <T extends Actions | null>(object: T) => {
7574
if (!object) return false;
@@ -114,19 +113,17 @@ class Panel {
114113
private init() {
115114
this.objectValues = new Values('.js-values', this.getDomElement('.js-values') as Element);
116115

117-
if (this.element) {
118-
this.objectInputData = new InputData(this.className, this.element);
119-
}
116+
this.objectInputData = new InputData(this.className, this.element);
117+
120118
this.objectGrid = new Grid('.js-grid', this.getDomElement('.js-grid') as Element);
121119
this.objectHints = new Hints('.js-hints', this.getDomElement('.js-hints') as Element);
122120

123-
if (this.element) {
124-
this.objectDifferent = new Different(
125-
'.js-miscellaneous',
126-
this.getDomElement('.js-miscellaneous') as Element,
127-
this.element,
128-
);
129-
}
121+
this.objectDifferent = new Different(
122+
'.js-miscellaneous',
123+
this.getDomElement('.js-miscellaneous') as Element,
124+
this.element,
125+
);
126+
130127
this.objectCopyCode = new CopyCodeButton('.js-code', this.getDomElement('.js-code') as Element);
131128

132129
this.objectKeyboardControl = new KeyboardControl(
@@ -136,7 +133,6 @@ class Panel {
136133
}
137134

138135
private getDomElement(nameElement: string) {
139-
if (!this.element) return null;
140136
return this.element.querySelector(nameElement);
141137
}
142138

src/plugins/rangeSliderFox/mvc/controller/Controller.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { boundMethod } from 'autobind-decorator';
22

3+
import { NAME_PLUGIN } from '@shared/constants';
4+
35
import RangeSliderOptions from '../../globInterface';
46
import { ObserverOptions } from '../../Observer';
57
import Model from '../model/Model';
@@ -51,7 +53,7 @@ class Controller {
5153
if (!this.view) return false;
5254
const element = this.view.element as Element;
5355
if (element.constructor.name !== 'HTMLInputElement') return false;
54-
$.data(element, 'RangeSliderFox', null);
56+
$.data(element, NAME_PLUGIN, null);
5557
this.view.destroy();
5658
this.view = null;
5759
this.model = null;

src/plugins/rangeSliderFox/mvc/view/View.ts

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { boundMethod } from 'autobind-decorator';
22

3+
import { RANGE_SLIDER_NAME } from '@shared/constants';
4+
35
import { Observer, ObserverOptions } from '../../Observer';
46
import RangeSliderOptions from '../../globInterface';
57
import Handle from './subView/Handle';
@@ -9,9 +11,7 @@ import Grid from './subView/Grid';
911
import UpdateTip from './view.d';
1012

1113
class View extends Observer {
12-
private rangeSliderName: string = '';
13-
14-
private wrapperSlider: Element | null = null;
14+
private wrapperSlider: Element | null;
1515

1616
private rangeSlider: Element | null = null;
1717

@@ -39,26 +39,25 @@ class View extends Observer {
3939

4040
onHandle: (() => void) | null = null;
4141

42-
element: Element | null = null;
42+
element: Element;
4343

4444
constructor(element: Element) {
4545
super();
4646
this.element = element;
47-
this.rangeSliderName = 'range-slider-fox';
4847
this.wrapperSlider = this.element.parentElement;
4948

5049
this.init();
5150
}
5251

5352
destroy() {
54-
if (!this.element || !this.wrapperSlider) return false;
53+
if (!this.wrapperSlider) return false;
5554

5655
const typeElem = this.element.constructor.name;
5756
if (typeElem === 'HTMLInputElement') {
5857
const input = this.element as HTMLInputElement;
5958
input.value = ' ';
6059
}
61-
const element = this.wrapperSlider.querySelector(`.js-${this.rangeSliderName}`);
60+
const element = this.wrapperSlider.querySelector(`.js-${RANGE_SLIDER_NAME}`);
6261
if (element) { element.remove(); }
6362

6463
this.handle = null;
@@ -70,8 +69,6 @@ class View extends Observer {
7069
}
7170

7271
setValueInput(from: number, to: number, type: string) {
73-
if (!this.element) return false;
74-
7572
let string = '';
7673
if (this.element.constructor.name === 'HTMLInputElement') {
7774
const input = this.element as HTMLInputElement;
@@ -134,23 +131,23 @@ class View extends Observer {
134131
createDomElementBase() {
135132
this.rangeSlider = View.createElement(
136133
'div',
137-
[this.rangeSliderName, `js-${this.rangeSliderName}`],
134+
[RANGE_SLIDER_NAME, `js-${RANGE_SLIDER_NAME}`],
138135
);
139136
this.rangeSliderTop = View.createElement('div', [
140-
`${this.rangeSliderName}__top`,
141-
`js-${this.rangeSliderName}__top`,
137+
`${RANGE_SLIDER_NAME}__top`,
138+
`js-${RANGE_SLIDER_NAME}__top`,
142139
]);
143140
this.rangeSliderCenter = View.createElement('div', [
144-
`${this.rangeSliderName}__center`,
145-
`js-${this.rangeSliderName}__center`,
141+
`${RANGE_SLIDER_NAME}__center`,
142+
`js-${RANGE_SLIDER_NAME}__center`,
146143
]);
147144
this.rangeSliderBottom = View.createElement('div', [
148-
`${this.rangeSliderName}__bottom`,
149-
`js-${this.rangeSliderName}__bottom`,
145+
`${RANGE_SLIDER_NAME}__bottom`,
146+
`js-${RANGE_SLIDER_NAME}__bottom`,
150147
]);
151148
this.rangeSliderLine = View.createElement('span', [
152-
`${this.rangeSliderName}__line`,
153-
`js-${this.rangeSliderName}__line`,
149+
`${RANGE_SLIDER_NAME}__line`,
150+
`js-${RANGE_SLIDER_NAME}__line`,
154151
]);
155152

156153
this.rangeSliderCenter.appendChild(this.rangeSliderLine);
@@ -164,7 +161,7 @@ class View extends Observer {
164161
}
165162

166163
async setOrientation(type: string) {
167-
const modifier = `${this.rangeSliderName}_vertical`;
164+
const modifier = `${RANGE_SLIDER_NAME}_vertical`;
168165
const { classList } = this.rangeSlider as Element;
169166
this.vertical = type === 'vertical';
170167

@@ -408,8 +405,6 @@ class View extends Observer {
408405
const attributes = new Map();
409406

410407
const getDataAttribute = (item: string) => {
411-
if (!this.element) return false;
412-
413408
const attribute = `data-${item}`;
414409
if (this.element.hasAttribute(attribute)) {
415410
const value = this.element.getAttribute(attribute) ?? '';
@@ -466,7 +461,7 @@ class View extends Observer {
466461
});
467462

468463
observer.observe(
469-
(this.element as Element),
464+
this.element,
470465
{
471466
attributeFilter: nameAttributes,
472467
},
@@ -486,19 +481,10 @@ class View extends Observer {
486481
this.createDomElementBase(); // create basic DOM elements
487482
this.bindEvent(); // add event listeners
488483

489-
this.handle = new Handle(
490-
(this.rangeSliderCenter as HTMLElement),
491-
this.rangeSliderName,
492-
);
493-
this.hints = new Hints(
494-
this.rangeSliderTop as Element,
495-
this.rangeSliderName,
496-
);
497-
this.bar = new Bar(
498-
this.rangeSliderCenter as HTMLElement,
499-
this.rangeSliderName,
500-
);
501-
this.grid = new Grid(this.rangeSliderBottom as Element, this.rangeSliderName);
484+
this.handle = new Handle((this.rangeSliderCenter as HTMLElement));
485+
this.hints = new Hints(this.rangeSliderTop as Element);
486+
this.bar = new Bar(this.rangeSliderCenter as HTMLElement);
487+
this.grid = new Grid(this.rangeSliderBottom as Element);
502488

503489
this.createListeners();
504490
this.changeAttributes();

src/plugins/rangeSliderFox/mvc/view/subView/Bar.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ import {
44
getProperty,
55
setProperty,
66
} from '@shared/helpers/readWriteProperties';
7+
8+
import { RANGE_SLIDER_NAME } from '@shared/constants';
9+
710
import { Observer } from '../../../Observer';
811

912
class Bar extends Observer {
1013
private rangeSliderCenter: Element;
1114

12-
private rangeSliderName: string;
13-
1415
private elementBar: HTMLElement | null = null;
1516

1617
private bar: boolean = false;
1718

1819
private vertical: boolean = false;
1920

20-
constructor(element: HTMLElement | Element, rangeSliderName: string) {
21+
constructor(element: HTMLElement | Element) {
2122
super();
22-
this.rangeSliderName = rangeSliderName;
2323
this.rangeSliderCenter = element;
2424
}
2525

@@ -73,7 +73,7 @@ class Bar extends Observer {
7373
if (this.bar && this.elementBar) return false;
7474
if (!this.bar && !this.elementBar) return false;
7575

76-
const barName = `${this.rangeSliderName}__bar`;
76+
const barName = `${RANGE_SLIDER_NAME}__bar`;
7777
this.elementBar = Bar.createElement('span', [barName, `js-${barName}`]);
7878
this.rangeSliderCenter.appendChild(this.elementBar);
7979

0 commit comments

Comments
 (0)