-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtoolbar.js
102 lines (87 loc) · 2.74 KB
/
toolbar.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
import './toolbar.scss';
import template from './toolbar.html';
const ToolbarComponent = { bindings: { wide: '=' }, controller, template };
function controller($document, $mdSidenav, $mdToast, $state, $timeout, $window, WikiService, wikidata) {
const vm = this;
const baseUrl = $window.__env.baseUrl;
vm.isLoggedIn = false;
vm.loading = true;
vm.mobile = {};
vm.search = { showSearch: false };
// actions
vm.goTo = goTo;
vm.goToItem = goToItem;
vm.login = login;
vm.logout = logout;
vm.mobile.closeSearch = closeSearch;
vm.mobile.openSearch = openSearch;
vm.toggleSidebar = () => $mdSidenav('left').toggle();
vm.querySearch = text => wikidata.getSearch(text);
init();
// functions
function closeSearch() {
vm.mobile.showSearch = false;
}
function goTo(place) {
vm.toggleSidebar();
$timeout(() => $state.go(place), 150);
}
function goToItem(item) {
if (!item) { return; }
wikidata.getRecursive(item.id, 'wdt:P31/wdt:P279').then((response) => {
const ids = response.map(prop => prop.value_id);
if (ids.includes('Q56061') || ids.includes('Q5107')) {
$state.go('main.list', { id: item.id.substring(1), heritage: 1, c: undefined });
} else if (ids.includes('Q811979')) {
$state.go('main.object', { id: item.id.substring(1) });
} else {
$state.go('main.object', { id: item.id.substring(1) });
/*
$mdToast.show($mdToast.simple()
.position('top right')
.textContent(`${item.label} is not an architectural structure or territorial entity`)
.hideDelay(2000));
*/
}
closeSearch();
});
}
function init() {
WikiService.getUserInfo().then((response) => {
vm.isLoggedIn = response;
vm.loading = false;
});
}
function login() {
vm.loading = true;
const current = $window.location.href;
$window.location.href = `${baseUrl}/login?next=${encodeURIComponent(current)}`;
}
function logout() {
$window.location.href = `${baseUrl}/logout`;
}
function openSearch() {
vm.mobile.showSearch = true;
$timeout(() => {
const input = document.querySelector('#searchField');
angular.element(input)[0].focus();
return true;
}, 100);
}
}
export default () => {
angular
.module('monumental')
.component('moToolbar', ToolbarComponent)
.directive('moToolbarScroll', ($window) => {
let lastPosition = 0;
return (scope) => {
angular.element($window).bind('scroll', function () {
if (this.pageYOffset > lastPosition) { scope.isHidden = true; }
if (this.pageYOffset < lastPosition) { scope.isHidden = false; }
lastPosition = this.pageYOffset;
scope.$apply();
});
};
});
};