<commit_before>class PopupPage {
  constructor() {
    this.findElements()
    this.shiftPressed = false
    this.ctrlPressed = false
    this.commandSelected = false
    this.altPressed = false
  }

  findElements() {
    // Context switching and shortcuts:
    this.contextSwitch = document.getElementById('context-switch')
    for (let i of SHORTCUTS) {
      this[`shortcuts${i}`] = document.querySelectorAll(`.shortcut-${i}`)
      this[`shortcutText${i}`] = document.getElementById(`shortcut-text${i}`)
      this[`shortcutLogo${i}`] = document.getElementById(`shortcut-logo${i}`)
    }

    // Active context:
    this.repo = document.getElementById('repository')
    this.user = document.getElementById('user')
    this.org = document.getElementById('organization')
    this.project = document.getElementById('project')
    this.milestone = document.getElementById('milestone')
    this.repoLogo = document.getElementById('repo-logo')
    this.userLogo = document.getElementById('user-logo')
    this.orgLogo = document.getElementById('org-logo')
    this.projectLogo = document.getElementById('project-logo')
    this.milestoneLogo = document.getElementById('milestone-logo')

    // Shortcuts:
    this.fShortcuts = document.querySelectorAll('.shortcut-f')
    this.tShortcuts = document.querySelectorAll('.shortcut-t')
    this.sShortcuts = document.querySelectorAll('.shortcut-s')
    this.rShortcuts = document.querySelectorAll('.shortcut-r')
    this.oShortcuts = document.querySelectorAll('.shortcut-o')
    this.iShortcuts = document.querySelectorAll('.shortcut-i')
    this.pShortcuts = document.querySelectorAll('.shortcut-p')
    this.mShortcuts = document.querySelectorAll('.shortcut-m')
    this.vShortcuts = document.querySelectorAll('.shortcut-v')

    // Modifiers:
    this.closedIssueModifers = document.querySelectorAll('.closed-issues')
    this.newIssue = document.getElementById('new-issue')
    this.mergedPRModifiers = document.querySelectorAll('.merged-pull-requests')
    this.closedPRModifiers = document.querySelectorAll('.closed-pull-requests')
    this.newPullRequest = document.getElementById('new-pull-request')

    // Containers:
    this.repoCommands = document.getElementById('repo-commands')
    this.userCommands = document.getElementById('user-commands')
    this.orgCommands = document.getElementById('org-commands')
    this.projectCommands = document.getElementById('project-commands')
    this.milestoneCommands = document.getElementById('milestone-commands')

    this.welcome = document.getElementById('welcome')
    this.shortcuts = document.querySelectorAll('.shortcut')
  }

  runAfterDelay(action) {
    this.commandSelected = true
    if (this.shortcutTimer) {
      clearTimeout(this.shortcutTimer)
    }
    this.shortcutTimer = setTimeout(() => {
      const highlights = document.querySelectorAll('.highlighted')
      for (let i = 0; i < highlights.length; i++) {
        highlights[i].classList.remove('highlighted')
      }
      this.ctrlPressed = false
      this.shiftPressed = false
      this.commandSelected = false
      this.altPressed = false
      action()
    }, 400)
  }

  openFileFinder() {
    HubnavStorage.load().then(options => {
      if (options.repository && options.repository.length > 0) {
        this.highlightShortcut(this.fShortcuts)
        const defaultBranch = options.defaultBranch || 'master'
        const path = `/find/${defaultBranch}`
        this.openTab(this.repoUrl(options.repository, path, options.githubUrl))
      } else {
        this.openRepoSelect()
      }
    })
  }

  openRepoSelect() {
    this.highlightShortcut(this.rShortcuts)
    this.openOptions('select-repository')
  }

  openGlobalSearch() {
    this.highlightShortcut(this.sShortcuts)
    this.openTab('https://github.com/search')
  }

  openTeams() {
    HubnavStorage.load().then(options => {
      if (options.user && options.user.length > 0 && options.userIsOrg) {
        this.highlightShortcut(this.tShortcuts)
        const org = encodeURIComponent(options.user)
        this.openTab(`https://github.com/orgs/${org}/teams`)
      } else {
        this.openOptions()
      }
    })
  }

  highlightShortcut(shortcuts) {
    for (let i = 0; i < shortcuts.length; i++) {
      shortcuts[i].classList.add('highlighted')
      this.highlightModifier(shortcuts[i])
    }
  }

  highlightModifier(shortcut) {
    if (!this.shiftPressed && !this.ctrlPressed && !this.altPressed) {
      return
    }

    const parent = shortcut.parentNode
    if (!parent.classList.contains('with-modifier')) {
      return
    }

    let modifier = null
    if (this.shiftPressed) {
      modifier = parent.querySelector('.shift-modifier')
    } else if (this.ctrlPressed) {
      modifier = parent.querySelector('.ctrl-modifier')
    } else if (this.altPressed) {
      modifier = parent.querySelector('.alt-modifier')
    }

    if (modifier) {
      modifier.classList.add('highlighted')
    }
  }

  openIssues() {
    HubnavStorage.load().then(options => {
      if (options.active === 'user' && options.user && options.user.length > 0) {
        this.highlightShortcut(this.iShortcuts)
        let params = '?utf8=✓&q=is%3Aissue'
        if (this.shiftPressed) { // closed
          params += '+is%3Aclosed'
        } else { // open
          params += '+is%3Aopen'
        }
        const user = encodeURIComponent(options.user)
        if (options.userIsOrg) { // organization
          params += `+org%3A${user}`
        } else { // user
          params += `+author%3A${user}`
          if (options.scope && options.scope.length > 0) {
            if (options.scope.indexOf('/') > -1) { // repository
              const ownerAndName = options.scope.split('/')
              params += `+repo%3A${ownerAndName[0]}%2F${ownerAndName[1]}`
            } else { // organization
              params += `+org%3A${options.scope}`
            }
          }
        }
        this.openTab(`https://github.com/issues${params}`)

      } else if (options.active === 'repository' && options.repository &&
                 options.repository.length > 0) {
        this.highlightShortcut(this.iShortcuts)
        let path = '/issues'
        if (this.shiftPressed) { // closed
          path += '?utf8=✓&q=is%3Aissue+is%3Aclosed'
        } else if (this.ctrlPressed) { // new
          path += '/new'
        }
        this.openTab(this.repoUrl(options.repository, path, options.githubUrl))

      } else if (options.active === 'project' && options.projectNumber &&
                 options.projectNumber.length > 0 && options.projectRepo &&
                 options.projectRepo.length > 0) {
        this.highlightShortcut(this.iShortcuts)
        const args = this.argsForProjectUrl('issues')
        this.openTab(this.repoProjectUrl(options.projectRepo, options.projectNumber, args))

      } else if (options.active === 'project' && options.projectNumber &&
                 options.projectNumber.length > 0 && options.projectOrg &&
                 options.projectOrg.length > 0) {
        this.highlightShortcut(this.iShortcuts)
        const args = this.argsForProjectUrl('issues')
        this.openTab(this.orgProjectUrl(options.projectOrg, options.projectNumber, args))

      } else {
        this.openRepoSelect()
      }
    })
  }

  openHomeForContext() {
    HubnavStorage.load().then(options => {
      if (options.active === 'repository' && options.repository && options.repository.length > 0) {
        this.highlightShortcut(this.vShortcuts)
        this.openTab(this.repoUrl(options.repository, null, options.githubUrl))
      } else if (options.active === 'user' && options.user && options.user.length > 0) {
        this.highlightShortcut(this.vShortcuts)
        const user = encodeURIComponent(options.user)
        this.openTab(`https://github.com/${user}`)<commit_msg>Refactor openIssues to pull out functions<commit_after>class PopupPage {
  constructor() {
    this.findElements()
    this.shiftPressed = false
    this.ctrlPressed = false
    this.commandSelected = false
    this.altPressed = false
  }

  findElements() {
    // Context switching and shortcuts:
    this.contextSwitch = document.getElementById('context-switch')
    for (let i of SHORTCUTS) {
      this[`shortcuts${i}`] = document.querySelectorAll(`.shortcut-${i}`)
      this[`shortcutText${i}`] = document.getElementById(`shortcut-text${i}`)
      this[`shortcutLogo${i}`] = document.getElementById(`shortcut-logo${i}`)
    }

    // Active context:
    this.repo = document.getElementById('repository')
    this.user = document.getElementById('user')
    this.org = document.getElementById('organization')
    this.project = document.getElementById('project')
    this.milestone = document.getElementById('milestone')
    this.repoLogo = document.getElementById('repo-logo')
    this.userLogo = document.getElementById('user-logo')
    this.orgLogo = document.getElementById('org-logo')
    this.projectLogo = document.getElementById('project-logo')
    this.milestoneLogo = document.getElementById('milestone-logo')

    // Shortcuts:
    this.fShortcuts = document.querySelectorAll('.shortcut-f')
    this.tShortcuts = document.querySelectorAll('.shortcut-t')
    this.sShortcuts = document.querySelectorAll('.shortcut-s')
    this.rShortcuts = document.querySelectorAll('.shortcut-r')
    this.oShortcuts = document.querySelectorAll('.shortcut-o')
    this.iShortcuts = document.querySelectorAll('.shortcut-i')
    this.pShortcuts = document.querySelectorAll('.shortcut-p')
    this.mShortcuts = document.querySelectorAll('.shortcut-m')
    this.vShortcuts = document.querySelectorAll('.shortcut-v')

    // Modifiers:
    this.closedIssueModifers = document.querySelectorAll('.closed-issues')
    this.newIssue = document.getElementById('new-issue')
    this.mergedPRModifiers = document.querySelectorAll('.merged-pull-requests')
    this.closedPRModifiers = document.querySelectorAll('.closed-pull-requests')
    this.newPullRequest = document.getElementById('new-pull-request')

    // Containers:
    this.repoCommands = document.getElementById('repo-commands')
    this.userCommands = document.getElementById('user-commands')
    this.orgCommands = document.getElementById('org-commands')
    this.projectCommands = document.getElementById('project-commands')
    this.milestoneCommands = document.getElementById('milestone-commands')

    this.welcome = document.getElementById('welcome')
    this.shortcuts = document.querySelectorAll('.shortcut')
  }

  runAfterDelay(action) {
    this.commandSelected = true
    if (this.shortcutTimer) {
      clearTimeout(this.shortcutTimer)
    }
    this.shortcutTimer = setTimeout(() => {
      const highlights = document.querySelectorAll('.highlighted')
      for (let i = 0; i < highlights.length; i++) {
        highlights[i].classList.remove('highlighted')
      }
      this.ctrlPressed = false
      this.shiftPressed = false
      this.commandSelected = false
      this.altPressed = false
      action()
    }, 400)
  }

  openFileFinder() {
    HubnavStorage.load().then(options => {
      if (options.repository && options.repository.length > 0) {
        this.highlightShortcut(this.fShortcuts)
        const defaultBranch = options.defaultBranch || 'master'
        const path = `/find/${defaultBranch}`
        this.openTab(this.repoUrl(options.repository, path, options.githubUrl))
      } else {
        this.openRepoSelect()
      }
    })
  }

  openRepoSelect() {
    this.highlightShortcut(this.rShortcuts)
    this.openOptions('select-repository')
  }

  openGlobalSearch() {
    this.highlightShortcut(this.sShortcuts)
    this.openTab('https://github.com/search')
  }

  openTeams() {
    HubnavStorage.load().then(options => {
      if (options.user && options.user.length > 0 && options.userIsOrg) {
        this.highlightShortcut(this.tShortcuts)
        const org = encodeURIComponent(options.user)
        this.openTab(`https://github.com/orgs/${org}/teams`)
      } else {
        this.openOptions()
      }
    })
  }

  highlightShortcut(shortcuts) {
    for (let i = 0; i < shortcuts.length; i++) {
      shortcuts[i].classList.add('highlighted')
      this.highlightModifier(shortcuts[i])
    }
  }

  highlightModifier(shortcut) {
    if (!this.shiftPressed && !this.ctrlPressed && !this.altPressed) {
      return
    }

    const parent = shortcut.parentNode
    if (!parent.classList.contains('with-modifier')) {
      return
    }

    let modifier = null
    if (this.shiftPressed) {
      modifier = parent.querySelector('.shift-modifier')
    } else if (this.ctrlPressed) {
      modifier = parent.querySelector('.ctrl-modifier')
    } else if (this.altPressed) {
      modifier = parent.querySelector('.alt-modifier')
    }

    if (modifier) {
      modifier.classList.add('highlighted')
    }
  }

  openIssues() {
    HubnavStorage.load().then(options => {
      if (options.active === 'user' && options.user && options.user.length > 0) {
        this.openUserIssues(options)

      } else if (options.active === 'repository' && options.repository &&
                 options.repository.length > 0) {
        this.openRepoIssues(options)

      } else if (options.active === 'project' && options.projectNumber &&
                 options.projectNumber.length > 0 && options.projectRepo &&
                 options.projectRepo.length > 0) {
        this.openRepoProjectIssues(options)

      } else if (options.active === 'project' && options.projectNumber &&
                 options.projectNumber.length > 0 && options.projectOrg &&
                 options.projectOrg.length > 0) {
        this.openOrgProjectIssues(options)

      } else {
        this.openRepoSelect()
      }
    })
  }

  openUserIssues(options) {
    this.highlightShortcut(this.iShortcuts)
    let params = '?utf8=✓&q=is%3Aissue'
    if (this.shiftPressed) { // closed
      params += '+is%3Aclosed'
    } else { // open
      params += '+is%3Aopen'
    }
    const user = encodeURIComponent(options.user)
    if (options.userIsOrg) { // organization
      params += `+org%3A${user}`
    } else { // user
      params += `+author%3A${user}`
      if (options.scope && options.scope.length > 0) {
        if (options.scope.indexOf('/') > -1) { // repository
          const ownerAndName = options.scope.split('/')
          params += `+repo%3A${ownerAndName[0]}%2F${ownerAndName[1]}`
        } else { // organization
          params += `+org%3A${options.scope}`
        }
      }
    }
    this.openTab(`https://github.com/issues${params}`)
  }

  openRepoIssues(options) {
    this.highlightShortcut(this.iShortcuts)
    let path = '/issues'
    if (this.shiftPressed) { // closed
      path += '?utf8=✓&q=is%3Aissue+is%3Aclosed'
    } else if (this.ctrlPressed) { // new
      path += '/new'
    }
    this.openTab(this.repoUrl(options.repository, path, options.githubUrl))
  }

  openRepoProjectIssues(options) {
    this.highlightShortcut(this.iShortcuts)
    const args = this.argsForProjectUrl('issues')
    this.openTab(this.repoProjectUrl(options.projectRepo, options.projectNumber, args))
  }

  openOrgProjectIssues(options) {
    this.highlightShortcut(this.iShortcuts)
    const args = this.argsForProjectUrl('issues')
    this.openTab(this.orgProjectUrl(options.projectOrg, options.projectNumber, args))
  }

  openHomeForContext() {
    HubnavStorage.load().then(options => {
      if (options.active === 'repository' && options.repository && options.repository.length > 0) {
        this.highlightShortcut(this.vShortcuts)
        this.openTab(this.repoUrl(options.repository, null, options.githubUrl))
      } else if (options.active === 'user' && options.user && options.user.length > 0) {
        this.highlightShortcut(this.vShortcuts)
        const user = encodeURIComponent(options.user)
        this.openTab(`https://github.com/${user}`)
{'fraction_non_alphanumeric': 0.13987158504343447, 'fraction_numerical': 0.003902807503462168, 'mean_word_length': 3.0528061224489798, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4508950', 'n_tokens_mistral': 4783, 'n_tokens_neox': 4710, 'n_words': 698}