gist/web/app/js/app.js

194 lines
5.2 KiB
JavaScript
Raw Normal View History

2015-05-06 20:35:30 +02:00
var randomString = function(length, chars) {
var result = '';
2015-05-05 23:24:37 +02:00
2015-05-06 20:35:30 +02:00
for (var i = length; i > 0; --i) {
result += chars[Math.round(Math.random() * (chars.length - 1))];
}
return result;
}
2015-05-05 23:24:37 +02:00
2015-05-06 20:35:30 +02:00
var JsonFormatter = {
stringify: function(cipherParams) {
var jsonObj = {
ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64)
};
2015-05-05 23:24:37 +02:00
2015-05-06 20:35:30 +02:00
if (cipherParams.iv) {
jsonObj.iv = cipherParams.iv.toString();
}
if (cipherParams.salt) {
jsonObj.s = cipherParams.salt.toString();
}
2015-05-05 23:24:37 +02:00
2015-05-06 20:35:30 +02:00
return JSON.stringify(jsonObj);
},
2015-05-05 23:24:37 +02:00
2015-05-06 20:35:30 +02:00
parse: function(jsonStr) {
var jsonObj = JSON.parse(jsonStr);
var cipherParams = CryptoJS.lib.CipherParams.create({
ciphertext: CryptoJS.enc.Base64.parse(jsonObj.ct)
});
2015-05-05 23:24:37 +02:00
2015-05-06 20:35:30 +02:00
if (jsonObj.iv) {
cipherParams.iv = CryptoJS.enc.Hex.parse(jsonObj.iv)
}
if (jsonObj.s) {
cipherParams.salt = CryptoJS.enc.Hex.parse(jsonObj.s)
}
2015-05-05 23:24:37 +02:00
2015-05-06 20:35:30 +02:00
return cipherParams;
2015-05-05 23:24:37 +02:00
}
2015-05-06 20:35:30 +02:00
};
var editorEvents = function() {
$('textarea').on('keyup change', function() {
$(this).attr('rows', Math.min(30, Math.max(10, $(this).val().split("\n").length)));
});
$('#options input').change(function() {
var $input = $(this);
var $label = $($input.data('id'));
$label.html($label.data('tpl').replace('%value%', $input.data('title')));
});
$('#options li, #options a').click(function() {
$(this).find('label').trigger('click');
});
$('#options label').click(function(e) {
e.stopPropagation();
});
$('#options input:checked').each(function() {
$(this).trigger('change');
});
2015-05-06 22:46:29 +02:00
2016-09-24 16:07:35 +02:00
var key = getKey();
if (key) {
$('.show-diff').each(function() {
var href = $(this).attr('href');
href = href.replace('#', '#key=' + key + '&');
$(this).attr('href', href);
});
}
2015-05-08 16:44:59 +02:00
$('.show-diff').click(function() {
$($(this).data('target')).toggle();
});
2015-05-11 18:40:27 +02:00
2016-09-24 16:07:35 +02:00
var diffLinkTest1 = (document.location.href).indexOf('#diff-') !== -1;
var diffLinkTest2 = (document.location.href).indexOf('&diff-') !== -1;
if (diffLinkTest1 || diffLinkTest2) {
2015-05-11 18:40:27 +02:00
var anchor = '#' + (document.location.href).toString().split('#')[1];
2016-09-24 16:07:35 +02:00
2015-05-11 18:40:27 +02:00
$('.show-diff[href="' + anchor + '"]').click();
}
2015-05-06 20:35:30 +02:00
}
var myEvents = function() {
$('.btn-delete').click(function() {
2016-12-23 10:28:09 +01:00
if (confirm(trans('form.confirm'))) {
$('#delete_id').val($(this).data('id'));
$('#form-deletion form').submit();
}
});
2017-06-25 19:13:27 +02:00
$(document).on('change keyup keydown', '#form-api-key', function() {
$(this).val($(this).data('key'));
});
}
2015-05-06 20:35:30 +02:00
var mainEditorEvents = function() {
$('#main-form').submit(function(e) {
2016-09-24 14:43:15 +02:00
if ($('.cipher-input:checked').val() === 'yes' || typeof cipherGistClone !== 'undefined') {
var key = getKey();
if (key) {
var passphrase = key;
} else {
var passphrase = randomString(256, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
}
2015-05-06 20:35:30 +02:00
var content = $('#form_content').val();
var encrypted = CryptoJS.AES.encrypt(content, passphrase, {
format: JsonFormatter
});
$(this).attr('action', $(this).attr('action') + '#key=' + passphrase);
$('#form_content').val(encrypted);
}
});
}
2015-05-11 21:06:41 +02:00
var getKey = function() {
var url = document.location.href;
var parts = url.split('#key=');
2015-05-09 01:03:51 +02:00
2015-05-11 21:06:41 +02:00
if (parts.length === 2) {
2016-09-24 16:07:35 +02:00
return parts[1].split('&')[0];
2015-05-08 16:44:59 +02:00
}
2015-05-11 21:06:41 +02:00
return null;
}
var viewerEvents = function() {
2017-10-15 02:07:41 +02:00
var $render = $('#viewer code[data-cipher]');
2015-05-11 21:06:41 +02:00
2015-05-08 16:44:59 +02:00
$(document).ready(function() {
2015-05-11 21:06:41 +02:00
var key = getKey();
2016-12-23 10:28:09 +01:00
2016-09-24 14:43:15 +02:00
var $cipherEditor = $('.cipher-editor');
2015-05-11 21:06:41 +02:00
var $embedInput = $('#embed-input');
2015-05-08 16:44:59 +02:00
2016-10-03 19:11:26 +02:00
var to = ' ';
2016-09-24 14:43:15 +02:00
if (key) {
$('.cipher-link').each(function() {
var href = $(this).attr('href');
href = href + '#key=' + key;
$(this).attr('href', href);
2015-05-08 16:44:59 +02:00
});
2015-05-11 21:06:41 +02:00
2016-09-24 14:43:15 +02:00
if (0 !== $render.length || $cipherEditor.length !== 0) {
if ($render.length !== 0) {
var decrypted = CryptoJS.AES.decrypt($render.html(), key, {
format: JsonFormatter
});
$render.text(decrypted.toString(CryptoJS.enc.Utf8));
2017-10-15 02:07:41 +02:00
$render.attr('class', $render.data('class'));
Prism.highlightAll();
2016-09-24 14:43:15 +02:00
to = ' data-key="#key=' + key + '" ';
} else {
var decrypted = CryptoJS.AES.decrypt($cipherEditor.val(), key, {
format: JsonFormatter
});
$cipherEditor.val(decrypted.toString(CryptoJS.enc.Utf8));
}
}
2016-10-03 19:11:26 +02:00
}
2015-05-11 21:06:41 +02:00
2016-10-03 19:11:26 +02:00
if ($embedInput.length) {
$embedInput.val($embedInput.val().replace('%key%', to));
2015-05-08 16:44:59 +02:00
}
});
}
2015-05-06 20:35:30 +02:00
var bootstrap = function() {
editorEvents();
2015-05-08 16:44:59 +02:00
viewerEvents();
myEvents();
2015-05-06 20:35:30 +02:00
mainEditorEvents();
}
bootstrap();