Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't rely on indexes in chrome.tabs.move

I have tabs like this:

  • tab A
  • tab B
  • tab C
  • tab D
  • tab E

to move tab A after tab C I use

chrome.tabs.move(tabA.id, {'index': tabC.index+1});

but I get it after tab D
If I move any tab from down to up in the list, it works fine, but if "upper" tab is moved down, indexes get messed (upper tab is removed from indexes list and target index is down by 1)
Do I need to somehow compare moved tab index and target index and based on that calculate resulting index (if A < C) or is there easier solution for this?

like image 209
kl123 Avatar asked Nov 01 '22 01:11

kl123


1 Answers

I've had some experiences with the Chrome API too, and if there's one thing I learned, then it's that the API just behaves as it does.
If new indices get assigned to tabs before the current one is inserted, but after it's been removed, then that's just that.
It's probably best to just go with a check like you suggested:

function moveAfter(what, target)
{
    target = (typeof target === 'number' target : target.index) + 1;
    if(what.constructor === Array.prototype.constructor)
    {
        var w = [];
        var t = target;
        for(var i = 0; i < what.length)
        {
            if(what[i].index < target)
            {
                t--;
            }
            w.push(what[i].id);
        }
        what = w;
        target = t;
    }
    else
    {
        if(what.index < target)
        {
            target--;
        }
        what = what.id;
    }
    chrome.tabs.move(what, {index: target});
}

what can be a Tab object or an array of Tabs.
target can be a Tab object or a tab index (without the +1).

like image 186
Siguza Avatar answered Nov 13 '22 01:11

Siguza