Files
wekan/client/components/boards/exportScope.js
T
Lauri Ojansivu b77299c806 Fix the client bundle dying at startup: a component must load its own
template.

Reported from testi.wekan.fi, on the SIGN-IN page:

  Uncaught TypeError: can't access property "helpers",
    Template.exportScopeBody is undefined
  [UserAccounts] Warning no template passwordInput found!

`Template.exportScopeBody.helpers(...)` runs at module scope and throws when
the template is not defined yet. That does not break one popup: it stops the
module evaluating, so every template registered after it never registers -
which is why a sign-in form nobody had touched came up blank.

The central lists in client/features/*.js import a component's .jade before
its .js, and that is enough for a component nobody else imports. The export
popup body is imported by sidebar.js, by cardDetails.js and by the import
page, so whichever of those the graph reaches first evaluates it, long
before the feature list gets to the template. It imports its own .jade now.
The module cache makes the second import free, and the order stops being a
hope.

tests/clientBundleImports.test.cjs gains the rule - a component .js that
other components import, that registers on a template of the same basename,
must import that template - and it found a second file already in that
state: migrationProgress.js, imported by boardBody.js. Fixed the same way
rather than excepted, since it is one line and the failure it prevents takes
the whole client with it.

Thanks to xet7 !
2026-08-14 02:11:10 +03:00

313 lines
12 KiB
JavaScript

// ITS OWN TEMPLATE FIRST. This module is imported by sidebar.js, by
// cardDetails.js and by the import page, and any of them can be evaluated
// before client/features/boards.js gets to the .jade - at which point
// `Template.exportScopeBody` is undefined and `.helpers()` on it throws at
// module scope. That does not break this popup, it breaks THE WHOLE BUNDLE:
// evaluation stops, every template registered after it never registers, and
// the sign-in page comes up with "no template passwordInput found".
//
// A component whose .js is imported by other components cannot rely on the
// central import order in client/features/*.js. Importing the template here
// makes the order a fact rather than a hope; the module cache means the second
// import costs nothing.
import './exportScope.jade';
import { ReactiveDict } from 'meteor/reactive-dict';
import { Template } from 'meteor/templating';
import { FlowRouter } from 'meteor/ostrio:flow-router-extra';
import { Meteor } from 'meteor/meteor';
import { Session } from 'meteor/session';
import { ReactiveCache } from '/imports/reactiveCache';
import { Accounts } from 'meteor/accounts-base';
import { BOARD_EXPORT_FIELDS } from '/models/lib/exportFields';
import { exportLocaleParams } from '/client/lib/exportLocale';
// The "what do you want in it" half of an export, for a board, a swimlane or a
// list (#1173 "Add Feature: Print Board with Params").
//
// One body, used by three popups. A board export, a swimlane export and a list
// export differ by ONE query parameter - which cards are in scope - so writing
// the checkbox list and the two download links three times would be three places
// for a section to go missing from. `exportScopeBody` is included by each popup
// with its own scope, and everything else is shared.
//
// The SELECTION is shared too, and deliberately: it is module-level, so ticking
// "no attachments, no comments" once applies to the next export as well, in the
// same session. Somebody printing a board rarely wants a different shape for
// each list of it.
const selection = new ReactiveDict();
BOARD_EXPORT_FIELDS.forEach(({ field }) => selection.set(field, true));
// `card-details` is what makes an export the card layout rather than the
// spreadsheet table; it starts on, like everything else.
selection.set('card-details', true);
function selectedFields() {
return ['card-details', ...BOARD_EXPORT_FIELDS.map(({ field }) => field)]
.filter((field, index, all) => all.indexOf(field) === index)
.filter(field => selection.get(field));
}
// The scope a popup was opened with: {} for a board, {swimlaneId} or {listId}
// for the other two.
function currentScope() {
const data = Template.currentData() || {};
const scope = {};
if (data.swimlaneId) scope.swimlaneId = data.swimlaneId;
if (data.listId) scope.listId = data.listId;
return scope;
}
function scopeTitle() {
const data = Template.currentData() || {};
return data.title || 'export';
}
// ONE url builder for every format and every scope. A second one would be a
// second place for a query parameter to go missing from - which is how the card
// popup's checkboxes ended up driving the Excel download and not the PDF.
// A CARD has export routes of its own - /lists/:listId/cards/:cardId/exportPDF
// and exportExcel - which produce the one card rather than a board document with
// one card in it. They are the same renderers underneath, so the popup uses them
// when it is a card being exported and the board routes otherwise, instead of
// there being two ways to ask for the same file.
function routeFor(path) {
const data = Template.currentData() || {};
if (!data.cardId || !data.listId) return path;
if (path.endsWith('exportPDF')) {
return '/api/boards/:boardId/lists/:listId/cards/:cardId/exportPDF';
}
if (path.endsWith('exportExcel')) {
return '/api/boards/:boardId/lists/:listId/cards/:cardId/exportExcel';
}
return path;
}
function exportUrl(path, extra = {}) {
const boardId = Session.get('currentBoard');
const data = Template.currentData() || {};
const route = routeFor(path);
const params = { boardId };
if (route.includes(':listId')) params.listId = data.listId;
if (route.includes(':cardId')) params.cardId = data.cardId;
return FlowRouter.path(route, params, {
authToken: Accounts._storedLoginToken(),
fields: selectedFields().join(','),
...currentScope(),
...exportLocaleParams(),
...extra,
});
}
function exportFilename(extension) {
const name = String(scopeTitle())
.replace(/[^a-z0-9._-]+/gi, '-')
.replace(/-+/g, '-')
.replace(/^-|-$/g, '') || 'export';
return `${name}.${extension}`;
}
// The import half's state. Module-level like the selection, because the popup is
// destroyed and recreated as it is reopened and a half-finished import should
// still be able to say how it went.
const importState = new ReactiveDict();
importState.set('busy', false);
importState.set('error', '');
importState.set('done', '');
// A WeKan export is either the document itself or a .zip with the document
// inside it. Reading the .zip in the BROWSER is what keeps the server out of it:
// the method takes the same object either way, so there is one import path and
// not two.
async function readExportFile(file) {
const name = String(file.name || '').toLowerCase();
if (!name.endsWith('.zip')) {
// A .json already carries its attachments as base64 under `file`, which is
// what the importer reads.
return JSON.parse(await file.text());
}
const JSZip = (await import('jszip')).default;
const zip = await JSZip.loadAsync(file);
const entry = zip.file('wekan.json') || zip.file(/wekan\.json$/)[0];
if (!entry) throw new Error('import-not-wekan-export');
const doc = JSON.parse(await entry.async('string'));
// The FILES beside the document. The export writes them as
// `attachments/<attachmentId>-<name>`, and the id is what ties a file back to
// its metadata row - two attachments called "photo.png" are two entries.
//
// They go onto the SAME `file` field a .json export uses, so the server has
// one import path rather than one per container. That is also the tradeoff to
// know about: the archive is unpacked in the browser and sent as base64, so a
// .zip full of large attachments costs memory on the way in. It is the shape
// the board import has always used, and the reason to reach for a .zip is
// still the same - the JSON of a board with many attachments may be too large
// for one string, while its archive is not.
const byId = new Map(
(Array.isArray(doc.attachments) ? doc.attachments : [])
.filter(attachment => attachment && attachment._id)
.map(attachment => [attachment._id, attachment]),
);
const files = zip.file(/^attachments\//);
for (const archived of files || []) {
const base = archived.name.slice('attachments/'.length);
const dash = base.indexOf('-');
const attachmentId = dash === -1 ? base : base.slice(0, dash);
const attachment = byId.get(attachmentId);
// A file whose row is not in the document is one the selection left out, or
// a hand-made archive: skipping it is better than inventing a row for it.
if (!attachment) continue;
attachment.file = await archived.async('base64');
}
return doc;
}
// A .json is small enough to travel as a document: it is parsed here and sent
// over DDP, which is the path the per-menu import has always used.
function importJsonDocument(doc, target) {
return new Promise((resolve, reject) => {
Meteor.call('importScoped', target, doc, selectedFields(),
(err, res) => (err ? reject(err) : resolve(res)));
});
}
// POST the archive itself to /api/import/zip. `fetch` streams a File body, so
// the browser does not hold a copy either.
async function uploadZipImport(file, target) {
const params = new URLSearchParams({
authToken: Accounts._storedLoginToken() || '',
boardId: target.boardId,
fields: selectedFields().join(','),
});
for (const key of ['swimlaneId', 'listId', 'cardId', 'checklistId']) {
if (target[key]) params.set(key, target[key]);
}
const response = await fetch(`/api/import/zip?${params.toString()}`, {
method: 'POST',
headers: { 'Content-Type': 'application/zip' },
body: file,
});
const answer = await response.json().catch(() => ({}));
if (!response.ok || !answer.ok) {
throw new Error(answer.error || 'import-scoped-failed');
}
return answer.counts;
}
Template.exportScopeBody.helpers({
// Import writes to the board, so unlike export it is offered only to somebody
// who may change it.
canImport() {
const user = ReactiveCache.getCurrentUser();
return Boolean(user && !user.isWorker && !user.isCommentOnly
&& !user.isReadOnly && !user.isReadAssignedOnly);
},
importBusy() { return importState.get('busy'); },
importError() { return importState.get('error'); },
importDone() { return importState.get('done'); },
exportFields() {
return BOARD_EXPORT_FIELDS.map(({ field, label }) => ({
field,
label,
checked: selection.get(field),
}));
},
cardDetailsChecked() {
return selection.get('card-details');
},
exportUrlPDF() {
return exportUrl('/api/boards/:boardId/exportPDF');
},
exportUrlExcel() {
return exportUrl('/api/boards/:boardId/exportExcel');
},
exportUrlJson() {
return exportUrl('/api/boards/:boardId/export');
},
// The same document without the base64 file data - #5870's option, offered
// wherever an export is offered rather than only on the board menu.
exportUrlJsonNoAttachments() {
return exportUrl('/api/boards/:boardId/export', { attachments: 'false' });
},
exportUrlZip() {
return exportUrl('/api/boards/:boardId/exportZip');
},
exportFilenameJson() {
return exportFilename('json');
},
exportFilenameZip() {
return exportFilename('zip');
},
exportFilenamePDF() {
return exportFilename('pdf');
},
exportFilenameExcel() {
return exportFilename('xlsx');
},
});
Template.exportScopeBody.events({
async 'change .js-import-file'(event) {
const file = event.currentTarget.files && event.currentTarget.files[0];
if (!file) return;
const data = Template.currentData() || {};
const target = {
boardId: Session.get('currentBoard'),
...currentScope(),
};
// A card's menu imports below THAT card, so the target is the card the popup
// was opened on - the same id the export side uses as its scope.
if (data.cardId) target.cardId = data.cardId;
importState.set('error', '');
importState.set('done', '');
importState.set('busy', true);
try {
const counts = String(file.name || '').toLowerCase().endsWith('.zip')
// A .zip goes to the server AS A FILE. Unpacking it here and sending
// base64 over DDP is what made a large archive expensive: 2 GB of
// attachments became 2.7 GB in one message. The route streams it to
// disk, reads the archive's directory, and pipes one attachment at a
// time into storage.
? await uploadZipImport(file, target)
: await importJsonDocument(await readExportFile(file), target);
importState.set('done', Object.entries(counts || {})
.filter(([, n]) => n > 0)
.map(([what, n]) => `${n} ${what}`)
.join(', ') || '0');
} catch (error) {
const message = error && error.message === 'import-not-wekan-export'
? 'import-not-wekan-export'
: 'import-scoped-failed';
importState.set('error', message);
// The reason, for somebody reading the console - the popup has room for a
// sentence, not for a stack.
console.error('scoped import failed', error);
} finally {
importState.set('busy', false);
event.currentTarget.value = '';
}
},
'click .js-export-field-toggle'(event) {
// The popup stays open: choosing five sections should not be five reopens.
event.preventDefault();
const field = event.currentTarget.dataset.field;
selection.set(field, !selection.get(field));
},
'click .js-export-card-details-toggle'(event) {
event.preventDefault();
selection.set('card-details', !selection.get('card-details'));
},
});
// `exportUrlFor` is what the board popup uses to build the same URLs with the
// same selection - one query string, built in one place, whichever popup asks.
export {
selectedFields,
selection,
readExportFile,
exportUrl as exportUrlFor,
exportFilename as exportFilenameFor,
};