-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathicons.js
159 lines (138 loc) · 4.95 KB
/
icons.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
155
156
157
158
159
(function () {
// create notification div
let notification = document.createElement("div");
document.body.appendChild(notification);
notification.setAttribute(
"style",
`
position: fixed;
top: 10px;
left: 50%;
transform: translate(-50%, 0%);
padding: 5px 15px;
font-size: 14px;
border-radius: 80px;
background: rgb(0, 0, 0);
color: white;
display: flex;
align-items: center;
justify-content: center;
transition: 0.3s;
opacity: 0.0;
`
);
// notification appear/disappear timeoutID
let notificationAppearTimeoutID = null;
if (!window.jQuery) {
// load jquery js
let script = document.createElement("SCRIPT");
script.src =
"https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";
script.type = "text/javascript";
document.getElementsByTagName("head")[0].appendChild(script);
}
// poll for jQuery to come into existence
let checkReady = function (cb) {
window.jQuery
? cb()
: window.setTimeout(function () {
checkReady(cb);
}, 20);
};
// start polling...
checkReady(() => {
(async () => {
/*
*********************************************** 1. Now JQuery is loaded ********************************************************
*********************************************** 2. Fetch the svg icons from /icons folder **************************************
*********************************************** 3. This is for the GitHub Page using api.github.com/repos/... ******************
*/
// variables for whole icon category/icons
const icons = []
// find the /icons folder from the main repository
const repositoryUrl = `https://api.github.com/repos/rnbwdev/raincons/git/trees/main`;
const repositoryContent = await fetch(repositoryUrl).then((res) => res.json());
const iconsFolderObj = repositoryContent.tree.filter(
(node) => node.type !== "blob"
);
// get all of the icons
const iconsFolderContent = await fetch(iconsFolderObj[0].url).then((res) => res.json());
iconsFolderContent.tree.forEach((node) => {
if (node.path.includes(".")) {
if (node.path.search(".svg") != -1) {
icons.push(node.path.substring(0, node.path.length - 4))
}
}
});
// build the content html for the icon-categories/icons
let contentHtml = ``;
contentHtml += `
<div class="gap-m column">
<!-- SVGIcons -->
<div class="gap-l box-l">`;
icons.map(icon => {
contentHtml += `
<svg-icon class="icon-xs" html="${icon}">
${icon}
</svg-icon>
`;
});
contentHtml += `
</div>
</div>
`;
// append the built html to the #content
$("#content").append(contentHtml);
// add event handlers after html page is completed
// filter when keyboard is released
$("#searchInput").on("keyup", function (event) {
// display CSS constants
const NONE = "none";
const BLOCK = "block";
// variables
let total_icons = document.getElementsByTagName("svg-icon");
let search_content = $(this).val();
if (search_content === "") {
// show all icons
for (let i = 0; i < total_icons.length; i++) {
$(total_icons[i]).css("display", BLOCK);
}
} else {
// show matched icons except .post-icon
for (let i = 0; i < total_icons.length; i++) {
if ($(total_icons[i]).hasClass("post-icon")) {
continue;
}
let item = total_icons[i].getAttribute('html');
if (item.search(search_content) == -1) {
$(total_icons[i]).css("display", NONE);
} else {
$(total_icons[i]).css("display", BLOCK);
}
}
}
});
// copy svg-icon iconName to clipboard on clicking
$("svg-icon:not(.post-icon)").click(function () {
// get iconName from svg-icon
let iconName = $(this)[0].getAttribute('html')
let iconHtml = `<svg-icon>${iconName}</svg-icon>`;
// copy iconName to clipboard
let dummy = document.createElement("textarea");
document.body.appendChild(dummy);
dummy.value = iconHtml;
dummy.select();
document.execCommand("copy");
document.body.removeChild(dummy);
// show notification
clearTimeout(notificationAppearTimeoutID);
$(notification).css("opacity", "1.0");
notification.innerHTML = `Copied - <svg-icon>${iconName}</svg-icon>`;
// hide notification after 3s delay
notificationAppearTimeoutID = setTimeout(() => {
$(notification).css("opacity", "0.0");
}, 3 * 1000);
});
})();
});
})();