gristlabs_grist-core/app/client/widgets/TextBox.js
Paul Fitzpatrick 1654a2681f (core) move client code to core
Summary:
This moves all client code to core, and makes minimal fix-ups to
get grist and grist-core to compile correctly.  The client works
in core, but I'm leaving clean-up around the build and bundles to
follow-up.

Test Plan: existing tests pass; server-dev bundle looks sane

Reviewers: dsagal

Reviewed By: dsagal

Differential Revision: https://phab.getgrist.com/D2627
2020-10-02 13:24:21 -04:00

72 lines
2.4 KiB
JavaScript

var _ = require('underscore');
var ko = require('knockout');
var dom = require('../lib/dom');
var dispose = require('../lib/dispose');
var kd = require('../lib/koDom');
var AbstractWidget = require('./AbstractWidget');
var modelUtil = require('../models/modelUtil');
const {fromKoSave} = require('app/client/lib/fromKoSave');
const {alignmentSelect, buttonToggleSelect} = require('app/client/ui2018/buttonSelect');
const {testId} = require('app/client/ui2018/cssVars');
const {cssRow} = require('app/client/ui/RightPanel');
const {Computed} = require('grainjs');
/**
* TextBox - The most basic widget for displaying text information.
*/
function TextBox(field) {
AbstractWidget.call(this, field);
this.alignment = this.options.prop('alignment');
let wrap = this.options.prop('wrap');
this.wrapping = this.autoDispose(ko.computed({
read: () => {
let w = wrap();
if (w === null || w === undefined) {
// When user has yet to specify a desired wrapping state, GridView and DetailView have
// different default states. GridView defaults to wrapping disabled, while DetailView
// defaults to wrapping enabled.
return (this.field.viewSection().parentKey() === 'record') ? false : true;
} else {
return w;
}
},
write: val => wrap(val)
}));
modelUtil.addSaveInterface(this.wrapping, val => wrap.saveOnly(val));
this.autoDispose(this.wrapping.subscribe(() =>
this.field.viewSection().events.trigger('rowHeightChange')
));
}
dispose.makeDisposable(TextBox);
_.extend(TextBox.prototype, AbstractWidget.prototype);
TextBox.prototype.buildConfigDom = function() {
const wrapping = Computed.create(null, use => use(this.wrapping));
wrapping.onWrite((val) => modelUtil.setSaveValue(this.wrapping, Boolean(val)));
return dom('div',
cssRow(
dom.autoDispose(wrapping),
alignmentSelect(fromKoSave(this.alignment)),
dom('div', {style: 'margin-left: 8px;'},
buttonToggleSelect(wrapping, [{value: true, icon: 'Wrap'}]),
testId('tb-wrap-text')
)
)
);
};
TextBox.prototype.buildDom = function(row) {
let value = row[this.field.colId()];
return dom('div.field_clip',
kd.style('text-align', this.alignment),
kd.toggleClass('text_wrapping', this.wrapping),
kd.text(() => row._isAddRow() ? '' : this.valueFormatter().format(value()))
);
};
module.exports = TextBox;