13

Found a JavaScript bug when using the hammer. Steps to reproduce:

  1. Close a question as duplicate (or find an existing one)
  2. Edit the list of duplicate targets
  3. Click the link to add a new duplicate target
  4. Click on one of the pre-filled questions

Nothing happens, unless you click on the title, which acts as a normal link.

By searching for a target, it can be made to work – just the pre-selected options don't respond to click events like they should.

6
  • 1
    Found a Javascript bug ... well, that is why the book about The Bad Parts has so many pages ...
    – rene
    Commented Aug 5, 2022 at 15:18
  • 2
    it's a feature: the pre-selected options never works so you never have to select them ;) Commented Aug 5, 2022 at 15:48
  • 2
    Thanks for reporting this. I found it a couple of weeks ago, but every time I see I forget to raise a bug about it.
    – Dharman Mod
    Commented Aug 6, 2022 at 9:54
  • 1
    I forgot about it and repeated it on Duplicate selection doesn't allow to pick an item without searching
    – Dharman Mod
    Commented Nov 2, 2022 at 17:56
  • @Dharman yeah I should have posted this on MSE as it's almost certainly a network-wide issue
    – miken32
    Commented Nov 2, 2022 at 18:46
  • I have reproduced this bug as well. As a workaround: it is possible to drag the link into the text field at the top. Commented Nov 4, 2022 at 6:08

2 Answers 2

8

Posting this here because this one is ed, hopefully it helps whoever handles the ticket.

It appears that in duplicate-targets-edited they've forgotten to provide a callback to asyncLoad which would normally handle making the originals clickable:

This is the current code:

