-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
109 lines (94 loc) · 3.04 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var app = angular.module('lookup', []);
app.controller('AppCtrl', function ($scope, $http) {
$scope.filterData = {};
$scope.search = function () {
if($scope.orgname.length >= 3) {
var url = "https://data.brreg.no/enhetsregisteret/enhet.json";
var params = {
size: 10,
$filter: makeFilter($scope)
};
$http.get(url, {params: params}, {})
.then(function (response) {
$scope.clearLookup();
$scope.search_result = response.data;
});
} else {
$scope.clearSearch();
}
};
$scope.clearSearch = function () {
$scope.search_result = null;
};
$scope.lookup = function (num) {
if(num > 0) {
var url = "https://data.brreg.no/enhetsregisteret/enhet/" + num + ".json";
$http.get(url, {}, {})
.then(function (response) {
$scope.org_data = response.data;
});
}
};
$scope.clearLookup = function () {
$scope.org_data = null;
};
$scope.prev = function() {
if(url = prevUrl($scope.search_result.links)) {
$http.get(url, {}, {})
.then(function (response) {
$scope.page = response.data.page.page + 1;
$scope.search_result = response.data;
});
}
};
$scope.next = function() {
if(url = nextUrl($scope.search_result.links)) {
$http.get(url, {}, {})
.then(function (response) {
$scope.page = response.data.page.page + 1;
$scope.search_result = response.data;
});
}
};
$scope.hasPrev = function () {
return prevUrl($scope.search_result.links) != null;
};
$scope.hasNext = function () {
return nextUrl($scope.search_result.links) != null;
};
});
function prevUrl(data) {
for(var i in data) {
if(data[i].rel == "prev")
return data[i].href;
}
return null;
}
function nextUrl(data) {
for(var i in data) {
if(data[i].rel == "next")
return data[i].href;
}
return null;
}
function joinArray(sep, a) {
var str = "";
for(var i in a) {
if(a[i] != "") {
if(i != 0) {
str += sep;
}
str += a[i]
}
}
return str;
}
function makeFilter(scope) {
return joinArray(" and ", [
scope.orgname ? "startswith(navn,'" + scope.orgname + "')" : "",
scope.filterData.min_employees > 0 ? "antallAnsatte ge " + scope.filterData.min_employees : "",
scope.filterData.max_employees ? "antallAnsatte le " + scope.filterData.max_employees : "",
scope.filterData.from_date ? "stiftelsesdato ge datetime'" + new Date(scope.filterData.from_date).toJSON() + "'": "",
scope.filterData.to_date ? "stiftelsesdato le datetime'" + new Date(scope.filterData.to_date).toJSON() + "'" : "",
]);
}