-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno_more_jquery.js
154 lines (136 loc) · 3.87 KB
/
no_more_jquery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) 2016 Matthew Brennan Jones <matthew.brennan.jones@gmail.com>
// This software uses a MIT style license
// https://github.com/SoftwareAddictionShow/no-more-jquery
"use strict";
// Great website for reasons not to use jquery:
// http://youmightnotneedjquery.com
let style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = '.hidden { display: none !important; }';
document.getElementsByTagName('head')[0].appendChild(style);
function $(selector) {
if(! selector || selector.length === 0) {
return null;
} else if (selector[0] === '#') {
return document.querySelector(selector);
} else if(selector[0] === '.') {
return document.querySelectorAll(selector);
} else {
return document.getElementsByTagName(selector);
}
return null;
}
function hide(selector) {
let elements = document.querySelectorAll(selector);
for (let i=0; i<elements.length; ++i) {
elements[i].classList.add('hidden');
}
}
function show(selector) {
let elements = document.querySelectorAll(selector);
for (let i=0; i<elements.length; ++i) {
elements[i].classList.remove('hidden');
}
}
function is_hidden(selector) {
let element = document.querySelector(selector);
if (element) {
return element.classList.contains('hidden');
}
return false;
}
function documentOnReady(cb) {
if (document.readyState !== 'loading') {
cb();
} else {
document.addEventListener('DOMContentLoaded', cb);
}
}
function httpGet(url, cb, timeout) {
httpRequest(url, 'GET', cb, timeout);
}
function httpPost(url, cb, timeout) {
httpRequest(url, 'POST', cb, timeout);
}
function httpRequest(url, method, cb, timeout) {
timeout = timeout || 3000;
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState === 4) {
cb(this.response, this.status);
} else if (this.readyState === 0) {
cb(null);
}
};
xhr.onerror = function() {
cb(null);
};
xhr.open(method, url, true);
xhr.timeout = timeout;
xhr.send(null);
}
// FIXME: This stacks a new set of events for each animation call
// FIXME: Use a unique random number, rather than this global
let g_anim_counter = 0;
function animateCSS(element, start_fields, end_fields, duration, iteration_count, direction) {
iteration_count = iteration_count || 1;
direction = direction || 'normal';
let anim_name = 'anim_' + (++g_anim_counter);
let style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = `
.${anim_name} {
animation-duration: ${duration};
animation-name: ${anim_name};
animation-iteration-count: ${iteration_count};
animation-direction: ${direction};
}
@keyframes ${anim_name} {
from {
${start_fields}
}
}
@keyframes ${anim_name} {
to {
${end_fields}
}
}`;
document.getElementsByTagName('head')[0].appendChild(style);
element.addEventListener('animationstart', function() {
console.info('animationstart');
}, false);
element.addEventListener('animationend', function() {
console.info('animationend');
document.getElementsByTagName('head')[0].removeChild(style);
}, false);
element.addEventListener('animationiteration', function() {
console.info('animationiteration');
}, false);
element.className = anim_name;
}
function animateValue(cb, old_value, new_value, target_time) {
let is_bigger = old_value > new_value;
let diff_value = is_bigger ? old_value - new_value : new_value - old_value;
let start_time = null;
let stepTime = function(timestamp) {
if (start_time === null) {
start_time = timestamp;
}
let elapsed_time = timestamp - start_time;
let percent = elapsed_time / target_time;
if (percent >= 1.0) {
percent = 1.0;
}
let trans_value = 0;
if (is_bigger) {
trans_value = old_value - (diff_value * percent);
} else {
trans_value= old_value + (diff_value * percent);
}
cb(trans_value);
if (percent !== 1.0) {
window.requestAnimationFrame(stepTime);
}
};
window.requestAnimationFrame(stepTime);
}