-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
executable file
·211 lines (189 loc) · 5.88 KB
/
popup.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
let searchField = document.getElementById('search-field');
let searchItems = document.getElementById('search-items');
let categoryList = document.getElementById('list');
let preview = document.getElementById('preview-area');
var searchResult = [];
var previousCopied;
/**
* Search for match
* @param query is the string that is searched
* @param str is the string where is searched
* @return 0 if not found otherwise number of occurance
*/
function occurances(query, str) {
// initially does not match
var cnt= 0;
// split query string
var queries = query.split(" ");
for(var q of queries) {
if(q != "" && str.search(q) >= 0) {
cnt++;
}
}
return cnt;
}
/**
* This function clears the search data list and list from the UI
* @return void
*/
function clearSearchList() {
while(searchResult.length > 0) {
searchResult.pop();
}
while(searchItems.firstChild) {
searchItems.removeChild(searchItems.firstChild);
}
}
/**
* This function adds click listener on every item
* @param:list is a ul element (only for copy purpose)
* @param:listItem is a li element where the click listener is attached
* @param:item is data object of a single item
*/
function addClickListener(list, listItem, item) {
listItem.addEventListener("click", function() {
listItem.classList.add("code-copied");
// removing the @code-copied class form the previousy copied item
// and making the current item as pervious item
previousCopied.classList.remove("code-copied");
previousCopied = listItem;
// Copy text to clipboard
let copyText = document.createElement('textarea');
list.appendChild(copyText);
copyText.value = item.code;
copyText.select();
document.execCommand("copy");
list.removeChild(copyText);
});
}
/**
* This function adds mouse hover listener on every item
* @param:list is a ul element (only for copy purpose)
* @param:listItem is a li element where the click listener is attached
* @param:item is data object of a single item
*/
function addHoverListener(listItem, item) {
listItem.addEventListener("mouseenter", function() {
preview.innerHTML = item.code;
});
}
/**
* This function creates list items and adds them in the list
* @param:list is the ul element
* @param:item is the data object of a single item
* @return void
*/
function addItemsInTheList(list, item) {
let listItem = document.createElement("li");
listItem.setAttribute("class", "item-element");
// show the title of the code snippet
let spanTitle = document.createElement("span");
spanTitle.setAttribute("class", "item-element");
spanTitle.innerHTML = item.title;
listItem.appendChild(spanTitle);
// show the message "Code copied" when clicked
let spanText = document.createElement("span");
spanText.setAttribute("class", "bs-copy-code");
spanText.innerHTML = "Code copied";
listItem.appendChild(spanText);
let spanIcon = document.createElement("span");
spanIcon.setAttribute("class", "code-copy-icon");
listItem.appendChild(spanIcon);
previousCopied = listItem;
addClickListener(list, listItem, item);
addHoverListener(listItem, item);
list.appendChild(listItem);
}
/**
* This function initializes the search operations
* @param:categories is the full data set
* @return void
*/
function initSearch(categories) {
searchField.addEventListener("keyup", function() {
clearSearchList();
// initially there is no search result
var found = false;
var searchStr = searchField.value;
if(searchStr == "") {
// hide the search results
categoryList.classList.remove("overlay");
searchItems.classList.remove("show");
searchItems.classList.add("hide");
} else {
// show the search reslts
categoryList.classList.add("overlay");
searchItems.classList.remove("hide");
searchItems.classList.add("show");
}
// search for str
let list = document.createElement('ul');
searchItems.appendChild(list);
searchStr = searchStr.toLowerCase();
for(var ctg of categories) {
var cnt = occurances(searchStr, ctg.title);
if(cnt != 0) {
found = true;
// show all the list items
for(var itm of ctg.items) {
itm.count = occurances(searchStr, itm.title) + 5;
searchResult.push(itm);
}
} else {
// search in the list items
for(var itm of ctg.items) {
var cnt = occurances(searchStr, itm.title);
if(cnt != 0) {
found = true;
// show the item
itm.count = cnt;
searchResult.push(itm);
}
}
}
}
// if found then show the items
// otherwise show the not found message
if(found) {
// sorting the search result desc by the occurance
searchResult.sort(function(a, b) {
return b.count - a.count;
});
for(var item of searchResult) {
addItemsInTheList(list, item);
}
} else {
let message = document.createElement("p");
message.classList.add('no-result-text');
message.innerHTML = "No result found for <strong> \"" + searchField.value + "\"</strong>";
searchItems.removeChild(list);
searchItems.appendChild(message);
}
});
}
/**
* This fucnntion creates the view of the full list
* @param:categories is the full data set
* @return void
*/
function createCategories(categories) {
for(let ctg of categories) {
let category = document.createElement('div');
category.setAttribute("class", "bs-category");
// The heading/title of the categories
let title = document.createElement('h3');
title.setAttribute("class", "tf-bs-heading");
title.innerHTML = ctg.title;
category.appendChild(title);
let list = document.createElement('ul');
category.appendChild(list);
for(let itm of ctg.items) {
addItemsInTheList(list, itm);
}
categoryList.appendChild(category);
}
}
// creating the full list of items in the UI
createCategories(categories);
// initilizing the search functionality
initSearch(categories);