<commit_before>_                           = require 'lodash'
kd                          = require 'kd'
hljs                        = require 'highlight.js'

KDView                      = kd.View
KDButtonView                = kd.ButtonView
KDContextMenu               = kd.ContextMenu
KDCustomScrollView          = kd.CustomScrollView
KDNotificationView          = kd.NotificationView
KDFormViewWithFields        = kd.FormViewWithFields

KodingSwitch                = require 'app/commonviews/kodingswitch'
MemberAutoCompleteItemView  = require 'app/commonviews/memberautocompleteitemview'
MemberAutoCompletedItemView = require 'app/commonviews/memberautocompleteditemview'

KodingListController        = require 'app/kodinglist/kodinglistcontroller'

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


module.exports = class AccountCredentialListController extends KodingListController


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

    options.limit            or= 30
    options.noItemFoundText   ?= "You don't have any credentials"
    options.model             ?= remote.api.JCredential

    super options, data


  bindEvents: ->

    super

    { provider } = @getOptions()
    listView     = @getListView()

    listView.on 'ItemDeleted', @bound 'showNoItemWidget'

    listView.on 'ItemAction', ({ action, item, options }) =>

      credential    = item.getData()
      { provider }  = credential

      switch action

        when 'ShowItem'
          @fetchCredentialData credential, (err, data) ->
            return  if showError err

            { meta }        = data
            meta            = helper.prepareCredentialMeta meta
            meta.identifier = credential.identifier

            cred = JSON.stringify meta, null, 2
            cred = hljs.highlight('json', cred).value

            listView.showCredential { credential, cred }

        when 'EditItem'
          @fetchCredentialData credential, (err, data) ->
            return  if showError err

            Tracker.track Tracker.USER_EDIT_CREDENTIALS

            data.meta  = helper.prepareCredentialMeta data.meta
            data.title = credential.title

            listView.showCredentialEditModal { provider, credential, data }


    @once 'FetchProcessSucceeded', (params) ->
      { items } = params
      if provider and items?.length is 0
        @showAddCredentialFormFor provider


  # Override parent method and show different confirm modal.
  removeItem: (item, options = {}) ->

    listView    = @getListView()
    credential  = item.getData()

    if credential.inuse
      new kd.NotificationView { title: 'This credential is currently in-use' }
      return

    credential.isBootstrapped (err, bootstrapped) =>

      kd.warn 'Bootstrap check failed:', { credential, err }  if err

      listView.askForConfirm { credential, bootstrapped }, ({ action, modal }) =>

        modal['button_Remove'].disable()

        switch action

          when 'Remove'
            @removeCredential item, (err) -> modal.destroy()

          when 'DestroyAll'
            @destroyResources credential, (err) =>
              if err
                modal.buttons.DestroyAll.hideLoader()
                modal.buttons.Remove.enable()
              else
                @removeCredential item, -> modal.destroy()


  removeCredential: (item, callback) -><commit_msg>credentials: destroy modal after button actions are done<commit_after>_                           = require 'lodash'
kd                          = require 'kd'
hljs                        = require 'highlight.js'

KDView                      = kd.View
KDButtonView                = kd.ButtonView
KDContextMenu               = kd.ContextMenu
KDCustomScrollView          = kd.CustomScrollView
KDNotificationView          = kd.NotificationView
KDFormViewWithFields        = kd.FormViewWithFields

KodingSwitch                = require 'app/commonviews/kodingswitch'
MemberAutoCompleteItemView  = require 'app/commonviews/memberautocompleteitemview'
MemberAutoCompletedItemView = require 'app/commonviews/memberautocompleteditemview'

KodingListController        = require 'app/kodinglist/kodinglistcontroller'

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


module.exports = class AccountCredentialListController extends KodingListController


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

    options.limit            or= 30
    options.noItemFoundText   ?= "You don't have any credentials"
    options.model             ?= remote.api.JCredential

    super options, data


  bindEvents: ->

    super

    { provider } = @getOptions()
    listView     = @getListView()

    listView.on 'ItemDeleted', @bound 'showNoItemWidget'

    listView.on 'ItemAction', ({ action, item, options }) =>

      credential    = item.getData()
      { provider }  = credential

      switch action

        when 'ShowItem'
          @fetchCredentialData credential, (err, data) ->
            return  if showError err

            { meta }        = data
            meta            = helper.prepareCredentialMeta meta
            meta.identifier = credential.identifier

            cred = JSON.stringify meta, null, 2
            cred = hljs.highlight('json', cred).value

            listView.showCredential { credential, cred }

        when 'EditItem'
          @fetchCredentialData credential, (err, data) ->
            return  if showError err

            Tracker.track Tracker.USER_EDIT_CREDENTIALS

            data.meta  = helper.prepareCredentialMeta data.meta
            data.title = credential.title

            listView.showCredentialEditModal { provider, credential, data }


    @once 'FetchProcessSucceeded', (params) ->
      { items } = params
      if provider and items?.length is 0
        @showAddCredentialFormFor provider


  # Override parent method and show different confirm modal.
  removeItem: (item, options = {}) ->

    listView    = @getListView()
    credential  = item.getData()

    if credential.inuse
      new kd.NotificationView { title: 'This credential is currently in-use' }
      return

    credential.isBootstrapped (err, bootstrapped) =>

      kd.warn 'Bootstrap check failed:', { credential, err }  if err

      listView.askForConfirm { credential, bootstrapped }, ({ action, modal }) =>

        switch action

          when 'Remove'
            @removeCredential item, -> modal.destroy()

          when 'DestroyAll'
            @destroyResources credential, (err) =>
              if err
                modal['button_DestroyAll'].hideLoader()
                modal['button_Remove'].hideLoader()
                modal.destroy()
              else
                @removeCredential item, -> modal.destroy()


  removeCredential: (item, callback) ->
{'fraction_non_alphanumeric': 0.08524728758630407, 'fraction_numerical': 0.0011272368606453432, 'mean_word_length': 2.056847545219638, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '4229292', 'n_tokens_mistral': 1918, 'n_tokens_neox': 1793, 'n_words': 438}