<commit_before>/* global $ MB server relEditor GM_info */
'use strict';
// ==UserScript==
// @name         MusicBrainz: Set relation attributes in relationships editor
// @namespace    mbz-loujine
// @author       loujine
// @version      2018.1.3
// @downloadURL  https://bitbucket.org/loujine/musicbrainz-scripts/raw/default/mbz-setattributes.user.js
// @updateURL    https://bitbucket.org/loujine/musicbrainz-scripts/raw/default/mbz-setattributes.user.js
// @supportURL   https://bitbucket.org/loujine/musicbrainz-scripts
// @icon         https://bitbucket.org/loujine/musicbrainz-scripts/raw/default/icon.png
// @description  musicbrainz.org: Set attributes (live, partial, solo) in relationships editor
// @compatible   firefox+tampermonkey
// @license      MIT
// @require      https://greasyfork.org/scripts/13747-mbz-loujine-common/code/mbz-loujine-common.js?version=241520
// @include      http*://*musicbrainz.org/release/*/edit-relationships
// @grant        none
// @run-at       document-end
// ==/UserScript==

function setAttributes(relationType, attrId, toggle) {
    const recordings = MB.relationshipEditor.UI.checkedRecordings();
    recordings.forEach(function(recording) {
        recording.relationships().forEach(function(relation) {
            if (relation.entityTypes !== relationType) {
                return;
            }
            var attrs = relation.attributes(),
                attr = attrs.filter(
                    // attrId already in relation attributes
                    el => el.type.id === attrId
                );
            if (!attr.length) {
                attrs.push({type: MB.attrInfoByID[attrId]});
            } else if (toggle) {
                attrs.splice(attrs.indexOf(attr), 1);
            }
            relation.setAttributes(attrs);
        });
    });
}


(function displayToolbar(relEditor) {
    relEditor.container(document.querySelector('div.tabs'))
             .insertAdjacentHTML('beforeend', `
        <h3>Recording-Work relation attributes</h3>
        <input type="button" id="setLive" value="Set live">
        <input type="button" id="setPartial" value="Set partial">
        <input type="button" id="setInstrumental" value="Set instrumental">
        <input type="button" id="toggleLive" value="Toggle live">
        <input type="button" id="togglePartial" value="Toggle partial">
        <input type="button" id="toggleInstrumental" value="Toggle instrumental">
        <h3>Recording-Artist relation attributes</h3>
        <input type="button" id="toggleSolo" value="Toggle solo">
    `);
})(relEditor);


$(document).ready(function() {
    for (let attr of ['Live', 'Partial', 'Instrumental']) {
        document.getElementById(`set${attr}`).addEventListener('click', () => {
            setAttributes('recording-work', server.attr[attr.toLowerCase()], false);<commit_msg>[set reledit attributes] Simplify code with ES6<commit_after>/* global $ MB server relEditor GM_info */
'use strict';
// ==UserScript==
// @name         MusicBrainz: Set relation attributes in relationships editor
// @namespace    mbz-loujine
// @author       loujine
// @version      2018.1.4
// @downloadURL  https://bitbucket.org/loujine/musicbrainz-scripts/raw/default/mbz-setattributes.user.js
// @updateURL    https://bitbucket.org/loujine/musicbrainz-scripts/raw/default/mbz-setattributes.user.js
// @supportURL   https://bitbucket.org/loujine/musicbrainz-scripts
// @icon         https://bitbucket.org/loujine/musicbrainz-scripts/raw/default/icon.png
// @description  musicbrainz.org: Set attributes (live, partial, solo) in relationships editor
// @compatible   firefox+tampermonkey
// @license      MIT
// @require      https://greasyfork.org/scripts/13747-mbz-loujine-common/code/mbz-loujine-common.js?version=241520
// @include      http*://*musicbrainz.org/release/*/edit-relationships
// @grant        none
// @run-at       document-end
// ==/UserScript==

function setAttributes(relationType, attrId, toggle) {
    const recordings = MB.relationshipEditor.UI.checkedRecordings();
    for (const recording of recordings) {
        for (const relation of recording.relationships().filter(
            rel => rel.entityTypes === relationType
        )) {
            const attrs = relation.attributes(),
                attr = attrs.filter(el => el.type.id === attrId);
            if (!attr.length) {
                attrs.push({type: MB.attrInfoByID[attrId]});
            } else if (toggle) {
                attrs.splice(attrs.indexOf(attr), 1);
            }
            relation.setAttributes(attrs);
        }
    }
}


(function displayToolbar(relEditor) {
    relEditor.container(document.querySelector('div.tabs'))
             .insertAdjacentHTML('beforeend', `
        <h3>Recording-Work relation attributes</h3>
        <input type="button" id="setLive" value="Set live">
        <input type="button" id="setPartial" value="Set partial">
        <input type="button" id="setInstrumental" value="Set instrumental">
        <input type="button" id="toggleLive" value="Toggle live">
        <input type="button" id="togglePartial" value="Toggle partial">
        <input type="button" id="toggleInstrumental" value="Toggle instrumental">
        <h3>Recording-Artist relation attributes</h3>
        <input type="button" id="toggleSolo" value="Toggle solo">
    `);
})(relEditor);


$(document).ready(function() {
    for (const attr of ['Live', 'Partial', 'Instrumental']) {
        document.getElementById(`set${attr}`).addEventListener('click', () => {
            setAttributes('recording-work', server.attr[attr.toLowerCase()], false);
{'fraction_non_alphanumeric': 0.14595941616233535, 'fraction_numerical': 0.008009967960128159, 'mean_word_length': 2.932120363890833, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 30, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2328031', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1622, 'n_words': 302}