Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove jquery dependency #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 51 additions & 29 deletions forum-updater.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
var qS = document.querySelector.bind(document),
wmdButtonBar = qS('.wmd-button-bar');

function toggle(el){
if(el.style.display === "none") el.style.display = "initial";
else el.style.display = "none";
}

function hasClass(el, class){
if (el.classList)
el.classList.contains(class);
else
new RegExp('(^| )' + class + '( |$)', 'gi').test(el.class);
}

function toggleClass(el, class){
if(hasClass(el, class)) el.classList.remove(class);
else el.classList.add(class);
}

// jquery equivlent of .not
function jqNot(nodelist, sel){
[].filter.call(nodelist, function(el) {
return !el.matches(sel);
});
}

function forumUpdater() {
// for use below, they need to be defined globally
var cannedResponses,
HtmlListOfCannedResponses;
HtmlListOfCannedResponses,
cannedResponsesContainer = qS('.canned-response-container');

// get the canned responses
function getCannedResponses() {
Expand All @@ -10,8 +38,7 @@ function forumUpdater() {
localStorage.setItem("Canned Responses", JSON.stringify(cannedResponses));
}

cannedResponses = localStorage.getItem("Canned Responses");
cannedResponses = JSON.parse(cannedResponses);
cannedResponses = JSON.parse(localStorage.getItem("Canned Responses"));

return cannedResponses;
};
Expand All @@ -22,69 +49,69 @@ function forumUpdater() {

HtmlListOfCannedResponses = '<li id="new-canned-response"><a href="javascript:void(0);">Create new</a></li>';

for (i in cannedResponses) {
for (var i in cannedResponses)
HtmlListOfCannedResponses += '<li data-canned-response-id="' + i + '"><a href="javascript:void(0);">' + cannedResponses[i]["name"] + '</a></li>';
}


return HtmlListOfCannedResponses;
};

// add the canned response button to the formatting bar
function addCannedResponseButton() {
$('.wmd-button-bar .wmd-button-row').append('<div class=\"wmd-spacer\" id=\"wmd-spacer3\"></div><button class=\"wmd-button wmd-canned-response-button\" id=\"wmd-canned-response-button\" title=\"Canned responses\" aria-label=\"Canned responses\"></button>');
$('.wmd-button-bar').append('<div class="canned-response-container" id="canned-response-container"><ul id="canned-responses-list">' + HtmlListOfCannedResponses + '</ul></div>');
$('.canned-response-container').hide();
qS('.wmd-button-bar .wmd-button-row').innerHTML += '<div class=\"wmd-spacer\" id=\"wmd-spacer3\"></div><button class=\"wmd-button wmd-canned-response-button\" id=\"wmd-canned-response-button\" title=\"Canned responses\" aria-label=\"Canned responses\"></button>';
wmdButtonBar.innerHTML += '<div class="canned-response-container" id="canned-response-container"><ul id="canned-responses-list">' + HtmlListOfCannedResponses + '</ul></div>';
cannedResponsesContainer.style.display = "none";
};

// create a new canned response
function newCannedResponse() {
var textarea = document.querySelector("textarea"),
var textarea = qS("textarea"),
start = textarea.selectionStart,
end = textarea.selectionEnd,
cannedResponseText = textarea.value.substring(start, end),
cannedResponseName = prompt("Please name your canned response:", cannedResponseText.slice(0, 10) + "...");

if (!cannedResponseName) {
if (!cannedResponseName)
return "You need to name your canned response!";
}

cannedResponses[Object.keys(cannedResponses).length] = { "name": cannedResponseName, "body": cannedResponseText }
localStorage.setItem("Canned Responses", JSON.stringify(cannedResponses));

generateHtmlListOfCannedResponses();

$('#canned-responses-list').html(HtmlListOfCannedResponses);
qS('#canned-responses-list').innerHTML = HtmlListOfCannedResponses;
};

// put the canned response text into the textarea
function prefillWithCannedResponse(text) {
$('.wmd-input')[0].value = text;
$('#canned-response-container').toggle();
qS('.wmd-input')[0].value = text;
toggle(cannedResponsesContainer);
};

// collect all the other functions together and run them
function runTheCannedResponseFunctions() {
generateHtmlListOfCannedResponses();
addCannedResponseButton();

$('#wmd-canned-response-button').click(function() {
$('.canned-response-container').toggle();
$('.wmd-button-bar').toggleClass("active");
qS('#wmd-canned-response-button').addEventListener("click", function() {
toggle(cannedResponsesContainer);
toggleClass(wmdButtonBar, "active");


$('#canned-responses-list li').not('#new-canned-response').click(function() {
prefillWithCannedResponse(cannedResponses[$(this).attr("data-canned-response-id")]["body"]);
$('.wmd-button-bar').toggleClass("active");
jqNot(qS('#canned-responses-list li'), '#new-canned-response').addEventListener("click", function() {
prefillWithCannedResponse(cannedResponses[this.getAttribute("data-canned-response-id")]["body"]);
toggleClass(wmdButtonBar, "active");
});

$('#new-canned-response').click(function() {
qS('#new-canned-response').addEventListener("click", function() {
newCannedResponse();
$('.wmd-button-bar').toggleClass("active");
toggleClass(wmdButtonBar, "active");
});
});
}

// detect when the text box is popped up, then run
var target = document.querySelector('#reply-control'),
var target = qS('#reply-control'),
config = {
attributes: true,
childList: true,
Expand All @@ -94,15 +121,10 @@ function forumUpdater() {

var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if ($('#wmd-canned-response-button').length < 1) {
if (document.querySelectorAll('#wmd-canned-response-button').length < 1)
runTheCannedResponseFunctions();
}
});
});

observer.observe(target, config);
}

var script = document.createElement("script");
script.textContent = "$(document).ready(" + forumUpdater.toString() + ")";
document.documentElement.appendChild(script);