Skip to content

Commit 8c4cbc2

Browse files
author
Nicholas Thomson
committed
Merge pull request #20 from cloudspace/angular_refactor
Angular refactor
2 parents b92f9d8 + f946e43 commit 8c4cbc2

27 files changed

+554
-735
lines changed

app/index.html

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<script src="vendor/leaflet-dist/leaflet.js"></script>
5353
<script src="vendor/leaflet.markerclusterer/dist/leaflet.markercluster.js"></script>
5454
<script src="vendor/ngInfiniteScroll/ng-infinite-scroll.js"></script>
55+
<script src="vendor/crossfilter/crossfilter.js"></script>
5556
<!-- endbuild -->
5657

5758
<!-- build:js({.tmp,app}) scripts/scripts.js -->
@@ -61,15 +62,18 @@
6162

6263
<script src="scripts/services/authentication.js"></script>
6364
<script src="scripts/services/model.js"></script>
64-
<script src="scripts/services/companyModel.js"></script>
65-
<script src="scripts/services/categoryModel.js"></script>
66-
<script src="scripts/services/investorModel.js"></script>
65+
<script src="scripts/services/Company.js"></script>
66+
<script src="scripts/services/Category.js"></script>
67+
<script src="scripts/services/Investor.js"></script>
68+
<script src="scripts/services/componentData.js"></script>
6769

6870
<script src="scripts/controllers/crunchinator.js"></script>
6971

7072
<script src="scripts/directives/d3Bars.js"></script>
7173
<script src="scripts/directives/leafCluster.js"></script>
72-
<script src="vendor/crossfilter/crossfilter.js"></script>
74+
<script src="scripts/directives/listDisplay.js"></script>
75+
<script src="scripts/directives/listToggle.js"></script>
76+
<script src="scripts/directives/listSelect.js"></script>
7377
<!-- endbuild -->
7478

7579
</body>

app/scripts/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
angular.module('crunchinatorApp.models', []);
44
angular.module('crunchinatorApp.directives', []);
5+
angular.module('crunchinatorApp.services', []);
56
angular.module('crunchinatorApp.controllers', [
67
'ui.state',
78
'ui.bootstrap',
89
'configuration',
910
'crunchinatorApp.models',
1011
'crunchinatorApp.directives',
12+
'crunchinatorApp.services',
1113
'infinite-scroll'
1214
]);
1315

app/scripts/controllers/crunchinator.js

Lines changed: 28 additions & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -15,216 +15,43 @@ angular.module('crunchinatorApp.controllers')
1515
});
1616
})
1717

