<commit_before>/*
 * Set of function to facilitate the display and edition of metadata in
 * entity lists.
 */
import { mapGetters } from 'vuex'

export const descriptorMixin = {

  created () {
  },

  mounted () {
  },

  beforeDestroy () {
  },

  computed: {
    ...mapGetters([
      'selectedAssets',
      'selectedShots',
      'selectedEdits'
    ]),
    descriptorLength () {
      if (this.shotMetadataDescriptors.length !== undefined) {
        return this.shotMetadataDescriptors.length
      }
      if (this.assetMetadataDescriptors.length !== undefined) {
        return this.assetMetadataDescriptors.length
      }
      if (this.editMetadataDescriptors.length !== undefined) {
        return this.editMetadataDescriptors.length
      }
      return 0
    }
  },

  methods: {
    onAddMetadataClicked () {
      this.$emit('add-metadata')
    },

    emitMetadataChanged (entry, descriptor, value) {
      this.$emit('metadata-changed', {
        entry, descriptor, value
      })
    },

    onMetadataFieldChanged (entry, descriptor, event) {
      if (this.selectedShots.has(entry.id)) {
        // if the line is selected, also modify the cells of the other selected lines
        this.selectedShots.forEach((shot, _) => {
          this.emitMetadataChanged(shot, descriptor, event.target.value)
        })
      } else if (this.selectedAssets.has(entry.id)) {
        // if the line is selected, also modify the cells of the other selected lines
        this.selectedAssets.forEach((asset, _) => {
          this.emitMetadataChanged(asset, descriptor, event.target.value)
        })
      } else if (this.selectedEdits.has(entry.id)) {
        // if the line is selected, also modify the cells of the other selected lines
        this.selectedEdits.forEach((edit, _) => {
          this.emitMetadataChanged(edit, descriptor, event.target.value)
        })
      } else {
        this.emitMetadataChanged(entry, descriptor, event.target.value)
      }
    },

    onMetadataChecklistChanged (entry, descriptor, option, event) {
      var values = this.getMetadataChecklistValues(descriptor, entry)
      values[option] = event.target.checked
      event.target.value = JSON.stringify(values)
      this.onMetadataFieldChanged(entry, descriptor, event)
    },

    onSortByMetadataClicked () {
      const columnId = this.lastMetadaDataHeaderMenuDisplayed
      const column =
        this.currentProduction.descriptors.find(d => d.id === columnId)
      this.$emit('change-sort', {
        type: 'metadata',
        column: column.field_name,
        name: column.name
      })
      this.showMetadataHeaderMenu()
    },

    onEditMetadataClicked () {
      this.$emit('edit-metadata', this.lastMetadaDataHeaderMenuDisplayed)
      this.showMetadataHeaderMenu()
    },

    onDeleteMetadataClicked () {
      this.$emit('delete-metadata', this.lastMetadaDataHeaderMenuDisplayed)
      this.showMetadataHeaderMenu()
    },

    showMetadataHeaderMenu (columnId, event) {
      const headerMenuEl = this.$refs.headerMetadataMenu.$el
      if (headerMenuEl.className === 'header-menu') {
        headerMenuEl.className = 'header-menu hidden'
      } else {
        headerMenuEl.className = 'header-menu'
        const headerElement = event.srcElement.parentNode.parentNode
        const headerBox = headerElement.getBoundingClientRect()
        const left = headerBox.left - 3
        const top = headerBox.bottom + 11
        const width = Math.max(100, headerBox.width - 1)
        headerMenuEl.style.left = left + 'px'
        headerMenuEl.style.top = top + 'px'
        headerMenuEl.style.width = width + 'px'
      }
      this.lastMetadaDataHeaderMenuDisplayed = columnId
    },

    getDescriptorChoicesOptions (descriptor) {
      const values = descriptor.choices.map(c => ({ label: c, value: c }))
      return [{ label: '', value: '' }, ...values]
    },

    getMetadataFieldValue (descriptor, entity) {
      return entity.data ? entity.data[descriptor.field_name] || '' : ''
    },

    getDescriptorChecklistValues (descriptor) {
      const values = descriptor.choices.reduce(
        function (result, c) {
          if (c.startsWith('[x] ')) {
            result.push({ text: c.slice(4), checked: true })
          } else if (c.startsWith('[ ] ')) {
            result.push({ text: c.slice(4), checked: false })
          }
          return result
        },
        []
      )
      return values.length === descriptor.choices.length ? values : []
    },

    getMetadataChecklistValues (descriptor, entity) {
      var values = {}
      try {
        values = JSON.parse(this.getMetadataFieldValue(descriptor, entity))
      } catch {
        values = {}
      }
      this.getDescriptorChecklistValues(descriptor).forEach(
        function (option) {<commit_msg>[lists] Fix choice descriptors edge case<commit_after>/*
 * Set of function to facilitate the display and edition of metadata in
 * entity lists.
 */
import { mapGetters } from 'vuex'

export const descriptorMixin = {

  created () {
  },

  mounted () {
  },

  beforeDestroy () {
  },

  computed: {
    ...mapGetters([
      'selectedAssets',
      'selectedShots',
      'selectedEdits'
    ]),
    descriptorLength () {
      if (this.shotMetadataDescriptors.length !== undefined) {
        return this.shotMetadataDescriptors.length
      }
      if (this.assetMetadataDescriptors.length !== undefined) {
        return this.assetMetadataDescriptors.length
      }
      if (this.editMetadataDescriptors.length !== undefined) {
        return this.editMetadataDescriptors.length
      }
      return 0
    }
  },

  methods: {
    onAddMetadataClicked () {
      this.$emit('add-metadata')
    },

    emitMetadataChanged (entry, descriptor, value) {
      this.$emit('metadata-changed', {
        entry, descriptor, value
      })
    },

    onMetadataFieldChanged (entry, descriptor, event) {
      if (this.selectedShots.has(entry.id)) {
        // if the line is selected, also modify the cells of the other selected lines
        this.selectedShots.forEach((shot, _) => {
          this.emitMetadataChanged(shot, descriptor, event.target.value)
        })
      } else if (this.selectedAssets.has(entry.id)) {
        // if the line is selected, also modify the cells of the other selected lines
        this.selectedAssets.forEach((asset, _) => {
          this.emitMetadataChanged(asset, descriptor, event.target.value)
        })
      } else if (this.selectedEdits.has(entry.id)) {
        // if the line is selected, also modify the cells of the other selected lines
        this.selectedEdits.forEach((edit, _) => {
          this.emitMetadataChanged(edit, descriptor, event.target.value)
        })
      } else {
        this.emitMetadataChanged(entry, descriptor, event.target.value)
      }
    },

    onMetadataChecklistChanged (entry, descriptor, option, event) {
      var values = this.getMetadataChecklistValues(descriptor, entry)
      values[option] = event.target.checked
      event.target.value = JSON.stringify(values)
      this.onMetadataFieldChanged(entry, descriptor, event)
    },

    onSortByMetadataClicked () {
      const columnId = this.lastMetadaDataHeaderMenuDisplayed
      const column =
        this.currentProduction.descriptors.find(d => d.id === columnId)
      this.$emit('change-sort', {
        type: 'metadata',
        column: column.field_name,
        name: column.name
      })
      this.showMetadataHeaderMenu()
    },

    onEditMetadataClicked () {
      this.$emit('edit-metadata', this.lastMetadaDataHeaderMenuDisplayed)
      this.showMetadataHeaderMenu()
    },

    onDeleteMetadataClicked () {
      this.$emit('delete-metadata', this.lastMetadaDataHeaderMenuDisplayed)
      this.showMetadataHeaderMenu()
    },

    showMetadataHeaderMenu (columnId, event) {
      const headerMenuEl = this.$refs.headerMetadataMenu.$el
      if (headerMenuEl.className === 'header-menu') {
        headerMenuEl.className = 'header-menu hidden'
      } else {
        headerMenuEl.className = 'header-menu'
        const headerElement = event.srcElement.parentNode.parentNode
        const headerBox = headerElement.getBoundingClientRect()
        const left = headerBox.left - 3
        const top = headerBox.bottom + 11
        const width = Math.max(100, headerBox.width - 1)
        headerMenuEl.style.left = left + 'px'
        headerMenuEl.style.top = top + 'px'
        headerMenuEl.style.width = width + 'px'
      }
      this.lastMetadaDataHeaderMenuDisplayed = columnId
    },

    getDescriptorChoicesOptions (descriptor) {
      const values = descriptor.choices.map(c => ({ label: c, value: c }))
      return [{ label: '', value: '' }, ...values]
    },

    getMetadataFieldValue (descriptor, entity) {
      return entity.data ? entity.data[descriptor.field_name] || '' : ''
    },

    getDescriptorChecklistValues (descriptor) {
      const values = descriptor.choices.reduce((result, choice) => {
        if (choice && choice.startsWith('[x] ')) {
          result.push({ text: choice.slice(4), checked: true })
        } else if (choice && choice.startsWith('[ ] ')) {
          result.push({ text: choice.slice(4), checked: false })
        }
        return result
      },
      [])
      return values.length === descriptor.choices.length ? values : []
    },

    getMetadataChecklistValues (descriptor, entity) {
      var values = {}
      try {
        values = JSON.parse(this.getMetadataFieldValue(descriptor, entity))
      } catch {
        values = {}
      }
      this.getDescriptorChecklistValues(descriptor).forEach(
        function (option) {
{'fraction_non_alphanumeric': 0.11742738589211618, 'fraction_numerical': 0.002074688796680498, 'mean_word_length': 2.5840148698884757, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '6422001', 'n_tokens_mistral': 2702, 'n_tokens_neox': 2668, 'n_words': 559}