<commit_before>angular.module('app.controllers')
  .controller 'JournalIndexController', ($scope, $routeParams, $location, $timeout, db, $modal) ->
    $scope.currentDate = moment()
    if $routeParams.month && $routeParams.year
      $scope.currentDate.year(+$routeParams.year).month(+$routeParams.month - 1)

    db.memories().getByDynamicFilter({date: {month: $scope.currentDate.month(), year: $scope.currentDate.year()}, onlyParents: true}).then (memories) -> 
      db.events().getByDynamicFilter({date: {month: $scope.currentDate.month(), year: $scope.currentDate.year()}}).then (events) -> $scope.$apply ->
        memories = memories.map((item) -> {type: 'memories', modifiedAt: item.modifiedAt, data: item, date: item.date})
        events = events.map((item) -> {type: 'events', modifiedAt: item.modifiedAt, data: item, date: item.date})

        allItems = _.sortBy(memories.concat(events), 'date').reverse()
        $scope.dates = _(allItems).pluck('date').map((date) -> moment(date).format('L')).uniq().valueOf()
        $scope.items = _.groupBy(allItems, (item) -> moment(item.date).format('L'))

    $scope.nextMonth = ->
      $scope.currentDate.add(1, 'months')
      $location.path('/journal/' + $scope.currentDate.year().toString() + '/' + ($scope.currentDate.month()+1).toString())
    $scope.prevMonth = ->
      $scope.currentDate.add(-1, 'months')
      $location.path('/journal/' + $scope.currentDate.year().toString() + '/' + ($scope.currentDate.month()+1).toString())
    
    $scope.newMemoryUsingModal = ->      
      db.preloaded.item = null
      dialog = $modal({template: '/partials/memories/formDialog.html', show: true});

    $scope.showItemUsingModal = (type, item) ->
      db.preloaded.item = item
      if type == 'events'
        dialog = $modal({template: '/partials/events/showDialog.html', show: true});
      else
        $location.url("/memories/#{item.id}")
      if dialog
        dialog.$scope.$on 'editItem', (event) ->
          dialog.$scope.$destroy()
          $timeout -> 
            $scope.editItemUsingModal(type, item)

    $scope.editItemUsingModal = (type, item) ->
      db.preloaded.item = angular.copy(item)
      if type == 'memories'
        dialog = $modal({template: '/partials/memories/formDialog.html', show: true});
        dialog.$scope.$on 'itemEdited', (event, newItem) ->
          angular.extend(item, newItem)
      else if type == 'events'
        dialog = $modal({template: '/partials/events/formDialog.html', show: true});
        dialog.$scope.$on 'itemEdited', (event, newItem) ->
          angular.extend(item, newItem)


    return<commit_msg>fix edit in a modal<commit_after>angular.module('app.controllers')
  .controller 'JournalIndexController', ($scope, $routeParams, $location, $timeout, db, $modal) ->
    $scope.currentDate = moment()
    if $routeParams.month && $routeParams.year
      $scope.currentDate.year(+$routeParams.year).month(+$routeParams.month - 1)

    db.memories().getByDynamicFilter({date: {month: $scope.currentDate.month(), year: $scope.currentDate.year()}, onlyParents: true}).then (memories) -> 
      db.events().getByDynamicFilter({date: {month: $scope.currentDate.month(), year: $scope.currentDate.year()}}).then (events) -> $scope.$apply ->
        memories = memories.map((item) -> {type: 'memories', modifiedAt: item.modifiedAt, data: item, date: item.date})
        events = events.map((item) -> {type: 'events', modifiedAt: item.modifiedAt, data: item, date: item.date})

        allItems = _.sortBy(memories.concat(events), 'date').reverse()
        $scope.dates = _(allItems).pluck('date').map((date) -> moment(date).format('L')).uniq().valueOf()
        $scope.items = _.groupBy(allItems, (item) -> moment(item.date).format('L'))

    $scope.nextMonth = ->
      $scope.currentDate.add(1, 'months')
      $location.path('/journal/' + $scope.currentDate.year().toString() + '/' + ($scope.currentDate.month()+1).toString())
    $scope.prevMonth = ->
      $scope.currentDate.add(-1, 'months')
      $location.path('/journal/' + $scope.currentDate.year().toString() + '/' + ($scope.currentDate.month()+1).toString())
    
    $scope.newMemoryUsingModal = ->      
      db.preloaded.item = null
      dialog = $modal({template: '/partials/memories/formDialog.html', show: true});

    $scope.showItemUsingModal = (type, item) ->
      db.preloaded.item = item
      if type == 'events'
        dialog = $modal({template: '/partials/events/showDialog.html', show: true});
      else
        $location.url("/memories/#{item.id}")
      if dialog
        dialog.$scope.$on 'editItem', (event) ->
          dialog.$scope.$hide()
          dialog.$scope.$destroy()
          $timeout -> 
            $scope.editItemUsingModal(type, item)

    $scope.editItemUsingModal = (type, item) ->
      db.preloaded.item = angular.copy(item)
      if type == 'memories'
        dialog = $modal({template: '/partials/memories/formDialog.html', show: true});
        dialog.$scope.$on 'itemEdited', (event, newItem) ->
          angular.extend(item, newItem)
      else if type == 'events'
        dialog = $modal({template: '/partials/events/formDialog.html', show: true});
        dialog.$scope.$on 'itemEdited', (event, newItem) ->
          angular.extend(item, newItem)


    return
{'fraction_non_alphanumeric': 0.1874527588813303, 'fraction_numerical': 0.001889644746787604, 'mean_word_length': 3.2479935794542536, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, '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': '3810602', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1618, 'n_words': 284}