-
Notifications
You must be signed in to change notification settings - Fork 14
/
rocker.ts
175 lines (167 loc) · 4.83 KB
/
rocker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// [Smoosic](https://github.com/AaronDavidNewman/Smoosic)
// Copyright (c) Aaron David Newman 2021.
/**
* @module /ui/dialog/components/rocker
* **/
import { buildDom } from '../../../common/htmlHelpers';
import { SuiComponentBase, SuiDialogNotifier, SuiComponentParent } from './baseComponent';
declare var $: any;
/**
* Basic dialog component info.
* @category SuiDialogParams
*/
export interface SuiRockerComponentParams {
id: string,
classes: string,
dataType?: string,
increment?: number,
label: string,
smoName: string,
control: string
}
/**
* A numeric input box with +- buttons. Adjustable type and scale
* */
export class SuiRockerComponent extends SuiComponentBase {
static get dataTypes() {
return ['int', 'float', 'percent'];
}
static get increments(): Record<string, number> {
return { 'int': 1, 'float': 0.1, 'percent': 10 };
}
static get parsers(): Record<string, string> {
return { 'int': '_getIntValue', 'float': '_getFloatValue', 'percent': '_getPercentValue' };
}
initialValue: number = 0;
dataType: string;
increment: number = 1;
parser: string;
constructor(dialog: SuiDialogNotifier, params: SuiRockerComponentParams) {
super(dialog, params);
this.dataType = params.dataType ?? 'int';
this.increment = params.increment ?? SuiRockerComponent.increments[this.dataType];
if (SuiRockerComponent.dataTypes.indexOf(this.dataType) < 0) {
throw new Error('dialog element invalid type ' + this.dataType);
}
if (this.dataType === 'int' && this.increment < 1) {
throw new Error('int component with decimal increment');
}
this.parser = SuiRockerComponent.parsers[this.dataType];
this.dialog = dialog;
}
get html() {
const b = buildDom;
const id = this.parameterId;
const r = b('div').classes(this.makeClasses('rockerControl smoControl')).attr('id', id).attr('data-param', this.smoName)
.append(
b('button').classes('increment').append(
b('span').classes('icon icon-circle-up'))).append(
b('button').classes('decrement').append(
b('span').classes('icon icon-circle-down'))).append(
b('input').attr('type', 'text').classes('rockerInput')
.attr('id', id + '-input')).append(
b('label').attr('for', id + '-input').text(this.label));
return r;
}
get parameterId() {
return this.id;
}
handleChange() {
this.changeFlag = true;
this.dialog.changed();
this.changeFlag = false;
}
bind() {
const pid = this.parameterId;
const input = this._getInputElement();
let val = 0;
$('#' + pid).find('button.increment').off('click').on('click',
() => {
val = (this as any)[this.parser]();
if (this.dataType === 'percent') {
val = 100 * val;
}
$(input).val(val + this.increment);
this.handleChanged();
}
);
$('#' + pid).find('button.decrement').off('click').on('click',
() => {
val = (this as any)[this.parser]();
if (this.dataType === 'percent') {
val = 100 * val;
}
$(input).val(val - this.increment);
this.handleChanged();
}
);
$(input).off('blur').on('blur',
() => {
val = (this as any)[this.parser]();
if (val !== this.initialValue) {
this.initialValue = val;
this.handleChanged();
}
}
);
}
_getInputElement() {
const pid = this.parameterId;
return $(this.dialog.dgDom.element).find('#' + pid).find('input');
}
_getIntValue() {
let val:number = parseInt(this._getInputElement().val(), 10);
val = isNaN(val) ? 0 : val;
return val;
}
_getFloatValue() {
let val = parseFloat(this._getInputElement().val());
val = isNaN(val) ? 1.0 : val;
return val;
}
_getPercentValue() {
let val = parseFloat(this._getInputElement().val());
val = isNaN(val) ? 1 : val;
return val / 100;
}
_setIntValue(val: string | number) {
this._getInputElement().val(val);
}
setValue(value: number) {
if (this.dataType === 'percent') {
value = value * 100;
}
this._setIntValue(value);
this.initialValue = value;
}
getValue() {
return (this as any)[this.parser]();
}
}
/**
* Create rocker composite
* @param{id}
*/
export interface SuiRockerCompositeParams {
id: string,
classes: string,
dataType?: string,
increment?: number,
defaultValue: number,
label: string,
smoName: string,
control: string,
parentControl: SuiComponentParent
}
export class SuiRockerComposite extends SuiRockerComponent {
parentControl: SuiComponentParent;
constructor(dialog: SuiDialogNotifier, parameters: SuiRockerCompositeParams) {
super(dialog, parameters);
this.parentControl = parameters.parentControl;
}
handleChanged() {
this.changeFlag = true;
this.parentControl.changed();
this.changeFlag = false;
}
}