diff options
author | luxagraf <sng@luxagraf.net> | 2018-11-29 16:57:26 -0600 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2018-11-29 16:57:26 -0600 |
commit | 4974eb58480f413c67f5f6e8fac430186eda2b62 (patch) | |
tree | 7402ecba682dbda38db4b6db221cc8378a390977 /scripts/src | |
parent | 0c2a092e8d8ad33a1c306ee9efca0da96eb56415 (diff) |
uploading all recent changes ahead of sys upgrade
Diffstat (limited to 'scripts/src')
-rw-r--r-- | scripts/src/js.cookie.js | 165 | ||||
-rw-r--r-- | scripts/src/main-nav.js | 71 | ||||
-rw-r--r-- | scripts/src/overlay.js | 253 | ||||
-rw-r--r-- | scripts/src/util.js | 107 |
4 files changed, 596 insertions, 0 deletions
diff --git a/scripts/src/js.cookie.js b/scripts/src/js.cookie.js new file mode 100644 index 0000000..9a0945e --- /dev/null +++ b/scripts/src/js.cookie.js @@ -0,0 +1,165 @@ +/*! + * JavaScript Cookie v2.2.0 + * https://github.com/js-cookie/js-cookie + * + * Copyright 2006, 2015 Klaus Hartl & Fagner Brack + * Released under the MIT license + */ +;(function (factory) { + var registeredInModuleLoader = false; + if (typeof define === 'function' && define.amd) { + define(factory); + registeredInModuleLoader = true; + } + if (typeof exports === 'object') { + module.exports = factory(); + registeredInModuleLoader = true; + } + if (!registeredInModuleLoader) { + var OldCookies = window.Cookies; + var api = window.Cookies = factory(); + api.noConflict = function () { + window.Cookies = OldCookies; + return api; + }; + } +}(function () { + function extend () { + var i = 0; + var result = {}; + for (; i < arguments.length; i++) { + var attributes = arguments[ i ]; + for (var key in attributes) { + result[key] = attributes[key]; + } + } + return result; + } + + function init (converter) { + function api (key, value, attributes) { + var result; + if (typeof document === 'undefined') { + return; + } + + // Write + + if (arguments.length > 1) { + attributes = extend({ + path: '/' + }, api.defaults, attributes); + + if (typeof attributes.expires === 'number') { + var expires = new Date(); + expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5); + attributes.expires = expires; + } + + // We're using "expires" because "max-age" is not supported by IE + attributes.expires = attributes.expires ? attributes.expires.toUTCString() : ''; + + try { + result = JSON.stringify(value); + if (/^[\{\[]/.test(result)) { + value = result; + } + } catch (e) {} + + if (!converter.write) { + value = encodeURIComponent(String(value)) + .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent); + } else { + value = converter.write(value, key); + } + + key = encodeURIComponent(String(key)); + key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent); + key = key.replace(/[\(\)]/g, escape); + + var stringifiedAttributes = ''; + + for (var attributeName in attributes) { + if (!attributes[attributeName]) { + continue; + } + stringifiedAttributes += '; ' + attributeName; + if (attributes[attributeName] === true) { + continue; + } + stringifiedAttributes += '=' + attributes[attributeName]; + } + return (document.cookie = key + '=' + value + stringifiedAttributes); + } + + // Read + + if (!key) { + result = {}; + } + + // To prevent the for loop in the first place assign an empty array + // in case there are no cookies at all. Also prevents odd result when + // calling "get()" + var cookies = document.cookie ? document.cookie.split('; ') : []; + var rdecode = /(%[0-9A-Z]{2})+/g; + var i = 0; + + for (; i < cookies.length; i++) { + var parts = cookies[i].split('='); + var cookie = parts.slice(1).join('='); + + if (!this.json && cookie.charAt(0) === '"') { + cookie = cookie.slice(1, -1); + } + + try { + var name = parts[0].replace(rdecode, decodeURIComponent); + cookie = converter.read ? + converter.read(cookie, name) : converter(cookie, name) || + cookie.replace(rdecode, decodeURIComponent); + + if (this.json) { + try { + cookie = JSON.parse(cookie); + } catch (e) {} + } + + if (key === name) { + result = cookie; + break; + } + + if (!key) { + result[name] = cookie; + } + } catch (e) {} + } + + return result; + } + + api.set = api; + api.get = function (key) { + return api.call(api, key); + }; + api.getJSON = function () { + return api.apply({ + json: true + }, [].slice.call(arguments)); + }; + api.defaults = {}; + + api.remove = function (key, attributes) { + api(key, '', extend(attributes, { + expires: -1 + })); + }; + + api.withConverter = init; + + return api; + } + + return init(function () {}); +})); diff --git a/scripts/src/main-nav.js b/scripts/src/main-nav.js new file mode 100644 index 0000000..fa6f25a --- /dev/null +++ b/scripts/src/main-nav.js @@ -0,0 +1,71 @@ +function hideOnClickOutsided(element, btn) { + // given a menu element and btn, hide the menu + // whenever the click is not on either the element + // or the btn the opened it + const outsideClickListener = event => { + if (!element.contains(event.target) && (event.target.id != btn)) { // or use: event.target.closest(selector) === null + if (isVisible(element)) { + element.classList.remove('active') + removeClickListener() + } + } + } + + const removeClickListener = () => { + document.removeEventListener('click', outsideClickListener) + } + document.addEventListener('click', outsideClickListener) +} +const isVisible = elem => !!elem && !!( elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length ) // source (2018-03-11): https://github.com/jquery/jquery/blob/master/src/css/hiddenVisibleSelectors.js + +//---------------------------------------- +//Initialize main menu bar with progressive enhancements +//---------------------------------------------- +// Account Menu +var account_a = document.getElementById("account-menu"); +var account_div = document.getElementById("user-menu"); +//Add button function +account_a.addEventListener('click', function(e){ + e.preventDefault(); + account_div.classList.toggle('active') + account_div.focus(); + hideOnClickOutsided(account_div, account_a.id); +}, false); + +// Notebooks Menu +var notebook_a = document.getElementById("notebook-menu-link"); +var notebook_div = document.getElementById("notebooks-menu"); +//Add button function +notebook_a.addEventListener('click', function(e){ + e.preventDefault(); + notebook_div.classList.toggle('active') + notebook_div.focus(); + hideOnClickOutsided(notebook_div, notebook_a.id); +}, false); + + +//function buildNotebookMenu () { +// var data = JSON.parse(this.responseText); +// var div = document.getElementById("notebooks-menu"); +// var ul = document.createElement("ul"); +// ul.classList.add("vertical","list-style-none"); +// div.appendChild(ul); +// for(var i in data) { +// var li = document.createElement("li"); +// var a = document.createElement("a"); +// a.setAttribute("href", data[i]['json_absolute_url']); +// a.innerHTML = data[i]['name']; +// li.append(a); +// ul.append(li); +// } +// var li = document.createElement("li"); +// var a = document.createElement("a"); +// a.setAttribute("href", '/user/{{user.username}}/notebooks/'); +// a.innerHTML = "View all" +// li.append(a); +// ul.append(li); +//} +// +// +//// getJSON("{%url 'notebook-api-list' %}", buildNotebookMenu); + diff --git a/scripts/src/overlay.js b/scripts/src/overlay.js new file mode 100644 index 0000000..b40d911 --- /dev/null +++ b/scripts/src/overlay.js @@ -0,0 +1,253 @@ +'use strict'; +/** + * @name Novicell overlay + * @desc Simple script that opens an overlay / modal with some content form either a selector or an URL + * @author Danni Larsen (DLA), Michael Sølvsteen (MSL), Signe Helbo Poulsen (SHP), Emil Skytte Ankersen (EAN) + * @example novicell.overlay.create({ 'selector': SELECTOR, 'url': URL, 'class':'CLASSNAME', 'onCreate': FUNCTIONNAME, 'onLoaded': FUNCTIONNAME, 'onDestroy': FUNCTIONNAME }); + * @requires none + */ + +var novicell = novicell || {}; + +novicell.overlay = novicell.overlay || new function () { + var self = this; + var options = {}; + var overlayElem; + var overlayContainer; + var overlayContent; + var backdrop; + var content; + var onCreate; + var onLoaded; + var onDestroy; + var isVideo = false; + + this.create = function (opts) { + var self = this; + // Set global options + options = opts; + + // Call onCreate callback + if (typeof options.onCreate === 'function') { + options.onCreate(); + } + + // Remove existing overlays + self.destroy(); + + // Check if content comes from a DOM selector + if (options.hasOwnProperty('selector') && options.selector !== null) { + var element = document.querySelector(options.selector); + + if (element) { + content = element.innerHTML; + constructOverlay(); + } else { + console.warn('novicell.overlay: element does not exist. Please provide a valid selector for use in document.querySelector.'); + return; + } + } + + // Check if content comes from a HTML element + else if (options.hasOwnProperty('element') && options.element !== null) { + var element = options.element; + + if (element) { + content = element.innerHTML; + constructOverlay(); + } else { + console.warn('novicell.overlay: element does not exist. Please provide a valid DOM element.'); + return; + } + } + + // Or if content comes from an ID + else if (options.hasOwnProperty('videoId')) { + if (options.videoId !== null) { + var src = ''; + isVideo = true; + + if(options.type == 'vimeo') { + src = 'https://player.vimeo.com/video/' + options.videoId + '?autoplay=' + options.autoplay; + } + else if(options.type == 'youtube') { + src = 'https://www.youtube.com/embed/' + options.videoId + '?autoplay=' + options.autoplay + '&rel=0'; + } + else { + return; + } + + var iframe = document.createElement('iframe'); + iframe.setAttribute('src', src); + iframe.setAttribute('frameborder', 0); + iframe.setAttribute('allowfullscreen', ''); + iframe.setAttribute('width', '100%'); + iframe.setAttribute('height', '100%'); + + content = iframe.outerHTML; + + constructOverlay(); + } else { + console.warn('novicell.overlay: video-id is empty. Please provide a video-id for use in video embed code (we support only Vimeo and YouTube).'); + return; + } + } + // If nothing is working, send error to los consolé + else { + console.error('novicell.overlay: no content to display! Please set a selector or a url to load.') + return; + } + }; + + this.destroy = function () { + if(document.querySelector('#js-novi-overlay')) { + // Remove elements + overlayElem.parentElement.removeChild(overlayElem); + backdrop.parentElement.removeChild(backdrop); + + // Stop listening for close overlay events + document.removeEventListener('keyup', self.destroy); + + // Remove class on body + document.documentElement.classList.remove('no-scroll', 'novi-overlay--open'); + + // Reset video variable + isVideo = false; + + // Call onDestroy callback + if (typeof options.onDestroy === 'function') { + options.onDestroy(); + } + } + }; + + function constructOverlay() { + // Create backdrop + setupBackdrop(); + + // Create the overlay + setupOverlay(); + + // Create content for overlay + setupOverlayContainer(); + + // Create close button + setupCloseButton(); + + // Add class to body-element + document.documentElement.classList.add('no-scroll'); + + // Call onLoaded callback + if (typeof options.onLoaded === 'function') { + options.onLoaded(); + } + }; + + function setupBackdrop() { + // Create the backdrop + backdrop = document.createElement('div'); + backdrop.classList.add('novi-backdrop'); + backdrop.id = 'js-novi-backdrop'; + + backdrop.addEventListener('click', function(e){ + if(e.target.classList.contains('novi-overlay') || e.target.classList.contains('novi-overlay__container')) { + self.destroy(); + } + }); + + // Add backdrop to overlay element + document.querySelector('body').appendChild(backdrop); + }; + + /* + * Helper functions for HTML elements + */ + function setupOverlay() { + // Create the overlay + overlayElem = document.createElement('div'); + overlayElem.classList.add('novi-overlay'); + overlayElem.id = 'js-novi-overlay'; + + // Set class for the overlay, if set in options + if (options.hasOwnProperty('class')) { + overlayElem.classList.add(options.class); + } + + // Add overlay to overlay element + // document.querySelector('body').appendChild(overlayElem); + backdrop.appendChild(overlayElem); + }; + + function setupOverlayContainer() { + // Create content for overlay + overlayContainer = document.createElement('div'); + overlayContainer.classList.add('novi-overlay__container'); + + // Create scroll element + overlayContent = document.createElement('div'); + overlayContent.classList.add('novi-overlay__content'); + + if(isVideo) { + overlayContent.classList.add('novi-overlay__content--video') + } + + // Set content + overlayContent.innerHTML = content; + overlayContainer.appendChild(overlayContent); + + // Add overlayContainer to overlay element + overlayElem.appendChild(overlayContainer); + }; + + function setupCloseButton() { + // Create the button + var btnClose = document.createElement('button'); + btnClose.classList.add('novi-overlay-close', 'button--close'); + btnClose.type = 'button'; + btnClose.id = 'js-novi-overlay-close'; + + // Add eventlistener for button click + btnClose.addEventListener('click', self.destroy); + + // Add eventlistener for esc key + document.addEventListener('keydown', function (e) { + if (e.keyCode === 27) { + self.destroy(); + } + }); + + // Add close button to overlay element + overlayContent.appendChild(btnClose); + }; + + /* + * Helper functions for getting content + */ + function get(url) { + // Return a new promise. + return new Promise(function (resolve, reject) { + // Do the usual XHR stuff + var req = new XMLHttpRequest(); + req.open('GET', url); + + req.onload = function () { + if (req.status >= 200 && req.status < 400) { + // Success!! + resolve(req.response); + } else { + // Error!! + reject(Error(req.statusText)); + } + }; + + // Handle network errors + req.onerror = function () { + reject(Error("Network Error")); + }; + + // Make the request + req.send(); + }); + }; + +}(); diff --git a/scripts/src/util.js b/scripts/src/util.js new file mode 100644 index 0000000..3a4efc4 --- /dev/null +++ b/scripts/src/util.js @@ -0,0 +1,107 @@ +function getJSON(url, callback) { + var request = new XMLHttpRequest(); + request.addEventListener("load", callback); + request.open('GET', url, true); + request.onload = function() { + if (request.status >= 200 && request.status < 400) { + //console.log(request.responseText); + } else { + console.log("server error"); + } + }; + request.onerror = function() { + console.log("error on request"); + }; + request.send(); +} + +function edit_note(btn, title, qcontainer, quill, url){ + var formElement = document.getElementById("note-edit-form"); + if (window.editing === false) { + title.setAttribute("contenteditable", true); + title.classList.add('highlight') + qcontainer.classList.remove('inactive') + quill.enable(true); + btn.innerHTML = "Save" + btn.classList.add("save"); + window.editing = true; + window.titlecontents = title.innerHTML + } else { + if (window.quillchange === true || window.titlecontents != title.innerHTML) { + var form_note_title = document.getElementById('id_title'); + var note_html = document.getElementById('id_body_html'); + var note_text = document.getElementById('id_body_text'); + var note_qjson = document.getElementById('id_body_qjson'); + var new_title = document.getElementById('id_title'); + new_title.value = title.innerHTML; + note_html.innerHTML = quill.root.innerHTML; + note_text.innerHTML = quill.getText(); + note_qjson.innerHTML = JSON.stringify(quill.getContents()); + console.log(note_text); + var request = new XMLHttpRequest(); + request.open("PATCH", url); + var csrftoken = Cookies.get('csrftoken'); + request.setRequestHeader("X-CSRFToken", csrftoken) + request.onload = function() { + if (request.status >= 200 && request.status < 400) { + console.log(request); + window.quillchange = false; + } else { + console.log(request); + console.log("server error"); + } + }; + request.onerror = function() { + console.log("error on request"); + }; + request.send(new FormData(formElement)); + } + title.setAttribute("contenteditable", false); + title.classList.remove('highlight') + qcontainer.classList.add('inactive'); + quill.enable(false); + btn.innerHTML = "Edit" + btn.classList.remove("save"); + document.body.focus(); + editing = false; + } + return false; +} + + +function get_login_form() { + var request = new XMLHttpRequest(); + request.open('GET', '/login/', true); + request.onload = function() { + if (request.status >= 200 && request.status < 400) { + } else { + console.log("server error"); + } + }; + request.onerror = function() { + console.log("error on request"); + }; + request.send(); +} + +//Global init for Quill +function initQuill(el) { + window.quill = new Quill(el, { + modules: { + syntax: true, // Include syntax module + toolbar: [ + [{ header: [1, 2, 3, 4, false] }], + ['bold', 'italic', 'underline', 'blockquote'], + [{ 'list': 'bullet'}, { 'list': 'ordered'},{ 'list': 'check'} ], + ['link', 'code-block', 'image', 'video', 'formula',], + [{ 'color': [] }, { 'background': [] }], // dropdown with defaults from theme + [{ 'font': [] }], + ] + }, + theme: 'snow', + enable: false + }); + window.quill.on('text-change', function() { + window.quillchange = true; + }); +} |