1
0
mirror of https://github.com/Tygs/0bin.git synced 2023-08-10 21:13:00 +03:00
0bin/static/js/behavior.js

204 lines
5.2 KiB
JavaScript
Raw Normal View History

2012-04-26 13:38:55 +04:00
;
2012-04-26 23:04:36 +04:00
2012-04-28 19:11:32 +04:00
/* Start random number generator seeding ASAP *%
2012-04-24 22:15:38 +04:00
sjcl.random.startCollectors();
2012-04-28 19:11:32 +04:00
/* Ensure jquery use cache for ajax requests */
$.ajaxSetup({ cache: true });
2012-04-24 22:15:38 +04:00
zerobin = {
2012-04-26 23:04:36 +04:00
encrypt: function(key, content) {
2012-04-28 21:57:18 +04:00
content = sjcl.codec.base64.fromBits(sjcl.codec.utf8String.toBits(content));
2012-04-26 23:04:36 +04:00
return sjcl.encrypt(key, lzw.compress(content));
},
decrypt: function(key, content) {
2012-04-28 21:57:18 +04:00
content = lzw.decompress(sjcl.decrypt(key, content));
return sjcl.codec.utf8String.fromBits(sjcl.codec.base64.toBits(content));
2012-04-26 23:04:36 +04:00
},
make_key: function() {
2012-04-26 17:26:58 +04:00
return sjcl.codec.base64.fromBits(sjcl.random.randomWords(8, 0), 0);
},
get_date: function(){
var date = new Date();
return date.getDate()+"-"+(date.getMonth()+1)+"-"+date.getFullYear();
},
get_time: function(){
var date = new Date();
var h=date.getHours();
var m=date.getMinutes();
var s=date.getSeconds();
if (h<10) {h = "0" + h}
if (m<10) {m = "0" + m}
if (s<10) {s = "0" + s}
return h+":"+m+":"+s;
},
support_localstorage: function(){
if (localStorage){
2012-04-28 21:57:18 +04:00
return true;
}else{
return false;
}
},
store_paste: function(url){
if (zerobin.support_localstorage){
var date = new Date();
var paste = zerobin.get_date()+" "+zerobin.get_time()+";"+url;
if (localStorage.length > 19)
void removeItem(0);
localStorage.setItem(localStorage.length, paste);
}
},
get_pastes: function(){
2012-04-28 21:57:18 +04:00
if (zerobin.support_localstorage){
var pastes = '';
2012-04-28 21:57:18 +04:00
for (i=localStorage.length-1; i>=0; i--)
{
if (localStorage.getItem(i).split(';')[0].split(' ')[0] == zerobin.get_date()){
var display_date = localStorage.getItem(i).split(';')[0].split(' ')[1];
2012-04-28 21:55:00 +04:00
var on_at = 'at ';
}else{
var display_date = zerobin.get_date();
2012-04-28 21:55:00 +04:00
var on_at = 'on ';
}
2012-04-28 21:55:00 +04:00
pastes = pastes + '<li><a class="items" href="' + localStorage.getItem(i).split(';')[1] + '">' + on_at + display_date + '</a></li>';
}
if (!pastes){
return '<i class="grey">Your previous pastes will be saved in your browser <a href="http://www.w3.org/TR/webstorage/">localStorage</a>.</i>';
}
return pastes;
}else{
return 'Sorry your browser does not support LocalStorage, We cannot display your previous pastes.';
}
2012-04-26 23:04:36 +04:00
}
};
2012-04-26 13:38:55 +04:00
2012-04-26 17:26:58 +04:00
$(function(){
2012-04-26 13:38:55 +04:00
2012-04-28 19:11:32 +04:00
/**
On the create paste page:
On click on the send button, compress and encrypt data before
posting it using ajax. Then redirect to the address of the
newly created paste, adding the key in the hash.
*/
2012-04-28 16:50:48 +04:00
$('button[type=submit]').live("click", function(e){
2012-04-26 13:38:55 +04:00
e.preventDefault();
var paste = $('textarea').val();
if (paste.trim()) {
var expiration = $('#expiration').val();
2012-04-26 17:26:58 +04:00
var key = zerobin.make_key();
var data = {content: zerobin.encrypt(key, paste), expiration: expiration}
2012-04-24 22:15:38 +04:00
2012-04-26 13:38:55 +04:00
$.post('/paste/create', data)
.error(function(error) {
2012-04-26 13:38:55 +04:00
alert('Paste could not be saved. Please try again later.');
})
.success(function(data) {
var paste_url = '/paste/' + data['paste'] + '#' + key;
window.location = (paste_url);
zerobin.store_paste(paste_url);
2012-04-26 13:38:55 +04:00
});
}
2012-04-24 22:15:38 +04:00
2012-04-26 13:38:55 +04:00
});
2012-04-24 22:15:38 +04:00
2012-04-28 19:11:32 +04:00
/** On the display paste page.
2012-04-28 21:57:18 +04:00
Decrypt and decompress the paste content, add syntax coloration then
setup the copy to clipboard button.
2012-04-28 19:11:32 +04:00
*/
2012-04-26 17:26:58 +04:00
var content = $('#paste-content').text().trim();
var key = window.location.hash.substring(1);
2012-04-28 21:57:18 +04:00
var error = false;
2012-04-26 17:26:58 +04:00
if (content && key) {
try {
$('#paste-content').text(zerobin.decrypt(key, content));
} catch(err) {
2012-04-28 21:57:18 +04:00
error = true;
2012-04-26 17:26:58 +04:00
alert('Could not decrypt data (Wrong key ?)');
}
2012-04-28 21:57:18 +04:00
content = '';
if (!error) {
prettyPrint();
/* Setup flash clipboard button */
ZeroClipboard.setMoviePath('/static/js/ZeroClipboard.swf' );
var clip = new ZeroClipboard.Client();
clip.addEventListener('onMouseUp', function(){
clip.setText($('#paste-content').text());
2012-04-28 19:11:32 +04:00
});
2012-04-28 21:57:18 +04:00
clip.addEventListener('complete', function(){
$('#copy-success').show();
});
clip.addEventListener('onLoad', function(){
2012-04-28 19:11:32 +04:00
});
2012-04-28 21:57:18 +04:00
clip.glue('clip-button', 'clip-container' );
window.onresize = clip.reposition;
}
2012-04-26 17:26:58 +04:00
}
2012-04-24 22:15:38 +04:00
2012-04-28 19:11:32 +04:00
/* Synchronize expiration select boxes value */
$('.paste-option select').live('change', function(){
var value = $(this).val();
$('.paste-option select').val(value);
});
2012-04-28 19:11:32 +04:00
/* Resize Textarea according to content */
$('#content').elastic();
2012-04-28 15:25:43 +04:00
/* Display bottom paste option buttons when needed */
2012-04-28 14:51:30 +04:00
$('#content').live('keyup change', function(){
2012-04-28 19:11:32 +04:00
if($('#content').height() < 400 ){
$('.paste-option.down').remove();
}
else {
if ($('.paste-option').length == 1) {
$('.paste-option').clone().addClass('down').appendTo('form.well');
}
}
});
/* Display previous pastes */
$('.previous-pastes .items').html(zerobin.get_pastes());
2012-04-28 21:58:30 +04:00
2012-04-28 21:55:00 +04:00
/* clone a paste */
$('.btn-clone').click(function(e){
e.preventDefault();
2012-04-28 22:18:05 +04:00
var content_clone = '' ;
2012-04-28 21:55:00 +04:00
$("#paste-content li").each(function(index) {
content_clone = content_clone + $(this).text() + '\n';
});
$('.submit-form').show();
2012-04-28 22:09:51 +04:00
$('.paste-form').remove();
2012-04-28 21:55:00 +04:00
$('#content').val(content_clone);
2012-04-28 22:09:51 +04:00
$('#content').trigger('change');
2012-04-28 21:55:00 +04:00
});
});