You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

105 lines
2.2 KiB

  1. define([
  2. 'js/system/events',
  3. 'html!ui/templates/context/template',
  4. 'css!ui/templates/context/styles',
  5. 'html!ui/templates/context/templateItem'
  6. ], function (
  7. events,
  8. template,
  9. styles,
  10. templateItem
  11. ) {
  12. return {
  13. tpl: template,
  14. modal: true,
  15. config: null,
  16. postRender: function () {
  17. this.onEvent('onContextMenu', this.onContextMenu.bind(this));
  18. this.onEvent('onHideContextMenu', this.onMouseDown.bind(this));
  19. this.onEvent('mouseDown', this.onMouseDown.bind(this));
  20. this.onEvent('onUiKeyDown', this.onUiKeyDown.bind(this));
  21. $('.ui-container').on('mouseup', this.onMouseDown.bind(this));
  22. },
  23. onContextMenu: function (config, e) {
  24. this.config = config;
  25. let container = this.el.find('.list')
  26. .empty();
  27. config.forEach((c, i) => {
  28. const text = (c.text || c);
  29. const hotkey = c.hotkey;
  30. const suffix = c.suffix;
  31. const html = templateItem
  32. .replace('$TEXT$', text);
  33. const row = $(html)
  34. .appendTo(container);
  35. if (hotkey)
  36. row.find('.hotkey').html(`(${hotkey})`);
  37. else if (suffix)
  38. row.find('.hotkey').html(`${suffix}`);
  39. if (c.callback) {
  40. row.on('click', this.onClick.bind(this, i, c.callback));
  41. row.on('click', events.emit.bind(events, 'onClickContextItem'));
  42. } else {
  43. row.addClass('no-hover');
  44. if (text.includes('---'))
  45. row.addClass('divider');
  46. }
  47. });
  48. const pos = {
  49. left: e.clientX,
  50. top: e.clientY
  51. };
  52. //Check for a customEvent, like long touch
  53. if (_.isIos()) {
  54. pos.left = e.detail.clientX;
  55. pos.top = e.detail.clientY;
  56. }
  57. pos['max-height'] = window.innerHeight - pos.top - 10;
  58. this.el
  59. .css(pos)
  60. .show();
  61. },
  62. onClick: function (index, callback) {
  63. this.el.hide();
  64. callback();
  65. },
  66. onMouseDown: function (e) {
  67. if (!this.el.is(':visible') || (e && (e.cancel || e.button === 2)))
  68. return;
  69. this.config = null;
  70. this.el.hide();
  71. },
  72. onUiKeyDown: function (keyEvent) {
  73. if (!this.config || !this.el.is(':visible'))
  74. return;
  75. const configEntry = this.config.find(({ hotkey }) => hotkey === keyEvent.key);
  76. if (!configEntry)
  77. return;
  78. configEntry.callback();
  79. keyEvent.consumed = true;
  80. this.el.hide();
  81. }
  82. };
  83. });