<commit_before>define(['text!./search-filter-themes.html', 'app', 'lodash'], function(template, app, _) { 'use strict';

	//==============================================
	//
	//
	//==============================================
	app.directive('searchFilterThemes', ["$http", function ($http) {
    return {
        restrict: 'EAC',
        template: template,
        replace: true,
        scope: {
              title: '@title',
              items: '=ngModel',
              field: '@field',
              query: '=query',
        },
        controller : ['$scope', '$element', '$location', 'Thesaurus', function ($scope, $element, $location, thesaurus)
        {
            $scope.expanded = false;
            $scope.selectedItems = [];

            $scope.isSelected = function(item) {
                return $.inArray(item.symbol, $scope.selectedItems) >= 0;
            };

            $scope.closeDialog = function() {
                $element.find("#dialogSelect").modal("hide");
            };

            $scope.actionSelect = function(item) {

                if($scope.isSelected(item)) {
                    $scope.selectedItems.splice($.inArray(item.symbol, $scope.selectedItems), 1);
                } else {
                    $scope.selectedItems.push(item.symbol);
                }

                $scope.updateQuery();
            };

            $scope.actionExpand = function() {

                var count1 = Math.ceil($scope.items.length/3);
                var count2 = Math.ceil(($scope.items.length-count1)/2);
                var count3 = Math.ceil(($scope.items.length-count2-count1));

                $scope.items1 = $scope.items.slice(0, count1);
                $scope.items2 = $scope.items.slice(count1, count2+count2);
                $scope.items3 = $scope.items.slice(count1+count2, count1+count2+count3);

                console.log($scope.items1);
                console.log($scope.items2);
                console.log($scope.items3);

                $element.find("#dialogSelect").modal("show");
            };

            $scope.$on('onExpand', function(scope) {
                if(scope!=$scope && $scope.expanded)
                    $scope.expanded = false;
            });

            $scope.filterx = function(item) {
                console.log(item);
                //return item.selected;
            };

            $scope.ccc = function(item) {
                return $scope.isSelected(item) ? 'facet selected' : 'facet unselected';
            };

            $scope.updateQuery = function() {

                console.log($scope.query);

                $scope.query = '';

                $scope.selectedItems.forEach(function(item) {
                    $scope.query += ($scope.query==='' ? '' : ' OR ') + $scope.field+':' + item;
                });

                if($scope.query!=='')
                    $scope.query = '(' + $scope.query + ')';
                else
                    $scope.query = '*:*';

                console.log($scope.query);
            };

            var unselect = $scope.unselect = function (item) {
                if(!item.selected) item.indeterminate = false;
                if(item.narrowerTerms) item.narrowerTerms.forEach(unselect);
            };

            function setBroaders(broaderTerms, selected) {

                if(!broaderTerms) return;

                broaderTerms.forEach(function (term) {

                    term.indeterminateCounterA = term.indeterminateCounterA + (selected ? 1 : -1);
                    console.log(term.indeterminateCounterA);
                    term.indeterminate = !term.selected && (term.indeterminateCounterA + term.indeterminateCounterB) > 0;

                    setBroaders(term.broaderTerms, selected);
                });
            }

            function setNarrowers(narrowerTerms, selected) {

                if(!narrowerTerms) return;

                narrowerTerms.forEach(function (term) {

                    term.indeterminateCounterB = term.indeterminateCounterB + (selected ? 1 : -1);
                    console.log(term.indeterminateCounterB);
                    term.indeterminate = !term.selected && (term.indeterminateCounterA + term.indeterminateCounterB) > 0;

                    setNarrowers(term.narrowerTerms, selected);
                });
            }

            $scope.onclick = function (scope, evt) {
                scope.item.selected = !scope.item.selected;
                $scope.ts(scope, evt);
            };

            $scope.ts = function (scope) {

                var term = scope.item;

                term.indeterminate = !term.selected && (term.indeterminateCounterA + term.indeterminateCounterB) > 0;

                setBroaders(term.broaderTerms, term.selected);
                setNarrowers(term.narrowerTerms, term.selected);

                buildQuery();
            };

            function buildQuery () {
                var conditions = [];
                buildConditions(conditions, $scope.terms);

                if(conditions.length===0) $scope.query = '*:*';
                else {
                    var query = '';
                    conditions.forEach(function (condition) { query = query + (query==='' ? '( ' : ' OR ') + condition; });
                    query += ' )';
                    $scope.query = query;
                }

				// update querystring

                var items = _(_.values($scope.termsMap)).where({ selected : true }).map(function(o) { return o.identifier; }).value();

                if(_.isEmpty(items))
                    items = null;

                $location.replace();
                $location.search("theme", items);
            }

            function buildConditions (conditions, items) {
                items.forEach(function (item) {
                    if(item.selected)
                        conditions.push('thematicAreas_REL_ss:'+item.identifier);
                    else if(item.narrowerTerms) {
                        buildConditions(conditions, item.narrowerTerms);
                    }
                });
            }

            function flatten(items, collection) {
                items.forEach(function (item) {
                    item.selected = false;
                    item.indeterminateCounterA = 0;
                    item.indeterminateCounterB = 0;
                    collection[item.identifier] = item;
                    if(item.narrowerTerms)
                        flatten(item.narrowerTerms, collection);
                });
                return collection;
            }//flatten(items, collection)

				}//link
    }; // return
  }]);  //app.directive('searchFilterCountries
});// define
<commit_msg>added functionality of parent directive, removed more unused code.<commit_after>define(['text!./search-filter-themes.html', 'app', 'lodash','angular'], function(template, app, _,angular) { 'use strict';

	//==============================================
	//
	//
	//==============================================
	app.directive('searchFilterThemes',['$http','Thesaurus','$timeout', function ($http,thesaurus,$timeout) {
    return {
        restrict: 'EAC',
        template: template,
        replace: true,
				require : '^search',
        scope: {
              title: '@title',
              items: '=ngModel',
              facet: '@facet',
							count: '=count' // total count of all children subquires needed for 0 result combinations
        },
          link : function ($scope, $element, $attr, searchCtrl)
        {
            $scope.expanded = false;
            var termsMap =[];
						$scope.termsArray=[];
						$scope.terms = [];
            $scope.termsModal = {};

						buildTermsAndQuery();
            $scope.$watch('items',function(){searchCtrl.updateTerms(termsMap,$scope.items,$scope.facet);});

						//=======================================================================
      			//
      			//=======================================================================
            function buildTermsAndQuery() {
                    if(_.isEmpty(termsMap)){ // get terms once and save

												$http.get('/api/v2013/thesaurus/domains/CBD-SUBJECTS/terms').success(function (data) {
														$scope.terms = thesaurus.buildTree(data);

														termsMap   = flatten($scope.terms, {});
														$scope.termsArray = _.values(termsMap);

														termsMap=searchCtrl.updateTerms(termsMap,$scope.items,$scope.facet);
                            searchCtrl.buildChildQuery(termsMap,$scope.items,$scope.facet);
												});

                    }else{
														termsMap = searchCtrl.updateTerms(termsMap,$scope.items,$scope.facet);
														//$scope.termsArray = _.values(termsMap);
														searchCtrl.buildChildQuery(termsMap,$scope.items,$scope.facet);
                    }

            }//buildTermsAndQuery()

            //=======================================================================
      			//
      			//=======================================================================
            $scope.refresh = function (item,forceDelete){
							var term = item;

										term.indeterminate = !term.selected && (term.indeterminateCounterA + term.indeterminateCounterB) > 0;
										setBroaders(term.broaderTerms, term.selected);
										setNarrowers(term.narrowerTerms, term.selected);
			              searchCtrl.refresh(item,forceDelete,termsMap,$scope.items,$scope.facet);
            };//$scope.refresh

						// =======================================================================
						// Jquery to find modal and executes the event on when opened calling our callback
						// which our call back then calls $timeout whcih will ensure an angular context.
						// Better then apply call as onlly exectues when the digest is done.
						//
						// =======================================================================
						$element.find("#dialogSelectThemes").on('show.bs.modal', function(){

										$timeout(function(){ //Ensure angular context
														buildTermsAndQuery();
														$scope.termsModal=angular.copy($scope.terms);// unbinded display for modal for first view
										});

						});//$element.find("#dialogSelect").on('show.bs.modal', function(){

						//=======================================================================
      			//
      			//=======================================================================
            function setBroaders(broaderTerms, selected) {

                if(!broaderTerms) return;

                broaderTerms.forEach(function (term) {
                    term.indeterminateCounterA = term.indeterminateCounterA + (selected ? 1 : -1);
                    term.indeterminate = !term.selected && (term.indeterminateCounterA + term.indeterminateCounterB) > 0;
                    setBroaders(term.broaderTerms, selected);
                });
            }//setBroaders(broaderTerms, selected)

						//=======================================================================
						//
						//=======================================================================
            function setNarrowers(narrowerTerms, selected) {

                if(!narrowerTerms) return;

                narrowerTerms.forEach(function (term) {
                    term.indeterminateCounterB = term.indeterminateCounterB + (selected ? 1 : -1);
                    term.indeterminate = !term.selected && (term.indeterminateCounterA + term.indeterminateCounterB) > 0;
                    setNarrowers(term.narrowerTerms, selected);
                });
            }//setNarrowers(narrowerTerms, selected)


						//=======================================================================
      			//
      			//=======================================================================
            function flatten(items, collection) {
                items.forEach(function (item) {
                    item.selected = false;
                    item.indeterminateCounterA = 0;
                    item.indeterminateCounterB = 0;
                    collection[item.identifier] = item;
                    if(item.narrowerTerms)
                        flatten(item.narrowerTerms, collection);
                });
                return collection;
            }//flatten(items, collection)

				}//link
    }; // return
  }]);  //app.directive('searchFilterCountries
});// define

{'fraction_non_alphanumeric': 0.20170363227258117, 'fraction_numerical': 0.004018000642880103, 'mean_word_length': 1.8251986379114642, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '3511314', 'n_tokens_mistral': 3610, 'n_tokens_neox': 3011, 'n_words': 492}