422: New $ and html functions, moved navigation
This commit is contained in:
102
index.js
102
index.js
@@ -1,17 +1,19 @@
|
|||||||
HTMLElement.prototype.$ = function(query){
|
HTMLElement.prototype.$ = function(selector) {
|
||||||
return this.querySelectorAll(query)
|
return window.$(selector, this)
|
||||||
|
}
|
||||||
|
DocumentFragment.prototype.$ = function(selector) {
|
||||||
|
return window.$(selector, this)
|
||||||
|
}
|
||||||
|
window.$ = function(selector, el = document) {
|
||||||
|
if(selector[0] === "#" || selector.includes("[name")) {
|
||||||
|
return el.querySelector(selector)
|
||||||
|
} else {
|
||||||
|
return el.querySelectorAll(selector);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
HTMLElement.prototype.addAttribute = function(name) {
|
HTMLElement.prototype.addAttribute = function(name) {
|
||||||
this.setAttribute(name, "")
|
this.setAttribute(name, "")
|
||||||
}
|
}
|
||||||
window.$ = function(selector) {
|
|
||||||
let query = document.querySelectorAll(selector);
|
|
||||||
if(selector[0] === "#") {
|
|
||||||
return query[0];
|
|
||||||
} else {
|
|
||||||
return query;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
window.css = function css(cssString) {
|
window.css = function css(cssString) {
|
||||||
let container = document.querySelector("style#quillStyles");
|
let container = document.querySelector("style#quillStyles");
|
||||||
@@ -36,77 +38,23 @@ window.css = function css(cssString) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
window.html = function html(elementString) {
|
window.html = function html(htmlString) {
|
||||||
let parser = new DOMParser();
|
let container = document.createElement('div');
|
||||||
let doc = parser.parseFromString(elementString, 'text/html');
|
container.innerHTML = htmlString;
|
||||||
let parsedElements = Array.from(doc.body.children);
|
|
||||||
if(parsedElements.length === 1) {
|
// If there's only one child, return it directly
|
||||||
return parsedElements[0]
|
if (container.children.length === 1) {
|
||||||
} else {
|
return container.children[0];
|
||||||
let fragment = document.createDocumentFragment();
|
|
||||||
parsedElements.forEach(node => {
|
|
||||||
fragment.appendChild(node);
|
|
||||||
});
|
|
||||||
return fragment;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function trailingSlash(url) {
|
// If there are multiple children, use a DocumentFragment
|
||||||
return url.endsWith("/") ? url : url+"/";
|
let fragment = document.createDocumentFragment();
|
||||||
}
|
while (container.firstChild) {
|
||||||
function noTrailingSlash(url) {
|
fragment.appendChild(container.firstChild);
|
||||||
return url.endsWith("/") ? url.substring(0, url.length-1) : url;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let oldPushState = history.pushState;
|
return fragment;
|
||||||
history.pushState = function pushState() {
|
|
||||||
let ret = oldPushState.apply(this, arguments);
|
|
||||||
window.dispatchEvent(new Event('pushstate'));
|
|
||||||
window.dispatchEvent(new Event('locationchange'));
|
|
||||||
return ret;
|
|
||||||
};
|
};
|
||||||
window.addEventListener('popstate', () => {
|
|
||||||
window.dispatchEvent(new Event('locationchange'));
|
|
||||||
});
|
|
||||||
|
|
||||||
window.addEventListener('locationchange', locationChange);
|
|
||||||
let urlBeforeChange = window.location.href;
|
|
||||||
|
|
||||||
window.navigateTo = function(url) {
|
|
||||||
window.history.pushState({}, '', url);
|
|
||||||
}
|
|
||||||
|
|
||||||
Object.defineProperty(window, 'routes', {
|
|
||||||
configurable: true,
|
|
||||||
enumerable: true,
|
|
||||||
set: function(newValue) {
|
|
||||||
Object.defineProperty(window, 'routes', {
|
|
||||||
value: newValue,
|
|
||||||
writable: false,
|
|
||||||
configurable: false,
|
|
||||||
enumerable: true
|
|
||||||
});
|
|
||||||
|
|
||||||
locationChange();
|
|
||||||
},
|
|
||||||
get: function() {
|
|
||||||
return window.routes;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
function locationChange() {
|
|
||||||
console.log("location change")
|
|
||||||
let URL = window.location.pathname.split("/").filter(d => (d !== 'Web') && (!d.includes('.html'))).join("/")
|
|
||||||
if(URL === "") URL = "/"
|
|
||||||
let wrapper = document.querySelector("#wrapper");
|
|
||||||
if(wrapper) {
|
|
||||||
wrapper.replaceWith(new window.routes[URL]())
|
|
||||||
} else {
|
|
||||||
document.body.prepend(new window.routes[URL]())
|
|
||||||
document.body.children[0].id = "wrapper"
|
|
||||||
}
|
|
||||||
urlBeforeChange = window.location.href;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function detectMobile() {
|
function detectMobile() {
|
||||||
const mobileDeviceRegex = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i;
|
const mobileDeviceRegex = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i;
|
||||||
|
|||||||
48
navigation.js
Normal file
48
navigation.js
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
let oldPushState = history.pushState;
|
||||||
|
history.pushState = function pushState() {
|
||||||
|
let ret = oldPushState.apply(this, arguments);
|
||||||
|
window.dispatchEvent(new Event('pushstate'));
|
||||||
|
window.dispatchEvent(new Event('locationchange'));
|
||||||
|
return ret;
|
||||||
|
};
|
||||||
|
window.addEventListener('popstate', () => {
|
||||||
|
window.dispatchEvent(new Event('locationchange'));
|
||||||
|
});
|
||||||
|
|
||||||
|
window.addEventListener('locationchange', locationChange);
|
||||||
|
let urlBeforeChange = window.location.href;
|
||||||
|
|
||||||
|
window.navigateTo = function(url) {
|
||||||
|
window.history.pushState({}, '', url);
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.defineProperty(window, 'routes', {
|
||||||
|
configurable: true,
|
||||||
|
enumerable: true,
|
||||||
|
set: function(newValue) {
|
||||||
|
Object.defineProperty(window, 'routes', {
|
||||||
|
value: newValue,
|
||||||
|
writable: false,
|
||||||
|
configurable: false,
|
||||||
|
enumerable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
locationChange();
|
||||||
|
},
|
||||||
|
get: function() {
|
||||||
|
return window.routes;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
function locationChange() {
|
||||||
|
console.log("location change")
|
||||||
|
let URL = window.location.pathname.split("/").filter(d => (d !== 'Web') && (!d.includes('.html'))).join("/")
|
||||||
|
if(URL === "") URL = "/"
|
||||||
|
let wrapper = document.querySelector("#wrapper");
|
||||||
|
if(wrapper) {
|
||||||
|
wrapper.replaceWith(new window.routes[URL]())
|
||||||
|
} else {
|
||||||
|
document.body.prepend(new window.routes[URL]())
|
||||||
|
document.body.children[0].id = "wrapper"
|
||||||
|
}
|
||||||
|
urlBeforeChange = window.location.href;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user