jquery.contactsmenu.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. }, function(jqXHR) {
  70. $list.find('ul').find('li').addClass('hidden');
  71. var title;
  72. if (jqXHR.status === 404) {
  73. title = t('core', 'No action available');
  74. } else {
  75. title = t('core', 'Error fetching contact actions');
  76. }
  77. var template = Handlebars.compile(ENTRY);
  78. $list.find('ul').append(template({
  79. hyperlink: '#',
  80. title: title
  81. }));
  82. });
  83. });
  84. $(document).click(function(event) {
  85. var clickedList = ($list.has(event.target).length > 0);
  86. var clickedTarget = ($div.has(event.target).length > 0);
  87. $div.each(function() {
  88. if ($(this).is(event.target)) {
  89. clickedTarget = true;
  90. }
  91. });
  92. if (clickedList || clickedTarget) {
  93. return;
  94. }
  95. $list.addClass('hidden');
  96. $list.hide();
  97. });
  98. };
  99. }(jQuery));