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.

104 lines
2.2 KiB

  1. /**
  2. * Copyright (c) 2017 Georg Ehrke <oc.list@georgehrke.com>
  3. * This file is licensed under the Affero General Public License version 3 or
  4. * later.
  5. * See the COPYING-README file.
  6. */
  7. (function ($) {
  8. var ENTRY = ''
  9. + '<li>'
  10. + ' <a href="{{hyperlink}}">'
  11. + ' {{#if icon}}<img src="{{icon}}">{{/if}}'
  12. + ' <span>{{title}}</span>'
  13. + ' </a>'
  14. + '</li>';
  15. var LIST = ''
  16. + '<div class="menu popovermenu bubble hidden contactsmenu-popover">'
  17. + ' <ul>'
  18. + ' <li>'
  19. + ' <a>'
  20. + ' <span class="icon-loading-small"></span>'
  21. + ' </a>'
  22. + ' </li>'
  23. + ' </ul>'
  24. + '</div>';
  25. $.fn.contactsMenu = function(shareWith, shareType, appendTo) {
  26. // 0 - user, 4 - email, 6 - remote
  27. var allowedTypes = [0, 4, 6];
  28. if (allowedTypes.indexOf(shareType) === -1) {
  29. return;
  30. }
  31. var $div = this;
  32. appendTo.append(LIST);
  33. var $list = appendTo.find('div.contactsmenu-popover');
  34. $div.click(function() {
  35. if (!$list.hasClass('hidden')) {
  36. $list.addClass('hidden');
  37. $list.hide();
  38. return;
  39. }
  40. $list.removeClass('hidden');
  41. $list.show();
  42. if ($list.hasClass('loaded')) {
  43. return;
  44. }
  45. $list.addClass('loaded');
  46. $.ajax(OC.generateUrl('/contactsmenu/findOne'), {
  47. method: 'POST',
  48. data: {
  49. shareType: shareType,
  50. shareWith: shareWith
  51. }
  52. }).then(function(data) {
  53. $list.find('ul').find('li').addClass('hidden');
  54. var actions;
  55. if (!data.topAction) {
  56. actions = [{
  57. hyperlink: '#',
  58. title: t('core', 'No action available')
  59. }];
  60. } else {
  61. actions = [data.topAction].concat(data.actions);
  62. }
  63. actions.forEach(function(action) {
  64. var template = Handlebars.compile(ENTRY);
  65. $list.find('ul').append(template(action));
  66. });
  67. if (actions.length === 0) {
  68. }
  69. });
  70. }).catch(function(reason) {
  71. // TODO
  72. });
  73. $(document).click(function(event) {
  74. var clickedList = $.contains($list, event.target);
  75. var clickedTarget = $.contains($div, event.target);
  76. $div.each(function() {
  77. if ($(this).is(event.target)) {
  78. clickedTarget = true;
  79. }
  80. });
  81. if (clickedList || clickedTarget) {
  82. return;
  83. }
  84. $list.addClass('hidden');
  85. $list.hide();
  86. });
  87. };
  88. }(jQuery));