-
Notifications
You must be signed in to change notification settings - Fork 16
/
ArchiveBase.js
51 lines (44 loc) · 1.86 KB
/
ArchiveBase.js
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
import { stringify } from '@stratumn/canonicaljson';
// Other Archive Repos
import { ArchiveItem } from '@internetarchive/dweb-archivecontroller';
// This repo
import React from 'react';
import ReactDOM from 'react-dom';
import {AJS_on_dom_loaded} from './Util';
import {Page} from "./components/Page";
// See other almost DUPLICATEDCODE#003 (iaux and dweb-archive)
function _onefield(key, value) {
return Array.isArray(value)
? value.map(v => _onefield(key, v)).join(' OR ')
// This next line uses stringify instead of toString() because we want '"abc"' and '1' i.e. quotes if its a string
: (`${key}:${stringify(value)}`);
}
function queryFrom(query) {
// Turn query as object into a viable query string for a Url (but not yet urlencoded)
return Object.entries(query).map(kv => _onefield(kv[0], kv[1])).join(' AND '); // k1:v1 AND k2:v2
}
// End of DUPLICATEDCODE#0003
const searchConfig = {
rows: 30, // How many to retrieve per page, smaller numbers load quicker, but then scroll down will have to get next page
};
export default class ArchiveBase extends ArchiveItem {
/**
* Subclass of ArchiveItem that can also handle a search and other parameterisation of display
* Fields:
*
*/
constructor({ and='', download=false, identifier = undefined, message=undefined, metaapi = undefined, noCache=false, page=1,
query=undefined, rows=searchConfig.rows, sort=[]}={}) {
super({identifier, metaapi, sort});
this.and = and;
this.download = download; // True if want download directory
this.noCache= noCache;
this.page = page;
this.query = (typeof(query) === "object")
? queryFrom(query) // form { creator: "Foo bar" ... }
: query ; // Note this should be an UNUUENCODED query or an object
this.rows= rows;
this.state = {}; // This will be automatic when moves to React
this.message = message;
}
}