-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapplication.js
355 lines (290 loc) · 11.5 KB
/
application.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
function htmlspecialchars(string, quote_style, charset, double_encode) {
// http://kevin.vanzonneveld.net
// + original by: Mirek Slugen
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Nathan
// + bugfixed by: Arno
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// + input by: Ratheous
// + input by: Mailfaker (http://www.weedem.fr/)
// + reimplemented by: Brett Zamir (http://brett-zamir.me)
// + input by: felix
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// % note 1: charset argument not supported
// * example 1: htmlspecialchars("<a href='test'>Test</a>", 'ENT_QUOTES');
// * returns 1: '<a href='test'>Test</a>'
// * example 2: htmlspecialchars("ab\"c'd", ['ENT_NOQUOTES', 'ENT_QUOTES']);
// * returns 2: 'ab"c'd'
// * example 3: htmlspecialchars("my "&entity;" is still here", null, null, false);
// * returns 3: 'my "&entity;" is still here'
var optTemp = 0, i = 0, noquotes= false;
if (typeof quote_style === 'undefined' || quote_style === null) {
quote_style = 2;
}
string = string.toString();
if (double_encode !== false) { // Put this first to avoid double-encoding
string = string.replace(/&/g, '&');
}
string = string.replace(/</g, '<').replace(/>/g, '>');
var OPTS = {
'ENT_NOQUOTES': 0,
'ENT_HTML_QUOTE_SINGLE' : 1,
'ENT_HTML_QUOTE_DOUBLE' : 2,
'ENT_COMPAT': 2,
'ENT_QUOTES': 3,
'ENT_IGNORE' : 4
};
if (quote_style === 0) {
noquotes = true;
}
if (typeof quote_style !== 'number') { // Allow for a single string or an array of string flags
quote_style = [].concat(quote_style);
for (i=0; i < quote_style.length; i++) {
// Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4
if (OPTS[quote_style[i]] === 0) {
noquotes = true;
}
else if (OPTS[quote_style[i]]) {
optTemp = optTemp | OPTS[quote_style[i]];
}
}
quote_style = optTemp;
}
if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
string = string.replace(/'/g, ''');
}
if (!noquotes) {
string = string.replace(/"/g, '"');
}
return string;
}
function auto_resize(el) {
// http://googlecode.blogspot.com/2009/07/gmail-for-mobile-html5-series.html
var TEXTAREA_LINE_HEIGHT = 13;
var textarea = $(el);
var newHeight = textarea.scrollHeight;
var currentHeight = textarea.clientHeight;
if (newHeight > currentHeight) {
textarea.style.height = newHeight + (2 * TEXTAREA_LINE_HEIGHT) + 'px';
}
}
function unix_timestamp() {
return Math.round(new Date().getTime() / 1000);
}
function get_caret_position(el) {
if (! $(el)) return false;
el = $(el);
if (el.selectionStart) {
return el.selectionStart;
} else if (document.selection) {
el.focus();
var r = document.selection.createRange();
if (r == null) {
return 0;
}
var re = el.createTextRange(),
rc = re.duplicate();
re.moveToBookmark(r.getBookmark());
rc.setEndPoint('EndToStart', re);
return rc.text.length;
}
return 0;
}
function set_caret_position(el, pos){
if (! $(el)) return false;
el = $(el);
if (el.setSelectionRange) {
el.focus();
el.setSelectionRange(pos, pos);
} else if (el.createTextRange) {
var range = el.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}
function get_scroll_position(el) {
if (! $(el)) return false;
offset = $(el).cumulativeScrollOffset();
return offset.top;
}
function set_scroll_position(el, pos) {
if (! $(el)) return false;
$(el).scrollTop = pos;
}
var checking_if_name_exists = false;
function validate_name_exists() {
just_clicked_bubble = true;
if (! $('change_url_input')) return false;
el = $('change_url_input');
el.value = el.value.toLowerCase().replace("'", '').replace(/[^-a-z0-9]/g, '-').replace(/--+/g, '-').replace(/^-+|-+$/g, '').substr(0,50);
if (el.value == '' || el.value == pad_name) {
$('bubble_for_change_url').hide();
$('message_for_change_url_unavailable').hide();
el.value = pad_name;
} else {
if (! checking_if_name_exists) {
el.addClassName('loading');
new Ajax.Request('/ajax/check_if_name_exists/' + el.value, {
onComplete: function(transport) {
checking_if_name_exists = false;
if (transport.responseText == 'false') {
$('message_for_change_url_unavailable').hide();
$('form_for_set_name').submit();
} else {
if (! $('bubble_for_change_url').visible()) $('bubble_for_change_url').show();
$('message_for_change_url_unavailable').show();
el.removeClassName('loading');
$('change_url_input').activate();
}
}
});
}
}
return false;
}
function _update_contents(contents_value) {
// Immediately save contents
if (saving_pad || ((last_saved_on + seconds_before_save) > unix_timestamp()) ) {
return true;
}
window.clearTimeout(update_contents_timeout);
saving_pad = true;
unsaved_changes = false;
last_saved_on = unix_timestamp();
$('unsaved').hide();
$('loading').show();
caret_position = get_caret_position('contents');
scroll_position = get_scroll_position('contents');
new Ajax.Request(window.location.href, {
parameters: {
contents: contents_value,
caret_position: caret_position,
scroll_position: scroll_position
},
onSuccess: function(transport) {
chars_on_last_save = contents_value.length;
saving_pad = false;
$('loading').hide();
},
onFailure: function(transport) {
$('unsaved').show();
$('loading').hide();
if (403 == transport.status) {
alert("Sorry, you seem to be logged out. Refresh the page and log in again.");
disable_autosave = true;
if (contents_observer) contents_observer = false;
if (update_contents_timeout) update_contents_timeout = false;
} else {
alert("Sorry, couldn't save contents. Try again in a few seconds.");
}
}
});
}
function _determine_update_contents(contents_value) {
window.clearTimeout(update_contents_timeout);
update_contents_timeout = _update_contents.delay(seconds_before_save, contents_value);
if (Math.abs(chars_on_last_save - contents_value.length) > new_chars_before_save) {
window.clearTimeout(update_contents_timeout);
_update_contents(contents_value);
}
}
var new_chars_before_save = 50;
var seconds_before_save = 2;
var chars_on_last_save = 0;
var last_saved_on = 0;
var contents_observer = false;
var update_contents_timeout = false;
var saving_pad = false;
var unsaved_changes = false;
var just_clicked_bubble = false;
Event.observe(window, 'load', function() {
if ($('contents')) {
if (caret_position) {
set_caret_position('contents', caret_position);
} else {
$('contents').focus();
}
}
if ($$('.bubble')) {
Event.observe(document, 'click', function(e) {
if (just_clicked_bubble) {
$$('.bubble').invoke('hide');
$('bubble_for_' + just_clicked_bubble).show();
if (just_clicked_bubble == 'change_url' && $('change_url_input')) $('change_url_input').activate();
if (just_clicked_bubble == 'set_password' && $('set_password_input')) $('set_password_input').activate();
if (just_clicked_bubble == 'share_this' && $('share_this_input')) $('share_this_input').activate();
just_clicked_bubble = false;
} else {
$$('.bubble').invoke('hide');
}
});
}
if ($('contents') && scroll_position) set_scroll_position('contents', scroll_position);
if (pad_name && ! disable_autosave && $('contents')) {
chars_on_last_save = $('contents').value.length;
$('printable_contents').innerHTML = htmlspecialchars($('contents').value);
contents_observer = new Form.Element.Observer(
'contents',
0.25,
function(el, value) {
$('unsaved').show();
unsaved_changes = true;
_determine_update_contents(value);
}
);
// Save contents when the cursor moves
Event.observe(window, 'mousemove', function(e) {
if (unsaved_changes) {
_update_contents($('contents').value);
}
});
// Save contents when the page loses focus
Event.observe(window, 'blur', function(e) {
if (unsaved_changes) {
_update_contents($('contents').value);
}
});
// Save contents before unload. Prototype mucks with onBeforeUnload
window.onbeforeunload = function() {
if (unsaved_changes) {
_update_contents($('contents').value);
return "You have unsaved content.\n\nPlease wait a few seconds before leaving the page. The content will save automatically.";
}
}
if (is_iphone_os) {
auto_resize('contents');
Event.observe('contents', 'keyup', function(e) {
auto_resize('contents');
});
}
Event.observe('contents', 'keydown', function(e) {
if (e.keyCode == Event.KEY_TAB) {
// Catch and support tabs
var tab = ' ';
var t = Event.element(e);
var s_start = t.selectionStart;
var s_end = t.selectionEnd;
t.value = t.value.slice(0, s_start).concat(tab).concat(t.value.slice(s_end, t.value.length));
t.selectionStart = t.selectionEnd = s_start + 1;
Event.stop(e);
} else if ((e.ctrlKey || e.metaKey) && e.keyCode == 83) {
// Save on ⌘S / ⌃S
if (unsaved_changes) {
_update_contents($('contents').value);
}
Event.stop(e);
}
// } else if (e.ctrlKey || e.altKey || e.metaKey) {
// // Save on ⌘ and ⌃
//
// if (unsaved_changes) {
// _update_contents($('contents').value);
// }
// }
$('printable_contents').innerHTML = htmlspecialchars($('contents').value);
});
}
});