aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/help-scout/webview.js
diff options
context:
space:
mode:
Diffstat (limited to 'recipes/help-scout/webview.js')
-rw-r--r--recipes/help-scout/webview.js163
1 files changed, 163 insertions, 0 deletions
diff --git a/recipes/help-scout/webview.js b/recipes/help-scout/webview.js
new file mode 100644
index 0000000..bd84dd7
--- /dev/null
+++ b/recipes/help-scout/webview.js
@@ -0,0 +1,163 @@
1const _path = _interopRequireDefault(require('path'));
2
3function _interopRequireDefault(obj) {
4 return obj && obj.__esModule ? obj : { default: obj };
5}
6
7/**
8 * Help Scout integration plugin for Ferdium
9 *
10 * @summary Integrates Help Scout into the Ferdium application
11 * @since 1.2.0
12 */
13
14
15/**
16 * Scripts specific to ticket pages
17 *
18 * @since 1.2.0
19 */
20let ticketScripts = {
21 init : function() {
22 this.processCopy();
23 },
24 /**
25 * Handles clicking the copy link
26 *
27 * @since 1.2.0
28 * @return {void}
29 */
30 processCopy : function() {
31 $('#copyLink').on('click', function(e) {
32 e.preventDefault();
33
34 copyToClipboard();
35
36 $('.link-copied').fadeIn('fast').css('display', 'block');
37 });
38
39 $('#closeLink').on('click', function(e) {
40 e.preventDefault();
41
42 $('.link-copied').fadeOut('fast', function () {
43 $(this).css('display', 'none');
44 });
45 });
46 }
47};
48
49
50/**
51 * The core Ferdium message handler
52 *
53 * @since 1.0.0
54 */
55module.exports = (Ferdium) => {
56 Ferdium.injectCSS(_path.default.join(__dirname, 'service.css'));
57
58 /**
59 * Get messages for the Ferdium loop
60 *
61 * @since 1.0.0
62 * @return {void}
63 */
64 function getMessages() {
65 let mine = '';
66 let unassigned = '';
67 let total = '0';
68
69 /**
70 * Since Help Scout loads things asyncronously,
71 * we have to trigger everything in the loop for
72 * it to get recognized.
73 */
74 addCopyLink();
75 ticketScripts.init();
76
77 if ($('.dropdown.mailboxes').length > 0 && $('.dropdown.mailboxes a').hasClass('active')) {
78 // Individual tickets
79 mine = $('li.mine a .badge').text();
80 unassigned = $('li.unassigned a .badge').text();
81 } else if (window.location.href === 'https://secure.helpscout.net/dashboard/') {
82 // Main dashboard
83 mine = 0;
84 unassigned = 0;
85
86 $('.card.mailbox .c-list').each(function() {
87 let m = $(this).find('a:nth-child(2)').find('.count').text();
88 let u = $(this).find('a:first-child').find('.count').text();
89
90 if ($.isNumeric(m)) {
91 mine += Number.parseInt(m);
92 }
93
94 if ($.isNumeric(u)) {
95 unassigned += Number.parseInt(u);
96 }
97 });
98
99 mine = mine.toString();
100 unassigned = unassigned.toString();
101 }
102
103 if (mine !== '') {
104 total = mine;
105 }
106
107 if (unassigned !== '') {
108 total = total + '/' + unassigned;
109 }
110
111 Ferdium.setBadge(total);
112 }
113
114 Ferdium.loop(getMessages);
115};
116
117
118/**
119 * Add copy link to the conversation toolbar
120 *
121 * @since 1.2.0
122 * @return {void}
123 */
124function addCopyLink() {
125 if ($('.convo-toolbar').length > 0 && $('#copyLink').length === 0) {
126 $('#actions-dd .more').append('<li class="actions-dd"><a id="copyLink" class="actions-dd">Copy Link</a></li>');
127 $('.c-convo-toolbar').after('<div class="link-copied" style="display: none">Ticket URL copied to clipboard!<a id="closeLink">x</a></div>');
128 }
129}
130
131
132/**
133 * Process copying URLs to clipboard
134 *
135 * @since 1.1.
136 * @return {void}
137 */
138function copyToClipboard() {
139 let targetId = '_hiddenURLField';
140 let target = document.querySelector(targetId);
141
142 if(!target) {
143 target = document.createElement('textarea');
144 target.style.position = 'absolute';
145 target.style.left = '-9999px';
146 target.style.top = '0';
147 target.id = targetId;
148 document.body.append(target);
149 }
150
151 target.textContent = window.location.href;
152
153 let currentFocus = document.activeElement;
154
155 target.focus();
156 target.setSelectionRange(0, target.value.length);
157
158 document.execCommand('copy');
159
160 if(currentFocus && typeof currentFocus.focus === 'function') {
161 currentFocus.focus();
162 }
163}