-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
editable.js
106 lines (98 loc) · 3.42 KB
/
editable.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
$(function($) {
// Add AJAX submit handler for each editable form.
$('.editable-form').submit(function() {
var form = $(this);
var loading = $('#editable-loading');
var showError = function(msg) {
if (msg) {
msg = ': ' + msg;
} else {
msg = '';
}
alert('An error occurred' + msg);
loading.hide();
form.show();
};
loading.css({
top: ($(window).height() - loading.height()) / 2 + $(window).scrollTop() + "px",
left: ($(window).width() - loading.width()) / 2 + $(window).scrollLeft() + "px"
});
form.hide();
loading.show();
if (typeof tinyMCE != "undefined" ) {
tinyMCE.triggerSave();
}
form.ajaxSubmit({success: function(data) {
if (data && data != '<head></head><body></body>') {
showError(data);
} else {
location.reload();
}
}, error: function() {
showError();
}});
return false;
})
// Iterate through each of the editable areas and set them up.
function realign() {
$.each($('.editable-original'), function(i, editable) {
editable = $(editable);
// Position the editable area's edit link.
var link = editable.next('.editable-link');
link.css({top: editable.offset().top,
left: editable.offset().left - link.width() - 12});
// Apply the editable area's overlay handler.
var expose = {color: '#333', loadSpeed: 200, opacity: 0.9};
var overlay = {expose: expose, closeOnClick: true, close: ':button'};
link.overlay(overlay);
// Position the editable area's highlight.
link.next('.editable-highlight').css({
width: editable.width(), height: editable.height(),
top: editable.offset().top, left: editable.offset().left
});
});
}
realign();
// Show/hide the editable area's highlight when mousing over/out the of
// the edit link.
$('.editable-link').hover(function(e) {
$(this).next('.editable-highlight').show();
}, function(e) {
$(this).next('.editable-highlight').hide();
});
$('.editable-original').on('resize', function(e) {
realign();
});
// Add the toolbar HTML and handlers.
var cookie = 'mezzanine-admin-toolbar';
var at = ('; ' + document.cookie).indexOf('; ' + cookie + '=');
var closed = false;
if (at > -1) {
closed = document.cookie.substr(at +
cookie.length + 1).split(';')[0];
}
$(window.__toolbar_html).appendTo('body');
$('#editable-toolbar-toggle').click(function() {
var toggle = $(this);
var controls = $('.editable-link, ' +
'#editable-toolbar *[id!=editable-toolbar-toggle]');
if (toggle.text() == '<<') {
toggle.text('>>');
controls.hide();
document.cookie = cookie + '=1; path=/';
} else {
toggle.text('<<');
controls.show();
document.cookie = cookie + '=; path=/';
}
return false;
});
$('#editable-toolbar-logout').click(function() {
$('#editable-toolbar').submit();
return false;
});
$('#editable-toolbar').fadeIn('slow');
if (!closed) {
$('#editable-toolbar-toggle').click();
}
});