<commit_before>'use strict';

var _        = require('lodash');
var d3       = require('d3');
var moment   = require('moment');

var api      = require('../../data/api');
var Dropdown = require('../../component/dropdown');

module.exports = {
	template: require('./template.html'),

	data: function () {
		return {
			regions: [],
			indicators: [],
			pagination: {
				limit: 20,
				offset: 0,
				total_count: 0
			},
			table: {
				loading: false,
				columns: ['region', 'campaign'],
				rows: []
			},
			campaign: {
				start: '',
				end: ''
			}
		};
	},

	attached: function () {
		var self = this;

		this._regions = new Dropdown({
			el     : '#regions',
			source : api.regions,
			mapping: {
				'parent_region_id': 'parent',
				'name'            : 'title',
				'id'              : 'value'
			}
		});

		this._regions.$on('dropdown-value-changed', function (items) {
			self.regions = items;
		});

		this._indicators = new Dropdown({
			el     : '#indicators',
			source : api.indicators,
			mapping: {
				'short_name': 'title',
				'id'        : 'value'
			}
		});

		this._indicators.$on('dropdown-value-changed', function (items) {
			self.indicators = items;
		});

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

	computed: {

		hasSelection: function () {
			return this.regions.length > 0 && this.indicators.length > 0;
		}

	},

	methods: {

		refresh: function (pagination) {
			if (!this.hasSelection) {
				return;
			}

			var self = this;

			var regionNames = _.indexBy(this.regions, 'value');
			var regions     = _.map(this.regions, 'value');
			var options     = { indicator__in : [] };
			var columns     = [{
					prop: 'region',
					display: 'Region',
					format: function (v) {
						return regionNames[v].title;
					}
				}, {
					prop: 'campaign',
					display: 'Campaign'
				}];

			if (regions.length > 0) {
				options.region__in = regions;
			}

			if (this.campaign.start) {
				options.campaign_start = this.campaign.start;
			}

			if (this.campaign.end) {
				options.campaign_end = this.campaign.end;
			}

			this.indicators.forEach(function (indicator) {
				options.indicator__in.push(indicator.value);
				columns.push({
					prop   : indicator.value,
					display: indicator.title,
					classes: 'numeric',
					format : function (v) {
						return (isNaN(v) || _.isNull(v)) ? '' : d3.format('n')(v);
					}
				});
			});

			_.defaults(options, this.pagination);

			this.table.loading = true;
			this.table.columns = columns;
			this.table.rows    = [];

			api.datapoints(options).done(function (data) {
				self.table.loading = false;

				self.pagination.limit   = Number(data.meta.limit);
				self.pagination.offset  = Number(data.meta.offset);
				self.pagination.total_count = Number(data.meta.total_count);

				if (!data.objects || data.objects.length < 1) {
					return;<commit_msg>Don't assign NaN to pagination controls<commit_after>'use strict';

var _        = require('lodash');
var d3       = require('d3');
var moment   = require('moment');

var api      = require('../../data/api');
var Dropdown = require('../../component/dropdown');

module.exports = {
	template: require('./template.html'),

	data: function () {
		return {
			regions: [],
			indicators: [],
			pagination: {
				limit: 20,
				offset: 0,
				total_count: 0
			},
			table: {
				loading: false,
				columns: ['region', 'campaign'],
				rows: []
			},
			campaign: {
				start: '',
				end: ''
			}
		};
	},

	attached: function () {
		var self = this;

		this._regions = new Dropdown({
			el     : '#regions',
			source : api.regions,
			mapping: {
				'parent_region_id': 'parent',
				'name'            : 'title',
				'id'              : 'value'
			}
		});

		this._regions.$on('dropdown-value-changed', function (items) {
			self.regions = items;
		});

		this._indicators = new Dropdown({
			el     : '#indicators',
			source : api.indicators,
			mapping: {
				'short_name': 'title',
				'id'        : 'value'
			}
		});

		this._indicators.$on('dropdown-value-changed', function (items) {
			self.indicators = items;
		});

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

	computed: {

		hasSelection: function () {
			return this.regions.length > 0 && this.indicators.length > 0;
		}

	},

	methods: {

		refresh: function (pagination) {
			if (!this.hasSelection) {
				return;
			}

			var self = this;

			var regionNames = _.indexBy(this.regions, 'value');
			var regions     = _.map(this.regions, 'value');
			var options     = { indicator__in : [] };
			var columns     = [{
					prop: 'region',
					display: 'Region',
					format: function (v) {
						return regionNames[v].title;
					}
				}, {
					prop: 'campaign',
					display: 'Campaign'
				}];

			if (regions.length > 0) {
				options.region__in = regions;
			}

			if (this.campaign.start) {
				options.campaign_start = this.campaign.start;
			}

			if (this.campaign.end) {
				options.campaign_end = this.campaign.end;
			}

			this.indicators.forEach(function (indicator) {
				options.indicator__in.push(indicator.value);
				columns.push({
					prop   : indicator.value,
					display: indicator.title,
					classes: 'numeric',
					format : function (v) {
						return (isNaN(v) || _.isNull(v)) ? '' : d3.format('n')(v);
					}
				});
			});

			_.defaults(options, this.pagination);

			this.table.loading = true;
			this.table.columns = columns;
			this.table.rows    = [];

			api.datapoints(options).done(function (data) {
				self.table.loading = false;

				_.assign(self.pagination, _.pluck(data.meta, 'limit', 'offset', 'total_count'));

				if (!data.objects || data.objects.length < 1) {
					return;
{'fraction_non_alphanumeric': 0.16996670755212898, 'fraction_numerical': 0.003854915016646224, 'mean_word_length': 2.4699088145896657, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '4526820', 'n_tokens_mistral': 2462, 'n_tokens_neox': 1894, 'n_words': 367}