18-
.controller('CrunchinatorCtrl', function CrunchinatorCtrl($scope, $http, ENV, CompanyModel, CategoryModel, InvestorModel) {
19-
$scope.environment = ENV;
20-
21-
$scope.filteredCompaniesList = [];
22-
$scope.filteredCategoriesList = [];
23-
$scope.filteredInvestorsList = [];
24-
$scope.lookingForList = [];
25-
26-
$scope.select_investor = function() {
27-
if($scope.selectedInvestors.indexOf($scope.selected_investor) === -1) {
28-
$scope.selectedInvestors.push($scope.selected_investor);
29-
inv_ids = _.pluck($scope.selectedInvestors, 'id');
30-
$scope.filteredCompanies();
31-
32-
$scope.selected_investor = '';
33-
$scope.clearLookingFor();
34-
}
18+
.controller('CrunchinatorCtrl', function CrunchinatorCtrl($scope, $http, ENV, Company, Category, Investor, ComponentData) {
19+
var filterData = {
20+
categoryIds: [],
21+
investorIds: [],
22+
companyIds: []
3523
};
3624

37-
$scope.select_company = function() {
38-
if($scope.selectedCompanies.indexOf($scope.selected_company) === -1) {
39-
$scope.selectedCompanies.push($scope.selected_company);
40-
company_ids = _.pluck($scope.selectedCompanies, 'id');
41-
$scope.filteredInvestors();
25+
$scope.companies = Company;
26+
$scope.investors = Investor;
27+
$scope.categories = Category;
4228

43-
$scope.selected_company = '';
44-
$scope.clearLookingFor();
45-
}
46-
};
47-
48-
$scope.removeInvestor = function(investor) {
49-
$scope.selectedInvestors.splice($scope.selectedInvestors.indexOf(investor), 1);
50-
inv_ids = _.pluck($scope.selectedInvestors, 'id');
51-
$scope.filteredCompanies();
52-
$scope.clearLookingFor();
53-
};
54-
55-
$scope.removeCompany = function(company) {
56-
$scope.selectedCompanies.splice($scope.selectedCompanies.indexOf(company), 1);
57-
company_ids = _.pluck($scope.selectedCompanies, 'id');
58-
$scope.filteredInvestors();
59-
$scope.clearLookingFor();
60-
};
61-
62-
$scope.geoJsonData = _.memoize(function(filteredCompanies) {
63-
var geojson = {
64-
'type': 'FeatureCollection',
65-
'features': []
66-
};
67-
if (!filteredCompanies || !filteredCompanies.length) { return geojson; }
68-
69-
_.each(filteredCompanies, function(company) {
70-
if(company.latitude && company.longitude) {
71-
geojson.features.push({
72-
'type': 'Feature',
73-
'geometry': {'type': 'Point', 'coordinates': [company.longitude, company.latitude]},
74-
'properties': 0
75-
});
76-
}
77-
78-
});
79-
80-
return geojson;
81-
82-
}, function(filteredCompanies) {
83-
return _.pluck(filteredCompanies, 'id').join('');
29+
Company.fetch().then(function() {
30+
Company.setupDimensions();
31+
Company.runFilters(filterData);
8432
});
8533

86-
/*$scope.totalRaisedGraphData = _.memoize(function(filteredCompanies) {
87-
if (!filteredCompanies || !filteredCompanies.length) { return; }
88-
var total_raised_data = [];
89-
for(var i = 1; i <= 10; i++){
90-
total_raised_data.push({
91-
label: '$'+i+' - $'+((i === 1 ? 0 : i)+1) + 'M',
92-
count: 0
93-
});
94-
}
95-
96-
_.each(filteredCompanies, function(company) {
97-
var label_index = Math.floor((company.total_funding + 1) / 1000000);
98-
total_raised_data[label_index].count++;
99-
});
100-
return total_raised_data;
101-
}, function(filteredCompanies) {
102-
return _.pluck(filteredCompanies, 'id').join('');
103-
});*/
104-
105-
$scope.resetSelection = function() {
106-
$scope.selectedCompanies = [];
107-
$scope.selectedCategories = [];
108-
$scope.selectedInvestors = [];
109-
110-
$scope.filteredCompanies();
111-
$scope.filteredCategories();
112-
$scope.filteredInvestors();
113-
};
114-
115-
var cat_ids = [];
116-
var company_ids = [];
117-
var inv_ids = [];
118-
$scope.toggleSelected = function(selectedItems, item) {
119-
$scope.selectedItem = item;
120-
var ind = selectedItems.indexOf(item);
121-
if (ind > -1) {
122-
//Remove item if its already selected
123-
selectedItems.splice(ind, 1);
124-
} else {
125-
//Add item if its not already selected
126-
selectedItems.push(item);
127-
}
128-
129-
cat_ids = _.pluck($scope.selectedCategories, 'id');
130-
company_ids = _.pluck($scope.selectedCompanies, 'id');
131-
inv_ids = _.pluck($scope.selectedInvestors, 'id');
132-
133-
$scope.filteredCompanies();
134-
$scope.filteredCategories();
135-
$scope.filteredInvestors();
136-
$scope.clearLookingFor();
137-
138-
$scope.updateLookingFor();
139-
140-
$scope.selectedCompanies = _.intersection($scope.selectedCompanies, $scope.filteredCompaniesList);
141-
$scope.selectedCategories = _.intersection($scope.selectedCategories, $scope.filteredCategoriesList);
142-
$scope.selectedInvestors = _.intersection($scope.selectedInvestors, $scope.filteredInvestorsList);
143-
};
144-
145-
var crossCompanies;
146-
var companiesDimension;
147-
var companiesByName;
148-
$scope.filteredCompanies = function() {
149-
if(crossCompanies) {
150-
companiesDimension.filterAll(); //clear filter
151-
companiesDimension.filter(function(c){ return ( cat_ids.length === 0 || cat_ids.indexOf(c.category_id) > -1) && (inv_ids.length === 0 || _.intersection(c.investor_ids[0], inv_ids).length > 0); });
152-
153-
$scope.filteredCompaniesList = companiesByName.bottom(Infinity);
154-
}
155-
};
156-
157-
var crossInvestors;
158-
var investorsDimension;
159-
var investorsByName;
160-
$scope.filteredInvestors = function() {
161-
if(crossInvestors) {
162-
investorsDimension.filterAll();
163-
investorsDimension.filter(function(i){ return (cat_ids.length === 0 || _.intersection(i.invested_category_ids, cat_ids).length > 0) && (company_ids.length === 0 || _.intersection(i.invested_company_ids, company_ids).length > 0); });
164-
165-
$scope.filteredInvestorsList = investorsByName.bottom(Infinity);
166-
}
167-
};
168-
169-
var crossCategories;
170-
var categoriesDimension;
171-
var categoriesByName;
172-
$scope.filteredCategories = function() {
173-
if(crossCategories) {
174-
categoriesDimension.filterAll();
175-
categoriesDimension.filter(function(c){ return (company_ids.length === 0 || _.intersection(company_ids, c.company_ids).length > 0) && (inv_ids.length === 0 || _.intersection(inv_ids, c.investor_ids).length > 0); });
176-
177-
$scope.filteredCategoriesList = categoriesByName.bottom(Infinity);
178-
}
179-
};
180-
181-
$scope.clearLookingFor = function() {
182-
$scope.lookingForList = [];
183-
$scope.updateLookingFor();
184-
};
34+
Category.fetch().then(function() {
35+
Category.setupDimensions();
36+
Category.runFilters(filterData);
37+
});
18538

186-
$scope.$watch('lookingFor', function(){
187-
$scope.clearLookingFor();
39+
Investor.fetch().then(function() {
40+
Investor.setupDimensions();
41+
Investor.runFilters(filterData);
18842
});
18943

190-
$scope.updateLookingFor = function() {
191-
var next_items = [];
192-
var current_count = $scope.lookingForList.length;
193-
var page_size = 100;
194-
if($scope.lookingFor === 'companies') {
195-
next_items = $scope.filteredCompaniesList.slice(current_count, current_count+page_size);
196-
}
197-
else if($scope.lookingFor === 'investors') {
198-
next_items = $scope.filteredInvestorsList.slice(current_count, current_count+page_size);
199-
}
200-
$scope.lookingForList = $scope.lookingForList.concat(next_items);
201-
};
44+
$scope.geoJsonData = ComponentData.companyGeoJson;
45+
$scope.totalFunding = ComponentData.totalFunding;
20246

203-
$scope.companies = CompanyModel;
204-
$scope.categories = CategoryModel;
205-
$scope.investors = InvestorModel;
47+
//Moves into a directive that handles how we do categories
48+
$scope.$on('filterAction', function() {
49+
filterData.categoryIds = _.pluck($scope.selectedCategories, 'id');
50+
filterData.companyIds = _.pluck($scope.selectedCompanies, 'id');
51+
filterData.investorIds = _.pluck($scope.selectedInvestors, 'id');
20652

207-
CompanyModel.fetch().then(function(){
208-
$scope.all_companies = CompanyModel.all();
209-
crossCompanies = crossfilter($scope.all_companies);
210-
companiesDimension = crossCompanies.dimension(function(company) { return company; });
211-
companiesByName = crossCompanies.dimension(function(company) {return company.name;});
212-
$scope.filteredCompanies();
213-
});
214-
InvestorModel.fetch().then(function(){
215-
$scope.all_investors = InvestorModel.all();
216-
crossInvestors = crossfilter($scope.all_investors);
217-
investorsDimension = crossInvestors.dimension(function(investor) { return investor; });
218-
investorsByName = crossInvestors.dimension(function(investor) {return investor.name;});
219-
$scope.filteredInvestors();
220-
});
221-
CategoryModel.fetch().then(function(){
222-
crossCategories = crossfilter(CategoryModel.all());
223-
categoriesDimension = crossCategories.dimension(function(category) { return category; });
224-
categoriesByName = crossCategories.dimension(function(category) {return category.name;});
225-
$scope.filteredCategories();
53+
Company.runFilters(filterData);
54+
Category.runFilters(filterData);
55+
Investor.runFilters(filterData);
22656
});
227-
228-
$scope.resetSelection();
229-
23057
});

app/scripts/directives/leafCluster.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ angular.module('crunchinatorApp.directives').directive('leafCluster', function()
4343
});
4444
var geoJsonLayer = L.geoJson(data, {
4545
onEachFeature: function (feature, layer) {
46-
layer.bindPopup(feature.properties.address);
46+
layer.bindPopup(feature.properties.name);
4747
}
4848
});
4949
markers.addLayer(geoJsonLayer);
50-
51-
5250
map.addLayer(markers);
53-
map.fitBounds(markers.getBounds());
51+
52+
if(data.features.length > 0) {
53+
map.fitBounds(markers.getBounds());
54+
}
5455
};
5556
}
5657
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
angular.module('crunchinatorApp.directives').directive('listDisplay', function() {
4+
return {
5+
restrict: 'EA',
6+
scope: {
7+
data: '=',
8+
title: '@',
9+
total: '='
10+
},
11+
templateUrl: 'views/list-display.tpl.html',
12+
link: function(scope) {
13+
scope.data = scope.data || [];
14+
scope.scrollItems = [];
15+
scope.$watch('data', function(){
16+
scope.filteredItems = scope.data;
17+
scope.scrollItems = [];
18+
scope.updateScrollItems();
19+
});
20+
21+
scope.updateScrollItems = function() {
22+
var next_items = [];
23+
var current_count = scope.scrollItems.length;
24+
var page_size = 100;
25+
next_items = scope.filteredItems.slice(current_count, current_count+page_size);
26+
scope.scrollItems = scope.scrollItems.concat(next_items);
27+
};
28+
}
29+
};
30+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
angular.module('crunchinatorApp.directives').directive('listSelect', ['$rootScope', function($rootScope) {
4+
return {
5+
restrict: 'EA',
6+
scope: {
7+
items: '=',
8+
title: '@',
9+
selected: '@'
10+
},
11+
templateUrl: 'views/list-select.tpl.html',
12+
link: function(scope) {
13+
scope.selectedItems = [];
14+
15+
scope.selectItem = function() {
16+
if(!_.contains(scope.selectedItems, scope.selectedItem)) {
17+
scope.selectedItems.push(scope.selectedItem);
18+
scope.selectedItem = '';
19+
}
20+
21+
scope.$parent[scope.selected] = scope.selectedItems;
22+
$rootScope.$broadcast('filterAction');
23+
};
24+
25+
scope.removeItem = function(item) {
26+
scope.selectedItems = _.without(scope.selectedItems, item);
27+
scope.$parent[scope.selected] = scope.selectedItems.slice(0);
28+
$rootScope.$broadcast('filterAction');
29+
};
30+
31+
}
32+
};
33+
}]);

0 commit comments

Comments
 (0)