From 777d49538b8fe1a47fc15d142249a53a61425072 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Tue, 1 Nov 2016 20:46:38 -0400 Subject: [PATCH] Move H.js library to HTMLhouse Previously this relied on the library stored on write.as --- static/js/h.js | 257 ++++++++++++++++++++++++++++++++++++++++++++++++++ templates/editor.html | 2 +- 2 files changed, 258 insertions(+), 1 deletion(-) create mode 100644 static/js/h.js diff --git a/static/js/h.js b/static/js/h.js new file mode 100644 index 0000000..d22b4cf --- /dev/null +++ b/static/js/h.js @@ -0,0 +1,257 @@ +/** + * H.js + * + * Lightweight, extremely bare-bones library for manipulating the DOM and + * saving some typing. + */ + +var Element = function(domElement) { + this.el = domElement; +}; + +/** + * Creates a toggle button that adds / removes the given class name from the + * given element. + * + * @param {Element} $el - The element to modify. + * @param {string} onClass - The class to add to the given element. + * @param {function} onFunc - Additional actions when toggling on. + * @param {function} offFunc - Additional actions when toggling off. + */ +Element.prototype.createToggle = function($el, onClass, onFunc, offFunc) { + this.on('click', function(e) { + if ($el.el.className === '') { + $el.el.className = onClass; + onFunc(new Element(this), e); + } else { + $el.el.className = ''; + offFunc(new Element(this), e); + } + e.preventDefault(); + }, false); +}; +Element.prototype.on = function(event, func) { + events = event.split(' '); + var el = this.el; + if (el == null) { + console.error("Error: element for event is null"); + return; + } + var addEvent = function(e) { + if (el.addEventListener) { + el.addEventListener(e, func, false); + } else if (el.attachEvent) { + el.attachEvent(e, func); + } + }; + if (events.length === 1) { + addEvent(event); + } else { + for(var i=0; i summaryLen) { + summary = summary.substring(0, summaryLen) + "..."; + } + return { + title: content.substring("# ".length, eol), + summary: summary, + }; + } + + var blankLine = content.indexOf("\n\n"); + if (blankLine !== -1 && blankLine <= eol && blankLine <= titleLen) { + // Title is in the format: + // + // Some title + // + // The body starts after that blank line above it. + var summary = content.substring(blankLine).trim(); + if (summary.length > summaryLen) { + summary = summary.substring(0, summaryLen) + "..."; + } + return { + title: content.substring(0, blankLine), + summary: summary, + }; + } + + // TODO: move this to the beginning + var title = content.trim(); + var summary = ""; + if (title.length > titleLen) { + // Content can't fit in the title, so figure out the summary + summary = title; + title = ""; + if (summary.length > summaryLen) { + summary = summary.substring(0, summaryLen) + "..."; + } + } else if (eol > 0) { + summary = title.substring(eol+1); + title = title.substring(0, eol); + } + return { + title: title, + summary: summary + }; + }; + + var post = getPostMeta(content); + post.id = id; + post.token = editToken; + post.created = created ? new Date(created) : new Date(); + post.client = "Pad"; + + return post; + }, + getTitleStrict: function(content) { + var eol = content.indexOf("\n"); + var title = ""; + var newContent = content; + if (content.indexOf("# ") === 0) { + // Title is in the format: + // # Some title + if (eol !== -1) { + // First line should start with # and end with \n + newContent = content.substring(eol).leftTrim(); + title = content.substring("# ".length, eol); + } + } + return { + title: title, + content: newContent + }; + }, +}; + +var He = { + create: function(name) { + return document.createElement(name); + }, + get: function(id) { + return document.getElementById(id); + }, + $: function(selector) { + var els = document.querySelectorAll(selector); + return els; + }, + postJSON: function(url, params, callback) { + var http = new XMLHttpRequest(); + + http.open("POST", url, true); + + // Send the proper header information along with the request + http.setRequestHeader("Content-type", "application/json"); + + http.onreadystatechange = function() { + if (http.readyState == 4) { + callback(http.status, JSON.parse(http.responseText)); + } + } + http.send(JSON.stringify(params)); + }, +}; + +String.prototype.leftTrim = function() { + return this.replace(/^\s+/,""); +}; diff --git a/templates/editor.html b/templates/editor.html index adbb75a..8912f17 100644 --- a/templates/editor.html +++ b/templates/editor.html @@ -49,7 +49,7 @@ - +