summaryrefslogtreecommitdiffstats
path: root/template/admin/rsrc/main.js
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2017-05-19 01:05:15 +0200
committerThomas Lange <code@nerdmind.de>2017-05-21 07:54:28 +0200
commiteeddde7ae72d7f68c0a734d1fe7004cde847d1e5 (patch)
tree041aeec3474fdd7169c5b6c235674a15423aceea /template/admin/rsrc/main.js
parent6b4faa47d02094d3f9fe637fc83431e7c49004cd (diff)
downloadblog-eeddde7ae72d7f68c0a734d1fe7004cde847d1e5.tar.gz
blog-eeddde7ae72d7f68c0a734d1fe7004cde847d1e5.tar.xz
blog-eeddde7ae72d7f68c0a734d1fe7004cde847d1e5.zip
Several changes have been made to the admin template, which together with the previous commits result in version 2.1.1:v2.1.1
+ Optimization [CSS]: Unused .fa-* classes have been removed. + Optimization [CSS]: Hyphens have been disabled for the text within the content editor. + Optimization [CSS]: Some elements now have a smaller padding value which has benefits if the page is displayed on a mobile device. + Optimization [HTML]: The HTML <section> elements of the formular have been replaced with simple <div> elements because <section> elements should only be used for self-contained areas which have nothing to do with the rest of the content. The using of <section> elements at this place was semantically incorrect. + Implemented [JS]: Tab indents which are made by pressing <tab> can now also be removed by pressing <shift>+<tab>. + Optimization [JS]: The functions "emoticonReplace" and "markdownReplace" has been renamed to "insertEmoticon" and "insertMarkdown" and have been restructured. + Optimization [JS]: The DOMContentLoaded event listeners are not required because the file is included with the "defer" attribute. + Optimization [JS]: A function called "delayed" was added which executes a callback function after an delay of 20 ms (the delayed execution of code is necessary for the "insertEmoticon" and "insertMarkdown" functions because the content editor otherwise will losing focus when clicking outside at the emoticon or markdown buttons while the "onmousedown" event listener is still executing).
Diffstat (limited to 'template/admin/rsrc/main.js')
-rw-r--r--template/admin/rsrc/main.js135
1 files changed, 71 insertions, 64 deletions
diff --git a/template/admin/rsrc/main.js b/template/admin/rsrc/main.js
index a93b8be..f6f7884 100644
--- a/template/admin/rsrc/main.js
+++ b/template/admin/rsrc/main.js
@@ -7,10 +7,10 @@ var next = document.getElementById("next-site");
//==============================================================================
// Handle arrow keys and change the location to the desired direction
//==============================================================================
-document.addEventListener("keyup", function(event) {
- if(!event.ctrlKey && !event.shiftKey) {
- (event.keyCode === 37 && prev) && (window.location.href = prev.getAttribute("href"));
- (event.keyCode === 39 && next) && (window.location.href = next.getAttribute("href"));
+document.addEventListener("keyup", function(e) {
+ if(!e.ctrlKey && !e.shiftKey) {
+ (e.keyCode === 37 && prev) && (window.location.href = prev.getAttribute("href"));
+ (e.keyCode === 39 && next) && (window.location.href = next.getAttribute("href"));
}
}, false);
@@ -30,124 +30,131 @@ var markdownTags = {
};
//==============================================================================
+// Timeout function for delayed execution of code
+//==============================================================================
+function delayed(callback) {
+ window.setTimeout(callback, 20);
+}
+
+//==============================================================================
// Set caret position in editor
//==============================================================================
function setCaretPosition(position) {
- window.setTimeout(function() {
- document.getElementById("content-editor").focus();
- document.getElementById("content-editor").setSelectionRange(position, position);
- }, 50);
-
+ document.getElementById("content-editor").setSelectionRange(position, position);
+ document.getElementById("content-editor").focus();
}
//==============================================================================
-// Insert markdown around text in editor
+// Insert emoticon after cursor in editor
//==============================================================================
-function markdownReplace(tagname) {
- var element = document.activeElement;
+function insertEmoticon(target, emoticon) {
+ var selectionStart = target.selectionStart;
+ var selectionEnd = target.selectionEnd;
- if(element.nodeName === 'TEXTAREA') {
- var selectionStart = element.selectionStart;
- var selectionEnd = element.selectionEnd;
+ var content = target.value;
+ target.value = content.slice(0, selectionStart) + emoticon + content.slice(selectionEnd);
- var selectedText = element.value.substring(selectionStart, selectionEnd);
-
- var content = element.value;
- element.value = content.slice(0, selectionStart) + markdownTags[tagname][0] + selectedText + markdownTags[tagname][1] + content.slice(selectionEnd);
-
- setCaretPosition(selectionStart + markdownTags[tagname][0].length + selectedText.length + markdownTags[tagname][1].length);
- }
+ delayed(function() {
+ setCaretPosition(selectionStart + emoticon.length);
+ });
}
//==============================================================================
-// Insert emoticon after cursor in editor
+// Insert markdown around text in editor
//==============================================================================
-function emoticonReplace(emoticon) {
- var element = document.activeElement;
+function insertMarkdown(target, markdown) {
+ var selectionStart = target.selectionStart;
+ var selectionEnd = target.selectionEnd;
- if(element.nodeName === 'TEXTAREA') {
- var selectionStart = element.selectionStart;
- var selectionEnd = element.selectionEnd;
+ var selectedText = target.value.substring(selectionStart, selectionEnd);
- var content = element.value;
- element.value = content.slice(0, selectionStart) + emoticon + content.slice(selectionEnd);
+ var content = target.value;
+ target.value = content.slice(0, selectionStart) + markdownTags[markdown][0] + selectedText + markdownTags[markdown][1] + content.slice(selectionEnd);
- setCaretPosition(selectionStart + emoticon.length);
- }
+ delayed(function() {
+ setCaretPosition(selectionStart + markdownTags[markdown][0].length + selectedText.length + markdownTags[markdown][1].length);
+ });
}
//==============================================================================
// Keep server-side session active if the user is writing a long text
//==============================================================================
-addEventListener("DOMContentLoaded", function() {
- setInterval(function() {
- var Request = new XMLHttpRequest();
- Request.open("HEAD", "", true);
- Request.send();
- }, 300000);
-}, false);
+setInterval(function() {
+ var Request = new XMLHttpRequest();
+ Request.open("HEAD", "", true);
+ Request.send();
+}, 300000);
//==============================================================================
-// Insert tab indent into editor if <tab> is pressed
+// Confirmation message for delete button
//==============================================================================
-addEventListener("DOMContentLoaded", function() {
+if(document.getElementById("delete-button")) {
+ document.getElementById("delete-button").onclick = function(e) {
+ return confirm(e.target.getAttribute('data-text'));
+ };
+}
+
+//==============================================================================
+// Insert or remove tab indent in editor if [<shift>+]<tab> is pressed
+//==============================================================================
+(function() {
if(document.getElementById("content-editor")) {
var element = document.getElementById("content-editor");
element.addEventListener('keydown', function(e) {
- if(e.keyCode === 9 && !e.ctrlKey && !e.shiftKey) {
+ if(e.keyCode === 9 && !e.ctrlKey) {
var selectionStart = element.selectionStart;
var selectionEnd = element.selectionEnd;
var content = element.value;
- element.value = content.substring(0, selectionStart) + "\t" + content.substring(selectionEnd);
+ if(e.shiftKey) {
+ if(content.substring(selectionStart, selectionStart -1) === "\t") {
+ element.value = content.substring(0, selectionStart - 1) + content.substring(selectionEnd);
+ setCaretPosition(selectionStart - 1);
+ }
+ }
+
+ else {
+ element.value = content.substring(0, selectionStart) + "\t" + content.substring(selectionEnd);
+ setCaretPosition(selectionStart + 1);
+ }
- setCaretPosition(selectionStart + 1);
e.preventDefault();
}
}, false);
}
-}, false);
+})();
//==============================================================================
-// Confirmation message for delete buttons
+// Emoticon button list
//==============================================================================
-addEventListener("DOMContentLoaded", function() {
- if(document.getElementById("delete-button")) {
- document.getElementById("delete-button").onclick = function(e) {
- return confirm(e.target.getAttribute('data-text'));
- };
- }
-}, false);
-
-//==============================================================================
-// Emoticon listener
-//==============================================================================
-addEventListener("DOMContentLoaded", function() {
+(function() {
if(document.getElementById("emoticon-list")) {
var list = document.getElementById("emoticon-list");
+ var node = document.getElementById("content-editor");
var items = list.getElementsByTagName("li");
for(var i = 0; i < items.length; ++i) {
items[i].onmousedown = function(e) {
- emoticonReplace(e.target.getAttribute('data-emoticon'));
+ insertEmoticon(node, e.target.getAttribute('data-emoticon'));
};
}
}
-}, false);
+})();
//==============================================================================
-// Markdown listener
+// Markdown button list
//==============================================================================
-addEventListener("DOMContentLoaded", function() {
+(function() {
if(document.getElementById("markdown-list")) {
var list = document.getElementById("markdown-list");
+ var node = document.getElementById("content-editor");
var items = list.getElementsByTagName("li");
for(var i = 0; i < items.length; ++i) {
items[i].onmousedown = function(e) {
- markdownReplace(e.target.getAttribute('data-markdown'));
+ insertMarkdown(node, e.target.getAttribute('data-markdown'));
};
}
}
-}, false); \ No newline at end of file
+})(); \ No newline at end of file