This repository has been archived by the owner on Sep 5, 2020. It is now read-only.
/
sidebar.js
220 lines (187 loc) · 5.9 KB
/
sidebar.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
Template Controllers
@module Templates
*/
/**
The sidebar template
@class [template] layout_sidebar
@constructor
*/
Template['layout_sidebar'].onRendered(function () {
var template = this,
$ul = template.$('nav > ul');
$ul.sortable({
containment: 'aside.sidebar',
axis: 'y',
// tolerance: 'pointer',
items: '> li:not(.browser)',
handle: 'button.main',
cancel: '.browser',
cursor: 'move',
delay: 150,
revert: 200,
start: function (e) {
$ul.sortable('refreshPositions');
},
update: function (e) {
// iterate over the lis and reposition the items
$ul.find('> li').each(function (index, test) {
var id = $(this).data('tab-id');
if (id) {
Tabs.update(id, { $set: { position: index + 1 } });
}
});
}
});
template.$('[data-tab-id]').on('mouseover', function () {
});
});
Template['layout_sidebar'].helpers({
/**
Return the tabs
@method (tabs)
*/
'tabs': function () {
return Tabs.find({}, { sort: { position: 1 } }).fetch();
},
/**
Return the correct name
@method (name)
*/
'name': function () {
return (this._id === 'browser') ? TAPi18n.__('mist.sidebar.buttons.browser') : this.name;
},
/**
Return the correct dapp icon
@method (icon)
*/
'icon': function () {
return (this._id === 'browser') ? 'icons/browse-icon@2x.png' : this.icon;
},
/**
Return the tabs sub menu as array
@method (subMenu)
*/
'subMenu': function () {
var template = Template.instance();
if (this._id === 'browser') {
return LastVisitedPages.find({}, { sort: { timestamp: -1 }, limit: 25 });
} else if (this.menu) {
var menu = _.toArray(this.menu);
// sort by position
menu.sort(function (a, b) {
if (a.position < b.position) {
return -1;
}
if (a.position > b.position) {
return 1;
}
return 0;
});
return menu;
}
},
/**
Returns connected accounts for dapp
@method (dappAccounts)
*/
'dappAccounts': function (limit) {
if (this.permissions) {
if (limit) {
return EthAccounts.find({ address: { $in: this.permissions.accounts || [] } },
{ limit: limit });
}
return EthAccounts.find({ address: { $in: this.permissions.accounts || [] } });
}
},
/**
Determines if the current tab is visible
@method (isSelected)
*/
'isSelected': function () {
return (LocalStore.get('selectedTab') === (this._id || 'browser')) ? 'selected' : '';
},
/**
It defines which tabs will have a remove button on the interface
@method (tabShouldBeRemovable)
*/
'tabShouldBeRemovable': function () {
return !_.contains(['browser', 'wallet'], this._id);
},
});
Template['layout_sidebar'].events({
/**
Select the current visible tab
@event click button.main
*/
'click nav button.main': function (e, template) {
LocalStore.set('selectedTab', this._id || 'browser');
},
/**
Call the submenu dapp callback
@event click ul.sub-menu button
*/
'click nav ul.sub-menu button': function (e, template) {
var tabId = $(e.currentTarget).parent().parents('li').data('tab-id');
var webview = $('webview[data-id="' + tabId + '"]')[0];
// browser
if (tabId === 'browser') {
webviewLoadStart.call(webview, tabId, { newURL: this.url, type: 'side-bar-click', preventDefault: function () {} });
// dapp tab
} else if (webview) {
webview.send('mistAPI_callMenuFunction', this.id);
LocalStore.set('selectedTab', tabId);
}
},
/**
Remove the current selected tab
// TODO show popup before to confirm
@event click button.remove-tab
*/
'click button.remove-tab': function () {
if (LocalStore.get('selectedTab') === this._id) {
LocalStore.set('selectedTab', 'browser');
}
Tabs.remove(this._id);
},
/**
Show connect account popup
@event click .accounts button'
*/
'click .accounts button': function (e, template) {
var initialTabCount = Tabs.find().fetch().length;
LocalStore.set('selectedTab', this._id);
var initialTabId = this._id;
mist.requestAccount(function (ev, addresses) {
dbSync.syncDataFromBackend(LastVisitedPages);
dbSync.syncDataFromBackend(Tabs).then(function () {
var tabCount = Tabs.find().fetch().length;
var tabId;
if (tabCount > initialTabCount) { // browse tab was pinned
tabId = Tabs.findOne({}, { sort: { position: -1 }, limit: 1 });
} else {
tabId = initialTabId;
}
Tabs.update(tabId, {
$set: {
'permissions.accounts': addresses
}
});
});
});
},
/**
Shows dapp submenu
@event mouseenter .sidebar-menu > li
*/
'mouseenter .sidebar-menu > li': function (e, template) {
var $this = $(e.currentTarget);
var tabTopOffset = $this.offset().top;
var $submenuContainer = $this.find('.submenu-container');
var $submenu = $this.find('.sub-menu');
var submenuHeaderHeight = $this.find('header').outerHeight();
var windowHeight = $(window).outerHeight();
$submenuContainer.css('top', tabTopOffset + 'px');
$submenu.css('max-height', (windowHeight - tabTopOffset - submenuHeaderHeight - 30) + 'px');
},
});