<commit_before>window.up = window.up || {};

window.up.startSurveyPortlet = function(window, _, params) {
    'use strict';

    var surveyName = params.surveyName || '';
    var n = params.n;

    var MODULE_NAME = n + '-survey-portlet';
    var PROFILE_ROOT = 'https://portal-mock-api-dev.herokuapp.com/api/';
    var USER = 'admin';

    if (!window.angular) {
        var ANGULAR_SCRIPT_ID = 'angular-uportal-script';

        var scr = document.getElementById(ANGULAR_SCRIPT_ID);

        if (!scr) {
            scr = document.createElement('script');
            scr.id =  ANGULAR_SCRIPT_ID;
            scr.type =  'text/javascript';
            scr.async =  true;
            scr.charset =  'utf-8';
            scr.src =  'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.js';

            document.body.appendChild(scr);
        }

        scr.addEventListener('load', bootstrap);
    } else {
        register(window.up.ngApp);
    }

    function bootstrap() {
        var app = angular.module(MODULE_NAME, []);
        register(app);
        angular.bootstrap(document.getElementById(n + '-survey-portlet'), [MODULE_NAME]);
    }

    function register(app) {
        app
        /**
         * @ngdoc function
         * @name ngPortalApp.controller:SurveyCtrl
         * @description
         * # SurveyCtrl
         * Controller of the ngPortalApp
         */
        .controller('SurveyCtrl', function ($scope, $http, $filter) {
            console.log(surveyName);
            $http.get('/survey-portlet/v1/surveys/' + (surveyName ? 'surveyByName/' + surveyName : ''))
            .success(function(surveys) {
                $scope.surveys = _.isArray(surveys) ? surveys : [surveys];
            });

            $scope.toggle = function(o) {
                o = o || {};
                o.shown = !o.shown;
            };

            $http.get(PROFILE_ROOT + 'surveyAnswers', {
                params: {
                    user: USER,
                }
            }).success(function(d) {
                //TODO FIXME
                if (d.length > 0) {
                    $scope.surveyData = d[0];
                }
            });

            $scope.save = function(survey) {
                var method = survey.id ? 'PUT' : 'POST';
                var base = '/survey-portlet/v1/surveys/';
                var url = survey.id ? base + survey.id : base ;

                $http({
                    method: method,
                    url: url,
                    data: survey,
                });

                survey = angular.copy(survey);

                _.each(survey.surveyQuestions, function(q, i) {
                    q = survey.surveyQuestions[i] = q.question;

                    // _.each(q.questionAnswers, function(a, j) {
                    //   q.questionAnswers[j] = a.answer;
                    // });

                    $http({
                        method: 'PUT',
                        url: base + 'questions/' + q.id,
                        data: q
                    });
                });

            };

            $scope.surveyData = {};
        })
        .directive('surveyQuestion', function () {
            /**
             * @ngdoc directive
             * @name ngPortalApp.directive#SurveyQuestion
             * @description A directive to display a survey question.
             * @restrict E
             */
            return {
                template: '<section class="question">' +
                    '<label class="text">{{def.question.text}}</label>' +
                    '<div class="answer" ng-repeat="ans in def.question.questionAnswers | orderBy:\'sequence\'">' +
                    '<label title="{{ans.answer.altText}}" aria-label="{{ans.answer.altText}}" >' +
                    '<img ng-if="ans.answer.imgUrl" ng-src="{{ans.answer.imgUrl}}" height="25px" width="25px"></img>' +
                    '<input ng-if="def.numAllowedAnswers === 1" type="radio" ng-model="survey[def.question.id]" ng-value="ans.answer.id" />' +
                    '<input ng-if="def.numAllowedAnswers > 1" type="checkbox" ng-model="survey[def.question.id][ans.answer.id]"/>' +
                    '{{ans.answer.text}}' +
                    '</label>' +
                    '<span class="glyphicon glyphicon-info-sign" ng-if="ans.answer.helpText" title="{{ans.answer.helpText}}"></span>' +
                    '</div>' +
                    '</section>',
                restrict: 'E',
                scope: {
                    def: '=',
                    survey: '=',
                },
                link: function postLink($scope, iEle, iAttrs) {
                }
            };
        })
        .directive('ngDrag', function (DragData) {
            return {
                restrict: 'A',
                link: function postLink(scope, element, iAttrs) {
                    element.attr('draggable', 'true');

                    element.on('dragstart', function(e) {
                        e.stopPropagation();<commit_msg>Add early return if already boostrapped.<commit_after>window.up = window.up || {};

window.up.startSurveyPortlet = function(window, _, params) {
    'use strict';
    
    var surveyName = params.surveyName || '';
    var n = params.n;

    var MODULE_NAME = n + '-survey-portlet';
    var PROFILE_ROOT = 'https://portal-mock-api-dev.herokuapp.com/api/';
    var USER = 'admin';

    if (!window.angular) {
        var ANGULAR_SCRIPT_ID = 'angular-uportal-script';

        var scr = document.getElementById(ANGULAR_SCRIPT_ID);

        if (!scr) {
            scr = document.createElement('script');
            scr.id =  ANGULAR_SCRIPT_ID;
            scr.type =  'text/javascript';
            scr.async =  true;
            scr.charset =  'utf-8';
            scr.src =  'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.js';

            document.body.appendChild(scr);
        }

        scr.addEventListener('load', bootstrap);
    } else {
        register(window.up.ngApp);
    }

    function bootstrap() {
        var app = angular.module(MODULE_NAME, []);
        register(app);
        angular.bootstrap(document.getElementById(n + '-survey-portlet'), [MODULE_NAME]);
    }

    function register(app) {
        app
        /**
         * @ngdoc function
         * @name ngPortalApp.controller:SurveyCtrl
         * @description
         * # SurveyCtrl
         * Controller of the ngPortalApp
         */
        .controller('SurveyCtrl', function ($scope, $http, $filter) {

            $http.get('/survey-portlet/v1/surveys/' + (surveyName ? 'surveyByName/' + surveyName : ''))
            .success(function(surveys) {
                $scope.surveys = _.isArray(surveys) ? surveys : [surveys];
            });

            $scope.toggle = function(o) {
                o = o || {};
                o.shown = !o.shown;
            };

            $http.get(PROFILE_ROOT + 'surveyAnswers', {
                params: {
                    user: USER,
                }
            }).success(function(d) {
                if (d.length > 0) {
                    $scope.surveyData = d[0];
                }
            });

            $scope.save = function(survey) {
                var method = survey.id ? 'PUT' : 'POST';
                var base = '/survey-portlet/v1/surveys/';
                var url = survey.id ? base + survey.id : base ;

                $http({
                    method: method,
                    url: url,
                    data: survey,
                });

                survey = angular.copy(survey);

                _.each(survey.surveyQuestions, function(q, i) {
                    q = survey.surveyQuestions[i] = q.question;

                    // _.each(q.questionAnswers, function(a, j) {
                    //   q.questionAnswers[j] = a.answer;
                    // });

                    $http({
                        method: 'PUT',
                        url: base + 'questions/' + q.id,
                        data: q
                    });
                });

            };

            $scope.surveyData = {};
        })
        .directive('surveyQuestion', function () {
            /**
             * @ngdoc directive
             * @name ngPortalApp.directive#SurveyQuestion
             * @description A directive to display a survey question.
             * @restrict E
             */
            return {
                template: '<section class="question">' +
                    '<label class="text">{{def.question.text}}</label>' +
                    '<div class="answer" ng-repeat="ans in def.question.questionAnswers | orderBy:\'sequence\'">' +
                        '<label title="{{ans.answer.altText}}" aria-label="{{ans.answer.altText}}" >' +
                            '<img ng-if="ans.answer.imgUrl" ng-src="{{ans.answer.imgUrl}}" height="25px" width="25px"></img>' +
                            '<input ng-if="def.numAllowedAnswers === 1" type="radio" ng-model="survey[def.question.id]" ng-value="ans.answer.id" />' +
                            '<input ng-if="def.numAllowedAnswers > 1" type="checkbox" ng-model="survey[def.question.id][ans.answer.id]"/>' +
                            '{{ans.answer.text}}' +
                        '</label>' +
                        '<span class="glyphicon glyphicon-info-sign" ng-if="ans.answer.helpText" title="{{ans.answer.helpText}}"></span>' +
                    '</div>' +
                    '</section>',
                restrict: 'E',
                scope: {
                    def: '=',
                    survey: '=',
                },
                link: function postLink($scope, iEle, iAttrs) {
                }
            };
        })
        .directive('ngDrag', function (DragData) {
            return {
                restrict: 'A',
                link: function postLink(scope, element, iAttrs) {
                    element.attr('draggable', 'true');

                    element.on('dragstart', function(e) {
                        e.stopPropagation();
{'fraction_non_alphanumeric': 0.1445436507936508, 'fraction_numerical': 0.002976190476190476, 'mean_word_length': 1.3531746031746033, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 35, 'https://': 4, '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': '2068764', 'n_tokens_mistral': 2929, 'n_tokens_neox': 2759, 'n_words': 469}