{"size": 6052, "ext": "js", "max_stars_repo_path": "app/scripts/remark.directive.js", "max_stars_repo_name": "ahjo-dashboard/dashboard-ui", "max_stars_repo_stars_event_min_datetime": "2016-02-09T13:45:20.000Z", "max_stars_repo_stars_event_max_datetime": "2017-01-16T04:01:55.000Z", "max_issues_repo_path": "app/scripts/remark.directive.js", "max_issues_repo_name": "ahjo-dashboard/dashboard-ui", "max_issues_repo_issues_event_min_datetime": "2016-02-05T12:30:24.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-01T23:10:36.000Z", "max_forks_repo_path": "app/scripts/remark.directive.js", "max_forks_repo_name": "ahjo-dashboard/dashboard-ui", "max_forks_repo_forks_event_min_datetime": "2016-02-04T07:35:38.000Z", "max_forks_repo_forks_event_max_datetime": "2016-11-23T11:40:16.000Z"}
{"max_stars_count": 2, "max_issues_count": 432, "max_forks_count": 7, "avg_line_length": 38.5477707006, "max_line_length": 171, "alphanum_fraction": 0.4251487112}
/**
 * (c) 2016 Tieto Finland Oy
 * Licensed under the MIT license.
 */
'use strict';

/**
 * @ngdoc directive
 * @name dashboard.proposalList:remarkDirective
 * @description
 * # remarkDirective
 */
angular.module('dashboard')
    .directive('dbRemark', [function () {

        var controller = ['$log', '$scope', 'AhjoRemarkSrv', '$rootScope', 'CONST', 'DialogUtils', function ($log, $scope, AhjoRemarkSrv, $rootScope, CONST, DialogUtils) {
            $log.log("dbRemark: CONTROLLER");
            var self = this;
            self.error = false;
            self.editorText = null;
            var previousText = null;
            self.remark = null;
            self.topic = null;
            self.loading = false;
            self.isUnsaved = false;
            self.isMobile = $rootScope.isMobile;

            function setUnsaved(unsaved) {
                if (unsaved !== self.isUnsaved) {
                    self.isUnsaved = unsaved;
                    $rootScope.$emit(CONST.REMARKISUNSAVED, self.isUnsaved);
                }
            }

            function getRemark(guid) {
                $log.debug("dbRemark.getRemark: " + guid);
                self.remark = null;
                self.editorText = null;
                if (angular.isString(guid)) {
                    self.loading = true;
                    AhjoRemarkSrv.get({ 'guid': guid }).$promise.then(function (response) {
                        $log.debug("dbRemark.getRemark: then");
                        if (angular.isObject(response) && angular.isObject(response.objects)) {
                            self.remark = response.objects;
                            self.editorText = self.remark.text;
                        }
                        else {
                            $log.error('dbRemark: getRemark invalid response');
                        }
                    }, function (/*error*/) {
                        $log.error("dbRemark.getRemark: error: ", arguments);
                    }).finally(function () {
                        $log.debug("dbRemark.getRemark: finally: ");
                        if (!self.remark) {
                            self.error = true;
                        }
                        self.loading = false;
                    });
                }
                else {
                    $log.error('dbRemark: getRemark parameter invalid');
                }
            }

            function postRemark(remark) {
                $log.debug("dbRemark: postRemark: ", arguments);
                var copy = angular.copy(remark);
                if (angular.isObject(copy)) {
                    self.loading = true;
                    AhjoRemarkSrv.post(remark).$promise.then(function (response) {
                        $log.debug("dbRemark: post then: ", arguments);
                        setUnsaved(false);
                        if (angular.isObject(response) && angular.isObject(response.objects)) {
                            self.remark = response.objects;
                            self.editorText = self.remark.text;
                        }
                        else {
                            $log.error('dbRemark: postRemark response invalid');
                        }
                    }, function (/*error*/) {
                        $log.error("dbRemark: post error: ", arguments);
                        DialogUtils.showError('STR_SAVE_FAILED');
                    }).finally(function () {
                        $log.debug("dbRemark: post finally");
                        self.loading = false;
                    });
                }
                else {
                    $log.error('dbRemark: postRemark parameter invalid');
                }
            }

            self.clicked = function (ok) {
                $log.debug("dbRemark: clicked " + ok + ': ' + self.editorText);
                if (angular.isObject(self.remark)) {
                    if (ok) {
                        self.remark.text = self.editorText;
                        postRemark(self.remark);
                    }
                    else {
                        self.editorText = self.remark.text;
                        setUnsaved(false);
                    }
                }
                else {
                    $log.error('dbRemark: clicked remark missing');
                }
            };

            self.changed = function () {
                $log.debug("dbRemark: changed");
                if (angular.isObject(self.remark)) {
                    previousText = angular.isString(previousText) ? previousText : '';
                    self.editorText = angular.isString(self.editorText) ? self.editorText : '';
                    if (previousText.length !== self.editorText.length) {
                        previousText = self.editorText;
                        setUnsaved(true);
                    }
                    else {
                        setUnsaved(false);
                    }
                }
                else {
                    $log.error('dbRemark: changed remark missing');
                }
            };

            $scope.$watch(function () {
                return {
                    topic: $scope.topic
                };
            }, function (data) {
                if (angular.isObject(data) && angular.isObject(data.topic) && !angular.equals(data.topic, self.topic)) {
                    self.topic = data.topic;
                    previousText = null;
                    setUnsaved(false);
                    getRemark(data.topic.topicGuid);
                }
            }, true);

            $scope.$on('$destroy', function () {
                $log.debug("dbRemark: DESTROY");
            });
        }];

        return {
            scope: {
                topic: '='
            },
            templateUrl: 'views/remark.directive.html',
            restrict: 'AE',
            controller: controller,
            controllerAs: 'c',
            replace: 'true'
        };
    }]);
