<commit_before>kd                          = require 'kd'
KDView                      = kd.View
KDButtonView                = kd.ButtonView
KDContextMenu               = kd.ContextMenu
KDNotificationView          = kd.NotificationView
KDFormViewWithFields        = kd.FormViewWithFields
KDAutoCompleteController    = kd.AutoCompleteController

KodingSwitch                = require 'app/commonviews/kodingswitch'
AccountListViewController   = require 'account/controllers/accountlistviewcontroller'
MemberAutoCompleteItemView  = require 'app/commonviews/memberautocompleteitemview'
MemberAutoCompletedItemView = require 'app/commonviews/memberautocompleteditemview'

remote                      = require('app/remote').getInstance()
globals                     = require 'globals'
showError                   = require 'app/util/showError'


module.exports = class AccountCredentialListController extends AccountListViewController


  constructor: (options = {}, data) ->

    options.noItemFoundText ?= "You don't have any credentials"
    super options, data

    @loadItems()


  loadItems: ->

    @removeAllItems()
    @showLazyLoader()

    { query, provider, requiredFields } = @getOptions()

    query          ?=  {}
    query.provider ?= provider        if provider
    query.fields   ?= requiredFields  if requiredFields

    { JCredential } = remote.api

    JCredential.some query, { limit: 30 }, (err, credentials) =>

      if provider? and credentials?.length is 0
        @showAddCredentialFormFor provider

      @hideLazyLoader()

      return if showError err, \
        KodingError : "Failed to fetch data, try again later."

      @instantiateListItems credentials


  loadView: ->

    super

    view = @getView()
    view.on 'ShowShareCredentialFormFor', @bound 'showShareCredentialFormFor'
    view.on 'ItemDeleted', (item) =>
      @removeItem item
      @noItemView.show()  if @listView.items.length is 0

    {provider, requiredFields} = @getOptions()

    if provider
    then @createAddDataButton()
    else @createAddCredentialMenu()


  createAddDataButton: ->

    @getView().parent.prepend addButton = new KDButtonView
      cssClass  : 'add-big-btn'
      title     : 'Create New'
      icon      : yes
      callback  : @lazyBound 'showAddCredentialFormFor', @getOption 'provider'


  createAddCredentialMenu: ->

    Providers    = globals.config.providers
    providerList = { }

    Object.keys(Providers).forEach (provider) =>

      return  if provider is 'custom'
      return  if Object.keys(Providers[provider].credentialFields).length is 0

      providerList[Providers[provider].title] =
        callback : =>
          @_addButtonMenu.destroy()<commit_msg>Account Settings: Credentials: Don't show "Add credential" menu when "dontShowCredentialMenu" variable is true or yes<commit_after>kd                          = require 'kd'
KDView                      = kd.View
KDButtonView                = kd.ButtonView
KDContextMenu               = kd.ContextMenu
KDNotificationView          = kd.NotificationView
KDFormViewWithFields        = kd.FormViewWithFields
KDAutoCompleteController    = kd.AutoCompleteController

KodingSwitch                = require 'app/commonviews/kodingswitch'
AccountListViewController   = require 'account/controllers/accountlistviewcontroller'
MemberAutoCompleteItemView  = require 'app/commonviews/memberautocompleteitemview'
MemberAutoCompletedItemView = require 'app/commonviews/memberautocompleteditemview'

remote                      = require('app/remote').getInstance()
globals                     = require 'globals'
showError                   = require 'app/util/showError'


module.exports = class AccountCredentialListController extends AccountListViewController


  constructor: (options = {}, data) ->

    options.noItemFoundText ?= "You don't have any credentials"
    super options, data

    @loadItems()


  loadItems: ->

    @removeAllItems()
    @showLazyLoader()

    { query, provider, requiredFields } = @getOptions()

    query          ?=  {}
    query.provider ?= provider        if provider
    query.fields   ?= requiredFields  if requiredFields

    { JCredential } = remote.api

    JCredential.some query, { limit: 30 }, (err, credentials) =>

      if provider? and credentials?.length is 0
        @showAddCredentialFormFor provider

      @hideLazyLoader()

      return if showError err, \
        KodingError : "Failed to fetch data, try again later."

      @instantiateListItems credentials


  loadView: ->

    super

    view = @getView()

    view.on 'ShowShareCredentialFormFor', @bound 'showShareCredentialFormFor'
    view.on 'ItemDeleted', (item) =>
      @removeItem item
      @noItemView.show()  if @listView.items.length is 0

    { provider, requiredFields, dontShowCredentialMenu } = @getOptions()

    if provider
      @createAddDataButton()
    else
      @createAddCredentialMenu()  unless dontShowCredentialMenu


  createAddDataButton: ->

    @getView().parent.prepend addButton = new KDButtonView
      cssClass  : 'add-big-btn'
      title     : 'Create New'
      icon      : yes
      callback  : @lazyBound 'showAddCredentialFormFor', @getOption 'provider'


  createAddCredentialMenu: ->

    Providers    = globals.config.providers
    providerList = { }

    Object.keys(Providers).forEach (provider) =>

      return  if provider is 'custom'
      return  if Object.keys(Providers[provider].credentialFields).length is 0

      providerList[Providers[provider].title] =
        callback : =>
          @_addButtonMenu.destroy()
{'fraction_non_alphanumeric': 0.08792391889467073, 'fraction_numerical': 0.0017943656917279742, 'mean_word_length': 2.7841140529531567, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, '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': '531494', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1480, 'n_words': 341}