2022-10-21 10:55:01 +00:00
|
|
|
/* globals KeyboardEvent */
|
|
|
|
|
|
|
|
const _ = require('underscore');
|
|
|
|
const ko = require('knockout');
|
|
|
|
const moment = require('moment-timezone');
|
|
|
|
const {nativeCompare, roundDownToMultiple, waitObs} = require('app/common/gutil');
|
|
|
|
const gutil = require('app/common/gutil');
|
2022-04-19 13:01:08 +00:00
|
|
|
const MANUALSORT = require('app/common/gristTypes').MANUALSORT;
|
2022-10-21 10:55:01 +00:00
|
|
|
const gristTypes = require('app/common/gristTypes');
|
|
|
|
const tableUtil = require('../lib/tableUtil');
|
|
|
|
const {DataRowModel} = require('../models/DataRowModel');
|
|
|
|
const {DynamicQuerySet} = require('../models/QuerySet');
|
|
|
|
const {SortFunc} = require('app/common/SortFunc');
|
|
|
|
const rowset = require('../models/rowset');
|
|
|
|
const Base = require('./Base');
|
2023-12-18 17:50:57 +00:00
|
|
|
const {getDefaultColValues} = require("./BaseView2");
|
2022-10-21 10:55:01 +00:00
|
|
|
const {Cursor} = require('./Cursor');
|
|
|
|
const FieldBuilder = require('../widgets/FieldBuilder');
|
|
|
|
const commands = require('./commands');
|
|
|
|
const BackboneEvents = require('backbone').Events;
|
2020-10-02 15:10:00 +00:00
|
|
|
const {ClientColumnGetters} = require('app/client/models/ClientColumnGetters');
|
2021-10-01 19:38:58 +00:00
|
|
|
const {reportError, reportSuccess} = require('app/client/models/errors');
|
2020-10-02 15:10:00 +00:00
|
|
|
const {urlState} = require('app/client/models/gristUrlState');
|
|
|
|
const {SectionFilter} = require('app/client/models/SectionFilter');
|
2023-12-18 17:50:57 +00:00
|
|
|
const {UnionRowSource} = require('app/client/models/UnionRowSource');
|
2023-04-28 09:20:28 +00:00
|
|
|
const {copyToClipboard} = require('app/client/lib/clipboardUtils');
|
2020-10-02 15:10:00 +00:00
|
|
|
const {setTestState} = require('app/client/lib/testState');
|
2020-11-11 20:25:37 +00:00
|
|
|
const {ExtraRows} = require('app/client/models/DataTableModelWithDiff');
|
2020-10-02 15:10:00 +00:00
|
|
|
const {createFilterMenu} = require('app/client/ui/ColumnFilterMenu');
|
2022-02-16 14:15:27 +00:00
|
|
|
const {closeRegisteredMenu} = require('app/client/ui2018/menus');
|
2022-10-17 09:47:16 +00:00
|
|
|
const {COMMENTS} = require('app/client/models/features');
|
2022-10-21 10:55:01 +00:00
|
|
|
const {DismissedPopup} = require('app/common/Prefs');
|
|
|
|
const {markAsSeen} = require('app/client/models/UserPrefs');
|
|
|
|
const {buildConfirmDelete, reportUndo} = require('app/client/components/modals');
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* BaseView forms the basis for ViewSection classes.
|
|
|
|
* @param {Object} viewSectionModel - The model for the viewSection represented.
|
2021-11-19 05:35:01 +00:00
|
|
|
* @param {Boolean} options.isPreview - Whether the view is a read-only preview (e.g. Importer view).
|
2020-10-02 15:10:00 +00:00
|
|
|
* @param {Boolean} options.addNewRow - Whether to include an add row in the model.
|
|
|
|
*/
|
|
|
|
function BaseView(gristDoc, viewSectionModel, options) {
|
|
|
|
Base.call(this, gristDoc);
|
|
|
|
|
|
|
|
this.options = options || {};
|
|
|
|
this.viewSection = viewSectionModel;
|
|
|
|
this._name = this.viewSection.titleDef.peek();
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Observable models mapped to the document
|
|
|
|
|
|
|
|
// Instantiate the models for the view metadata and for the data itself.
|
|
|
|
// The table should never change for a given view, so no need to watch the table() observable.
|
|
|
|
this.schemaModel = this.viewSection.table();
|
|
|
|
|
|
|
|
// Check if we are making a comparison with another document.
|
|
|
|
this.comparison = this.gristDoc.comparison;
|
|
|
|
|
|
|
|
// TODO: but accessing by tableId identifier may be problematic when the table is renamed.
|
|
|
|
this.tableModel = this.gristDoc.getTableModelMaybeWithDiff(this.schemaModel.tableId());
|
2020-11-11 20:25:37 +00:00
|
|
|
this.extraRows = new ExtraRows(this.schemaModel.tableId(), this.comparison && this.comparison.details);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// We use a DynamicQuerySet as the underlying RowSource, with ColumnFilters applies on top of
|
|
|
|
// it. It filters based on section linking, re-querying as needed in case of onDemand tables.
|
|
|
|
this._queryRowSource = DynamicQuerySet.create(this, gristDoc.querySetManager, this.tableModel);
|
2023-08-16 17:27:35 +00:00
|
|
|
this._mainRowSource = this._queryRowSource;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
if (this.comparison) {
|
2020-11-11 20:25:37 +00:00
|
|
|
// Assign extra row ids for any rows added in the remote (right) table or removed in the
|
|
|
|
// local (left) table.
|
|
|
|
const extraRowIds = this.extraRows.getExtraRows();
|
2020-10-02 15:10:00 +00:00
|
|
|
this._mainRowSource = rowset.ExtendedRowSource.create(this, this._mainRowSource, extraRowIds);
|
|
|
|
}
|
|
|
|
|
2023-12-18 17:50:57 +00:00
|
|
|
// Rows that should temporarily be visible even if they don't match filters.
|
|
|
|
// This is so that a newly added row doesn't immediately disappear, which would be confusing.
|
|
|
|
this._exemptFromFilterRows = rowset.ExemptFromFilterRowSource.create(this);
|
|
|
|
this._exemptFromFilterRows.subscribeTo(this.tableModel);
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// Create a section filter and a filtered row source that subscribes to its changes.
|
2023-12-18 17:50:57 +00:00
|
|
|
// `sectionFilter` also provides `setFilterOverride()` to allow controlling a filter from a column menu.
|
|
|
|
// Whenever changes are made to filters, exempt rows are reset.
|
|
|
|
this._sectionFilter = SectionFilter.create(
|
|
|
|
this, this.viewSection, this.tableModel.tableData, () => this._exemptFromFilterRows.reset()
|
|
|
|
);
|
2020-10-02 15:10:00 +00:00
|
|
|
this._filteredRowSource = rowset.FilteredRowSource.create(this, this._sectionFilter.sectionFilterFunc.get());
|
|
|
|
this._filteredRowSource.subscribeTo(this._mainRowSource);
|
|
|
|
this.autoDispose(this._sectionFilter.sectionFilterFunc.addListener(filterFunc => {
|
|
|
|
this._filteredRowSource.updateFilter(filterFunc);
|
|
|
|
}));
|
|
|
|
|
2023-12-18 17:50:57 +00:00
|
|
|
this.rowSource = UnionRowSource.create(this, [this._filteredRowSource, this._exemptFromFilterRows]);
|
2022-10-17 09:47:16 +00:00
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// Sorted collection of all rows to show in this view.
|
2020-11-18 15:54:23 +00:00
|
|
|
this.sortedRows = rowset.SortedRowSet.create(this, null, this.tableModel.tableData);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// Re-sort when sortSpec changes.
|
2020-11-18 15:54:23 +00:00
|
|
|
this.sortFunc = new SortFunc(new ClientColumnGetters(this.tableModel, {unversioned: true}));
|
2020-10-02 15:10:00 +00:00
|
|
|
this.autoDispose(this.viewSection.activeDisplaySortSpec.subscribeInit(function(spec) {
|
|
|
|
this.sortFunc.updateSpec(spec);
|
|
|
|
this.sortedRows.updateSort((rowId1, rowId2) => {
|
|
|
|
var value = nativeCompare(rowId1 === "new", rowId2 === "new");
|
|
|
|
return value || this.sortFunc.compare(rowId1, rowId2);
|
|
|
|
});
|
|
|
|
}, this));
|
|
|
|
|
|
|
|
// Here we are subscribed to the bulk of the data (main table, possibly filtered).
|
2023-12-18 17:50:57 +00:00
|
|
|
this.sortedRows.subscribeTo(this.rowSource);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// We create a special one-row RowSource for the "Add new" row, in case we need it.
|
|
|
|
this.newRowSource = rowset.RowSource.create(this);
|
|
|
|
this.newRowSource.getAllRows = function() { return ['new']; };
|
|
|
|
|
|
|
|
// This is the LazyArrayModel containing DataRowModels, for rendering, e.g. with scrolly.
|
|
|
|
this.viewData = this.autoDispose(this.tableModel.createLazyRowsModel(this.sortedRows));
|
|
|
|
|
|
|
|
// Floating row model that is not destroyed when the row is scrolled out of view. It must be
|
|
|
|
// assigned manually to a rowId. Additionally, we override the saving of field values with a
|
|
|
|
// custom method that handles better positioning of cursor on adding a new row.
|
|
|
|
this.editRowModel = this.autoDispose(this.tableModel.createFloatingRowModel());
|
|
|
|
this.editRowModel._saveField =
|
|
|
|
(colName, value) => this._saveEditRowField(this.editRowModel, colName, value);
|
|
|
|
|
|
|
|
// Reset heights of rows when there is an action that affects them.
|
|
|
|
this.listenTo(this.viewData, 'rowModelNotify', rowModels => this.onRowResize(rowModels));
|
|
|
|
|
|
|
|
this.listenTo(this.viewSection.events, 'rowHeightChange', this.onResize );
|
|
|
|
|
|
|
|
// Create a command group for keyboard shortcuts common to all views.
|
|
|
|
this.autoDispose(commands.createGroup(BaseView.commonCommands, this, this.viewSection.hasFocus));
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Prepare logic for linking with other sections.
|
|
|
|
|
|
|
|
// A computed for the rowId of the row selected by section linking.
|
|
|
|
this.linkedRowId = this.autoDispose(ko.computed(() => {
|
2022-01-27 17:51:37 +00:00
|
|
|
let linking = this.viewSection.linkingState();
|
2020-10-02 15:10:00 +00:00
|
|
|
return linking && linking.cursorPos ? linking.cursorPos() : null;
|
|
|
|
}).extend({deferred: true}));
|
|
|
|
|
2023-02-08 10:07:57 +00:00
|
|
|
// Update the cursor whenever linkedRowId() changes (but only if we have any linking).
|
|
|
|
this.autoDispose(this.linkedRowId.subscribe(rowId => {
|
|
|
|
if (this.viewSection.linkingState.peek()) {
|
2023-09-25 22:48:18 +00:00
|
|
|
this.setCursorPos({rowId: rowId || 'new'}, true);
|
2023-02-08 10:07:57 +00:00
|
|
|
}
|
|
|
|
}));
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2023-06-21 14:43:22 +00:00
|
|
|
this.isLinkSource = this.autoDispose(ko.pureComputed(() => this.viewSection.linkedSections().all().length > 0));
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// Indicated whether editing the section should be disabled given the current linking state.
|
|
|
|
this.disableEditing = this.autoDispose(ko.computed(() => {
|
2022-01-27 17:51:37 +00:00
|
|
|
const linking = this.viewSection.linkingState();
|
2020-10-02 15:10:00 +00:00
|
|
|
return linking && linking.disableEditing();
|
|
|
|
}));
|
|
|
|
|
2023-04-28 09:20:28 +00:00
|
|
|
this.isPreview = this.options.isPreview ?? false;
|
2021-11-19 05:35:01 +00:00
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
this.enableAddRow = this.autoDispose(ko.computed(() => this.options.addNewRow &&
|
|
|
|
!this.viewSection.disableAddRemoveRows() && !this.disableEditing()));
|
|
|
|
|
|
|
|
// Hide the add row if editing is disabled via filter linking.
|
|
|
|
this.autoDispose(this.enableAddRow.subscribeInit(_enableAddRow => {
|
|
|
|
if (_enableAddRow) {
|
|
|
|
this.sortedRows.subscribeTo(this.newRowSource);
|
|
|
|
} else {
|
|
|
|
this.sortedRows.unsubscribeFrom(this.newRowSource);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Observables local to this view
|
|
|
|
this._isLoading = ko.observable(true);
|
|
|
|
this._pendingCursorPos = this.viewSection.lastCursorPos;
|
|
|
|
|
2022-02-19 09:46:49 +00:00
|
|
|
// Initialize the cursor with the previous cursor position indices, if they exist.
|
2020-10-02 15:10:00 +00:00
|
|
|
console.log("%s BaseView viewSection %s (%s) lastCursorPos %s", this._debugName, this.viewSection.getRowId(),
|
|
|
|
this.viewSection.table().tableId(), JSON.stringify(this.viewSection.lastCursorPos));
|
|
|
|
this.cursor = this.autoDispose(Cursor.create(null, this, this.viewSection.lastCursorPos));
|
|
|
|
|
|
|
|
this.currentColumn = this.autoDispose(ko.pureComputed(() =>
|
|
|
|
this.viewSection.viewFields().at(this.cursor.fieldIndex()).column()
|
|
|
|
).extend({rateLimit: 0})); // TODO Test this without the rateLimit
|
|
|
|
|
|
|
|
this.currentEditingColumnIndex = ko.observable(-1);
|
|
|
|
|
|
|
|
// A koArray of FieldBuilder objects, one for each view-section field.
|
|
|
|
this.fieldBuilders = this.autoDispose(
|
2021-11-19 05:35:01 +00:00
|
|
|
FieldBuilder.createAllFieldWidgets(this.gristDoc, this.viewSection.viewFields, this.cursor, {
|
|
|
|
isPreview: this.isPreview,
|
|
|
|
})
|
2020-10-02 15:10:00 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// An observable evaluating to the FieldBuilder for the field where the cursor is.
|
|
|
|
this.activeFieldBuilder = this.autoDispose(ko.pureComputed(() =>
|
|
|
|
this.fieldBuilders.at(this.cursor.fieldIndex())
|
|
|
|
));
|
|
|
|
|
2022-10-14 10:07:19 +00:00
|
|
|
// By default, a view doesn't support selectedColumns, but it can be overridden.
|
|
|
|
this.selectedColumns = null;
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// Observable for whether the data in this view is truncated, i.e. not all rows are included
|
|
|
|
// (this can only be true for on-demand tables).
|
|
|
|
this.isTruncated = ko.observable(false);
|
|
|
|
|
|
|
|
// This computed's purpose is the side-effect of calling makeQuery() initially and when any
|
|
|
|
// dependency changes.
|
|
|
|
this.autoDispose(ko.computed(() => {
|
|
|
|
this._isLoading(true);
|
2022-01-27 17:51:37 +00:00
|
|
|
const linkingFilter = this.viewSection.linkingFilter();
|
2021-08-10 18:21:03 +00:00
|
|
|
this._queryRowSource.makeQuery(linkingFilter.filters, linkingFilter.operations, (err) => {
|
2020-10-02 15:10:00 +00:00
|
|
|
if (this.isDisposed()) { return; }
|
(core) When changing a table for a page widget, unset widget-linking to avoid invalid values.
Summary:
Previously, using "Change Widget" allowed one to change the underlying table,
but would keep the linking settings. This could allow invalid settings which
would sometimes lead to JS errors. These manifested in production as
"UserError: Query error: n is not a function".
- Unset linking settings in this case, to avoid invalid values.
- In case invalid values are encountered (e.g. saved previously), treat them as
unset, to avoid JS errors.
- If an error does occur, report it with a stack trace.
Also, for testing, added 'selectBy' option to gristUtils helpers for using page-widget-picker.
Test Plan: Added test cases for resetting linking, and for ignoring invalid link settings.
Reviewers: alexmojaki
Reviewed By: alexmojaki
Differential Revision: https://phab.getgrist.com/D2993
2021-08-24 03:28:07 +00:00
|
|
|
if (err) { reportError(err); }
|
2023-12-18 17:50:57 +00:00
|
|
|
this._exemptFromFilterRows.reset();
|
2020-10-02 15:10:00 +00:00
|
|
|
this.onTableLoaded();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Reset cursor to the first row when filtering changes.
|
2022-01-27 17:51:37 +00:00
|
|
|
this.autoDispose(this.viewSection.linkingFilter.subscribe((x) => this.onLinkFilterChange()));
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// When sorting changes, reset the cursor to the first row. (The alternative of moving the
|
|
|
|
// cursor to stay at the same record is sometimes better, but sometimes more annoying.)
|
|
|
|
this.autoDispose(this.viewSection.activeSortSpec.subscribe(() => this.setCursorPos({rowIndex: 0})));
|
|
|
|
|
|
|
|
this.copySelection = ko.observable(null);
|
2020-10-09 21:39:13 +00:00
|
|
|
|
|
|
|
// Whether parts needed for printing should be rendered now.
|
|
|
|
this._isPrinting = ko.observable(false);
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
Base.setBaseFor(BaseView);
|
|
|
|
_.extend(Base.prototype, BackboneEvents);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* These commands are common to GridView and DetailView.
|
|
|
|
*/
|
|
|
|
BaseView.commonCommands = {
|
2022-01-05 20:14:44 +00:00
|
|
|
input: function(init) {
|
|
|
|
this.scrollToCursor(true).catch(reportError);
|
|
|
|
this.activateEditorAtCursor({init});
|
|
|
|
},
|
2023-12-08 05:59:42 +00:00
|
|
|
editField: function(event) { closeRegisteredMenu(); this.scrollToCursor(true); this.activateEditorAtCursor({event}); },
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
insertRecordBefore: function() { this.insertRow(this.cursor.rowIndex()); },
|
|
|
|
insertRecordAfter: function() { this.insertRow(this.cursor.rowIndex() + 1); },
|
|
|
|
|
|
|
|
insertCurrentDate: function() { this.insertCurrentDate(false); },
|
|
|
|
insertCurrentDateTime: function() { this.insertCurrentDate(true); },
|
|
|
|
|
|
|
|
copyLink: function() { this.copyLink().catch(reportError); },
|
2022-04-18 14:40:29 +00:00
|
|
|
|
2022-10-21 10:55:01 +00:00
|
|
|
deleteRecords: function(source) { this.deleteRecords(source); },
|
|
|
|
|
2022-04-18 14:40:29 +00:00
|
|
|
filterByThisCellValue: function() { this.filterByThisCellValue(); },
|
2022-10-17 09:47:16 +00:00
|
|
|
duplicateRows: function() { this._duplicateRows().catch(reportError); },
|
|
|
|
openDiscussion: function() { this.openDiscussionAtCursor(); },
|
2023-12-08 05:59:42 +00:00
|
|
|
viewAsCard: function() {
|
|
|
|
/* Overridden by subclasses.
|
|
|
|
*
|
|
|
|
* This is still needed so that <space> doesn't trigger the `input` command
|
|
|
|
* if a subclass doesn't support opening the current record as a card. */
|
|
|
|
},
|
2020-10-02 15:10:00 +00:00
|
|
|
};
|
|
|
|
|
2022-10-21 10:55:01 +00:00
|
|
|
BaseView.prototype.selectedRows = function() {
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
|
|
|
BaseView.prototype.deleteRows = function(rowIds) {
|
|
|
|
return this.tableModel.sendTableAction(['BulkRemoveRecord', rowIds]);
|
|
|
|
};
|
|
|
|
|
|
|
|
BaseView.prototype.deleteRecords = function(source) {
|
|
|
|
const rowIds = this.selectedRows();
|
|
|
|
if (this.viewSection.disableAddRemoveRows() || rowIds.length === 0){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const isKeyboard = source instanceof KeyboardEvent;
|
|
|
|
const popups = this.gristDoc.docPageModel.appModel.dismissedPopups;
|
|
|
|
const popupName = DismissedPopup.check('deleteRecords');
|
|
|
|
const onSave = async (remember) => {
|
|
|
|
if (remember) {
|
|
|
|
markAsSeen(popups, popupName);
|
|
|
|
}
|
|
|
|
return this.deleteRows(rowIds);
|
|
|
|
};
|
|
|
|
if (isKeyboard && !popups.get().includes(popupName)) {
|
|
|
|
// If we can't find it, use viewPane itself
|
|
|
|
this.scrollToCursor();
|
|
|
|
const selectedCell = this.viewPane.querySelector(".selected_cursor") || this.viewPane;
|
|
|
|
buildConfirmDelete(selectedCell, onSave, rowIds.length <= 1);
|
|
|
|
} else {
|
|
|
|
onSave().then(() => {
|
2023-11-21 20:16:38 +00:00
|
|
|
if (!this.isDisposed()) {
|
|
|
|
reportUndo(this.gristDoc, `You deleted ${rowIds.length} row${rowIds.length > 1 ? 's' : ''}.`);
|
|
|
|
}
|
2022-10-21 10:55:01 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* Sets the cursor to the given position, deferring if necessary until the current query finishes
|
2023-09-25 22:48:18 +00:00
|
|
|
* loading. isFromLink will be set when called as result of cursor linking(see Cursor.setCursorPos for info)
|
2020-10-02 15:10:00 +00:00
|
|
|
*/
|
2023-09-25 22:48:18 +00:00
|
|
|
BaseView.prototype.setCursorPos = function(cursorPos, isFromLink = false) {
|
2022-03-22 12:38:56 +00:00
|
|
|
if (this.isDisposed()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
if (!this._isLoading.peek()) {
|
2023-09-25 22:48:18 +00:00
|
|
|
this.cursor.setCursorPos(cursorPos, isFromLink);
|
2020-10-02 15:10:00 +00:00
|
|
|
} else {
|
|
|
|
// This is the first step; the second happens in onTableLoaded.
|
|
|
|
this._pendingCursorPos = cursorPos;
|
|
|
|
this.cursor.setLive(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a promise that's resolved when the query being loaded finishes loading.
|
|
|
|
* If no query is being loaded, it will resolve immediately.
|
|
|
|
*/
|
|
|
|
BaseView.prototype.getLoadingDonePromise = function() {
|
|
|
|
return waitObs(this._isLoading, (value) => !value);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start editing the selected cell.
|
|
|
|
* @param {String} input: If given, initialize the editor with the given input (rather than the
|
|
|
|
* original content of the cell).
|
|
|
|
*/
|
2021-05-17 14:05:49 +00:00
|
|
|
BaseView.prototype.activateEditorAtCursor = function(options) {
|
2020-10-02 15:10:00 +00:00
|
|
|
var builder = this.activeFieldBuilder();
|
|
|
|
if (builder.isEditorActive()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var rowId = this.viewData.getRowId(this.cursor.rowIndex());
|
|
|
|
// LazyArrayModel row model which is also used to build the cell dom. Needed since
|
|
|
|
// it may be used as a key to retrieve the cell dom, which is useful for editor placement.
|
|
|
|
var lazyRow = this.getRenderedRowModel(rowId);
|
2021-06-17 16:41:07 +00:00
|
|
|
if (!lazyRow) {
|
|
|
|
// TODO scroll into view. For now, just don't activate the editor.
|
|
|
|
return;
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
2021-06-17 16:41:07 +00:00
|
|
|
this.editRowModel.assign(rowId);
|
|
|
|
builder.buildEditorDom(this.editRowModel, lazyRow, options || {});
|
2020-10-02 15:10:00 +00:00
|
|
|
};
|
|
|
|
|
2022-10-17 09:47:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens discussion panel at the cursor position. Returns true if discussion panel was opened.
|
|
|
|
*/
|
|
|
|
BaseView.prototype.openDiscussionAtCursor = function(id) {
|
|
|
|
if (!COMMENTS().get()) { return false; }
|
|
|
|
var builder = this.activeFieldBuilder();
|
|
|
|
if (builder.isEditorActive()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var rowId = this.viewData.getRowId(this.cursor.rowIndex());
|
|
|
|
// LazyArrayModel row model which is also used to build the cell dom. Needed since
|
|
|
|
// it may be used as a key to retrieve the cell dom, which is useful for editor placement.
|
|
|
|
var lazyRow = this.getRenderedRowModel(rowId);
|
|
|
|
if (!lazyRow) {
|
|
|
|
// TODO scroll into view. For now, just don't start discussion.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this.editRowModel.assign(rowId);
|
|
|
|
builder.buildDiscussionPopup(this.editRowModel, lazyRow, id);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-03-17 03:45:44 +00:00
|
|
|
/**
|
|
|
|
* Move the floating RowModel for editing to the current cursor position, and return it.
|
|
|
|
*
|
|
|
|
* This is used for opening the formula editor in the side panel; the current row is used to get
|
|
|
|
* possible exception info from the formula.
|
|
|
|
*/
|
|
|
|
BaseView.prototype.moveEditRowToCursor = function() {
|
|
|
|
var rowId = this.viewData.getRowId(this.cursor.rowIndex());
|
|
|
|
this.editRowModel.assign(rowId);
|
|
|
|
return this.editRowModel;
|
|
|
|
};
|
|
|
|
|
2022-08-02 13:01:07 +00:00
|
|
|
// Get an anchor link for the current cell and a given view section to the clipboard.
|
|
|
|
BaseView.prototype.getAnchorLinkForSection = function(sectionId) {
|
|
|
|
const rowId = this.viewData.getRowId(this.cursor.rowIndex())
|
|
|
|
// If there are no visible rows (happens in some widget linking situations),
|
|
|
|
// pick an arbitrary row which will hopefully be close to the top of the table.
|
|
|
|
|| this.tableModel.tableData.findMatchingRowId({})
|
|
|
|
// If there are no rows at all, return the 'new record' row ID.
|
|
|
|
// Note that this case only happens in combination with the widget linking mentioned.
|
|
|
|
// If the table is empty but the 'new record' row is selected, the `viewData.getRowId` line above works.
|
|
|
|
|| 'new';
|
2022-10-17 21:45:42 +00:00
|
|
|
// The `fieldIndex` will be null if there are no visible columns.
|
|
|
|
const fieldIndex = this.cursor.fieldIndex.peek();
|
|
|
|
const field = fieldIndex !== null ? this.viewSection.viewFields().peek()[fieldIndex] : null;
|
|
|
|
const colRef = field?.colRef.peek();
|
2022-08-02 13:01:07 +00:00
|
|
|
return {hash: {sectionId, rowId, colRef}};
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// Copy an anchor link for the current row to the clipboard.
|
|
|
|
BaseView.prototype.copyLink = async function() {
|
|
|
|
const sectionId = this.viewSection.getRowId();
|
2022-08-02 13:01:07 +00:00
|
|
|
const anchorUrlState = this.getAnchorLinkForSection(sectionId);
|
2020-10-02 15:10:00 +00:00
|
|
|
try {
|
2022-08-02 13:01:07 +00:00
|
|
|
const link = urlState().makeUrl(anchorUrlState);
|
2020-10-02 15:10:00 +00:00
|
|
|
await copyToClipboard(link);
|
|
|
|
setTestState({clipboard: link});
|
2021-10-01 19:38:58 +00:00
|
|
|
reportSuccess('Link copied to clipboard', {key: 'clipboard'});
|
2020-10-02 15:10:00 +00:00
|
|
|
} catch (e) {
|
|
|
|
throw new Error('cannot copy to clipboard');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-18 14:40:29 +00:00
|
|
|
BaseView.prototype.filterByThisCellValue = function() {
|
|
|
|
const rowId = this.viewData.getRowId(this.cursor.rowIndex());
|
|
|
|
const col = this.viewSection.viewFields().peek()[this.cursor.fieldIndex()].column();
|
|
|
|
let value = this.tableModel.tableData.getValue(rowId, col.colId.peek());
|
|
|
|
|
|
|
|
// This mimics the logic in ColumnFilterMenu.addCountsToMap
|
|
|
|
// ChoiceList and Reflist values get 'flattened' out so we filter by each element within.
|
|
|
|
// In any other column type, complex values (even lists) get converted to JSON.
|
|
|
|
let filterValues;
|
2022-06-10 21:34:12 +00:00
|
|
|
const colType = col.type.peek();
|
|
|
|
if (gristTypes.isList(value) && gristTypes.isListType(colType)) {
|
2022-04-18 14:40:29 +00:00
|
|
|
filterValues = value.slice(1);
|
2022-06-10 21:34:12 +00:00
|
|
|
if (!filterValues.length) {
|
|
|
|
// If the list is empty, filter instead by an empty value for the whole list
|
|
|
|
filterValues = [colType === "ChoiceList" ? "" : null];
|
|
|
|
}
|
2022-04-18 14:40:29 +00:00
|
|
|
} else {
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
value = JSON.stringify(value);
|
|
|
|
}
|
|
|
|
filterValues = [value];
|
|
|
|
}
|
2022-11-17 20:17:51 +00:00
|
|
|
this.viewSection.setFilter(col.getRowId(), {filter: JSON.stringify({included: filterValues})});
|
2022-04-18 14:40:29 +00:00
|
|
|
};
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* Insert a new row immediately before the row at the given index if given an Integer. Otherwise
|
|
|
|
* insert a new row at the end.
|
|
|
|
*/
|
|
|
|
BaseView.prototype.insertRow = function(index) {
|
|
|
|
if (this.viewSection.disableAddRemoveRows() || this.disableEditing()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var rowId = this.viewData.getRowId(index);
|
|
|
|
var insertPos = Number.isInteger(rowId) ?
|
|
|
|
this.tableModel.tableData.getValue(rowId, 'manualSort') : null;
|
|
|
|
|
|
|
|
return this.sendTableAction(['AddRecord', null, { 'manualSort': insertPos }])
|
|
|
|
.then(rowId => {
|
|
|
|
if (!this.isDisposed()) {
|
2023-12-18 17:50:57 +00:00
|
|
|
this._exemptFromFilterRows.addExemptRow(rowId);
|
2020-10-02 15:10:00 +00:00
|
|
|
this.setCursorPos({rowId});
|
|
|
|
}
|
|
|
|
return rowId;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
BaseView.prototype._getDefaultColValues = function() {
|
2023-12-18 17:50:57 +00:00
|
|
|
return getDefaultColValues(this.viewSection);
|
2020-10-02 15:10:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enhances [Bulk]AddRecord actions to include the default values determined by the current
|
|
|
|
* section-linking filter.
|
|
|
|
*/
|
|
|
|
BaseView.prototype._enhanceAction = function(action) {
|
|
|
|
if (action[0] === 'AddRecord' || action[0] === 'BulkAddRecord') {
|
|
|
|
let colValues = this._getDefaultColValues();
|
|
|
|
let rowIds = action[1];
|
|
|
|
if (action[0] === 'BulkAddRecord') {
|
|
|
|
colValues = _.mapObject(colValues, v => rowIds.map(() => v));
|
|
|
|
}
|
|
|
|
Object.assign(colValues, action[2]);
|
|
|
|
return [action[0], rowIds, colValues];
|
|
|
|
} else {
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enhances a list of table actions and turns them from implicit-table actions into
|
|
|
|
* proper actions.
|
|
|
|
*/
|
|
|
|
BaseView.prototype.prepTableActions = function(actions) {
|
|
|
|
actions = actions.map(a => this._enhanceAction(a));
|
|
|
|
actions.forEach(action_ => {
|
|
|
|
action_.splice(1, 0, this.tableModel.tableData.tableId);
|
|
|
|
});
|
|
|
|
return actions;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut for `.tableModel.tableData.sendTableActions`, which also sets default values
|
|
|
|
* determined by the current section-linking filter, if any.
|
|
|
|
*/
|
|
|
|
BaseView.prototype.sendTableActions = function(actions, optDesc) {
|
|
|
|
return this.tableModel.sendTableActions(actions.map(a => this._enhanceAction(a)), optDesc);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut for `.tableModel.tableData.sendTableAction`, which also sets default values
|
|
|
|
* determined by the current section-linking filter, if any.
|
|
|
|
*/
|
|
|
|
BaseView.prototype.sendTableAction = function(action, optDesc) {
|
|
|
|
return action ? this.tableModel.sendTableAction(this._enhanceAction(action), optDesc) : null;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts the current date/time into the selected cell if the cell is of a compatible type
|
|
|
|
* (Text/Date/DateTime/Any).
|
|
|
|
* @param {Boolean} withTime: Whether to include the time in addition to the date. This is ignored
|
|
|
|
* for Date columns (assumed false) and for DateTime (assumed true).
|
|
|
|
*/
|
|
|
|
BaseView.prototype.insertCurrentDate = function(withTime) {
|
|
|
|
let column = this.currentColumn();
|
|
|
|
if (column.isRealFormula()) {
|
|
|
|
// Ignore the shortcut when in a formula column.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let type = column.pureType();
|
|
|
|
let value, now = Date.now();
|
|
|
|
const docTimezone = this.gristDoc.docInfo.timezone.peek();
|
|
|
|
if (type === 'Text' || type === 'Any') {
|
|
|
|
// Use document timezone. Don't forget to use uppercase HH for 24-hour time.
|
|
|
|
value = moment.tz(now, docTimezone).format('YYYY-MM-DD' + (withTime ? ' HH:mm:ss' : ''));
|
|
|
|
} else if (type === 'Date') {
|
|
|
|
// Get UTC midnight for the current date (as seen in docTimezone). This is a bit confusing. If
|
|
|
|
// it's "2019-11-14 23:30 -05:00", then it's "2019-11-15 04:30" in UTC. Since we measure time
|
|
|
|
// from Epoch UTC, we want the UTC time to have the correct date, so need to add the offset
|
|
|
|
// (-05:00) to get "2019-11-14 23:30" in UTC, and then round down to midnight.
|
|
|
|
const offsetMinutes = moment.tz(now, docTimezone).utcOffset();
|
|
|
|
value = roundDownToMultiple(now / 1000 + offsetMinutes * 60, 24*3600);
|
|
|
|
} else if (type === 'DateTime') {
|
|
|
|
value = now / 1000;
|
|
|
|
} else {
|
|
|
|
// Ignore the shortcut when in a column of an inappropriate type.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var rowId = this.viewData.getRowId(this.cursor.rowIndex());
|
|
|
|
this.editRowModel.assign(rowId);
|
|
|
|
this.editRowModel[column.colId()].setAndSave(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override the saving of field values to add some extra processing:
|
|
|
|
* - If a new row is saved, then we may need to adjust the row where the cursor is.
|
|
|
|
* - We add the edited or added row to ensure it's displayed regardless of current columnFilters.
|
|
|
|
* - We change the main view's row observables to see the new value immediately.
|
|
|
|
* TODO: When saving a formula in the addRow, the cursor moves down instead of staying in place.
|
|
|
|
* To fix that behavior, propose to factor out the `isAddRow` overrides from here
|
|
|
|
* into a `setNewRowColValues` on the editRowModel and have `FieldBuilder._saveEdit` call
|
|
|
|
* that instead of `updateColValues`.
|
|
|
|
*/
|
|
|
|
BaseView.prototype._saveEditRowField = function(editRowModel, colName, value) {
|
|
|
|
if (editRowModel._isAddRow.peek()) {
|
|
|
|
this.cursor.setLive(false);
|
|
|
|
const colValues = this._getDefaultColValues();
|
|
|
|
colValues[colName] = value;
|
|
|
|
|
|
|
|
return editRowModel.updateColValues(colValues)
|
|
|
|
// Once we know the new row's rowId, add it to column filters to make sure it's displayed.
|
|
|
|
.then(rowId => {
|
|
|
|
if (!this.isDisposed()) {
|
2023-12-18 17:50:57 +00:00
|
|
|
this._exemptFromFilterRows.addExemptRow(rowId);
|
2020-10-02 15:10:00 +00:00
|
|
|
this.setCursorPos({rowId});
|
|
|
|
}
|
|
|
|
return rowId;
|
|
|
|
})
|
|
|
|
.finally(() => !this.isDisposed() && this.cursor.setLive(true));
|
|
|
|
} else {
|
|
|
|
var rowId = editRowModel.getRowId();
|
|
|
|
// We are editing the floating "edit" rowModel, but to ensure that we see data in the main view
|
|
|
|
// (when the editor closes), we immediately update the main view's rowModel, if such exists.
|
|
|
|
var mainRowModel = this.getRenderedRowModel(rowId);
|
|
|
|
if (mainRowModel) {
|
|
|
|
mainRowModel[colName](value);
|
|
|
|
}
|
|
|
|
const ret = DataRowModel.prototype._saveField.call(editRowModel, colName, value)
|
2023-05-20 23:55:36 +00:00
|
|
|
// Display this rowId, even if it doesn't match the filter,
|
|
|
|
// unless the filter is on a Bool column
|
2020-10-02 15:10:00 +00:00
|
|
|
.then((result) => {
|
2023-05-20 23:55:36 +00:00
|
|
|
if (!this.isDisposed() && this.currentColumn().pureType() !== 'Bool') {
|
2023-12-18 17:50:57 +00:00
|
|
|
this._exemptFromFilterRows.addExemptRow(rowId);
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
})
|
|
|
|
.finally(() => !this.isDisposed() && mainRowModel && mainRowModel._assignColumn(colName));
|
|
|
|
return this.viewSection.isSorted() ? ret : null;
|
|
|
|
// Do not return the saveField call in the case that the column is unsorted: in this case,
|
|
|
|
// we assumes optimistically that the action is successful and browser events can
|
|
|
|
// continue being processed immediately without waiting.
|
|
|
|
// When sorted, we wait on the saveField call so we may determine where the row ends
|
|
|
|
// up for cursor movement purposes.
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses the current cursor selection to return a rich paste object with a reference to the data,
|
|
|
|
* and the selection ranges. See CopySelection.js
|
|
|
|
*
|
|
|
|
* @returns {pasteObj} - Paste object
|
|
|
|
*/
|
|
|
|
BaseView.prototype.copy = function(selection) {
|
2023-04-28 09:20:28 +00:00
|
|
|
// Clear the previous copy selection, if any.
|
|
|
|
commands.allCommands.clearCopySelection.run();
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
this.copySelection(selection);
|
|
|
|
|
|
|
|
return {
|
|
|
|
data: this.tableModel.tableData,
|
|
|
|
selection: selection
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses the current cursor selection to return a rich paste object with a reference to the data,
|
|
|
|
* the selection ranges and a callback that when called performs all of the actions needed for a cut.
|
|
|
|
*
|
|
|
|
* @returns {pasteObj} - Paste object
|
|
|
|
*/
|
|
|
|
BaseView.prototype.cut = function(selection) {
|
2023-04-28 09:20:28 +00:00
|
|
|
// Clear the previous copy selection, if any.
|
|
|
|
commands.allCommands.clearCopySelection.run();
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
this.copySelection(selection);
|
|
|
|
|
|
|
|
return {
|
|
|
|
data: this.tableModel.tableData,
|
|
|
|
selection: selection,
|
|
|
|
cutCallback: () => tableUtil.makeDeleteAction(selection)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to send paste actions from the cutCallback and a list of paste actions.
|
|
|
|
*/
|
|
|
|
BaseView.prototype.sendPasteActions = function(cutCallback, actions) {
|
|
|
|
let cutAction = null;
|
|
|
|
// If this is a cut -> paste, add the cut action and a description.
|
|
|
|
if (cutCallback) {
|
|
|
|
cutAction = cutCallback();
|
|
|
|
// If the cut occurs on an edit restricted cell, there may be no cut action.
|
|
|
|
if (cutAction) { actions.unshift(cutAction); }
|
|
|
|
}
|
2023-07-01 18:31:21 +00:00
|
|
|
return this.gristDoc.docData.sendActions(actions);
|
2020-10-02 15:10:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
BaseView.prototype.buildDom = function() {
|
|
|
|
throw new Error("Not Implemented");
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by ViewLayout to return view-specific controls to add into its ViewSection's title bar.
|
|
|
|
* By default builds nothing. Derived views may override.
|
|
|
|
*/
|
|
|
|
BaseView.prototype.buildTitleControls = function() {
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when table data gets loaded (if already loaded, then called immediately after the
|
|
|
|
* constructor). Derived views may override.
|
|
|
|
*/
|
|
|
|
BaseView.prototype.onTableLoaded = function() {
|
|
|
|
// Complete the setting of a pending cursor position (see setCursorPos() for the first half).
|
|
|
|
if (this._pendingCursorPos) {
|
|
|
|
this.cursor.setCursorPos(this._pendingCursorPos);
|
|
|
|
this._pendingCursorPos = null;
|
|
|
|
}
|
|
|
|
this._isLoading(false);
|
|
|
|
this.isTruncated(this._queryRowSource.isTruncated);
|
|
|
|
this.cursor.setLive(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when view gets resized. Derived views may override.
|
|
|
|
*/
|
|
|
|
BaseView.prototype.onResize = function() {
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when rows have changed and may potentially need resizing. Derived views may override.
|
|
|
|
* @param {Array<DataRowModel>} rowModels: Array of row models whose size may have changed.
|
|
|
|
*/
|
|
|
|
BaseView.prototype.onRowResize = function(rowModels) {
|
|
|
|
};
|
|
|
|
|
2020-12-15 23:52:21 +00:00
|
|
|
/**
|
|
|
|
* Called when user selects a different row which drives the link-filtering of this section.
|
|
|
|
*/
|
|
|
|
BaseView.prototype.onLinkFilterChange = function(rowId) {
|
2023-02-08 10:07:57 +00:00
|
|
|
// If this section is linked, go to the first row as the row previously selected may no longer
|
|
|
|
// be visible.
|
|
|
|
if (this.viewSection.linkingState.peek()) {
|
|
|
|
this.setCursorPos({rowIndex: 0});
|
|
|
|
}
|
2020-12-15 23:52:21 +00:00
|
|
|
};
|
|
|
|
|
2020-10-09 21:39:13 +00:00
|
|
|
/**
|
|
|
|
* Called before and after printing this section.
|
|
|
|
*/
|
|
|
|
BaseView.prototype.prepareToPrint = function(onOff) {
|
|
|
|
this._isPrinting(onOff);
|
|
|
|
};
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* Called to obtain the rowModel for the given rowId. Returns a rowModel if it belongs to the
|
|
|
|
* section and is rendered, otherwise returns null.
|
|
|
|
* Useful to tie a rendered row to the row being edited. Derived views may override.
|
|
|
|
*/
|
|
|
|
BaseView.prototype.getRenderedRowModel = function(rowId) {
|
|
|
|
return this.viewData.getRowModel(rowId);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the index of the last non-AddNew row in the grid.
|
|
|
|
*/
|
|
|
|
BaseView.prototype.getLastDataRowIndex = function() {
|
|
|
|
let last = this.viewData.peekLength - 1;
|
|
|
|
return (last >= 0 && this.viewData.getRowId(last) === 'new') ? last - 1 : last;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-11-19 20:30:11 +00:00
|
|
|
* Creates and opens ColumnFilterMenu for a given field/column, and returns its PopupControl.
|
2020-10-02 15:10:00 +00:00
|
|
|
*/
|
2022-11-17 20:17:51 +00:00
|
|
|
BaseView.prototype.createFilterMenu = function(openCtl, filterInfo, options) {
|
2022-12-20 02:06:39 +00:00
|
|
|
const {showAllFiltersButton, onClose} = options;
|
|
|
|
return createFilterMenu({
|
|
|
|
openCtl,
|
|
|
|
sectionFilter: this._sectionFilter,
|
|
|
|
filterInfo,
|
|
|
|
rowSource: this._mainRowSource,
|
|
|
|
tableData: this.tableModel.tableData,
|
|
|
|
gristDoc: this.gristDoc,
|
|
|
|
showAllFiltersButton,
|
|
|
|
onClose,
|
|
|
|
});
|
2020-10-02 15:10:00 +00:00
|
|
|
};
|
|
|
|
|
2021-03-05 15:17:07 +00:00
|
|
|
/**
|
|
|
|
* Whether the rows shown by this view are a proper subset of all rows in the table.
|
|
|
|
*/
|
|
|
|
BaseView.prototype.isFiltered = function() {
|
|
|
|
return this._filteredRowSource.getNumRows() < this.tableModel.tableData.numRecords();
|
|
|
|
};
|
|
|
|
|
2021-12-16 18:00:04 +00:00
|
|
|
/**
|
|
|
|
* Makes sure that active record is in the view.
|
2022-01-05 20:14:44 +00:00
|
|
|
* @param {Boolean} sync If the scroll should be performed synchronously. For typing we should scroll synchronously,
|
|
|
|
* for other cases asynchronously as there might be some other operations pending (see doScrollChildIntoView in koDom).
|
2021-12-16 18:00:04 +00:00
|
|
|
*/
|
2022-01-05 20:14:44 +00:00
|
|
|
BaseView.prototype.scrollToCursor = function() {
|
2021-12-16 18:00:04 +00:00
|
|
|
// to override
|
|
|
|
return Promise.resolve();
|
|
|
|
};
|
|
|
|
|
2022-04-19 13:01:08 +00:00
|
|
|
/**
|
|
|
|
* Return a list of manual sort positions so that inserting {numInsert} rows
|
|
|
|
* with the returned positions will place them in between index-1 and index.
|
|
|
|
* when the GridView is sorted by MANUALSORT
|
|
|
|
**/
|
2022-06-07 20:01:59 +00:00
|
|
|
BaseView.prototype._getRowInsertPos = function(index, numInserts) {
|
|
|
|
var rowId = this.viewData.getRowId(index);
|
|
|
|
var insertPos = this.tableModel.tableData.getValue(rowId, MANUALSORT);
|
|
|
|
return Array(numInserts).fill(insertPos);
|
2022-04-19 13:01:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Duplicates selected row(s) and returns inserted rowIds
|
|
|
|
*/
|
|
|
|
BaseView.prototype._duplicateRows = async function() {
|
|
|
|
if (this.viewSection.disableAddRemoveRows() || this.disableEditing()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Get current selection (we need only rowIds).
|
|
|
|
const selection = this.getSelection();
|
|
|
|
const rowIds = selection.rowIds;
|
|
|
|
const length = rowIds.length;
|
|
|
|
// Start assembling action.
|
|
|
|
const action = ['BulkAddRecord'];
|
|
|
|
// Put nulls as rowIds.
|
|
|
|
action.push(gutil.arrayRepeat(length, null));
|
|
|
|
const columns = {};
|
|
|
|
action.push(columns);
|
|
|
|
// Calculate new positions for rows using helper function. It requires
|
|
|
|
// index where we want to put new rows (it accepts new row index).
|
|
|
|
const lastSelectedIndex = this.viewData.getRowIndex(rowIds[length-1]);
|
|
|
|
columns.manualSort = this._getRowInsertPos(lastSelectedIndex + 1, length);
|
|
|
|
// Now copy all visible data.
|
|
|
|
for(const col of this.viewSection.columns.peek()) {
|
|
|
|
// But omit all formula columns (and empty ones).
|
|
|
|
const colId = col.colId.peek();
|
|
|
|
if (col.isFormula.peek()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
columns[colId] = rowIds.map(id => this.tableModel.tableData.getValue(id, colId));
|
|
|
|
// If all values in a column are censored, remove this column,
|
|
|
|
if (columns[colId].every(gristTypes.isCensored)) {
|
|
|
|
delete columns[colId]
|
|
|
|
} else {
|
|
|
|
// else remove only censored values
|
|
|
|
columns[colId].forEach((val, i) => {
|
|
|
|
if (gristTypes.isCensored(val)) {
|
|
|
|
columns[colId][i] = null;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const result = await this.sendTableAction(action, `Duplicated rows ${rowIds}`);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
module.exports = BaseView;
|