refactoring of javascripts

This commit is contained in:
Simon Vieille 2022-09-08 10:26:58 +02:00
parent a6a30078bb
commit 7d81521c8c
Signed by: deblan
GPG key ID: 579388D585F70417
12 changed files with 383 additions and 383 deletions

View file

@ -1,17 +1,19 @@
const App = function (components) {
this.components = components || []
}
class App {
constructor(components) {
this.components = components || []
}
App.prototype.add = function (c) {
this.components.push(c)
add(c) {
this.components.push(c)
return this
}
return this
}
App.prototype.init = function () {
for (let u = 0, x = this.components.length; u < x; u++) {
this.components[u].init()
}
init() {
for (let u = 0, x = this.components.length; u < x; u++) {
this.components[u].init()
}
}
}
module.exports = App

View file

@ -23,76 +23,78 @@ require('prismjs/plugins/keep-markup/prism-keep-markup')
require('prismjs/plugins/line-highlight/prism-line-highlight')
require('prismjs/plugins/line-numbers/prism-line-numbers')
const Code = function (w) {
this.window = w
class Code {
constructor(w) {
this.window = w
}
init() {
Prism.highlightAllUnder(document)
let elements = this.window.document.querySelectorAll('code[data-title], div[data-title]')
for (let i = 0, len = elements.length; i < len; i++) {
const element = elements[i]
if (element.tagName === 'CODE') {
var code = element
var pre = code.parentNode
var post = pre.parentNode
} else {
var code = element.querySelector('code')
if (!code) {
continue
}
var pre = code.parentNode
var post = pre.parentNode
}
if (!pre || !post) {
continue
}
pre.classList.add('with-title')
const title = this.window.document.createElement('div')
title.classList.add('code-title')
title.textContent = element.getAttribute('data-title')
post.insertBefore(title, pre)
}
elements = this.window.document.querySelectorAll('code.window')
for (let i = 0, len = elements.length; i < len; i++) {
const element = elements[i]
if (element.tagName === 'CODE') {
var code = element
var pre = code.parentNode
var post = pre.parentNode
} else {
var code = element.querySelector('code')
if (!code) {
continue
}
var pre = code.parentNode
var post = pre.parentNode
}
if (!pre || !post) {
continue
}
pre.classList.add('with-title')
const win = this.window.document.createElement('div')
win.classList.add('code-window')
win.textContent = 'Console'
post.insertBefore(win, pre)
}
}
}
Code.prototype.init = function () {
Prism.highlightAllUnder(document)
let elements = this.window.document.querySelectorAll('code[data-title], div[data-title]')
for (let i = 0, len = elements.length; i < len; i++) {
const element = elements[i]
if (element.tagName === 'CODE') {
var code = element
var pre = code.parentNode
var post = pre.parentNode
} else {
var code = element.querySelector('code')
if (!code) {
continue
}
var pre = code.parentNode
var post = pre.parentNode
}
if (!pre || !post) {
continue
}
pre.classList.add('with-title')
const title = this.window.document.createElement('div')
title.classList.add('code-title')
title.textContent = element.getAttribute('data-title')
post.insertBefore(title, pre)
}
elements = this.window.document.querySelectorAll('code.window')
for (let i = 0, len = elements.length; i < len; i++) {
const element = elements[i]
if (element.tagName === 'CODE') {
var code = element
var pre = code.parentNode
var post = pre.parentNode
} else {
var code = element.querySelector('code')
if (!code) {
continue
}
var pre = code.parentNode
var post = pre.parentNode
}
if (!pre || !post) {
continue
}
pre.classList.add('with-title')
const win = this.window.document.createElement('div')
win.classList.add('code-window')
win.textContent = 'Console'
post.insertBefore(win, pre)
}
}
module.exports = Code
module.exports = Code

View file

@ -1,30 +1,32 @@
const Routing = require('./routing')
const FormPnw = function (w) {
this.window = w
}
FormPnw.prototype.init = function () {
const doc = this.window.document
doc.addEventListener('mousemove', function () {
const forms = doc.querySelectorAll('form[data-form-bot]')
for (let i = 0, len = forms.length; i < len; i++) {
const form = forms[i]
let action = form.getAttribute('action')
action = action.replace(
Routing.generate('blog_tech_form_without_javascript', {
_domain: window.location.hostname
}, false) + '?page=', ''
)
action = decodeURIComponent(action)
form.setAttribute('action', action)
form.removeAttribute('data-form-bot')
class FormPnw {
constructor(w) {
this.window = w
}
init() {
const doc = this.window.document
doc.addEventListener('mousemove', function() {
const forms = doc.querySelectorAll('form[data-form-bot]')
for (let i = 0, len = forms.length; i < len; i++) {
const form = forms[i]
let action = form.getAttribute('action')
action = action.replace(
Routing.generate('blog_tech_form_without_javascript', {
_domain: window.location.hostname
}, false) + '?page=', ''
)
action = decodeURIComponent(action)
form.setAttribute('action', action)
form.removeAttribute('data-form-bot')
}
})
}
})
}
module.exports = FormPnw

View file

@ -1,34 +1,36 @@
const Mario = require('../../images/mario.gif')
const Knmc = function (w) {
this.window = w
}
Knmc.prototype.init = function () {
let chars = ''
const seq = '38384040373937396665'
const body = this.window.document.querySelector('body')
body.addEventListener('keyup', function (e) {
chars += e.keyCode.toString()
if (chars.indexOf(seq) !== -1) {
chars = ''
const url = Mario
const image = new Image()
image.classList.add('fixed')
image.style.position = 'fixed'
image.style.bottom = '-11px'
image.style.left = '-256px'
image.onload = function () {
image.classList.add('knmc')
body.appendChild(image)
}
image.src = url
class Knmc {
constructor(w) {
this.window = w
}
init() {
let chars = ''
const seq = '38384040373937396665'
const body = this.window.document.querySelector('body')
body.addEventListener('keyup', function(e) {
chars += e.keyCode.toString()
if (chars.indexOf(seq) !== -1) {
chars = ''
const url = Mario
const image = new Image()
image.classList.add('fixed')
image.style.position = 'fixed'
image.style.bottom = '-11px'
image.style.left = '-256px'
image.onload = function() {
image.classList.add('knmc')
body.appendChild(image)
}
image.src = url
}
})
}
})
}
module.exports = Knmc

View file

@ -1,11 +1,10 @@
const lozad = require('lozad')
const LazyLoad = function () {
}
LazyLoad.prototype.init = function () {
const observer = lozad('.lazy-img')
observer.observe()
class LazyLoad {
init() {
const observer = lozad('.lazy-img')
observer.observe()
}
}
module.exports = LazyLoad

View file

@ -1,34 +1,36 @@
const tingle = require('tingle.js/src/tingle.js')
const MeshViewer = function (w) {
this.window = w
}
class MeshViewer {
constructor(w) {
this.window = w
}
MeshViewer.prototype.init = function () {
const openers = this.window.document.querySelectorAll('*[data-modal]')
init() {
const openers = this.window.document.querySelectorAll('*[data-modal]')
for (let i = 0, len = openers.length; i < len; i++) {
openers[i].addEventListener('click', (e) => {
e.preventDefault()
for (let i = 0, len = openers.length; i < len; i++) {
openers[i].addEventListener('click', (e) => {
e.preventDefault()
let target = e.target
let target = e.target
if (target.tagName != 'A') {
target = target.parentNode
}
if (target.tagName != 'A') {
target = target.parentNode
}
const modal = new tingle.modal({
footer: false,
stickyFooter: false,
closeMethods: ['overlay', 'button', 'escape'],
closeLabel: 'Close',
cssClass: ['tingle-modal-box--mesh']
})
const modal = new tingle.modal({
footer: false,
stickyFooter: false,
closeMethods: ['overlay', 'button', 'escape'],
closeLabel: 'Close',
cssClass: ['tingle-modal-box--mesh']
})
modal.setContent('<iframe src="' + target.getAttribute('href') + '"></iframe>')
modal.open()
})
}
modal.setContent('<iframe src="' + target.getAttribute('href') + '"></iframe>')
modal.open()
})
}
}
}
module.exports = MeshViewer

View file

@ -1,46 +1,48 @@
require('particles.js')
const Particles = function (w) {
this.window = w
class Particles {
constructor(w) {
this.window = w
}
start() {
if (this.window.innerWidth < 708) {
return
}
const height = this.header.offsetHeight
const width = this.header.offsetWidth
let canvas
this.particles.style.maxHeight = height + 'px'
this.particles.style.maxWidth = width + 'px'
particlesJS.load('particles', '/js/particles.json?v=3')
}
clean() {
this.particles.innerHTML = ''
}
init() {
this.particles = this.window.document.getElementById('particles')
if (!this.particles) {
return
}
this.header = this.particles.parentNode
this.clean()
this.start()
const that = this
this.window.addEventListener('resize', function() {
that.clean()
that.start()
}, false)
}
}
Particles.prototype.start = function () {
if (this.window.innerWidth < 708) {
return
}
const height = this.header.offsetHeight
const width = this.header.offsetWidth
let canvas
this.particles.style.maxHeight = height + 'px'
this.particles.style.maxWidth = width + 'px'
particlesJS.load('particles', '/js/particles.json?v=3')
}
Particles.prototype.clean = function () {
this.particles.innerHTML = ''
}
Particles.prototype.init = function () {
this.particles = this.window.document.getElementById('particles')
if (!this.particles) {
return
}
this.header = this.particles.parentNode
this.clean()
this.start()
const that = this
this.window.addEventListener('resize', function () {
that.clean()
that.start()
}, false)
}
module.exports = Particles
module.exports = Particles

View file

@ -1,120 +1,122 @@
const Routing = require('./routing')
const Post = function (w) {
this.window = w
}
Post.prototype.commentsEvents = function () {
const document = this.window.document
const parentCommentIdField = document.getElementById('user_comment_parentCommentId')
if (!parentCommentIdField) {
return
}
const isAnswerAlert = document.getElementById('answer-alert')
const cancelAnswerButton = document.getElementById('cancel-answer')
const toogleAnswerAlert = function () {
if (parentCommentIdField.value) {
isAnswerAlert.classList.remove('hidden')
} else {
isAnswerAlert.classList.add('hidden')
}
}
toogleAnswerAlert()
const answerButtons = document.querySelectorAll('a[data-answer]')
for (let i = 0, len = answerButtons.length; i < len; i++) {
answerButtons[i].addEventListener('click', function (e) {
parentCommentIdField.value = e.target.getAttribute('data-answer')
toogleAnswerAlert()
}, false)
}
cancelAnswerButton.addEventListener('click', function (e) {
e.preventDefault()
parentCommentIdField.value = null
toogleAnswerAlert()
}, false)
const previewButton = document.querySelector('.preview-button')
const previewRender = document.querySelector('#preview')
previewButton.addEventListener('click', function () {
if (previewRender.innerHTML === '') {
previewRender.innerHTML = '<p>Chargement en cours…</p>'
class Post {
constructor(w) {
this.window = w
}
const content = document.querySelector('#user_comment_content').value
const httpRequest = new XMLHttpRequest()
commentsEvents() {
const document = this.window.document
httpRequest.onreadystatechange = function (data) {
if (httpRequest.readyState === 4 && httpRequest.status === 200) {
const json = JSON.parse(httpRequest.response)
previewRender.innerHTML = json.render
document.location.href = '#preview'
}
const parentCommentIdField = document.getElementById('user_comment_parentCommentId')
if (!parentCommentIdField) {
return
}
const isAnswerAlert = document.getElementById('answer-alert')
const cancelAnswerButton = document.getElementById('cancel-answer')
const toogleAnswerAlert = function() {
if (parentCommentIdField.value) {
isAnswerAlert.classList.remove('hidden')
} else {
isAnswerAlert.classList.add('hidden')
}
}
toogleAnswerAlert()
const answerButtons = document.querySelectorAll('a[data-answer]')
for (let i = 0, len = answerButtons.length; i < len; i++) {
answerButtons[i].addEventListener('click', function(e) {
parentCommentIdField.value = e.target.getAttribute('data-answer')
toogleAnswerAlert()
}, false)
}
cancelAnswerButton.addEventListener('click', function(e) {
e.preventDefault()
parentCommentIdField.value = null
toogleAnswerAlert()
}, false)
const previewButton = document.querySelector('.preview-button')
const previewRender = document.querySelector('#preview')
previewButton.addEventListener('click', function() {
if (previewRender.innerHTML === '') {
previewRender.innerHTML = '<p>Chargement en cours…</p>'
}
const content = document.querySelector('#user_comment_content').value
const httpRequest = new XMLHttpRequest()
httpRequest.onreadystatechange = function(data) {
if (httpRequest.readyState === 4 && httpRequest.status === 200) {
const json = JSON.parse(httpRequest.response)
previewRender.innerHTML = json.render
document.location.href = '#preview'
}
}
httpRequest.open('POST', Routing.generate('api_blog_comment_preview'))
httpRequest.setRequestHeader(
'Content-Type',
'application/x-www-form-urlencoded'
)
httpRequest.send('content=' + encodeURIComponent(content))
}, false)
}
httpRequest.open('POST', Routing.generate('api_blog_comment_preview'))
httpRequest.setRequestHeader(
'Content-Type',
'application/x-www-form-urlencoded'
)
httpRequest.send('content=' + encodeURIComponent(content))
}, false)
}
imagesEvents() {
const document = this.window.document
let isFullscreen = false
const images = document.querySelectorAll('.body img')
Post.prototype.imagesEvents = function () {
const document = this.window.document
let isFullscreen = false
const images = document.querySelectorAll('.body img')
const handleClick = function(image) {
if (isFullscreen) {
if (document.exitFullscreen) {
document.exitFullscreen()
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen()
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen()
}
} else {
if (image.requestFullscreen) {
image.requestFullscreen()
} else if (image.webkitRequestFullscreen) {
image.webkitRequestFullscreen()
} else if (image.mozRequestFullScreen) {
image.mozRequestFullScreen()
}
}
const handleClick = function (image) {
if (isFullscreen) {
if (document.exitFullscreen) {
document.exitFullscreen()
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen()
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen()
}
} else {
if (image.requestFullscreen) {
image.requestFullscreen()
} else if (image.webkitRequestFullscreen) {
image.webkitRequestFullscreen()
} else if (image.mozRequestFullScreen) {
image.mozRequestFullScreen()
}
isFullscreen = !isFullscreen
}
for (let i = 0, len = images.length; i < len; i++) {
const image = images[i]
if (image.parentNode.tagName === 'A') {
continue
}
(function(i) {
i.addEventListener('click', function() {
handleClick(i)
}, false)
})(image)
}
}
isFullscreen = !isFullscreen
}
for (let i = 0, len = images.length; i < len; i++) {
const image = images[i]
if (image.parentNode.tagName === 'A') {
continue
init() {
this.commentsEvents()
this.imagesEvents()
}
(function (i) {
i.addEventListener('click', function () {
handleClick(i)
}, false)
})(image)
}
}
Post.prototype.init = function () {
this.commentsEvents()
this.imagesEvents()
}
module.exports = Post

View file

@ -1,25 +1,27 @@
const QuickPost = function (w) {
this.window = w
}
class QuickPost {
constructor(w) {
this.window = w
}
QuickPost.prototype.init = function () {
const doc = this.window.document
init() {
const doc = this.window.document
const images = doc.querySelectorAll('.quick-image img')
const images = doc.querySelectorAll('.quick-image img')
for (let i = 0, len = images.length; i < len; i++) {
(function (image) {
const source = image.getAttribute('data-src')
const loader = new Image()
for (let i = 0, len = images.length; i < len; i++) {
(function(image) {
const source = image.getAttribute('data-src')
const loader = new Image()
loader.onload = function () {
image.style.backgroundImage = 'url(' + source + ')'
image.style.backgroundSize = 'cover'
}
loader.onload = function() {
image.style.backgroundImage = 'url(' + source + ')'
image.style.backgroundSize = 'cover'
}
loader.src = source
})(images[i])
}
loader.src = source
})(images[i])
}
}
}
module.exports = QuickPost

View file

@ -1,22 +1,24 @@
const Routing = require('./routing')
const SmallMenu = function (w) {
this.window = w
}
class SmallMenu {
constructor(w) {
this.window = w
}
SmallMenu.prototype.addEvent = function () {
const document = this.window.document
const menu = document.querySelector('.small-menu')
const opener = document.querySelector('.menu-opener')
addEvent() {
const document = this.window.document
const menu = document.querySelector('.small-menu')
const opener = document.querySelector('.menu-opener')
opener.addEventListener('click', () => {
menu.classList.toggle('is-open')
opener.classList.toggle('is-open')
})
}
opener.addEventListener('click', () => {
menu.classList.toggle('is-open')
opener.classList.toggle('is-open')
})
}
SmallMenu.prototype.init = function () {
this.addEvent()
init() {
this.addEvent()
}
}
module.exports = SmallMenu

View file

@ -1,38 +1,19 @@
const Stats = function () {
}
Stats.prototype.init = function () {
(function (f, a, t, h, o, m) {
a[h] = a[h] || function () {
(a[h].q = a[h].q || []).push(arguments)
class Stats {
init() {
(function(f, a, t, h, o, m) {
a[h] = a[h] || function() {
(a[h].q = a[h].q || []).push(arguments)
}
o = f.createElement('script'),
m = f.getElementsByTagName('script')[0]
o.async = 1;
o.src = t;
o.id = 'fathom-script'
m.parentNode.insertBefore(o, m)
})(document, window, '//ftm.deblan.org/tracker.js', 'fathom')
fathom('set', 'siteId', 'HQAWS')
fathom('trackPageview')
}
o = f.createElement('script'),
m = f.getElementsByTagName('script')[0]
o.async = 1; o.src = t; o.id = 'fathom-script'
m.parentNode.insertBefore(o, m)
})(document, window, '//ftm.deblan.org/tracker.js', 'fathom')
fathom('set', 'siteId', 'HQAWS')
fathom('trackPageview')
/*
var _paq = _paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
var u="//piwik.deblan.org/";
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', '1']);
var d= document;
var g= d.createElement('script');
var s= d.getElementsByTagName('script')[0];
g.type='text/javascript';
g.async=true;
g.defer=true;
g.src=u+'piwik.js';
s.parentNode.insertBefore(g,s);
*/
}
module.exports = Stats

View file

@ -1,13 +1,15 @@
const VideoRatio = function (w) {
this.window = w
}
class VideoRatio {
constructor(w) {
this.window = w
}
VideoRatio.prototype.init = function () {
const videos = this.window.document.querySelectorAll('.video-ratio')
init() {
const videos = this.window.document.querySelectorAll('.video-ratio')
for (let i = 0, len = videos.length; i < len; i++) {
videos[i].style.paddingBottom = videos[i].getAttribute('data-ratio')
}
for (let i = 0, len = videos.length; i < len; i++) {
videos[i].style.paddingBottom = videos[i].getAttribute('data-ratio')
}
}
}
module.exports = VideoRatio