jquery.contactsmenu.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 LIST = ''
  9. + '<div class="menu popovermenu menu-left hidden contactsmenu-popover">'
  10. + ' <ul>'
  11. + ' <li>'
  12. + ' <a>'
  13. + ' <span class="icon-loading-small"></span>'
  14. + ' </a>'
  15. + ' </li>'
  16. + ' </ul>'
  17. + '</div>';
  18. $.fn.contactsMenu = function(shareWith, shareType, appendTo) {
  19. // 0 - user, 4 - email, 6 - remote
  20. var allowedTypes = [0, 4, 6];
  21. if (allowedTypes.indexOf(shareType) === -1) {
  22. return;
  23. }
  24. var $div = this;
  25. appendTo.append(LIST);
  26. var $list = appendTo.find('div.contactsmenu-popover');
  27. $div.click(function() {
  28. if (!$list.hasClass('hidden')) {
  29. $list.addClass('hidden');
  30. $list.hide();
  31. return;
  32. }
  33. $list.removeClass('hidden');
  34. $list.show();
  35. if ($list.hasClass('loaded')) {
  36. return;
  37. }
  38. $list.addClass('loaded');
  39. $.ajax(OC.generateUrl('/contactsmenu/findOne'), {
  40. method: 'POST',
  41. data: {
  42. shareType: shareType,
  43. shareWith: shareWith
  44. }
  45. }).then(function(data) {
  46. $list.find('ul').find('li').addClass('hidden');
  47. var actions;
  48. if (!data.topAction) {
  49. actions = [{
  50. hyperlink: '#',
  51. title: t('core', 'No action available')
  52. }];
  53. } else {
  54. actions = [data.topAction].concat(data.actions);
  55. }
  56. actions.forEach(function(action) {
  57. var template = OC.ContactsMenu.Templates['jquery_entry'];
  58. $list.find('ul').append(template(action));
  59. });
  60. if (actions.length === 0) {
  61. }
  62. }, function(jqXHR) {
  63. $list.find('ul').find('li').addClass('hidden');
  64. var title;
  65. if (jqXHR.status === 404) {
  66. title = t('core', 'No action available');
  67. } else {
  68. title = t('core', 'Error fetching contact actions');
  69. }
  70. var template = OC.ContactsMenu.Templates['jquery_entry'];
  71. $list.find('ul').append(template({
  72. hyperlink: '#',
  73. title: title
  74. }));
  75. });
  76. });
  77. $(document).click(function(event) {
  78. var clickedList = ($list.has(event.target).length > 0);
  79. var clickedTarget = ($div.has(event.target).length > 0);
  80. $div.each(function() {
  81. if ($(this).is(event.target)) {
  82. clickedTarget = true;
  83. }
  84. });
  85. if (clickedList || clickedTarget) {
  86. return;
  87. }
  88. $list.addClass('hidden');
  89. $list.hide();
  90. });
  91. };
  92. }(jQuery));