-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjs-lists.js
222 lines (190 loc) · 8.42 KB
/
js-lists.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
212
213
214
215
216
217
218
219
220
221
222
/*
* JSLists v0.4.5
* © 2016 George Duff
*
* Release date: 01/06/2016
* The MIT License (MIT)
* Copyright (c) 2016 George Duff
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// TO DO LIST - Will get round to most of them at some point!
// Add folder & file icons dynamically from param
// Collapse All & Open All are ropey at best!
// Add a search function
// Make the margins user definable
// Add support for UL & OL
var blackCircle = '● ';
var openCircle = '◎ ';
(function() {
"use strict";
function define_JSLists() {
var JSLists = {};
var JSLists_Error = function(error, alertType) {
console.log(error);
}
var getUl = function(){
return document.getElementsByTagName("UL");
};
var getOl = function(){
return document.getElementsByTagName("OL");
};
var getAllLists = function(){
var olLists = Array.prototype.slice.call(document.getElementsByTagName("UL")),
ulLists = Array.prototype.slice.call(document.getElementsByTagName("OL"))
var gLists = olLists.concat(ulLists);
return gLists;
}
JSLists.searchList = function(listId, searchTerm) {
var i, j, lilNodes, liItems = document.getElementsByTagName("LI");
for(i=0; i<liItems.length; i++) {
if(liItems[i].hasChildNodes()) {
for(j=0; j<liItems[i].childNodes.length; j++) {
if(liItems[i].childNodes[j].innerHTML == searchTerm) {
//?????
}
}
}
}
}
JSLists.collapseAll = function(listId) {
var i, ulLists = document.getElementsByTagName("UL");
for(i=0; i<ulLists.length; i++) {
if(ulLists[i].className == "jsl-collapsed") {
console.log(ulLists[i].className + '\n' + '@');
}
};
};
JSLists.openAll = function(listId){
var i, olLists = Array.prototype.slice.call(document.getElementsByTagName("UL")),
ulLists = Array.prototype.slice.call(document.getElementsByTagName("OL"))
var gLists = olLists.concat(ulLists);
for(i=1; i<gLists.length; i++) {
gLists[i].setAttribute('class', 'jsl-open');
};
};
JSLists.padUnorderedLists = function(listId) {
var i, listItems = document.getElementById(listId).getElementsByTagName("UL");
for(i=0; i<listItems.length; i++) {
listItems[i].classList.add('jslist-ul');
}
};
JSLists.padOrderedLists = function(listId) {
var i, listItems = document.getElementById(listId).getElementsByTagName("UL");
for(i=0; i<listItems.length; i++) {
listItems[i].classList.add('jslist-ol');
}
};
JSLists.padLists = function(listId) {
var i, listItems = document.getElementById(listId).getElementsByTagName("LI");
for(i=0; i<listItems.length; i++) {
if(listItems[i].childNodes[0].className != "jsl-collapsed-arrow") {
listItems[i].classList.add('jslist-li');
}
}
for(i=1; i<listItems.length; i++) {
// console.log(listItems[i].childNodes.length);
if(listItems[i].classList = "jslist-li" && listItems[i].childNodes.length < 2) {
listItems[i].innerHTML = blackCircle + listItems[i].innerHTML
}
}
this.padUnorderedLists(listId);
this.padOrderedLists(listId);
};
JSLists.createTree = function(listId, bulletPoint) {
document.getElementById(listId).style.display = "none;"
var i, j, curElem, ulCount, olCount, listItems = document.getElementById(listId).getElementsByTagName('LI'); //this should be the main parent
//i=1 otherwise the root might disappear on click
for(i=1; i<listItems.length; i++) {
if(listItems[i].id.length > 0) {
curElem = document.getElementById(listItems[i].id);
ulCount = document.getElementById(listItems[i].id).getElementsByTagName("UL");
if(ulCount.length > 0){
for(j=0; j<ulCount.length; j++) {
if(ulCount[j].nodeName == "UL") {
break;
}
}
ulCount[j].setAttribute('class', 'jsl-collapsed');
var tglDiv = document.createElement("div");
tglDiv.setAttribute('class', 'jsl-list-closed');
tglDiv.setAttribute("id", listItems[i].id + i +'_tgl');
curElem.insertBefore(tglDiv, curElem.childNodes[0]);
let f = function(e) {
console.log('f THEN');
document.getElementById(e.target.id).classList.toggle('jsl-list-open');
document.getElementById(e.target.id).parentElement.lastElementChild.classList.toggle('jsl-open');
e.stopPropagation();
}
let el_tgl = document.getElementById(listItems[i].id + i +'_tgl')
let el_folder = el_tgl.nextSibling.nextSibling;
el.addEventListener('click', f,true);
el_folder.addEventListener('click', f,true);
}
} else {
listItems[i].setAttribute("id", listId+"tmp"+i);
curElem = document.getElementById(listId+"tmp"+i);
ulCount = document.getElementById(listItems[i].id).getElementsByTagName("UL");
if(ulCount.length > 0) { //There is a nested UL in this LI element, now find the position of the UL
for(j=0; j<ulCount.length; j++) {
if(ulCount[j].nodeName == "UL") {
break; //Multiple UL's? //Set class collapseAll here
}
}
ulCount[j].setAttribute('class', 'jsl-collapsed');
var tglDiv = document.createElement("div");
tglDiv.setAttribute('class', 'jsl-list-closed');
tglDiv.setAttribute("id", listItems[i].id + i +'_tgl');
curElem.insertBefore(tglDiv, curElem.childNodes[0]);
let el_tgl = document.getElementById(listItems[i].id + i +'_tgl')
let el_folder = el_tgl.nextElementSibling;
let el_span = el_tgl.nextElementSibling.nextElementSibling;
let f = function(e){
document.getElementById(e.target.id).classList.toggle('jsl-list-open');
document.getElementById(e.target.id).parentElement.lastElementChild.classList.toggle('jsl-open');
e.stopPropagation();
}
let g = function(e){
if (e.target.previousElementSibling && e.target.previousElementSibling.id){
document.getElementById(e.target.previousElementSibling.id).classList.toggle('jsl-list-open');
document.getElementById(e.target.previousElementSibling.id)
.parentElement.lastElementChild.classList.toggle('jsl-open');
e.stopPropagation();
}
}
let h = function(e){
if (e.target.previousElementSibling && e.target.previousElementSibling.previousElementSibling && e.target.previousElementSibling.previousElementSibling.id){
let head = document.getElementById(e.target.previousElementSibling.previousElementSibling.id);
head.classList.toggle('jsl-list-open');
head.parentElement.lastElementChild.classList.toggle('jsl-open');
e.stopPropagation();
}
}
el_tgl.addEventListener('click', f,true);
el_folder.addEventListener('click', g,true);
el_span.addEventListener('click', h,true);
}
listItems[i].removeAttribute("id");
}
}
setTimeout(function() {
document.getElementById(listId).style.display = "block;"
}, 50); // stops FOUC!
this.padLists(listId);
};
// JSLists.applyToList = function(listId, listType, applyIcons, applyTheme, themeNumber){
//Check the params here
// does the id exist?
JSLists.applyToList = function(listId, bulletPoint) {
this.createTree(listId, "UL");
};
return JSLists;
}
//define the JSLists library in the global namespace if it doesn't already exist
if(typeof(JSLists) === 'undefined') {
window.JSLists = define_JSLists();
} else {
console.log("JSLists already defined.");
}
})();