forked from artefactual/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipboard.js
199 lines (174 loc) · 5.06 KB
/
clipboard.js
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
(function ($) {
'use strict';
var Clipboard = function (element)
{
this.$element = element;
this.$menuButton = this.$element.find('> button');
this.$menuHeaderCount = this.$element.find('.top-dropdown-header > span');
this.$menuClearAll = this.$element.find('li#node_clearClipboard a');
this.$toggleButtons = $('button.clipboard');
this.init();
};
Clipboard.prototype = {
constructor: Clipboard,
init: function()
{
this.$toggleButtons.tooltip(
{
'placement': 'bottom',
'container': 'body'
});
this.$toggleButtons.on('click', $.proxy(this.toggle, this, true));
this.$menuClearAll.on('click', $.proxy(this.clear, this));
// Listener for wide buttons must be added like this
// as they are dynamically loaded in fullWidthTreeView.js
$(document).on('click', 'button.clipboard-wide', $.proxy(this.toggle, this, false));
},
toggle: function (reloadTooltip, event)
{
event.preventDefault();
var $button = $(event.target);
if (reloadTooltip)
{
$button.tooltip('hide');
}
$.ajax({
url: $button.data('clipboard-url'),
data: { slug: $button.data('clipboard-slug') },
context: this,
beforeSend: function()
{
// Add loading gif
},
success: function (data)
{
this.updateButton($button, data.added, reloadTooltip);
this.updateCounts(data.count);
},
error: function(error)
{
console.log(error);
},
complete: function()
{
// Remove loading gif
}
});
},
clear: function (event)
{
event.preventDefault();
this.showAlert();
$.ajax({
url: this.$menuClearAll.attr('href'),
type: 'DELETE',
context: this,
success: function (data)
{
if (data.success)
{
this.updateCounts(0);
this.$toggleButtons.each($.proxy(function (index, button) {
this.updateButton($(button), false, true);
}, this));
// Wide buttons must be selected like this as they
// are dynamically loaded in fullWidthTreeView.js
$(document).find('button.clipboard-wide').each($.proxy(function (index, button) {
this.updateButton($(button), false, false);
}, this));
}
},
error: function(error)
{
console.log(error);
}
});
},
updateButton: function ($button, added, reloadTooltip)
{
// If previous and current status don't match,
// change status, tooltip and button content
if ((!$button.hasClass('added') && added)
|| ($button.hasClass('added') && !added))
{
// Show alert when removing
if (!added)
{
this.showAlert();
}
$button.toggleClass('added');
var label = $button.attr('data-title');
var altLabel = $button.attr('data-alt-title');
$button.attr('data-alt-title', label);
$button.attr('data-title', altLabel);
$button.text(altLabel);
// Fix tooltip only in small buttons
if (reloadTooltip)
{
$button.tooltip()
.attr('data-original-title', altLabel)
.tooltip('fixTitle');
}
}
},
updateCounts: function (count)
{
// Menu button count
var $buttonSpan = this.$menuButton.find('> span');
if (!$buttonSpan.length && count > 0)
{
this.$menuButton.append('<span>' + count + '</span>');
}
else if (count > 0)
{
$buttonSpan.text(count);
}
else if ($buttonSpan.length)
{
$buttonSpan.remove();
}
// Menu dropdown header count
var pluralLabel = this.$menuHeaderCount.data('plural-label');
var singleLabel = this.$menuHeaderCount.data('single-label');
if (count != 1)
{
this.$menuHeaderCount.text(count + ' ' + pluralLabel);
}
else
{
this.$menuHeaderCount.text(count + ' ' + singleLabel);
}
},
showAlert: function()
{
// Show alert box in clipboard page if it is not already added
if ($('body').is('.user.clipboard') && $('#wrapper.container > .alert').length == 0)
{
$(
'<div class="alert">' +
'<button type="button" data-dismiss="alert" class="close">×</button>'
)
.append(this.$element.data('alert-message'))
.prependTo($('#wrapper.container'));
}
}
};
$.fn.clipboard = function()
{
var $this = this;
var data = $this.data('clipboard');
if (!data)
{
$this.data('clipboard', new Clipboard(this));
}
};
$.fn.clipboard.Constructor = Clipboard;
$(function ()
{
var $clipboard = $('#clipboard-menu');
if ($clipboard.length)
{
$clipboard.clipboard();
}
});
})(jQuery);