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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
/*!
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
/**
* Usage:
* <div piwik-dialog="showDialog">...</div>
* Will show dialog once showDialog evaluates to true.
*
* Will execute the "executeMyFunction" function in the current scope once the yes button is pressed.
*/
(function () {
angular.module('piwikApp').directive('piwikQuickAccess', QuickAccessDirective);
QuickAccessDirective.$inject = ['$rootElement', '$timeout', 'piwik', '$filter'];
function QuickAccessDirective ($rootElement, $timeout, piwik, $filter) {
return {
restrict: 'A',
replace: true,
scope: {},
templateUrl: 'plugins/CoreHome/angularjs/quick-access/quick-access.directive.html?cb=' + piwik.cacheBuster,
controller: 'QuickAccessController',
controllerAs: 'quickAccess',
link: function (scope, element, attrs) {
var menuIndex = -1; // the menu index is used to identify which element to click
var topMenuItems = []; // cache for top menu items
var leftMenuItems = []; // cache for left menu items
var segmentItems = []; // cache for segment items
var hasSegmentSelector = angular.element('.segmentEditorPanel').length;
scope.hasSitesSelector = angular.element('.top_controls [piwik-siteselector]').length;
var translate = $filter('translate');
var searchAreasTitle = '';
var searchAreas = [translate('CoreHome_MenuEntries')]
if (hasSegmentSelector) {
searchAreas.push(translate('CoreHome_Segments'))
}
if (scope.hasSitesSelector) {
searchAreas.push(translate('SitesManager_Sites'))
}
while (searchAreas.length) {
searchAreasTitle += searchAreas.shift();
if (searchAreas.length >= 2) {
searchAreasTitle += ', ';
} else if (searchAreas.length === 1) {
searchAreasTitle += ' ' + translate('General_And') + ' ';
}
}
scope.quickAccessTitle = translate('CoreHome_QuickAccessTitle', searchAreasTitle);
function trim(str) {
return str.replace(/^\s+|\s+$/g,'');
}
scope.getTopMenuItems = function()
{
if (topMenuItems && topMenuItems.length) {
return topMenuItems;
}
var category = _pk_translate('CoreHome_Menu');
$rootElement.find('#topRightBar .navbar-right li > a').each(function (index, element) {
var $element = $(element);
var text = trim($element.text());
if (!text) {
text = trim($element.attr('title')); // possibly a icon, use title instead
}
if (text) {
topMenuItems.push({name: text, index: ++menuIndex, category: category});
$element.attr('quick_access', menuIndex);
}
});
return topMenuItems;
};
scope.getLeftMenuItems = function ()
{
if (leftMenuItems && leftMenuItems.length) {
return leftMenuItems;
}
$rootElement.find('#secondNavBar .menuTab').each(function (index, element) {
var $element = angular.element(element);
var category = trim($element.find('> .item').text());
if (category && -1 !== category.lastIndexOf("\n")) {
// remove "\n\nMenu"
category = trim(category.substr(0, category.lastIndexOf("\n")));
}
$element.find('li .item').each(function (i, element) {
var $element = angular.element(element);
var text = trim($element.text());
if (text) {
leftMenuItems.push({name: text, category: category, index: ++menuIndex});
$element.attr('quick_access', menuIndex);
}
})
});
return leftMenuItems;
};
scope.getSegmentItems = function()
{
if (!hasSegmentSelector) {
return [];
}
if (segmentItems && segmentItems.length) {
return segmentItems;
}
var category = _pk_translate('CoreHome_Segments');
$rootElement.find('.segmentList [data-idsegment]').each(function (index, element) {
var $element = angular.element(element);
var text = trim($element.find('.segname').text());
if (text) {
segmentItems.push({name: text, category: category, index: ++menuIndex});
$element.attr('quick_access', menuIndex);
}
});
return segmentItems;
};
scope.activateSearch = function()
{
scope.$eval('view.searchActive = true');
$timeout(function () {
scope.$apply();
}, 0);
};
scope.deactivateSearch = function()
{
scope.$eval('search.term = ""');
scope.$eval('view.searchActive = false');
element.find('input').blur();
$timeout(function () {
scope.$apply();
}, 0);
};
function isElementInViewport(element) {
var rect = element.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= $(window).height() &&
rect.right <= $(window).width()
);
}
function getCurrentlySelectedElement(index)
{
var results = element.find('li.result');
if (results && results.length && results[scope.search.index]) {
return $(results[scope.search.index]);
}
}
function makeSureSelectedItemIsInViewport() {
var element = getCurrentlySelectedElement();
if (element && element[0] && !isElementInViewport(element[0])) {
scrollFirstElementIntoView(element);
}
}
function scrollFirstElementIntoView(element)
{
if (element && element[0] && element[0].scrollIntoView) {
// make sure search is visible
element[0].scrollIntoView();
}
}
scope.highlightPreviousItem = function()
{
if (0 >= (scope.search.index - 1)) {
scope.search.index = 0;
} else {
scope.search.index--;
}
makeSureSelectedItemIsInViewport();
};
scope.resetSearchIndex = function () {
scope.search.index = 0;
makeSureSelectedItemIsInViewport();
};
scope.highlightNextItem = function()
{
var numTotal = element.find('li.result').length;
if (numTotal <= (scope.search.index + 1)) {
scope.search.index = numTotal - 1;
} else {
scope.search.index++;
}
makeSureSelectedItemIsInViewport();
};
scope.clickQuickAccessMenuItem = function()
{
var selectedMenuElement = getCurrentlySelectedElement();
if (selectedMenuElement) {
$timeout(function () {
selectedMenuElement.click();
}, 20);
}
};
scope.selectMenuItem = function(index)
{
var target = $rootElement.find('[quick_access=' + index + ']');
if (target && target.length && target[0]) {
scope.deactivateSearch();
var actualTarget = target[0];
var href = $(actualTarget).attr('href');
if (href && href.length > 10 && actualTarget && actualTarget.click) {
try {
actualTarget.click();
} catch (e) {
$(actualTarget).click();
}
} else {
$(actualTarget).click();
}
}
};
Mousetrap.bind('f', function(event) {
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false; // IE
}
scrollFirstElementIntoView(element);
scope.activateSearch();
});
}
};
}
})();
|