aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/help-scout/webview.js
blob: c7f7c94a5a9a4939d2f85605823c6b2624bf7982 (plain) (blame)
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
function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : { default: obj };
}

const _path = _interopRequireDefault(require('path'));

/**
 * Help Scout integration plugin for Ferdium
 *
 * @summary     Integrates Help Scout into the Ferdium application
 * @since       1.2.0
 */

/**
 * Scripts specific to ticket pages
 *
 * @since       1.2.0
 */
const ticketScripts = {
  init() {
    this.processCopy();
  },
  /**
   * Handles clicking the copy link
   *
   * @since       1.2.0
   * @return      {void}
   */
  processCopy() {
    $('#copyLink').on('click', function (e) {
      e.preventDefault();

      copyToClipboard();

      $('.link-copied').fadeIn('fast').css('display', 'block');
    });

    $('#closeLink').on('click', function (e) {
      e.preventDefault();

      $('.link-copied').fadeOut('fast', function () {
        $(this).css('display', 'none');
      });
    });
  },
};

/**
 * The core Ferdium message handler
 *
 * @since       1.0.0
 */
module.exports = Ferdium => {
  Ferdium.injectCSS(_path.default.join(__dirname, 'service.css'));

  /**
   * Get messages for the Ferdium loop
   *
   * @since       1.0.0
   * @return      {void}
   */
  function getMessages() {
    let mine = '';
    let unassigned = '';
    let total = '0';

    /**
     * Since Help Scout loads things asyncronously,
     * we have to trigger everything in the loop for
     * it to get recognized.
     */
    addCopyLink();
    ticketScripts.init();

    if (
      $('.dropdown.mailboxes').length > 0 &&
      $('.dropdown.mailboxes a').hasClass('active')
    ) {
      // Individual tickets
      mine = $('li.mine a .badge').text();
      unassigned = $('li.unassigned a .badge').text();
    } else if (
      window.location.href === 'https://secure.helpscout.net/dashboard/'
    ) {
      // Main dashboard
      mine = 0;
      unassigned = 0;

      $('.card.mailbox .c-list').each(function () {
        const m = $(this).find('a:nth-child(2)').find('.count').text();
        const u = $(this).find('a:first-child').find('.count').text();

        if ($.isNumeric(m)) {
          mine += Number.parseInt(m);
        }

        if ($.isNumeric(u)) {
          unassigned += Number.parseInt(u);
        }
      });

      mine = mine.toString();
      unassigned = unassigned.toString();
    }

    if (mine !== '') {
      total = mine;
    }

    if (unassigned !== '') {
      total = `${total}/${unassigned}`;
    }

    Ferdium.setBadge(total);
  }

  Ferdium.loop(getMessages);
};

/**
 * Add copy link to the conversation toolbar
 *
 * @since       1.2.0
 * @return      {void}
 */
function addCopyLink() {
  if ($('.convo-toolbar').length > 0 && $('#copyLink').length === 0) {
    $('#actions-dd .more').append(
      '<li class="actions-dd"><a id="copyLink" class="actions-dd">Copy Link</a></li>',
    );
    $('.c-convo-toolbar').after(
      '<div class="link-copied" style="display: none">Ticket URL copied to clipboard!<a id="closeLink">x</a></div>',
    );
  }
}

/**
 * Process copying URLs to clipboard
 *
 * @since       1.1.
 * @return      {void}
 */
function copyToClipboard() {
  const targetId = '_hiddenURLField';
  let target = document.querySelector(targetId);

  if (!target) {
    target = document.createElement('textarea');
    target.style.position = 'absolute';
    target.style.left = '-9999px';
    target.style.top = '0';
    target.id = targetId;
    document.body.append(target);
  }

  target.textContent = window.location.href;

  const currentFocus = document.activeElement;

  target.focus();
  target.setSelectionRange(0, target.value.length);

  document.execCommand('copy');

  if (currentFocus && typeof currentFocus.focus === 'function') {
    currentFocus.focus();
  }
}