-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathui-grid-menu.js
322 lines (275 loc) · 10.7 KB
/
ui-grid-menu.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
(function(){
/**
* @ngdoc directive
* @name ui.grid.directive:uiGridMenu
* @element style
* @restrict A
*
* @description
* Allows us to interpolate expressions in `<style>` elements. Angular doesn't do this by default as it can/will/might? break in IE8.
*
* @example
<doc:example module="app">
<doc:source>
<script>
var app = angular.module('app', ['ui.grid']);
app.controller('MainCtrl', ['$scope', function ($scope) {
}]);
</script>
<div ng-controller="MainCtrl">
<div ui-grid-menu shown="true" ></div>
</div>
</doc:source>
<doc:scenario>
</doc:scenario>
</doc:example>
*/
angular.module('ui.grid')
.directive('uiGridMenu', ['$compile', '$timeout', '$window', '$document', 'gridUtil', 'uiGridConstants', 'i18nService',
function ($compile, $timeout, $window, $document, gridUtil, uiGridConstants, i18nService) {
var uiGridMenu = {
priority: 0,
scope: {
// shown: '&',
menuItems: '=',
autoHide: '=?'
},
require: '?^uiGrid',
templateUrl: 'ui-grid/uiGridMenu',
replace: false,
link: function ($scope, $elm, $attrs, uiGridCtrl) {
$scope.dynamicStyles = '';
if (uiGridCtrl && uiGridCtrl.grid && uiGridCtrl.grid.options && uiGridCtrl.grid.options.gridMenuTemplate) {
var gridMenuTemplate = uiGridCtrl.grid.options.gridMenuTemplate;
gridUtil.getTemplate(gridMenuTemplate).then(function (contents) {
var template = angular.element(contents);
var newElm = $compile(template)($scope);
$elm.replaceWith(newElm);
}).catch(angular.noop);
}
var setupHeightStyle = function(gridHeight) {
//menu appears under header row, so substract that height from it's total
// additional 20px for general padding
var gridMenuMaxHeight = gridHeight - uiGridCtrl.grid.headerHeight - 20;
$scope.dynamicStyles = [
'.grid' + uiGridCtrl.grid.id + ' .ui-grid-menu-mid {',
'max-height: ' + gridMenuMaxHeight + 'px;',
'}'
].join(' ');
};
if (uiGridCtrl) {
setupHeightStyle(uiGridCtrl.grid.gridHeight);
uiGridCtrl.grid.api.core.on.gridDimensionChanged($scope, function(oldGridHeight, oldGridWidth, newGridHeight, newGridWidth) {
setupHeightStyle(newGridHeight);
});
}
$scope.i18n = {
close: i18nService.getSafeText('columnMenu.close')
};
// *** Show/Hide functions ******
$scope.showMenu = function(event, args) {
if ( !$scope.shown ){
/*
* In order to animate cleanly we remove the ng-if, wait a digest cycle, then
* animate the removal of the ng-hide. We can't successfully (so far as I can tell)
* animate removal of the ng-if, as the menu items aren't there yet. And we don't want
* to rely on ng-show only, as that leaves elements in the DOM that are needlessly evaluated
* on scroll events.
*
* Note when testing animation that animations don't run on the tutorials. When debugging it looks
* like they do, but angular has a default $animate provider that is just a stub, and that's what's
* being called. ALso don't be fooled by the fact that your browser has actually loaded the
* angular-translate.js, it's not using it. You need to test animations in an external application.
*/
$scope.shown = true;
$scope.$applyAsync(function() {
$scope.shownMid = true;
$scope.$emit('menu-shown');
});
} else if ( !$scope.shownMid ) {
// we're probably doing a hide then show, so we don't need to wait for ng-if
$scope.shownMid = true;
$scope.$emit('menu-shown');
}
var docEventType = 'click';
if (args && args.originalEvent && args.originalEvent.type && args.originalEvent.type === 'touchstart') {
docEventType = args.originalEvent.type;
}
// Turn off an existing document click handler
angular.element(document).off('click touchstart', applyHideMenu);
$elm.off('keyup', checkKeyUp);
$elm.off('keydown', checkKeyDown);
// Turn on the document click handler, but in a timeout so it doesn't apply to THIS click if there is one
$timeout(function() {
angular.element(document).on(docEventType, applyHideMenu);
$elm.on('keyup', checkKeyUp);
$elm.on('keydown', checkKeyDown);
});
//automatically set the focus to the first button element in the now open menu.
gridUtil.focus.bySelector($elm, 'button[type=button]', true);
};
$scope.hideMenu = function(event) {
if ( $scope.shown ){
/*
* In order to animate cleanly we animate the addition of ng-hide, then use a $timeout to
* set the ng-if (shown = false) after the animation runs. In theory we can cascade off the
* callback on the addClass method, but it is very unreliable with unit tests for no discernable reason.
*
* The user may have clicked on the menu again whilst
* we're waiting, so we check that the mid isn't shown before applying the ng-if.
*/
$scope.shownMid = false;
$timeout( function() {
if ( !$scope.shownMid ){
$scope.shown = false;
$scope.$emit('menu-hidden');
}
}, 200);
}
angular.element(document).off('click touchstart', applyHideMenu);
$elm.off('keyup', checkKeyUp);
$elm.off('keydown', checkKeyDown);
};
$scope.$on('hide-menu', function (event, args) {
$scope.hideMenu(event, args);
});
$scope.$on('show-menu', function (event, args) {
$scope.showMenu(event, args);
});
// *** Auto hide when click elsewhere ******
var applyHideMenu = function(){
if ($scope.shown) {
$scope.$apply(function () {
$scope.hideMenu();
});
}
};
// close menu on ESC and keep tab cyclical
var checkKeyUp = function(event) {
if (event.keyCode === 27) {
$scope.hideMenu();
}
};
var checkKeyDown = function(event) {
var setFocus = function(elm) {
elm.focus();
event.preventDefault();
return false;
};
if (event.keyCode === 9) {
var firstMenuItem, lastMenuItem;
var menuItemButtons = $elm[0].querySelectorAll('button:not(.ng-hide)');
if (menuItemButtons.length > 0) {
firstMenuItem = menuItemButtons[0];
lastMenuItem = menuItemButtons[menuItemButtons.length - 1];
if (event.target === lastMenuItem && !event.shiftKey) {
setFocus(firstMenuItem);
} else if (event.target === firstMenuItem && event.shiftKey) {
setFocus(lastMenuItem);
}
}
}
};
if (typeof($scope.autoHide) === 'undefined' || $scope.autoHide === undefined) {
$scope.autoHide = true;
}
if ($scope.autoHide) {
angular.element($window).on('resize', applyHideMenu);
}
$scope.$on('$destroy', function unbindEvents() {
angular.element($window).off('resize', applyHideMenu);
angular.element(document).off('click touchstart', applyHideMenu);
$elm.off('keyup', checkKeyUp);
$elm.off('keydown', checkKeyDown);
});
if (uiGridCtrl) {
$scope.$on('$destroy', uiGridCtrl.grid.api.core.on.scrollBegin($scope, applyHideMenu ));
}
$scope.$on('$destroy', $scope.$on(uiGridConstants.events.ITEM_DRAGGING, applyHideMenu ));
}
};
return uiGridMenu;
}])
.directive('uiGridMenuItem', ['gridUtil', '$compile', 'i18nService', function (gridUtil, $compile, i18nService) {
var uiGridMenuItem = {
priority: 0,
scope: {
name: '=',
active: '=',
action: '=',
icon: '=',
shown: '=',
context: '=',
templateUrl: '=',
leaveOpen: '=',
screenReaderOnly: '='
},
require: ['?^uiGrid'],
templateUrl: 'ui-grid/uiGridMenuItem',
replace: false,
compile: function() {
return {
pre: function ($scope, $elm) {
if ($scope.templateUrl) {
gridUtil.getTemplate($scope.templateUrl)
.then(function (contents) {
var template = angular.element(contents);
var newElm = $compile(template)($scope);
$elm.replaceWith(newElm);
}).catch(angular.noop);
}
},
post: function ($scope, $elm, $attrs, controllers) {
var uiGridCtrl = controllers[0];
// TODO(c0bra): validate that shown and active are functions if they're defined. An exception is already thrown above this though
// if (typeof($scope.shown) !== 'undefined' && $scope.shown && typeof($scope.shown) !== 'function') {
// throw new TypeError("$scope.shown is defined but not a function");
// }
if (typeof($scope.shown) === 'undefined' || $scope.shown === null) {
$scope.shown = function() { return true; };
}
$scope.itemShown = function () {
var context = {};
if ($scope.context) {
context.context = $scope.context;
}
if (typeof(uiGridCtrl) !== 'undefined' && uiGridCtrl) {
context.grid = uiGridCtrl.grid;
}
return $scope.shown.call(context);
};
$scope.itemAction = function($event,title) {
$event.stopPropagation();
if (typeof($scope.action) === 'function') {
var context = {};
if ($scope.context) {
context.context = $scope.context;
}
// Add the grid to the function call context if the uiGrid controller is present
if (typeof(uiGridCtrl) !== 'undefined' && uiGridCtrl) {
context.grid = uiGridCtrl.grid;
}
$scope.action.call(context, $event, title);
if ( !$scope.leaveOpen ){
$scope.$emit('hide-menu');
} else {
// Maintain focus on the selected item
gridUtil.focus.bySelector(angular.element($event.target.parentElement), 'button[type=button]', true);
}
}
};
$scope.label = function(){
var toBeDisplayed = $scope.name;
if (typeof($scope.name) === 'function'){
toBeDisplayed = $scope.name.call();
}
return toBeDisplayed;
};
$scope.i18n = i18nService.get();
}
};
}
};
return uiGridMenuItem;
}]);
})();