PageSourceSearch

https://civics.cc/static/civics/angular/controllers/listController.js

js civics.cc collected 2026-10-04 19:48:57 UTC 7,504 bytes, 236 lines download raw bytes

1angular.module('civics.list_controller', [])
2
3/**
4 *   Controller for list displays
5 */
6.controller("ListController", function(items, Categories, meta, $scope, $rootScope, $http, DateRanger, Downloader)
7{
8
9    /**
10     *  Content setup
11     */
12    this.markers = items;
13    console.log(this.markers[0]);
14    for(i in this.markers)
15      this.markers[i].filtered = false;  // Show markers by default
16
17    /**
18     *  Section state
19     */
20    this.showing_map = true;
21    this.count = this.markers.length;
22    this.section = meta.showing;
23    this.format  = 'list';
24
25    // Set active links in Django menu
26    var links = document.querySelectorAll('.main-menu__link');
27    links.forEach( function(link){ link.classList.remove('active') })
28    if(this.section == 'initiatives')
29        links[1].classList.add('active')
30    else
31        links[2].classList.add('active')
32
33    // Close responsive menu popup if open
34    document.querySelector('.region-toolbar__right').classList.remove('active')
35
36    /**
37     *  Pager setup and methods
38     */
39    this.starting_index = 0;
40    this.items_per_page = 48;
41    this.starting_index = 0;
42    this.ending_index   = this.items_per_page;
43    this.current_items  = 0;
44
45    // Get previous results
46    this.previousPage = function(){
47        var next_index = this.starting_index - this.items_per_page;
48        this.starting_index = next_index < 0 ? 0 : next_index;
49        this.set_current_items();
50    }
51    // Get next results
52    this.nextPage = function(){
53        var next_index = this.starting_index + this.items_per_page;
54        this.starting_index = next_index > this.count ? this.starting_index : next_index;
55        this.set_current_items();
56    }
57    // Get number of current displayed elements
58    this.set_current_items = function(){
59        var factor = parseFloat(this.count - this.starting_index)/parseFloat(this.items_per_page);
60        this.current_items = factor > 1 ? this.items_per_page : this.count - this.starting_index;
61    }
62    this.set_current_items();
63
64    /**
65     *  Setup categories and filter settings
66     */
67
68    if(this.section == 'initiatives'){
69        this.cities = Categories.city_initiatives;
70    } else {
71        this.cities = Categories.city_events;
72    }
73    this.countries = Object.keys( this.cities ).sort();
74    this.topics     = Categories.topics;
75    this.spaces     = Categories.spaces;
76    this.agents     = Categories.agents;
77    this.activities = Categories.activities;
78
79
80    if(this.section == 'initiatives')
81      categories = ['cities', 'topics', 'spaces', 'agents'];
82    else
83      categories = ['cities', 'topics', 'activities', 'agents'];
84
85    // Selected categories
86    this.selected_categories = {};
87    for(i in categories)
88        this.selected_categories[ categories[i] ] = []
89
90    // Selected category tabs
91    this.selected_tabs = [];
92
93    // Active elements in legend
94    this.active_legend_items = {};
95
96    // Needed to filter events by date ranges
97    this.time_scope = 'all';
98
99    /**
100     *  Reset categories to default inactive state
101     */
102    this.resetLegend = function(){
103        for(var i in categories){
104            this.active_legend_items[categories[i]] = {};
105            for(var s in this[categories[i]]){
106                this.active_legend_items[categories[i]][s] = false;
107            }
108        }
109    }
110
111    // Apply default state
112    this.resetLegend();
113
114    this.filterMarkers = function()
115    {
116        var c = meta.count;
117        var filters_length = this.selected_tabs.length;
118        var today = new Date();
119
120        if(filters_length > 0){
121            items.forEach( angular.bind(this, function(marker){
122                // Filter by category
123                for(var cat in this.selected_categories){
124                    // Every marker is visible by default in each category
125                    marker.filtered = false;
126                    // If marker category is not in active list filter it
127                    if(this.selected_categories[cat].length > 0 &&
128                       this.selected_categories[cat].indexOf(marker[cat]) == -1){
129                        marker.filtered = true;
130                        c--;
131                        break;
132                    }
133                }
134                // Filter by date ranges
135                if(this.time_scope != 'all' && !DateRanger.check[this.time_scope](marker.date, marker.expiration)){
136                    marker.filtered = true;
137                    c--;
138                }
139            }));
140        } else {
141            items.forEach( angular.bind(this, function(marker){
142                marker.filtered = false;
143                if(this.time_scope != 'all' && !DateRanger.check[this.time_scope](marker.date, marker.expiration)){
144                    marker.filtered = true;
145                    c--;
146                }
147            }));
148            for(var cat in this.selected_categories){
149                this.selected_categories[cat] = [];
150            }
151        }
152        this.count = c;
153    };
154
155    /**
156     *   Time filters
157     */
158    $scope.$on('filter_by_date', angular.bind(this, function(event, args){
159        this.time_scope = args.query;
160        this.filterMarkers();
161    }));
162
163    /**
164     *  Toggle a filter
165     */
166    this.toggleFilter = function(category, subcategory, city){
167        var i = this.selected_categories[category].indexOf(subcategory);
168        this.active_legend_items[category][subcategory] = !this.active_legend_items[category][subcategory];
169        if(i == -1){
170            if(city) {
171                this.selected_categories[category].push(parseInt(subcategory));
172                this.selected_tabs.push({ 'k' : category, 'v': subcategory, 'n' : city.name });
173            } else {
174                this.selected_categories[category].push(subcategory);
175                this.selected_tabs.push({ 'k' : category, 'v': subcategory, 'n' : Categories[category][subcategory] });
176            }
177        } else {
178            var i = this.selected_categories[category].indexOf(subcategory);
179            this.selected_categories[category].splice(i, 1);
180        }
181        this.filterMarkers();
182        this.show_help = false;
183    }
184
185    /**
186     *  Remove a filter
187     */
188    this.removeFilter = function(index){
189        var f = this.selected_tabs.splice(index, 1);
190        var i = this.selected_categories[f[0].k].indexOf(f[0].v);
191        this.selected_categories[f[0].k].splice(i, 1);
192        this.filterMarkers();
193        this.show_help = false;
194    }
195
196    /**
197     *  Remove all filters
198     */
199    this.removeFilters = function(){
200        this.selected_tabs = [];
201        this.resetLegend();
202        this.filterMarkers();
203        this.show_help = false;
204    }
205
206    /**
207     *  Show popup of selected markers
208     */
209    this.show = function(marker){
210        var url = '/api/initiative?id=';
211        if(this.section == 'events')
212          url = '/api/event?id=';
213        $http.get(url + marker.pk, {
214            ignoreLoadingBar: true,
215        }).then( angular.bind(this, function(response){
216          $rootScope.$broadcast('open-marker', response.data);
217        }));
218    }
219
220    this.cityname = function(id){
221        return Categories.cities[id];
222    }
223
224    /**
225     *   Download XLS with filtered initiatives
226     */
227    this.download_xls = function(){
228        this.show_help = false;
229        Downloader.get(this.section, this.selected_categories);
230    }
231
232    $scope.$on('open-marker', angular.bind(this, function(){
233        this.sharing_url = false;
234        this.show_help = false;
235    }));
236});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.