forked from loujine/musicbrainz-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmb-reledit-copy_dates.user.js
119 lines (112 loc) · 4.09 KB
/
mb-reledit-copy_dates.user.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* global $ MB server relEditor */
'use strict';
// ==UserScript==
// @name MusicBrainz relation editor: Copy dates on recording relations
// @namespace mbz-loujine
// @author loujine
// @version 2021.1.19
// @downloadURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-reledit-copy_dates.user.js
// @updateURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-reledit-copy_dates.user.js
// @supportURL https://github.com/loujine/musicbrainz-scripts
// @icon https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/icon.png
// @description musicbrainz.org relation editor: Copy/remove dates on recording relations
// @compatible firefox+tampermonkey
// @license MIT
// @require https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mbz-loujine-common.js
// @include http*://*musicbrainz.org/release/*/edit-relationships
// @grant none
// @run-at document-end
// ==/UserScript==
function copyDate(from_relation, relation) {
['begin_date', 'end_date'].forEach(function (date) {
['day', 'month', 'year'].forEach(function (unit) {
relation[date][unit](from_relation[date][unit]());
});
});
}
function removeDate(relation) {
['begin_date', 'end_date'].forEach(function (date) {
['day', 'month', 'year'].forEach(function (unit) {
relation[date][unit]('');
});
});
relation.ended(false);
}
function referenceDate(relations) {
// look for one recording link with a date
// give priority to the most precise one (day > month > year)
for (const unit of ['day', 'month', 'year']) {
for (const [idx, rel] of relations.entries()) {
if (
rel.end_date[unit]() > 0 &&
Object.values(server.recordingLinkType).includes(
parseInt(rel.linkTypeID())
)
) {
return idx;
}
}
}
return -1;
}
function propagateDates(replace) {
const recordings = MB.relationshipEditor.UI.checkedRecordings();
recordings.forEach(function (recording) {
const relations = recording.relationships();
const idx = referenceDate(relations);
if (idx !== -1) {
relations.forEach(function (rel) {
const linkType = parseInt(rel.linkTypeID());
if (
!rel.removed() &&
Object.values(server.recordingLinkType).includes(linkType) &&
(replace || !rel.formatDatePeriod())
) {
copyDate(relations[idx], rel);
}
});
}
});
}
function removeDates() {
const recordings = MB.relationshipEditor.UI.checkedRecordings();
recordings.forEach(function (recording) {
const relations = recording.relationships();
relations.forEach(function (relation) {
if (!relation.removed()) {
removeDate(relation);
}
});
});
}
(function displayToolbar() {
relEditor.container(document.querySelector('div.tabs'))
.insertAdjacentHTML('beforeend', `
<h3>Dates</h3>
<label for="replaceDates">Replace dates if pre-existing: </label>
<input type="checkbox" id="replaceDates">
<br />
<input type="button" id="copyDates" value="Copy dates">
<input type="button" id="removeDates" value="Removes dates">
`);
})();
$(document).ready(function () {
let appliedNote = false;
document.getElementById('removeDates').addEventListener('click', () => {
removeDates();
relEditor.editNote(GM_info.script);
});
document.getElementById('copyDates').addEventListener('click', () => {
propagateDates(
document.querySelector('input#replaceDates').checked
);
if (!appliedNote) {
relEditor.editNote(
GM_info.script,
'Propagate recording dates from other relationships'
);
appliedNote = true;
}
});
return false;
});