/
slick.contextmenu.js
696 lines (612 loc) · 26.9 KB
/
slick.contextmenu.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
(function ($) {
// register namespace
$.extend(true, window, {
"Slick": {
"Plugins": {
"ContextMenu": ContextMenu
}
}
});
/***
* A plugin to add Context Menu (mouse right+click), it subscribes to the cell "onContextMenu" event.
* The "contextMenu" is defined in the Grid Options object
* You can use it to change a data property (only 1) through a list of Options AND/OR through a list of Commands.
* A good example of a Command would be an Export to CSV, that can be run from anywhere in the grid by doing a mouse right+click
*
* Note:
* There is only 1 list of Options, so typically that would be use for 1 column
* if you plan to use different Options for different columns, then the CellMenu plugin might be better suited.
*
* USAGE:
*
* Add the slick.contextmenu.(js|css) files and register it with the grid.
*
* To specify a menu in a column header, extend the column definition like so:
* var contextMenuPlugin = new Slick.Plugins.ContextMenu(columns, grid, options);
*
* Available grid options, by defining a contextMenu object:
*
* var options = {
* enableCellNavigation: true,
* contextMenu: {
* optionTitle: 'Change Priority',
* optionShownOverColumnIds: ["priority"],
* optionItems: [
* { option: 0, title: 'none', cssClass: 'italic' },
* { divider: true },
* "divider" // just the string is also accepted
* { option: 1, iconCssClass: 'fa fa-fire grey', title: 'Low' },
* { option: 3, iconCssClass: 'fa fa-fire red', title: 'High' },
* { option: 2, iconCssClass: 'fa fa-fire orange', title: 'Medium' },
* { option: 4, iconCssClass: 'fa fa-fire', title: 'Extreme', disabled: true },
* ],
* commandTitle: 'Commands',
* commandShownOverColumnIds: ["title", "complete", "start", "finish", "effortDriven"],
* commandItems: [
* { command: 'export-excel', title: 'Export to CSV', iconCssClass: 'fa fa-file-excel-o', cssClass: '' },
* { command: 'delete-row', title: 'Delete Row', cssClass: 'bold', textCssClass: 'red' },
* { command: 'help', title: 'Help', iconCssClass: 'fa fa-question-circle',},
* { divider: true },
* ],
* }
* };
*
*
* Available contextMenu properties:
* commandTitle: Title of the Command section (optional)
* commandItems: Array of Command item objects (command/title pair)
* commandShownOverColumnIds: Define which column to show the Commands list. If not defined (defaults), the menu will be shown over all columns
* optionTitle: Title of the Option section (optional)
* optionItems: Array of Options item objects (option/title pair)
* optionShownOverColumnIds: Define which column to show the Options list. If not defined (defaults), the menu will be shown over all columns
* hideCloseButton: Hide the Close button on top right (defaults to false)
* hideCommandSection: Hide the Commands section even when the commandItems array is filled (defaults to false)
* hideMenuOnScroll: Do we want to hide the Cell Menu when a scrolling event occurs (defaults to false)?
* hideOptionSection: Hide the Options section even when the optionItems array is filled (defaults to false)
* maxHeight: Maximum height that the drop menu will have, can be a number (250) or text ("none")
* width: Width that the drop menu will have, can be a number (250) or text (defaults to "auto")
* autoAdjustDrop: Auto-align dropup or dropdown menu to the left or right depending on grid viewport available space (defaults to true)
* autoAdjustDropOffset: Optionally add an offset to the auto-align of the drop menu (defaults to -4)
* autoAlignSide: Auto-align drop menu to the left or right depending on grid viewport available space (defaults to true)
* autoAlignSideOffset: Optionally add an offset to the left/right side auto-align (defaults to 0)
* menuUsabilityOverride: Callback method that user can override the default behavior of enabling/disabling the menu from being usable (must be combined with a custom formatter)
*
*
* Available menu Command/Option item properties:
* action: Optionally define a callback function that gets executed when item is chosen (and/or use the onCommand event)
* command: A command identifier to be passed to the onCommand event handlers (when using "commandItems").
* option: An option to be passed to the onOptionSelected event handlers (when using "optionItems").
* title: Menu item text.
* divider: Boolean which tell if the current item is a divider, not an actual command. You could also pass "divider" instead of an object
* disabled: Whether the item/command is disabled.
* hidden: Whether the item/command is hidden.
* tooltip: Item tooltip.
* cssClass: A CSS class to be added to the menu item container.
* iconCssClass: A CSS class to be added to the menu item icon.
* textCssClass: A CSS class to be added to the menu item text.
* iconImage: A url to the icon image.
* itemVisibilityOverride: Callback method that user can override the default behavior of showing/hiding an item from the list
* itemUsabilityOverride: Callback method that user can override the default behavior of enabling/disabling an item from the list
*
* The plugin exposes the following events:
*
* onAfterMenuShow: Fired after the menu is shown. You can customize the menu or dismiss it by returning false.
* Event args:
* cell: Cell or column index
* row: Row index
* grid: Reference to the grid.
*
* onBeforeMenuShow: Fired before the menu is shown. You can customize the menu or dismiss it by returning false.
* Event args:
* cell: Cell or column index
* row: Row index
* grid: Reference to the grid.
*
* onBeforeMenuClose: Fired when the menu is closing.
* Event args:
* cell: Cell or column index
* row: Row index
* grid: Reference to the grid.
* menu: Menu DOM element
*
* onCommand: Fired on menu option clicked from the Command items list
* Event args:
* cell: Cell or column index
* row: Row index
* grid: Reference to the grid.
* command: Menu command identified.
* item: Menu item selected
* column: Cell Column definition
* dataContext: Cell Data Context (data object)
* value: Value of the cell we triggered the context menu from
*
* onOptionSelected: Fired on menu option clicked from the Option items list
* Event args:
* cell: Cell or column index
* row: Row index
* grid: Reference to the grid.
* option: Menu option selected.
* item: Menu item selected
* column: Cell Column definition
* dataContext: Cell Data Context (data object)
*
*
* @param options {Object} Context Menu Options
* @class Slick.Plugins.ContextMenu
* @constructor
*/
function ContextMenu(optionProperties) {
var _contextMenuProperties;
var _currentCell = -1;
var _currentRow = -1;
var _grid;
var _gridOptions;
var _gridUid = "";
var _handler = new Slick.EventHandler();
var _self = this;
var $optionTitleElm;
var $commandTitleElm;
var $menu;
var _defaults = {
autoAdjustDrop: true, // dropup/dropdown
autoAlignSide: true, // left/right
autoAdjustDropOffset: -4,
autoAlignSideOffset: 0,
hideMenuOnScroll: false,
maxHeight: "none",
width: "auto",
optionShownOverColumnIds: [],
commandShownOverColumnIds: [],
};
function init(grid) {
_grid = grid;
_gridOptions = grid.getOptions();
_contextMenuProperties = $.extend({}, _defaults, optionProperties);
_gridUid = (grid && grid.getUID) ? grid.getUID() : "";
_handler.subscribe(_grid.onContextMenu, handleOnContextMenu);
if (_contextMenuProperties.hideMenuOnScroll) {
_handler.subscribe(_grid.onScroll, destroyMenu);
}
}
function setOptions(newOptions) {
_contextMenuProperties = $.extend({}, _contextMenuProperties, newOptions);
// on the array properties, we want to make sure to overwrite them and not just extending them
if (newOptions.commandShownOverColumnIds) {
_contextMenuProperties.commandShownOverColumnIds = newOptions.commandShownOverColumnIds;
}
if (newOptions.optionShownOverColumnIds) {
_contextMenuProperties.optionShownOverColumnIds = newOptions.optionShownOverColumnIds;
}
}
function destroy() {
_self.onAfterMenuShow.unsubscribe();
_self.onBeforeMenuShow.unsubscribe();
_self.onBeforeMenuClose.unsubscribe();
_self.onCommand.unsubscribe();
_self.onOptionSelected.unsubscribe();
_handler.unsubscribeAll();
if ($menu && $menu.remove) {
$menu.remove();
}
$commandTitleElm = null;
$optionTitleElm = null;
$menu = null;
}
function createMenu(e) {
var cell = _grid.getCellFromEvent(e);
_currentCell = cell && cell.cell;
_currentRow = cell && cell.row;
var columnDef = _grid.getColumns()[_currentCell];
var dataContext = _grid.getDataItem(_currentRow);
var isColumnOptionAllowed = checkIsColumnAllowed(_contextMenuProperties.optionShownOverColumnIds, columnDef.id);
var isColumnCommandAllowed = checkIsColumnAllowed(_contextMenuProperties.commandShownOverColumnIds, columnDef.id);
var commandItems = _contextMenuProperties.commandItems || [];
var optionItems = _contextMenuProperties.optionItems || [];
// make sure there's at least something to show before creating the Context Menu
if (!columnDef || (!isColumnCommandAllowed && !isColumnOptionAllowed) || (!commandItems.length && optionItems.length)) {
return;
}
// delete any prior context menu
destroyMenu(e);
// Let the user modify the menu or cancel altogether,
// or provide alternative menu implementation.
if (_self.onBeforeMenuShow.notify({
"cell": _currentCell,
"row": _currentRow,
"grid": _grid
}, e, _self) == false) {
return;
}
// create a new context menu
var maxHeight = isNaN(_contextMenuProperties.maxHeight) ? _contextMenuProperties.maxHeight : _contextMenuProperties.maxHeight + "px";
var width = isNaN(_contextMenuProperties.width) ? _contextMenuProperties.width : _contextMenuProperties.width + "px";
var menuStyle = "width: " + width + "; max-height: " + maxHeight;
var menu = $('<div class="slick-context-menu ' + _gridUid + '" style="' + menuStyle + '" />')
.css("top", e.pageY)
.css("left", e.pageX)
.css("display", "none");
var closeButtonHtml = '<button type="button" class="close" data-dismiss="slick-context-menu" aria-label="Close">'
+ '<span class="close" aria-hidden="true">×</span></button>';
// -- Option List section
if (!_contextMenuProperties.hideOptionSection && isColumnOptionAllowed && optionItems.length > 0) {
var $optionMenu = $('<div class="slick-context-menu-option-list" />');
if (!_contextMenuProperties.hideCloseButton) {
$(closeButtonHtml).on("click", destroyMenu).appendTo(menu);
}
$optionMenu.appendTo(menu);
populateOptionItems(
_contextMenuProperties,
$optionMenu,
optionItems,
{ cell: _currentCell, row: _currentRow, column: columnDef, dataContext: dataContext, grid: _grid }
);
}
// -- Command List section
if (!_contextMenuProperties.hideCommandSection && isColumnCommandAllowed && commandItems.length > 0) {
var $commandMenu = $('<div class="slick-context-menu-command-list" />');
if (!_contextMenuProperties.hideCloseButton && (!isColumnOptionAllowed || optionItems.length === 0 || _contextMenuProperties.hideOptionSection)) {
$(closeButtonHtml).on("click", destroyMenu).appendTo(menu);
}
$commandMenu.appendTo(menu);
populateCommandItems(
_contextMenuProperties,
$commandMenu,
commandItems,
{ cell: _currentCell, row: _currentRow, column: columnDef, dataContext: dataContext, grid: _grid }
);
}
menu.show();
menu.appendTo("body");
if (_self.onAfterMenuShow.notify({
"cell": _currentCell,
"row": _currentRow,
"grid": _grid
}, e, _self) == false) {
return;
}
return menu;
}
function destroyMenu(e, args) {
$menu = $menu || $(".slick-context-menu." + _gridUid);
if ($menu && $menu.length > 0) {
if (_self.onBeforeMenuClose.notify({
"cell": args && args.cell,
"row": args && args.row,
"grid": _grid,
"menu": $menu
}, e, _self) == false) {
return;
}
if ($menu && $menu.remove) {
$menu.remove();
$menu = null;
}
}
}
function checkIsColumnAllowed(columnIds, columnId) {
var isAllowedColumn = false;
if (columnIds && columnIds.length > 0) {
for (var o = 0, ln = columnIds.length; o < ln; o++) {
if (columnIds[o] === columnId) {
isAllowedColumn = true;
}
}
} else {
isAllowedColumn = true;
}
return isAllowedColumn;
}
function calculateAvailableSpaceBottom(element) {
var windowHeight = $(window).innerHeight() || 0;
var pageScroll = $(window).scrollTop() || 0;
if (element && element.offset && element.length > 0) {
var elementOffsetTop = element.offset().top;
return windowHeight - (elementOffsetTop - pageScroll);
}
return 0;
}
function calculateAvailableSpaceTop(element) {
var pageScroll = $(window).scrollTop() || 0;
if (element && element.offset && element.length > 0) {
var elementOffsetTop = element.offset().top;
return elementOffsetTop - pageScroll;
}
return 0;
}
function handleOnContextMenu(e, args) {
e.preventDefault();
var cell = _grid.getCellFromEvent(e);
var columnDef = _grid.getColumns()[cell.cell];
var dataContext = _grid.getDataItem(cell.row);
// run the override function (when defined), if the result is false it won't go further
if (!args) {
args = {};
}
args.cell = cell.cell;
args.row = cell.row;
args.columnDef = columnDef;
args.dataContext = dataContext;
args.grid = _grid;
if (!runOverrideFunctionWhenExists(_contextMenuProperties.menuUsabilityOverride, args)) {
return;
}
// create the DOM element
$menu = createMenu(e, args);
// reposition the menu to where the user clicked
if ($menu) {
repositionMenu(e);
$menu
.data("cell", _currentCell)
.data("row", _currentRow)
.show();
}
$("body").one("click", function (e) {
destroyMenu(e, { cell: _currentCell, row: _currentRow });
});
}
/** Construct the Option Items section. */
function populateOptionItems(contextMenu, optionMenuElm, optionItems, args) {
if (!args || !optionItems || !contextMenu) {
return;
}
// user could pass a title on top of the Options section
if (contextMenu && contextMenu.optionTitle) {
$optionTitleElm = $('<div class="title"/>').append(contextMenu.optionTitle);
$optionTitleElm.appendTo(optionMenuElm);
}
for (var i = 0, ln = optionItems.length; i < ln; i++) {
var item = optionItems[i];
// run each override functions to know if the item is visible and usable
var isItemVisible = runOverrideFunctionWhenExists(item.itemVisibilityOverride, args);
var isItemUsable = runOverrideFunctionWhenExists(item.itemUsabilityOverride, args);
// if the result is not visible then there's no need to go further
if (!isItemVisible) {
continue;
}
// when the override is defined, we need to use its result to update the disabled property
// so that "handleMenuItemOptionClick" has the correct flag and won't trigger an option clicked event
if (Object.prototype.hasOwnProperty.call(item, "itemUsabilityOverride")) {
item.disabled = isItemUsable ? false : true;
}
var $li = $('<div class="slick-context-menu-item"></div>')
.data("option", item.option || "")
.data("item", item)
.on("click", handleMenuItemOptionClick)
.appendTo(optionMenuElm);
if (item.divider || item === "divider") {
$li.addClass("slick-context-menu-item-divider");
continue;
}
// if the item is disabled then add the disabled css class
if (item.disabled || !isItemUsable) {
$li.addClass("slick-context-menu-item-disabled");
}
// if the item is hidden then add the hidden css class
if (item.hidden) {
$li.addClass("slick-context-menu-item-hidden");
}
if (item.cssClass) {
$li.addClass(item.cssClass);
}
if (item.tooltip) {
$li.attr("title", item.tooltip);
}
var $icon = $('<div class="slick-context-menu-icon"></div>')
.appendTo($li);
if (item.iconCssClass) {
$icon.addClass(item.iconCssClass);
}
if (item.iconImage) {
$icon.css("background-image", "url(" + item.iconImage + ")");
}
var $text = $('<span class="slick-context-menu-content"></span>')
.text(item.title)
.appendTo($li);
if (item.textCssClass) {
$text.addClass(item.textCssClass);
}
}
}
/** Construct the Command Items section. */
function populateCommandItems(contextMenu, commandMenuElm, commandItems, args) {
if (!args || !commandItems || !contextMenu) {
return;
}
// user could pass a title on top of the Commands section
if (contextMenu && contextMenu.commandTitle) {
$commandTitleElm = $('<div class="title"/>').append(contextMenu.commandTitle);
$commandTitleElm.appendTo(commandMenuElm);
}
for (var i = 0, ln = commandItems.length; i < ln; i++) {
var item = commandItems[i];
// run each override functions to know if the item is visible and usable
var isItemVisible = runOverrideFunctionWhenExists(item.itemVisibilityOverride, args);
var isItemUsable = runOverrideFunctionWhenExists(item.itemUsabilityOverride, args);
// if the result is not visible then there's no need to go further
if (!isItemVisible) {
continue;
}
// when the override is defined, we need to use its result to update the disabled property
// so that "handleMenuItemCommandClick" has the correct flag and won't trigger a command clicked event
if (Object.prototype.hasOwnProperty.call(item, "itemUsabilityOverride")) {
item.disabled = isItemUsable ? false : true;
}
var $li = $('<div class="slick-context-menu-item"></div>')
.data("command", item.command || "")
.data("item", item)
.on("click", handleMenuItemCommandClick)
.appendTo(commandMenuElm);
if (item.divider || item === "divider") {
$li.addClass("slick-context-menu-item-divider");
continue;
}
// if the item is disabled then add the disabled css class
if (item.disabled || !isItemUsable) {
$li.addClass("slick-context-menu-item-disabled");
}
// if the item is hidden then add the hidden css class
if (item.hidden) {
$li.addClass("slick-context-menu-item-hidden");
}
if (item.cssClass) {
$li.addClass(item.cssClass);
}
if (item.tooltip) {
$li.attr("title", item.tooltip);
}
var $icon = $('<div class="slick-context-menu-icon"></div>')
.appendTo($li);
if (item.iconCssClass) {
$icon.addClass(item.iconCssClass);
}
if (item.iconImage) {
$icon.css("background-image", "url(" + item.iconImage + ")");
}
var $text = $('<span class="slick-context-menu-content"></span>')
.text(item.title)
.appendTo($li);
if (item.textCssClass) {
$text.addClass(item.textCssClass);
}
}
}
function handleMenuItemCommandClick(e) {
var command = $(this).data("command");
var item = $(this).data("item");
if (!item || item.disabled || item.divider) {
return;
}
var row = $menu.data("row");
var cell = $menu.data("cell");
var columnDef = _grid.getColumns()[cell];
var dataContext = _grid.getDataItem(row);
var cellValue;
if (Object.prototype.hasOwnProperty.call(dataContext, columnDef && columnDef.field)) {
cellValue = dataContext[columnDef.field];
}
if (command != null && command !== "") {
// user could execute a callback through 2 ways
// via the onCommand event and/or an action callback
var callbackArgs = {
"cell": cell,
"row": row,
"grid": _grid,
"command": command,
"item": item,
"column": columnDef,
"dataContext": dataContext,
"value": cellValue
};
_self.onCommand.notify(callbackArgs, e, _self);
// execute action callback when defined
if (typeof item.action === "function") {
item.action.call(this, e, callbackArgs);
}
}
}
function handleMenuItemOptionClick(e) {
var option = $(this).data("option");
var item = $(this).data("item");
if (item.disabled || item.divider) {
return;
}
if (!_grid.getEditorLock().commitCurrentEdit()) {
return;
}
var row = $menu.data("row");
var cell = $menu.data("cell");
var columnDef = _grid.getColumns()[cell];
var dataContext = _grid.getDataItem(row);
if (option !== undefined) {
// user could execute a callback through 2 ways
// via the onOptionSelected event and/or an action callback
var callbackArgs = {
"cell": cell,
"row": row,
"grid": _grid,
"option": option,
"item": item,
"column": columnDef,
"dataContext": dataContext
};
_self.onOptionSelected.notify(callbackArgs, e, _self);
// execute action callback when defined
if (typeof item.action === "function") {
item.action.call(this, e, callbackArgs);
}
}
}
/**
* Reposition the menu drop (up/down) and the side (left/right)
* @param {*} event
*/
function repositionMenu(e) {
var $parent = $(e.target).closest(".slick-cell");
var menuOffsetLeft = e.pageX;
var menuOffsetTop = $parent ? $parent.offset().top : e.pageY;
var menuHeight = $menu.outerHeight() || 0;
var menuWidth = $menu.outerWidth() || _contextMenuProperties.width || 0;
var rowHeight = _gridOptions.rowHeight;
var dropOffset = _contextMenuProperties.autoAdjustDropOffset;
var sideOffset = _contextMenuProperties.autoAlignSideOffset;
// if autoAdjustDrop is enable, we first need to see what position the drop will be located
// without necessary toggling it's position just yet, we just want to know the future position for calculation
if (_contextMenuProperties.autoAdjustDrop) {
// since we reposition menu below slick cell, we need to take it in consideration and do our calculation from that element
var spaceBottom = calculateAvailableSpaceBottom($parent);
var spaceTop = calculateAvailableSpaceTop($parent);
var spaceBottomRemaining = spaceBottom + dropOffset - rowHeight;
var spaceTopRemaining = spaceTop - dropOffset + rowHeight;
var dropPosition = (spaceBottomRemaining < menuHeight && spaceTopRemaining > spaceBottomRemaining) ? 'top' : 'bottom';
if (dropPosition === 'top') {
$menu.removeClass("dropdown").addClass("dropup");
menuOffsetTop = menuOffsetTop - menuHeight - dropOffset;
} else {
$menu.removeClass("dropup").addClass("dropdown");
menuOffsetTop = menuOffsetTop + rowHeight + dropOffset;
}
}
// when auto-align is set, it will calculate whether it has enough space in the viewport to show the drop menu on the right (default)
// if there isn't enough space on the right, it will automatically align the drop menu to the left
// to simulate an align left, we actually need to know the width of the drop menu
if (_contextMenuProperties.autoAlignSide) {
var gridPos = _grid.getGridPosition();
var dropSide = ((menuOffsetLeft + menuWidth) >= gridPos.width) ? 'left' : 'right';
if (dropSide === 'left') {
$menu.removeClass("dropright").addClass("dropleft");
menuOffsetLeft = (menuOffsetLeft - menuWidth - sideOffset);
} else {
$menu.removeClass("dropleft").addClass("dropright");
menuOffsetLeft = menuOffsetLeft + sideOffset;
}
}
// ready to reposition the menu
$menu.css("top", menuOffsetTop);
$menu.css("left", menuOffsetLeft);
}
/**
* Method that user can pass to override the default behavior.
* In order word, user can choose or an item is (usable/visible/enable) by providing his own logic.
* @param overrideFn: override function callback
* @param args: multiple arguments provided to the override (cell, row, columnDef, dataContext, grid)
*/
function runOverrideFunctionWhenExists(overrideFn, args) {
if (typeof overrideFn === 'function') {
return overrideFn.call(this, args);
}
return true;
}
$.extend(this, {
"init": init,
"closeMenu": destroyMenu,
"destroy": destroy,
"pluginName": "ContextMenu",
"setOptions": setOptions,
"onAfterMenuShow": new Slick.Event(),
"onBeforeMenuShow": new Slick.Event(),
"onBeforeMenuClose": new Slick.Event(),
"onCommand": new Slick.Event(),
"onOptionSelected": new Slick.Event()
});
}
})(jQuery);