addButton.click(function (e) {

    addButton.loadPopup({
        url: '/questions/originals/' + questionId + '/find-originals-popup',
        target: $('.container')
    })
        .done(function ($popup) {
            StackExchange.vote_closingAndFlagging.close_initDuplicateSubPane();
            $popup.asyncLoad();

There should be a callback to StackExchange.vote_closingAndFlagging.close_afterLoadListOriginals which is not currently being done:

addButton.loadPopup({
    url: '/questions/originals/' + questionId + '/find-originals-popup',
    target: $('.container')
})
    .done(function ($popup) {
        StackExchange.vote_closingAndFlagging.close_initDuplicateSubPane();
        $popup.asyncLoad({
            callback: StackExchange.vote_closingAndFlagging.close_afterLoadListOriginals
        });

It does work elsewhere because when navigating to the duplicate closure window from the standard close dialogue the callback is appropriately attached in full.en.js

paneToShow
    .addClass('popup-active-pane')
    .show()
    .trigger('show')
    .asyncLoad({
        callback: closeAfterLoadListOriginals
    });

In the meantime, here's a simple UserScript that fixes the problem by simply overwriting the duplicateTargetsEdited with the fixed implementation:

// ==UserScript==
// @name         Fix Edit Originals
// @version      0.0.1
//
// @match        *://*.stackoverflow.com/questions/originals/*/edit
//
// @run-at       document-start
//
// @grant        none
//
// ==/UserScript==
/* globals StackExchange */

(function () {
    'use strict';

    const overWriteWithFix = () => {
        Object.defineProperty(StackExchange, 'duplicateTargetsEdited', {
            get() {
                return function (questionId, questionUrl) {

                    var saveButton = $('.js-save-origs');
                    var addButton = $('.js-add-orig');
                    var list = $('.js-originals-list');

                    var countOriginals = function () {
                        return $('.js-originals-list .js-remove-orig').length;
                    };

                    var getOriginalIds = function () {
                        return list.find('.js-remove-orig').map(function (i, v) {
                            return $(v).data('id');
                        });
                    };

                    var updateButtons = function () {
                        var originalCount = countOriginals();
                        saveButton.enable(originalCount > 0 && originalCount <= 5);
                        addButton.enable(originalCount < 5);
                    };

                    addButton.click(function (e) {

                        addButton.loadPopup({
                            url: '/questions/originals/' + questionId + '/find-originals-popup',
                            target: $('.container')
                        })
                            .done(function ($popup) {
                                StackExchange.vote_closingAndFlagging.close_initDuplicateSubPane();
                                $popup.asyncLoad({
                                    callback: StackExchange.vote_closingAndFlagging.close_afterLoadListOriginals
                                });

                                $('#add-original-form').submit(function (ev) {
                                    ev.preventDefault();

                                    var newOriginal = parseInt($('.js-original-question-id').val(), 10);
                                    var existingOrigs = getOriginalIds();
                                    for (var i = 0; i < existingOrigs.length; i++) {
                                        if (existingOrigs[i] == newOriginal) {
                                            $popup.find('.js-add-original').showErrorMessage(__tr(["The selected question is already listed!"], undefined, "en", []), {
                                                relativeToBody: false,
                                                position: {at: 'left center', my: 'right top'},
                                                css: {'z-index': 10}
                                            });
                                            return;
                                        }
                                    }

                                    $.get('/questions/originals/get-original-row/' + newOriginal, function (html) {
                                        list.append(html);
                                    })
                                        .fail(function () {
                                            setTimeout(function () {
                                                addButton.showErrorMessage(__tr(["Error fetching question details. Please try again."], undefined, "en", []), {
                                                    relativeToBody: true,
                                                    position: {at: 'right center', my: 'left top'},
                                                    css: {'z-index': 10}
                                                });
                                            }, 0);
                                        })
                                        .always(function () {
                                            StackExchange.helpers.closePopups();
                                            updateButtons();
                                        });
                                });

                                // the short story here is that initialization doesn't complete until the "show" event is triggered.
                                // but we can't trigger that NOW, because the popup isn't actually visible yet
                                // popup gets faded in after this callback returns...
                                // so, spin the event loop and listen for animation completed after it has had time to start.
                                // all this for the sake of re-using the duplicate dialog logic unchanged
                                setTimeout(function () {
                                    $popup.promise().then(function () {
                                        $popup.find(".close-as-duplicate-pane").trigger("show");
                                    });
                                }, 0);
                            });

                        e.preventDefault();
                    });

                    list.on('click', '.js-remove-orig',
                        function (e) {
                            $(this).closest('.js-original-question').remove();
                            updateButtons();
                            e.preventDefault();
                        }
                    );

                    saveButton.click(function (e) {

                        var originals = getOriginalIds().toArray();

                        $.post('/questions/originals/' + questionId + '/save-originals', {
                            originalsIdsJson: JSON.stringify(originals),
                            fkey: StackExchange.options.user.fkey
                        }).done(
                            function () {
                                location.href = questionUrl;
                            }
                        ).fail(
                            function (response) {
                                saveButton.showErrorMessage(response.responseJSON, {
                                    relativeToBody: true,
                                    position: {at: 'right center', my: 'left top', css: {'z-index': 10}}
                                });
                            }
                        );

                        e.preventDefault();
                    });

                    var $sortable = $('.js-originals-list.sortable');
                    StackExchange.loadJqueryUi().done(function () {
                        $sortable
                            .sortable({
                                axis: "y",
                                update: function () {
                                    saveButton.enable();
                                }
                            });
                    });
                }
            }
        });
    }

    if (window.StackExchange === undefined || Object.keys(window.StackExchange).length === 0) {
        Object.defineProperty(window, 'StackExchange', {
            configurable: true,
            internalSE: undefined,
            get() {
                return this.internalSE;
            },
            set(newStackExchange) {
                if (Object.keys(newStackExchange).length !== 0) {
                    delete window.StackExchange;
                    window.StackExchange = newStackExchange;
                    overWriteWithFix();
                } else {
                    this.internalSE = newStackExchange;
                }
            }
        });
    } else {
        overWriteWithFix();
    }
}());
1
  • 3
    This was indeed the solution, thank you. The fix is rolled out now.
    – Connell StaffMod
    Commented Feb 6, 2023 at 11:45
6

This is now fixed, as of Feb 6th, 2023.

You can now select one of the suggested duplicates, which loads the question into the popup box, then the "Add Original" button is enabled to confirm.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .