<commit_before>import _ from 'lodash'
import d3 from 'd3'

import api from '../../data/api'
import Dropdown from '../../component/dropdown'
import flattenChildren from '../../data/transform/flattenChildren'
import treeify from '../../data/transform/treeify'

export default {
  template: require('./template.html'),

  data: function () {
    return {
      indicator_set_id: 2,
      indicator_sets: require('./structure/indicator_sets'),
      noEditableSets: false,
      loaded: false,
      includeSublocations: false,
      locations: [],
      indicators: [],
      pagination: {
        total_count: 0
      },
      table: {
        loading: false,
        columns: ['location', 'campaign'],
        rows: []
      },

      campaigns: [],
      campaign_id: null,
      campaign_office_id: null
    }
  },

  created: function () {
    // processing on indicator sets data
    _.forEach(this.indicator_sets, function (d) {
      // copy values for v-select:
      d.value = d.id
      d.text = d.title
    })
  },

  ready: function () {
    this.$watch('campaign_id', this.refreshlocationsDropdown)

    this.load()
  },

  attached: function () {
    var self = this

    // setup locations dropdown
    self._locations = new Dropdown({
      el: '#locations'
    })
    self._locations.$on('dropdown-value-changed', function (items) {
      self.locations = _.values(items)
    })

    this.$on('page-changed', function (data) {
      this.refresh(data)
    })
  },

  computed: {
    hasSelection: function () {
      return this.locations.length > 0
    }
  },

  methods: {
    load: function () {
      var self = this

      var makeMap = function (data) {
        if (data.objects) {
          return _.indexBy(data.objects, 'id')
        } else {
          return null
        }
      }

      var connectChildren = function (map, parent_id_key, children_key) {
        _.forIn(map, function (d) {
          // obj has parent_id?
          if (d[parent_id_key] !== undefined && d[parent_id_key] !== null) {
            // parent found?
            if (map[d[parent_id_key]]) {
              var parent = map[d[parent_id_key]]
              if (!parent[children_key]) { parent[children_key] = [] }
              parent[children_key].push(d)
            }
          }
        })
        return map
      }

      Promise.all([

        // locations data
        api.locations(null, null, {'cache-control': 'no-cache'})
          .then(makeMap)
          .then(function (map) {
            // create array of children in each parent
            return connectChildren(map, 'parent_location_id', 'children')
          }),

        // indicators data
        api.indicators({ read_write: 'w' }, null, {'cache-control': 'no-cache'}).then(makeMap),

        // campaigns data
        api.campaign(null, null, {'cache-control': 'no-cache'}).then(function (data) {
          if (!data.objects) { return null }
          return data.objects
            .sort(function (a, b) {
              if (a.office === b.office) {
                return a.start_date > b.start_date ? -1 : 1
              }
              return a.office - b.office
            })
            .map(function (d) {
              d.text = d.slug
              d.value = d.id
              return d
            })
        })

      ]).then(function (allData) {
        self.$data.locationData = allData[0]
        self.$data.indicators = allData[1]
        self.$data.campaigns = allData[2]

        // set campaign id to first option
        self.$data.campaign_id = self.$data.campaigns[0].value
        self.$data.loaded = true

        self.filterIndicatorSets()
        self.refreshlocationsDropdown()
      })
    },

    refreshlocationsDropdown: function () {
      var self = this

      var campaign = _.find(self.$data.campaigns, function (d) { return d.id === parseInt(self.$data.campaign_id, 10) })

      var items = _.chain(self.$data.locationData)
              .filter(function (d) {<commit_msg>#743 fix the campaign can not load data on entry data page.<commit_after>import _ from 'lodash'
import d3 from 'd3'

import api from '../../data/api'
import Dropdown from '../../component/dropdown'
import flattenChildren from '../../data/transform/flattenChildren'
import treeify from '../../data/transform/treeify'

export default {
  template: require('./template.html'),

  data: function () {
    return {
      indicator_set_id: 2,
      indicator_sets: require('./structure/indicator_sets'),
      noEditableSets: false,
      loaded: false,
      includeSublocations: false,
      locations: [],
      indicators: [],
      pagination: {
        total_count: 0
      },
      table: {
        loading: false,
        columns: ['location', 'campaign'],
        rows: []
      },

      campaigns: [],
      campaign_id: null,
      campaign_office_id: null
    }
  },

  created: function () {
    // processing on indicator sets data
    _.forEach(this.indicator_sets, function (d) {
      // copy values for v-select:
      d.value = d.id
      d.text = d.title
    })
  },

  ready: function () {
    this.$watch('campaign_id', this.refreshlocationsDropdown)

    this.load()
  },

  attached: function () {
    var self = this

    // setup locations dropdown
    self._locations = new Dropdown({
      el: '#locations'
    })
    self._locations.$on('dropdown-value-changed', function (items) {
      self.locations = _.values(items)
    })

    this.$on('page-changed', function (data) {
      this.refresh(data)
    })
  },

  computed: {
    hasSelection: function () {
      return this.locations.length > 0
    }
  },

  methods: {
    load: function () {
      var self = this

      var makeMap = function (data) {
        if (data.objects) {
          return _.indexBy(data.objects, 'id')
        } else {
          return null
        }
      }

      var connectChildren = function (map, parent_id_key, children_key) {
        _.forIn(map, function (d) {
          // obj has parent_id?
          if (d[parent_id_key] !== undefined && d[parent_id_key] !== null) {
            // parent found?
            if (map[d[parent_id_key]]) {
              var parent = map[d[parent_id_key]]
              if (!parent[children_key]) { parent[children_key] = [] }
              parent[children_key].push(d)
            }
          }
        })
        return map
      }

      Promise.all([

        // locations data
        api.locations(null, null, {'cache-control': 'no-cache'})
          .then(makeMap)
          .then(function (map) {
            // create array of children in each parent
            return connectChildren(map, 'parent_location_id', 'children')
          }),

        // indicators data
        api.indicators({ read_write: 'w' }, null, {'cache-control': 'no-cache'}).then(makeMap),

        // campaigns data
        api.campaign(null, null, {'cache-control': 'no-cache'}).then(function (data) {
          if (!data.objects) { return null }
          return data.objects
            .sort(function (a, b) {
              if (a.office === b.office) {
                return a.start_date > b.start_date ? -1 : 1
              }
              return a.office - b.office
            })
            .map(function (d) {
              d.text = d.name
              d.value = d.id
              return d
            })
        })

      ]).then(function (allData) {
        self.$data.locationData = allData[0]
        self.$data.indicators = allData[1]
        self.$data.campaigns = allData[2]

        // set campaign id to first option
        self.$data.campaign_id = self.$data.campaigns[0].value
        self.$data.loaded = true

        self.filterIndicatorSets()
        self.refreshlocationsDropdown()
      })
    },

    refreshlocationsDropdown: function () {
      var self = this

      var campaign = _.find(self.$data.campaigns, function (d) { return d.id === parseInt(self.$data.campaign_id, 10) })

      var items = _.chain(self.$data.locationData)
              .filter(function (d) {
{'fraction_non_alphanumeric': 0.13584247258225324, 'fraction_numerical': 0.003614157527417747, 'mean_word_length': 1.8568885724457103, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '230908', 'n_tokens_mistral': 2413, 'n_tokens_neox': 2328, 'n_words': 529}