2022-11-10 17:59:24 +00:00
|
|
|
import * as commands from 'app/client/components/commands';
|
2024-02-14 21:18:09 +00:00
|
|
|
import { FieldRulesConfig } from 'app/client/components/Forms/FormConfig';
|
2022-11-10 17:59:24 +00:00
|
|
|
import { DataRowModel } from 'app/client/models/DataRowModel';
|
|
|
|
import { ViewFieldRec } from 'app/client/models/entities/ViewFieldRec';
|
|
|
|
import { KoSaveableObservable } from 'app/client/models/modelUtil';
|
|
|
|
import { NewAbstractWidget, Options } from 'app/client/widgets/NewAbstractWidget';
|
2023-09-21 16:57:58 +00:00
|
|
|
import { theme } from 'app/client/ui2018/cssVars';
|
2024-02-14 21:18:09 +00:00
|
|
|
import { dom, DomContents } from 'grainjs';
|
2022-11-10 17:59:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ToggleBase - The base class for toggle widgets, such as a checkbox or a switch.
|
|
|
|
*/
|
|
|
|
abstract class ToggleBase extends NewAbstractWidget {
|
2024-02-14 21:18:09 +00:00
|
|
|
public buildFormConfigDom(): DomContents {
|
|
|
|
return [
|
|
|
|
dom.create(FieldRulesConfig, this.field),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-10-25 15:04:40 +00:00
|
|
|
protected _addClickEventHandlers(row: DataRowModel) {
|
|
|
|
return [
|
|
|
|
dom.on('click', (event) => {
|
|
|
|
if (event.shiftKey) {
|
|
|
|
// Shift-click is for selection, don't also toggle the checkbox during it.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this.field.column().isRealFormula()) {
|
2023-12-08 05:59:42 +00:00
|
|
|
// Move the cursor here, and pretend that enter was pressed. This triggers an editing
|
2023-10-25 15:04:40 +00:00
|
|
|
// flow which is handled by CheckBoxEditor.skipEditor(). This way the edit applies to
|
|
|
|
// editRow, which handles setting default values based on widget linking.
|
|
|
|
commands.allCommands.setCursor.run(row, this.field);
|
2023-12-08 05:59:42 +00:00
|
|
|
commands.allCommands.input.run('<enter>');
|
2023-10-25 15:04:40 +00:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
dom.on('dblclick', (event) => {
|
|
|
|
// Don't start editing the field when a toggle is double-clicked.
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
}),
|
|
|
|
];
|
2022-11-10 17:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ToggleCheckBox extends ToggleBase {
|
|
|
|
constructor(field: ViewFieldRec, _options: Options = {}) {
|
2023-09-21 16:57:58 +00:00
|
|
|
super(field, {defaultTextColor: theme.toggleCheckboxFg.toString()});
|
2022-11-10 17:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public buildDom(row: DataRowModel) {
|
|
|
|
const value = row.cells[this.field.colId.peek()] as KoSaveableObservable<boolean>;
|
|
|
|
return dom('div.field_clip',
|
|
|
|
dom('div.widget_checkbox',
|
|
|
|
dom('div.widget_checkmark',
|
|
|
|
dom.show(value),
|
|
|
|
dom('div.checkmark_kick'),
|
|
|
|
dom('div.checkmark_stem')
|
|
|
|
),
|
2023-10-25 15:04:40 +00:00
|
|
|
this._addClickEventHandlers(row),
|
2022-11-10 17:59:24 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ToggleSwitch extends ToggleBase {
|
|
|
|
constructor(field: ViewFieldRec, _options: Options = {}) {
|
|
|
|
super(field, {defaultTextColor: '#2CB0AF'});
|
|
|
|
}
|
|
|
|
|
|
|
|
public buildDom(row: DataRowModel) {
|
|
|
|
const value = row.cells[this.field.colId.peek()] as KoSaveableObservable<boolean>;
|
|
|
|
return dom('div.field_clip',
|
|
|
|
dom('div.widget_switch',
|
|
|
|
dom.cls('switch_on', value),
|
|
|
|
dom.cls('switch_transition', row._isRealChange),
|
|
|
|
dom('div.switch_slider'),
|
|
|
|
dom('div.switch_circle'),
|
2023-10-25 15:04:40 +00:00
|
|
|
this._addClickEventHandlers(row),
|
2022-11-10 17:59:24 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|