/
ui-grid-menu.js
256 lines (212 loc) · 7.96 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
(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',
function ($compile, $timeout, $window, $document, gridUtil, uiGridConstants) {
var uiGridMenu = {
priority: 0,
scope: {
// shown: '&',
menuItems: '=',
autoHide: '=?'
},
require: '?^uiGrid',
templateUrl: 'ui-grid/uiGridMenu',
replace: false,
link: function ($scope, $elm, $attrs, uiGridCtrl) {
var self = this;
var menuMid;
var $animate;
// *** Show/Hide functions ******
self.showMenu = $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;
$timeout( 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);
// 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);
});
};
self.hideMenu = $scope.hideMenu = function(event, args) {
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);
};
$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();
});
}
};
if (typeof($scope.autoHide) === 'undefined' || $scope.autoHide === undefined) {
$scope.autoHide = true;
}
if ($scope.autoHide) {
angular.element($window).on('resize', applyHideMenu);
}
$scope.$on('$destroy', function () {
angular.element(document).off('click touchstart', applyHideMenu);
});
$scope.$on('$destroy', function() {
angular.element($window).off('resize', applyHideMenu);
});
if (uiGridCtrl) {
$scope.$on('$destroy', uiGridCtrl.grid.api.core.on.scrollBegin($scope, applyHideMenu ));
}
$scope.$on('$destroy', $scope.$on(uiGridConstants.events.ITEM_DRAGGING, applyHideMenu ));
},
controller: ['$scope', '$element', '$attrs', function ($scope, $element, $attrs) {
var self = this;
}]
};
return uiGridMenu;
}])
.directive('uiGridMenuItem', ['gridUtil', '$compile', 'i18nService', function (gridUtil, $compile, i18nService) {
var uiGridMenuItem = {
priority: 0,
scope: {
name: '=',
active: '=',
action: '=',
icon: '=',
shown: '=',
context: '=',
templateUrl: '=',
leaveOpen: '='
},
require: ['?^uiGrid', '^uiGridMenu'],
templateUrl: 'ui-grid/uiGridMenuItem',
replace: true,
compile: function($elm, $attrs) {
return {
pre: function ($scope, $elm, $attrs, controllers) {
var uiGridCtrl = controllers[0],
uiGridMenuCtrl = controllers[1];
if ($scope.templateUrl) {
gridUtil.getTemplate($scope.templateUrl)
.then(function (contents) {
var template = angular.element(contents);
var newElm = $compile(template)($scope);
$elm.replaceWith(newElm);
});
}
},
post: function ($scope, $elm, $attrs, controllers) {
var uiGridCtrl = controllers[0],
uiGridMenuCtrl = controllers[1];
// 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) {
// gridUtil.logDebug('itemAction');
$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');
}
}
};
$scope.i18n = i18nService.get();
}
};
}
};
return uiGridMenuItem;
}]);
})();