jQuery(function($) {
"use strict";
var TEMPLATES = new LuigiTemplate.Cache({
link: [
"
",
"",
"%{name|h}",
"",
"",
],
save: [
"",
"",
"%{name|h}",
"",
"",
],
none: [
"",
"",
"No saved results...",
"",
"",
],
help: [
"",
"",
"About Mathy...",
"",
"",
"",
"",
"%{examples}",
],
help_row: [
"",
"",
"%{num|h}. %{name|h}",
"",
"",
],
});
// init mathjax
var math = (function() {
var delay = 150, // delay after keystroke before updating
preview = document.getElementById("math-preview"),
buffer = document.getElementById("math-buffer"),
timeout = null, // timeout ID
is_running = false, // true when MathJax is processing
is_pending = false, // true when a typeset has been queued
old_text = null; // used to check if an update is needed
// Swap render buffers.
//
// Note from MathJax example: We use visibility:hidden rather than
// display:none since the results of running MathJax are more
// accurate that way.
function swap_buffers() {
var tmp_buffer = preview,
tmp_preview = buffer;
// update buffer
buffer = tmp_buffer;
buffer.style.visibility = "hidden";
buffer.style.position = "absolute";
// update preview
preview = tmp_preview;
preview.style.position = "";
preview.style.visibility = "";
}
function preview_done() {
is_running = is_pending = false;
swap_buffers();
}
function create_preview() {
// clear timeout value
timeout = null;
if (is_pending)
return;
// get editor text
var text = "\\[" + editor.getValue() + "\\]";
if (text === old_text)
return;
if (is_running) {
is_pending = true;
MathJax.Hub.Queue(create_preview);
} else {
buffer.innerHTML = old_text = text;
is_running = true;
MathJax.Hub.Queue(
['Typeset', MathJax.Hub, buffer],
preview_done
);
}
}
// init callback
var callback = MathJax.Callback(create_preview);
callback.autoReset = true;
return {
update: function() {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
timeout = setTimeout(callback, delay);
},
};
})();
// init editor
var autosave_timeout = null,
editor = ace.edit('editor');
editor.setShowPrintMargin(false);
editor.setTheme('ace/theme/monokai');
editor.getSession().setMode('ace/mode/latex');
editor.getSession().on('change', function(e) {
// queue update
math.update();
if (autosave_timeout) {
// clear autosave timeout
clearTimeout(autosave_timeout);
autosave_timeout = null;
}
// set autosave timeout
autosave_timeout = setTimeout(function() {
// autosave text
localStorage.setItem('mathy_last', editor.getValue());
}, 1000);
});
(function() {
var text = null;
// guard against old IE
if (window.URL) {
// load from query parameter
text = (new URL(document.location)).searchParams.get('t');
}
if (!text) {
// load autosaved text
text = localStorage.getItem('mathy_last');
}
if (!text)
return;
// set text
editor.setValue(text);
})();
$('#saves').on('click', 'a', function() {
if ($(this).parent().hasClass('disabled'))
return false;
// get text, hide dropdown
var text = $(this).data('text');
$('body').trigger('click');
setTimeout(function() {
// set editor text
editor.setValue(text);
}, 10);
// stop event
return false;
}).parent().on('show.bs.dropdown', function() {
// load saves
var saves = localStorage.getItem('mathy_saves');
saves = saves ? JSON.parse(saves) : [];
// build html
var html = '';
if (saves.length > 0) {
html = $.map(saves, function(row) {
return TEMPLATES.run('save', row);
}).join('');
} else {
html = TEMPLATES.run('none');
}
// populate html
$('#saves').html(html);
});
$('#save').click(function() {
// get name
var name = prompt('Enter name:', '');
if (!name)
return false;
// build sortable name
var sortable_name = name
.replace(/\s+/g, ' ')
.replace(/^\s+|\s+$/g, '')
.toLowerCase();
// load saves
var saves = localStorage.getItem('mathy_saves');
saves = saves ? JSON.parse(saves) : [];
if ($.grep(saves, function(row) {
return row.sort == sortable_name;
}).length > 0) {
if (!confirm("Replace existing \"" + name + " \"?"))
return false;
}
// exclude duplicates
saves = $.grep(saves, function(row) {
return row.sort != sortable_name;
});
// append to results
saves.push({
name: name,
sort: sortable_name,
text: editor.getValue(),
});
// write sorted saves
localStorage.setItem('mathy_saves', JSON.stringify(saves.sort(function(a, b) {
return b.sort < a.sort;
})));
return false;
});
$('#help').html(TEMPLATES.run('help', {
examples: $.map(DATA.examples, function(row, i) {
return TEMPLATES.run('help_row', $.extend({}, row, {
num: i + 1,
text: row.text.join('\n'),
}));
}).join(''),
})).on('click', 'a.example', function() {
var text = $(this).data('text');
// hide dropdown
$('body').trigger('click');
setTimeout(function() {
// load text
editor.setValue(text);
}, 10);
// stop event
return false;
});
// populate links
$('#about-dialog .modal-body ul').html($.map(DATA.links, function(row) {
return TEMPLATES.run('link', row);
}).join(''));
$('#link-dialog').on('show.bs.modal', function() {
$(this).find('input').val(document.location + '?' + $.param({
t: editor.getValue(),
}));
}).on('shown.bs.modal', function() {
$(this).find('input').focus().select();
});
});