<commit_before>#
# Component generated by Impac! Widget Generator!
# TODO:
# - colour shades by account sub-type (don't spend too much time on this one / not that important)
# - check if a different chart can be used for better time period selection
#
module = angular.module('impac.components.widgets.accounts-cash-balance', [])
module.controller('WidgetAccountsCashBalanceCtrl', ($scope, $q, $timeout, $filter, ImpacTheming) ->

  w = $scope.widget

  # Define settings
  # --------------------------------------
  $scope.orgDeferred = $q.defer()
  $scope.timePeriodDeferred = $q.defer()

  settingsPromises = [
    $scope.orgDeferred.promise,
    $scope.timePeriodDeferred.promise
  ]

  # Widget specific methods
  # --------------------------------------
  w.initContext = ->
    if $scope.isDataFound = w.content?

      $scope.groupedTable = w.content.grouped_table

      chartColors = ImpacTheming.get().chartColors.array

      # Set account colors by group sub-type
      setSeriesColors(chartColors)

      # Custom settings for the 'totals' serie.
      totals_serie = _.find(w.content.chart.series, (serie)-> serie.type == 'area')
      # TODO: add configurable in theming svc
      totals_serie.color = '#7badfc'

      # Wait for the next digest cycle to ensure the chart parent (.data-container) is shown.
      $timeout(->
        $scope.chart = buildChart(w.content.chart)
      )

  $scope.legendItemOnClick = (account)->
    serie = $scope.chart && getSerieByAccount($scope.chart.series, account)
    return unless serie
    visibility = if serie.visible then false else true
    serie.setVisible(visibility)

  $scope.getLegendItemIcon = (account)->
    serie = $scope.chart && getSerieByAccount($scope.chart.series, account)
    return 'fa-check-square-o' unless serie
    if serie.visible then 'fa-check-square-o' else 'fa-square-o'

  $scope.getLegendItemColor = (account)->
    serie = $scope.chart && getSerieByAccount($scope.chart.series, account)
    return '#000' unless serie
    serie.color

  # Private
  # ----

  getPeriod = ->
    w.metadata? && w.metadata.hist_parameters? && w.metadata.hist_parameters.period

  getSerieByAccount = (series, account)->
    _.find(series, (serie)-> (serie.uid || serie.options && serie.options.uid) == account.uid)

  getTodayMarker = ->
    projection_date = _.find(w.content.chart.labels, (label)-> moment(label) >= moment().startOf('day'))
    _.indexOf(w.content.chart.labels, projection_date)

  # TODO: Test with more accounts & add color shade variations for group accounts.
  setSeriesColors = (chartColors) ->
    counter = 0
    for group in $scope.groupedTable.groups
      counter = 0 if counter >= (chartColors.length - 1)
      groupColor = chartColors[counter++]
      for account in group
        serie = getSerieByAccount(w.content.chart.series, account)
        serie.color = groupColor

  buildChart = (data, onRender)->
    options =
      chart:
        type: 'line'
        zoomType: 'x'
        spacingTop:20
      title: null
      credits:
        enabled: false
      legend:
        enabled: false
      scrollbar:
        enabled: true
      plotOptions:
        series:
          fillOpacity: '0.3'
      xAxis:
        startOnTick: false
        minPadding: 0
        tickInterval: 1
        labels:
          style: textOverflow: 'none'
          formatter: ()->
            $filter('mnoDate')(data.labels[this.value], getPeriod())
        plotLines: [{
          # TODO: ImpacTheming config for today "plot" color.
          color: '#0055ff'
          value: getTodayMarker()
          width: 1.5
          label:
            text: 'Today'
            verticalAlign: 'top'
            textAlign: 'center'
            rotation: 0
            y: -5
        }]
      yAxis:
        startOnTick: true
        minPadding: 0
        title: null
        labels:
          formatter: ()->
            $filter('mnoCurrency')(this.value, w.metadata.currency, false, 0)
      series: data.series

    Highcharts.chart('cashBalanceChart', options, onRender)


  # Widget is ready: can trigger the "wait for settings to be ready"
  # --------------------------------------
  $scope.widgetDeferred.resolve(settingsPromises)
)
module.directive('widgetAccountsCashBalance', ->
  return {
    restrict: 'A',
    controller: 'WidgetAccountsCashBalanceCtrl'
  }
)
<commit_msg>add default period<commit_after>#
# Component generated by Impac! Widget Generator!
# TODO:
# - colour shades by account sub-type (don't spend too much time on this one / not that important)
# - check if a different chart can be used for better time period selection
#
module = angular.module('impac.components.widgets.accounts-cash-balance', [])
module.controller('WidgetAccountsCashBalanceCtrl', ($scope, $q, $timeout, $filter, ImpacTheming) ->

  w = $scope.widget

  # Define settings
  # --------------------------------------
  $scope.orgDeferred = $q.defer()
  $scope.timePeriodDeferred = $q.defer()

  settingsPromises = [
    $scope.orgDeferred.promise,
    $scope.timePeriodDeferred.promise
  ]

  # Widget specific methods
  # --------------------------------------
  w.initContext = ->
    if $scope.isDataFound = w.content?

      $scope.groupedTable = w.content.grouped_table

      chartColors = ImpacTheming.get().chartColors.array

      # Set account colors by group sub-type
      setSeriesColors(chartColors)

      # Custom settings for the 'totals' serie.
      totals_serie = _.find(w.content.chart.series, (serie)-> serie.type == 'area')
      # TODO: add configurable in theming svc
      totals_serie.color = '#7badfc'

      # Wait for the next digest cycle to ensure the chart parent (.data-container) is shown.
      $timeout(->
        $scope.chart = buildChart(w.content.chart)
      )

  $scope.legendItemOnClick = (account)->
    serie = $scope.chart && getSerieByAccount($scope.chart.series, account)
    return unless serie
    visibility = if serie.visible then false else true
    serie.setVisible(visibility)

  $scope.getLegendItemIcon = (account)->
    serie = $scope.chart && getSerieByAccount($scope.chart.series, account)
    return 'fa-check-square-o' unless serie
    if serie.visible then 'fa-check-square-o' else 'fa-square-o'

  $scope.getLegendItemColor = (account)->
    serie = $scope.chart && getSerieByAccount($scope.chart.series, account)
    return '#000' unless serie
    serie.color

  # Private
  # ----

  getPeriod = ->
    w.metadata? && w.metadata.hist_parameters? && w.metadata.hist_parameters.period || 'MONTHLY'

  getSerieByAccount = (series, account)->
    _.find(series, (serie)-> (serie.uid || serie.options && serie.options.uid) == account.uid)

  getTodayMarker = ->
    projection_date = _.find(w.content.chart.labels, (label)-> moment(label) >= moment().startOf('day'))
    _.indexOf(w.content.chart.labels, projection_date)

  # TODO: Test with more accounts & add color shade variations for group accounts.
  setSeriesColors = (chartColors) ->
    counter = 0
    for group in $scope.groupedTable.groups
      counter = 0 if counter >= (chartColors.length - 1)
      groupColor = chartColors[counter++]
      for account in group
        serie = getSerieByAccount(w.content.chart.series, account)
        serie.color = groupColor

  buildChart = (data, onRender)->
    options =
      chart:
        type: 'line'
        zoomType: 'x'
        spacingTop:20
      title: null
      credits:
        enabled: false
      legend:
        enabled: false
      scrollbar:
        enabled: true
      plotOptions:
        series:
          fillOpacity: '0.3'
      xAxis:
        startOnTick: false
        minPadding: 0
        tickInterval: 1
        labels:
          style: textOverflow: 'none'
          formatter: ()->
            $filter('mnoDate')(data.labels[this.value], getPeriod())
        plotLines: [{
          # TODO: ImpacTheming config for today "plot" color.
          color: '#0055ff'
          value: getTodayMarker()
          width: 1.5
          label:
            text: 'Today'
            verticalAlign: 'top'
            textAlign: 'center'
            rotation: 0
            y: -5
        }]
      yAxis:
        startOnTick: true
        minPadding: 0
        title: null
        labels:
          formatter: ()->
            $filter('mnoCurrency')(this.value, w.metadata.currency, false, 0)
      series: data.series

    Highcharts.chart('cashBalanceChart', options, onRender)


  # Widget is ready: can trigger the "wait for settings to be ready"
  # --------------------------------------
  $scope.widgetDeferred.resolve(settingsPromises)
)
module.directive('widgetAccountsCashBalance', ->
  return {
    restrict: 'A',
    controller: 'WidgetAccountsCashBalanceCtrl'
  }
)

{'fraction_non_alphanumeric': 0.1391941391941392, 'fraction_numerical': 0.005265567765567766, 'mean_word_length': 2.393009708737864, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, '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': '5015953', 'n_tokens_mistral': 2599, 'n_tokens_neox': 2572, 'n_words': 631}