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
|
jQuery(function($) {
"use strict";
var TEMPLATES = new LuigiTemplate.Cache({
});
function update_slug(name, slug) {
if (slug.prop('disabled')) {
slug.val(
name.val().toLowerCase()
.replace(/[^a-z0-9_-]+/g, '-')
.replace(/^-+|-+$/g, '')
);
}
}
$.each(['blog', 'page', 'project'], function(i, id) {
var p = '#' + id + '-edit-';
$(p + 'dialog').on('guff.loaded', function(ev) {
var r = ev.post_data,
slug_lock = (r.slug_lock == "1");
$(p + 'name').val(r.name);
$(p + 'slug').val(r.slug)
.prop('disabled', slug_lock ? 'disabled' : null);
$(p + 'slug-lock')
.toggleClass('btn-default', slug_lock)
.toggleClass('btn-primary', !slug_lock)
.find('fa')
.toggleClass('fa-lock', slug_lock)
.toggleClass('fa-unlock', !slug_lock);
var editor = CKEDITOR.instances[id + '-edit-body'];
if (!editor) {
// lazy-init editor
editor = CKEDITOR.replace(id + '-edit-body');
}
if (editor.status == 'ready') {
// editor is ready, set body data
editor.setData(r.body);
} else {
editor.once('instanceReady', function() {
editor.setData(r.body);
});
}
$(p + 'state a').removeClass('btn-primary').addClass('btn-default');
$(p + 'state a[data-val="' + r.state + '"]')
.toggleClass('btn-primary btn-default');
}).on('show.bs.modal', function() {
var me = $(this);
// reset close confirmation
me.data('close-dialog-confirmed', false);
// show loading text
me.find('.modal-body').addClass('hidden');
me.find('.modal-body.loading-text').removeClass('hidden');
// show content tab
me.find(p + 'tab-content').click();
send(id + '/get', {
post_id: me.data('post_id')
}).always(function() {
me.find('.modal-body.loading-text').addClass('hidden');
}).fail(function(r) {
var error = r.responseText;
try {
// try to extract error message from json response
var data = $.parseJSON(r.responseText);
if ('error' in data)
error = data.error;
} catch (e) {}
me.find('.modal-body.loading-error').removeClass('hidden')
.find('.error-text').text(error);
}).done(function(r) {
me.find('.modal-body.loading-done').removeClass('hidden');
me.trigger({
type: "guff.loaded",
post_data: r,
});
});
}).on('shown.bs.modal', function() {
$(p + 'name').focus();
}).on('hide.bs.modal', function() {
return (
$(this).data('close-dialog-confirmed') ||
confirm('Close without saving changes?')
);
}).on('guff.save', function(ev) {
$(p + 'confirm').addClass('disabled')
.find('.loading').toggleClass('hidden');
send(id + "/set", $.extend({
post_id: $(p + 'dialog').data('post_id'),
name: $(p + 'name').val(),
slug_lock: $(p + 'slug-lock').hasClass('btn-default') ? 't' : 'f',
slug: $(p + 'slug').val(),
body: CKEDITOR.instances[id + '-edit-body'].getData(),
state: $(p + 'state .btn-primary').data('val'),
}, ev.post_data)).always(function() {
$(p + 'confirm').removeClass('disabled')
.find('.loading').toggleClass('hidden');
}).fail(function(r) {
var error = r.responseText;
try {
var data = $.parseJSON(r.responseText);
if (data.error)
error = data.error;
} catch (e) {}
alert('Error: ' + error);
}).done(function(r) {
// reload posts
$('#posts-reload').click();
// dismiss dialog
$(p + 'dialog').data('close-dialog-confirmed', true).modal('hide');
});
}).find('button[data-dismiss="modal"]').click(function() {
// override close confirmation dialog
$(p + 'dialog').data('close-dialog-confirmed', true);
});
});
$('.post-name').keydown(function() {
var name = $(this),
slug = $(this).parents('.modal-body').find('.post-slug');
setTimeout(function() {
update_slug(name, slug);
}, 10);
});
$('.post-slug-lock').click(function() {
var modal_body = $(this).parents('.modal-body');
// toggle locked state
$(this).toggleClass('btn-default btn-primary')
.find('.fa').toggleClass('fa-lock fa-unlock');
var locked = $(this).hasClass('btn-default');
// update slug disabled state
var slug = modal_body.find('.post-slug');
slug.prop('disabled', locked ? 'disabled' : null);
if (locked) {
// auto-generate slug
update_slug(modal_body.find('.post-name'), slug);
}
// stop event
return false;
});
$('.state-buttons a').click(function() {
$(this).parent().find('.btn-primary')
.toggleClass('btn-default btn-primary');
$(this).toggleClass('btn-default btn-primary');
// stop event
return false;
});
});
|