-
Notifications
You must be signed in to change notification settings - Fork 5
/
doc-versions.js
78 lines (67 loc) · 2.41 KB
/
doc-versions.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//noinspection JSLint
var g = typeof global === 'undefined' ? window : global;
//noinspection JSLint,JSHint,ThisExpressionReferencesGlobalObjectJS
(function (papyrus) {
require('./doc');
var spcf = papyrus.spcf;
var Doc = papyrus.Doc;
Doc._p.getVersion = function (ver_id) {
var doc = this;
var inv = doc.get('.ui') || '';
var res = {
versionId: ver_id,
since: spcf.tok2long(doc.get(ver_id + '.ts')),
till: null,
href: papyrus.getUriBySpec(doc.spec + inv + '*' + ver_id.substr(1))
};
var tillAsTok = doc.get(ver_id + '.tt');
if (tillAsTok) {
res.till = spcf.tok2long(tillAsTok);
}
return res;
};
Doc._p.getAllVersions = function (versionIds) {
var doc = this;
if (!versionIds) { versionIds = doc.get('.VL'); }
if (!versionIds) { return []; }
var res = [];
versionIds = spcf.split(versionIds);
var i, len;
for (i = 0, len = versionIds.length; i < len; i++) {
var verId = versionIds[i];
var version = doc.getVersion(verId);
if (!version.till) { continue; }
res.push(version);
}
res.sort(function byTillDesc(a, b) { return b.till - a.till; });
var inv = doc.get('.ui') || '';
res.forEach(function(version, idx) {
if (idx === res.length - 1) return;
version.href = papyrus.getUriBySpec(doc.spec +
inv +
'*' + version.versionId.substr(1) +
'!' + res[idx + 1].versionId.substr(1));
});
return res;
};
Doc._p.findClosestVersion = function (versionId) {
versionId = '$' + versionId.substr(1);
var versions = this.getAllVersions();
for (var i = versions.length - 1; i >= 0; i--) {
if (versions[i].versionId >= versionId) {
return versions[i];
}
}
if (versions.length) {
return versions[0];
} else {
return null;
}
};
papyrus.addFilter('.VL', function versionListChanged(spec, versionIds) {
papyrus.emit('versionListChanged', this.getAllVersions(versionIds));
});
papyrus.addFilter('.ok', function refreshVersionsOnOk() {
papyrus.emit('versionListChanged', this.getAllVersions());
});
}(g['papyrus'] = g['papyrus'] || {}));