From 197a1f8326ed46714856473117c41e3158337516 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 16 Feb 2016 12:07:01 -0500 Subject: [PATCH] Update some javascript documentation, and add show/hide methods --- public/js/anime_edit.js | 2 +- public/js/base/AnimeClient.js | 45 ++++++++++++++++++++++++++++++++--- public/js/base/events.js | 2 +- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/public/js/anime_edit.js b/public/js/anime_edit.js index c35e930e..61235b09 100755 --- a/public/js/anime_edit.js +++ b/public/js/anime_edit.js @@ -40,7 +40,7 @@ mimeType: 'application/json', success: (res) => { if (res.status === 'completed') { - this.parentElement.addAttribute('hidden', 'hidden'); + _.hide(parent_sel); } _.showMessage('success', `Sucessfully updated ${title}`); diff --git a/public/js/base/AnimeClient.js b/public/js/base/AnimeClient.js index 815976e2..11fac8aa 100644 --- a/public/js/base/AnimeClient.js +++ b/public/js/base/AnimeClient.js @@ -2,8 +2,6 @@ var AnimeClient = (function(w) { 'use strict'; - const slice = Array.prototype.slice; - // ------------------------------------------------------------------------- // ! Base // ------------------------------------------------------------------------- @@ -40,7 +38,7 @@ var AnimeClient = (function(w) { if (selector.match(/^#([\w]+$)/)) { elements.push(document.getElementById(selector.split('#')[1])); } else { - elements = slice.apply(context.querySelectorAll(selector)); + elements = [].slice.apply(context.querySelectorAll(selector)); } return elements; @@ -53,6 +51,24 @@ var AnimeClient = (function(w) { scrollToTop() { w.scroll(0,0); }, + /** + * Hide the selected element + * + * @param {string|Element} sel - the selector of the element to hide + * @return {void} + */ + hide(sel) { + sel.setAttribute('hidden', 'hidden'); + }, + /** + * UnHide the selected element + * + * @param {string|Element} sel - the selector of the element to hide + * @return {void} + */ + show(sel) { + sel.removeAttribute('hidden'); + }, /** * Display a message box * @@ -164,6 +180,15 @@ var AnimeClient = (function(w) { }); } + /** + * Add an event listener + * + * @param {string|element} sel - the parent selector to bind to + * @param {string} event - event name(s) to bind + * @param {string|element} [target] - the element to directly bind the event to + * @param {function} listener - event listener callback + * @return {void} + */ _.on = function (sel, event, target, listener) { if (arguments.length === 3) { listener = target; @@ -203,6 +228,20 @@ var AnimeClient = (function(w) { return pairs.join("&"); }; + /** + * Make an ajax request + * + * Config:{ + * data: // data to send with the request + * type: // http verb of the request, defaults to GET + * success: // success callback + * error: // error callback + * } + * + * @param {string} url - the url to request + * @param {Object} config - the configuration object + * @return {void} + */ _.ajax = function(url, config) { // Set some sane defaults config = config || {}; diff --git a/public/js/base/events.js b/public/js/base/events.js index 22fc3715..541238c1 100644 --- a/public/js/base/events.js +++ b/public/js/base/events.js @@ -7,7 +7,7 @@ // Close event for messages ac.on('header', 'click', '.message', function () { - this.setAttribute('hidden', 'hidden'); + ac.hide(this); }); })(AnimeClient); \ No newline at end of file