forked from sendgrid/docs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodebuttons.js
More file actions
152 lines (122 loc) · 4.97 KB
/
Copy pathcodebuttons.js
File metadata and controls
152 lines (122 loc) · 4.97 KB
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
// From http://stackoverflow.com/questions/998245/how-can-i-detect-if-flash-is-installed-and-if-not-display-a-hidden-div-that-inf
var hasFlash = false;
try {
var fo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
if(fo) hasFlash = true;
}catch(e){
if(navigator.mimeTypes ["application/x-shockwave-flash"] !== undefined) hasFlash = true;
}
$(function() {
$("body").addClass("flash");
if(hasFlash){
$("body").addClass("flash");
//this is kinda hacky but let's infer the root from the home button
var root = $('.nav-link:eq(0) > a').attr('href').replace('/index.html',''),
path = root + '/flash/ZeroClipboard.swf';
// Default ZeroClipboard to the root of the Docs
ZeroClipboard.setDefaults({ moviePath: path, hoverClass: 'hover' });
// Initialize all zClipboards
glueClipboards();
}else{
$("body").removeClass("flash").addClass("no-flash");
glueClipboards();
}
$(".expandcode").click(function() {
if($("#overlay").length){
$("#overlay").trigger("close");
}else{
var codeId = $(this).attr("id").replace('expand_','');
var codeblockWrapper = $('#wrapper_' + codeId);
codeblockWrapper.addClass('expanded').css("height", codeblockWrapper.height());
var overlay = $('<div id="overlay"></div>');
overlay.appendTo(document.body);
codeblockWrapper.children().appendTo(overlay);
$("#overlay .expandcode").addClass('btn-danger');
$(this).html('<i class="icon-remove icon-white"> </i> Close');
//prevent the body from scrolling
$(document.body).css("overflow", "hidden");
overlay.on("close", function() {
var overlay = $('#overlay');
var codeId = overlay.children(".code").attr("id").replace('code_','');
var codeblockWrapper = $('#wrapper_' + codeId);
$("#overlay .expandcode").removeClass('btn-danger');
$("#overlay .expandcode").html('<i class="icon-fullscreen"> </i> Fullscreen');
overlay.children().appendTo(codeblockWrapper);
codeblockWrapper.css("height", "");
$(document.body).css("overflow", "auto");
overlay.remove();
});
overlay.on("click", function(e) {
if(e.target == $(this)[0]){
$(this).trigger("close");
}
});
var eventInfo = ['_trackEvent', 'Code Block', 'Expand', codeId];
_gaq.push(eventInfo);
}
ZeroClipboard.destroy();
glueClipboards();
});
// Fullscreen Code If Requested
var match = document.location.hash.match(/#(wrapper|code)(_\w{32})-full(screen)?/);
if(match){
$(window).scrollTop($('#wrapper' + match[2]).offset().top);
$('#wrapper' + match[2] + ' .expandcode').click();
}
});
function glueClipboards () {
if($("body").hasClass("flash")){
$(".copycode").each(function () {
var zclip = new ZeroClipboard($(this));
zclip.on('dataRequested', function ( client, args ) {
// Find the hash of the code
var codeId = $(this).attr("id").replace('copy_','');
// Get the text of the requested copy
var text = $('#code_' + codeId + ' .code').text();
// Get rid of the final \n as that's not really expected by users
text.replace(/\n$/,'');
client.setText( text );
});
zclip.on('complete', function ( client, args ) {
$(this).html('<i class="icon-check"> </i> Copied');
});
});
}else{
if(!document.flashFreeClipboard){
$(".copycode").click(function () {
// Find the hash of the code
var codeId = $(this).attr("id").replace('copy_','');
// Get the element of the requested copy
var codeElement = $('#code_' + codeId + ' .code');
// From http://stackoverflow.com/questions/6139107/programatically-select-text-in-a-contenteditable-html-element
var range = document.createRange();
range.selectNodeContents(codeElement[0]);
var select = window.getSelection();
select.removeAllRanges();
select.addRange(range);
var metaKey = navigator.appVersion.indexOf("Mac") ? "⌘" : "Ctrl",
copyPromptText = "Press " + metaKey + "-C to Copy";
var codeElementPosition = codeElement.position(),
codeElementWidth = codeElement.width();
$("#copy-prompt").remove();
$('<div id="copy-prompt">' + copyPromptText + '</div>').appendTo("body").css({
"top" : codeElementPosition.top,
"left" : codeElementPosition.left + (codeElementWidth - $("#copy-prompt").width())/2,
});
setTimeout(function () {
$("#copy-prompt").fadeOut("slow");
$(window).off("copyCode");
}, 2000);
$(window).on("keydown.copyCode", function (e) {
if(e.keyCode == 67 && e.metaKey == true){
$("#copy-prompt").fadeOut("fast");
$(window).off("copyCode");
}
});
var eventInfo = ['_trackEvent', 'Code Block', 'Copy', codeId];
_gaq.push(eventInfo);
});
document.flashFreeClipboard = true;
}
}
}