User:Fyan/Improving Tab Hierarchy

From MozillaWiki
Jump to: navigation, search
globals:
  tabs
  selectedTab
  newTabOffset := 0

# opens a new tab with the provided url
# default options: selected = true, position = END, opener = null
openTab(url, options):
  tab := new Tab(url)
  IF 'position' IN options
    tabs.splice(position, 0, tab)
  ELSE
    tabs.push(tab)
  IF options.opener
    tab.opener = options.opener
  IF options.selected
    selectedTab := tab
  RETURN tab

# opens a blank tab
openNewTab():
  tab := openTab('about:blank', { opener: selectedTab })
  tab.onLocationChangeOrDeselect := function()
    DELETE tab.onLocationChangeOrDeselect
    DELETE tab.opener

# triggered when a new tab is opened from a tab,
# including window.open, link cmd+click, and context menu
openURLInNewTab(url, openerTab, selected):
  IF selected
    newTabOffset := 0
  newTabOffset := newTabOffset + 1
  position := openerTab.position + newTabOffset
  openTab(url, { selected: selected, position: position, opener: openerTab })

# triggered when a tab is closed by any mechanism
onTabClose(closedTab):
  prev := closedTab.previousSibling
  next := closedTab.nextSibling
  IF closedTab.opener
    IF next AND next.opener = closedTab.opener
      selectedTab := next
      RETURN
    FOR tab := prev; tab; tab := tab.previousSibling
      IF tab.opener = closedTab.opener
        selectedTab := tab
        RETURN
    IF prev AND prev = closedTab.opener
      selectedTab := prev
      RETURN
  IF next
    selectedTab := next
  ELSE IF prev
    selectedTab := prev
  ELSE IF getBoolPref('closeWindowWithLastTab')
    closeWindow()
  ELSE
    openNewTab()

# triggered when the user explicitly switches tabs (not due to tab close, etc.)
onTabUserSelect(fromTab, toTab):
  newTabOffset := 0
  IF fromTab.opener != toTab AND toTab.opener != fromTab AND
     fromTab.opener != toTab.opener
    FOR EACH tab IN tabs:
      tab.opener := NULL

# triggered when the user explicitly moves tabs
onTabUserMove():
  FOR EACH tab IN tabs
    tab.opener := NULL