<commit_before>import Component from '@ember/component';
import { computed } from '@ember/object';
import { guidFor } from '@ember/object/internals';
import { inject } from '@ember/service';
import moment from 'moment';
import { task } from 'ember-concurrency';
import layout from '../templates/components/power-calendar';

export default Component.extend({
  layout,
  classNames: ['ember-power-calendar'],
  powerCalendarService: inject('power-calendar'),
  momentService: inject('moment'),
  navComponent: 'power-calendar/nav',
  daysComponent: 'power-calendar/days',
  center: null,

  // Lifecycle chooks
  init() {
    this._super(...arguments);
    let changeCenter = (newCenter, calendar, e) => {
      return this.get('changeCenterTask').perform(moment(newCenter), calendar, e);
    };
    this.publicActions = {
      changeCenter,
      moveCenter: (step, unit, calendar, e) => {
        let newCenter = moment(this.get('currentCenter')).add(step, unit);
        return changeCenter(newCenter, calendar, e);
      },
      select: (...args) => this.send('select', ...args)
    };
    this.registerCalendar();
    let onInit = this.get('onInit');
    if (onInit) {
      onInit(this.get('publicAPI'));
    }
  },

  willDestroy() {
    this._super(...arguments);
    this.unregisterCalendar();
  },

  // CPs
  currentCenter: computed('center', function() {
    let center = this.get('center');
    if (center) {
      return moment(center);
    }
    return moment(this.get('selected') || this.get('powerCalendarService').getDate());
  }),

  publicAPI: computed('_publicAPI', function() {
    return this.get('_publicAPI');
  }),

  _publicAPI: computed('selected', 'currentCenter', 'locale', 'momentService.locale', 'changeCenterTask.isRunning', function() {
    return {
      uniqueId: guidFor(this),
      selected: this.get('selected'),
      loading: this.get('changeCenterTask.isRunning'),
      center: this.get('currentCenter'),
      locale: this.get('locale') || this.get('momentService.locale') || moment.locale(),
      actions: this.get('publicActions')
    };
  }),

  // Actions
  actions: {
    select(day, calendar, e) {
      let action = this.get('onSelect');
      if (action) {
        action(day, calendar, e);
      }
    }
  },

  // Tasks
  changeCenterTask: task(function* (newCenterMoment, calendar, e) {
    yield this.get('onCenterChange')(
      { date: newCenterMoment.toDate(), moment: newCenterMoment },
      calendar,
      e
    );
  }),

  // Methods
  registerCalendar() {
    if (window) {
      window.__powerCalendars = window.__powerCalendars || {}; // TODO: weakmap??
      window.__powerCalendars[guidFor(this)] = this;
    }
  },
<commit_msg>Assert that `onCenterChange` is present when attempting to move the center of a calendar. (#147)<commit_after>import Component from '@ember/component';
import { computed } from '@ember/object';
import { guidFor } from '@ember/object/internals';
import { inject } from '@ember/service';
import moment from 'moment';
import { task } from 'ember-concurrency';
import layout from '../templates/components/power-calendar';
import { assert } from '@ember/debug';

export default Component.extend({
  layout,
  classNames: ['ember-power-calendar'],
  powerCalendarService: inject('power-calendar'),
  momentService: inject('moment'),
  navComponent: 'power-calendar/nav',
  daysComponent: 'power-calendar/days',
  center: null,

  // Lifecycle chooks
  init() {
    this._super(...arguments);
    let changeCenter = (newCenter, calendar, e) => {
      return this.get('changeCenterTask').perform(moment(newCenter), calendar, e);
    };
    this.publicActions = {
      changeCenter,
      moveCenter: (step, unit, calendar, e) => {
        let newCenter = moment(this.get('currentCenter')).add(step, unit);
        return changeCenter(newCenter, calendar, e);
      },
      select: (...args) => this.send('select', ...args)
    };
    this.registerCalendar();
    let onInit = this.get('onInit');
    if (onInit) {
      onInit(this.get('publicAPI'));
    }
  },

  willDestroy() {
    this._super(...arguments);
    this.unregisterCalendar();
  },

  // CPs
  currentCenter: computed('center', function() {
    let center = this.get('center');
    if (center) {
      return moment(center);
    }
    return moment(this.get('selected') || this.get('powerCalendarService').getDate());
  }),

  publicAPI: computed('_publicAPI', function() {
    return this.get('_publicAPI');
  }),

  _publicAPI: computed('selected', 'currentCenter', 'locale', 'momentService.locale', 'changeCenterTask.isRunning', function() {
    return {
      uniqueId: guidFor(this),
      selected: this.get('selected'),
      loading: this.get('changeCenterTask.isRunning'),
      center: this.get('currentCenter'),
      locale: this.get('locale') || this.get('momentService.locale') || moment.locale(),
      actions: this.get('publicActions')
    };
  }),

  // Actions
  actions: {
    select(day, calendar, e) {
      let action = this.get('onSelect');
      if (action) {
        action(day, calendar, e);
      }
    }
  },

  // Tasks
  changeCenterTask: task(function* (newCenterMoment, calendar, e) {
    let action = this.get('onCenterChange');
    assert('You attempted to move the center of a calendar that doesn\'t receive an `onCenterChange` action.', typeof action === 'function');
    let value = { date: newCenterMoment.toDate(), moment: newCenterMoment };
    yield action(value, calendar, e);
  }),

  // Methods
  registerCalendar() {
    if (window) {
      window.__powerCalendars = window.__powerCalendars || {}; // TODO: weakmap??
      window.__powerCalendars[guidFor(this)] = this;
    }
  },

{'fraction_non_alphanumeric': 0.1629746835443038, 'fraction_numerical': 0.0005274261603375527, 'mean_word_length': 3.44453125, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2471433', 'n_tokens_mistral': 1693, 'n_tokens_neox': 1670, 'n_words': 369}