<commit_before>/*
 * Copyright 2014 <NAME> AS
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict';

angular.module('momusApp.controllers')
    .controller('PublicationCtrl', function ($scope, $http, PublicationService, $templateRequest, MessageModal) {

        var today = new Date();

        $scope.viewYear = today.getFullYear();
        $scope.publications = [];
        $scope.yearsInDropdown = [];

        $scope.currentPage = 1;
        $scope.pubsPerPage = 10;
        $scope.numPubs = 0;

        $scope.editing = {};

        $scope.dateOptions = {// Needed for the date picker, always start weeks on a monday
            'starting-day': 1
        };


        PublicationService.getAll().success(function (data) {
            $scope.publications = data;
        });

        $scope.yearFilter = function (publication) {
            if($scope.viewYear == "Alle"){
                return true;
            }
            else{
                return publication.release_date && publication.release_date.indexOf($scope.viewYear) != -1;
            }
        };

        $scope.yearChanged = function(){
            $scope.numPubs = $scope.publications.filter(function(pub){
                return $scope.yearFilter(pub);
            }).length;
        };

        $scope.pageChanged = function(){
            console.log($scope.currentPage);
        };

        calculateYearsInDropdownMenu();

        function calculateYearsInDropdownMenu() {
            var sinceYearX = 2009;

            for (var i = today.getFullYear(); i > sinceYearX; i--) {
                $scope.yearsInDropdown.push(i);
            }
        }

        $scope.editPublication = function (publication) {
            $scope.editing = angular.copy(publication); // always work on a copy
            $scope.editingIndex = $scope.publications.indexOf(publication);

            // clear form errors
            if (!$scope.editing.release_date) {
                $scope.editing.release_date = '';
            }
            $scope.publicationForm.$setPristine();
        };

        /**
         * This method saves either a new publication or an already existing publication.
         */
        $scope.saveEditedPublication = function () {
            $scope.isSaving = true;
            if (!$scope.editing.id) { // no id means it's a new one
                PublicationService.createNew($scope.editing)
                    .success(function (savedPublication) {
                        $scope.editing = savedPublication;
                        $scope.publications.push(savedPublication);
                        $scope.isSaving = false;
                    });
            } else { // it's an old one
                PublicationService.updateMetadata($scope.editing)
                    .success(function (savedPublication) {
                        $scope.publications[$scope.editingIndex] = savedPublication;
                        $scope.editPublication(savedPublication);
                        $scope.isSaving = false;
                    });
            }
        };

        $scope.showHelp = function(){
            $templateRequest("partials/templates/help/publicationHelp.html").then(function(template){
                MessageModal.info(template);
            })
        }

    });


<commit_msg>MOM-159 Fixed a bug that made the publications list not update on creating new<commit_after>/*
 * Copyright 2014 Studentmediene i Trondheim AS
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict';

angular.module('momusApp.controllers')
    .controller('PublicationCtrl', function ($scope, $http, PublicationService, $templateRequest, MessageModal) {

        var today = new Date();

        $scope.viewYear = today.getFullYear();
        $scope.publications = [];
        $scope.yearsInDropdown = [];

        $scope.currentPage = 1;
        $scope.pubsPerPage = 10;
        $scope.numPubs = 0;

        $scope.editing = {};

        $scope.dateOptions = {// Needed for the date picker, always start weeks on a monday
            'starting-day': 1
        };


        PublicationService.getAll().success(function (data) {
            $scope.publications = data;
        });

        $scope.yearFilter = function (publication) {
            if($scope.viewYear == "Alle"){
                return true;
            }
            else{
                return publication.release_date && publication.release_date.indexOf($scope.viewYear) != -1;
            }
        };

        $scope.yearChanged = function(){
            $scope.numPubs = $scope.publications.filter(function(pub){
                return $scope.yearFilter(pub);
            }).length;
        };

        $scope.pageChanged = function(){
        };

        calculateYearsInDropdownMenu();

        function calculateYearsInDropdownMenu() {
            var sinceYearX = 2009;

            for (var i = today.getFullYear(); i > sinceYearX; i--) {
                $scope.yearsInDropdown.push(i);
            }
        }

        $scope.editPublication = function (publication) {
            $scope.editing = angular.copy(publication); // always work on a copy
            $scope.editingIndex = $scope.publications.indexOf(publication);

            // clear form errors
            if (!$scope.editing.release_date) {
                $scope.editing.release_date = '';
            }
            $scope.publicationForm.$setPristine();
        };

        /**
         * This method saves either a new publication or an already existing publication.
         */
        $scope.saveEditedPublication = function () {
            $scope.isSaving = true;
            if (!$scope.editing.id) { // no id means it's a new one
                PublicationService.createNew($scope.editing)
                    .success(function (savedPublication) {
                        $scope.publications.push(savedPublication);
                        $scope.editPublication(savedPublication);
                        $scope.isSaving = false;
                    });
            } else { // it's an old one
                PublicationService.updateMetadata($scope.editing)
                    .success(function (savedPublication) {
                        $scope.publications[$scope.editingIndex] = savedPublication;
                        $scope.editPublication(savedPublication);
                        $scope.isSaving = false;
                    });
            }
        };

        $scope.showHelp = function(){
            $templateRequest("partials/templates/help/publicationHelp.html").then(function(template){
                MessageModal.info(template);
            })
        }

    });



{'fraction_non_alphanumeric': 0.10698945724326435, 'fraction_numerical': 0.005076142131979695, 'mean_word_length': 1.845925925925926, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5826898', 'n_tokens_mistral': 2032, 'n_tokens_neox': 1921, 'n_words': 475}