forked from jsbin/jsbin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave.js
More file actions
72 lines (60 loc) · 1.95 KB
/
save.js
File metadata and controls
72 lines (60 loc) · 1.95 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
// to allow for download button to be introduced via beta feature
$('a.save').click(function (event) {
event.preventDefault();
saveCode('save', window.location.pathname.indexOf('/edit') !== -1);
return false;
});
$('a.clone').click(function (event) {
event.preventDefault();
var $form = $('form')
.append('<input type="hidden" name="javascript" />')
.append('<input type="hidden" name="html" />');
$form.find('input[name=javascript]').val(editors.javascript.getCode());
$form.find('input[name=html]').val(editors.html.getCode());
$form.find('input[name=method]').val('save,new');
$form.submit();
return false;
});
function saveCode(method, ajax, ajaxCallback) {
// create form and post to it
var $form = $('form')
.append('<input type="hidden" name="javascript" />')
.append('<input type="hidden" name="html" />');
$form.find('input[name=javascript]').val(editors.javascript.getCode());
$form.find('input[name=html]').val(editors.html.getCode());
$form.find('input[name=method]').val(method);
if (ajax) {
$.ajax({
url: $form.attr('action'),
data: $form.serialize(),
dataType: 'json',
type: 'post',
success: function (data) {
$('form').attr('action', data.url + '/save');
ajaxCallback && ajaxCallback();
if (window.history && window.history.pushState) {
window.history.pushState(null, data.edit, data.edit);
$('#jsbinurl').attr('href', data.url).text(data.url.replace(/http:\/\//, ''));
updateTitle(true)
} else {
window.location = data.edit;
}
},
error: function () {
}
});
} else {
$form.submit();
}
}
$document.keydown(function (event) {
if (event.metaKey && event.which == 83) {
if (event.shiftKey == false) {
$('#save').click();
event.preventDefault();
} else if (event.shiftKey == true) {
$('.clone').click();
event.preventDefault();
}
}
});