diff options
author | luxagraf <sng@luxagraf.net> | 2018-12-06 07:03:24 -0600 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2018-12-06 07:03:24 -0600 |
commit | 4eaaa999c89293677dade99568a279c934e0b8d9 (patch) | |
tree | c543451d3ca149681460f45ab6161a813cf4ed29 /scripts | |
parent | cb21e59587ab328e9a3e06c9d57c2bafce281715 (diff) |
redid scripts to use just npm
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/gulpfile.js | 17 | ||||
-rw-r--r-- | scripts/package.json | 40 | ||||
-rw-r--r-- | scripts/src/js.cookie.js | 165 | ||||
-rw-r--r-- | scripts/src/main-nav.js | 56 | ||||
-rw-r--r-- | scripts/src/overlay.js | 253 | ||||
-rw-r--r-- | scripts/src/util.js | 79 |
6 files changed, 59 insertions, 551 deletions
diff --git a/scripts/gulpfile.js b/scripts/gulpfile.js deleted file mode 100644 index 67c5bf7..0000000 --- a/scripts/gulpfile.js +++ /dev/null @@ -1,17 +0,0 @@ -const { src, dest } = require('gulp'); -const uglify = require('gulp-uglify'); -const rename = require('gulp-rename'); -const concat = require('gulp-concat'); -const babel = require('gulp-babel'); - -exports.default = function() { - return src('src/*.js') - .pipe(babel({ - presets: ['@babel/env'] - })) - .pipe(concat('package.js')) - .pipe(uglify()) - .pipe(dest('../media/js/')) - .pipe(rename({ extname: '.min.js' })) - .pipe(dest('../media/js/')); -} diff --git a/scripts/package.json b/scripts/package.json index 778b5e6..890274e 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -1,20 +1,24 @@ { - "name": "scripts", - "version": "1.0.0", - "description": "javascript for aite", - "main": "common.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "luxagraf", - "license": "ISC", - "devDependencies": { - "@babel/core": "^7.1.6", - "@babel/preset-env": "^7.1.6", - "gulp": "^4.0.0", - "gulp-babel": "^8.0.0", - "gulp-concat": "^2.6.1", - "gulp-rename": "^1.4.0", - "gulp-uglify": "^3.0.1" - } + "name": "notes-app", + "version": "1.0.0", + "author": "luxagraf", + "description": "Compile JS for Notes app", + "main": "src/js/index.js", + "scripts": { + "eslint": "eslint src/*.js --fix", + "babel": "mkdir -p tmp && babel src/*.js -d tmp", + "include": "cp src/lib/*.js tmp/", + "uglify:local": "mkdir -p ../media/js && uglifyjs tmp/*.js -m -c -o ../media/js/main.min.js", + "uglify:deploy": "mkdir -p ../media/js && uglifyjs tmp/*.js -m -c drop_console=true -o ../media/js/main.min.js", + "cleanup": "rm -rf tmp", + "build": "pnpm run babel && pnpm run include && pnpm run uglify:local && pnpm run cleanup", + "deploy": "pnpm run babel && pnpm run include && pnpm run uglify && pnpm run cleanup", + "watch": "watch 'pnpm run build' ." + }, + "devDependencies": { + "@babel/cli": "^7.1.5", + "@babel/core": "^7.1.6", + "@babel/preset-env": "^7.1.6", + "eslint": "^5.9.0" + } } diff --git a/scripts/src/js.cookie.js b/scripts/src/js.cookie.js deleted file mode 100644 index 9a0945e..0000000 --- a/scripts/src/js.cookie.js +++ /dev/null @@ -1,165 +0,0 @@ -/*! - * 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 index fa6f25a..c85cb4f 100644 --- a/scripts/src/main-nav.js +++ b/scripts/src/main-nav.js @@ -5,44 +5,46 @@ function hideOnClickOutsided(element, btn) { 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() + element.classList.remove('active'); + removeClickListener(); } } - } + }; const removeClickListener = () => { - document.removeEventListener('click', outsideClickListener) - } - document.addEventListener('click', outsideClickListener) + 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 +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 +//if we find the menu items //---------------------------------------------- // 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); +if (document.getElementById('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); diff --git a/scripts/src/overlay.js b/scripts/src/overlay.js deleted file mode 100644 index b40d911..0000000 --- a/scripts/src/overlay.js +++ /dev/null @@ -1,253 +0,0 @@ -'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 index 3a4efc4..dfaeeea 100644 --- a/scripts/src/util.js +++ b/scripts/src/util.js @@ -1,87 +1,24 @@ -function getJSON(url, callback) { +function getJSON(method, url, callback) { var request = new XMLHttpRequest(); - request.addEventListener("load", callback); - request.open('GET', url, true); + request.addEventListener('load', callback); + request.open(method, url, true); request.onload = function() { if (request.status >= 200 && request.status < 400) { //console.log(request.responseText); } else { - console.log("server error"); + console.log('server error'); } }; request.onerror = function() { - console.log("error on request"); + 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(); + getJSON('GET', '/login/', function(e){ + console.log(e); + }); } //Global init for Quill |