-
Notifications
You must be signed in to change notification settings - Fork 1
/
modules.js
333 lines (285 loc) · 9.15 KB
/
modules.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
* @file Helper functions to get information about modules from the drupal
* module listing page.
*/
function ModuleList() {
var cache = {};
/**
* Helper function to get a value from an object or array
* Takes the item and an arbitrary number of indexes to traverse
* Returns the value or the value that failed if the value doesn't exist
*/
function get(item) {
var l = 0, result = item;
while(++l < arguments.length && (result = result[arguments[l]])){}
return result;
}
function getModulesList() {
if(cache.modules) return cache.modules;
var parent, items, modules = {},
doc = document,
iframe = document.getElementsByClassName('overlay-active');
if (iframe && iframe[0] && iframe[0].tagName == 'IFRAME') {
doc = iframe[0].contentDocument;
}
parent = doc.getElementById('system-modules') || false;
modules.length = 0;
if(parent) {
items = parent.getElementsByTagName('label');
var l = items.length;
while(l--) {
var item = items[l],
check = get(item, 'parentElement', 'previousElementSibling'),
text = get(item, 'children', 0, 'innerText');
check = check ? check.getElementsByTagName('input')[0] : null;
var name = text.toLowerCase();
var machine = name.replace(/ /g, '_');
modules[text.toLowerCase()] = {
label : item,
check : check,
title : text,
name : name,
machine : machine
};
if (!document.getElementsByClassName('rubik-processed')) {
var backlink = document.createElement('a');
backlink.href='#'
backlink.innerText = '[top]';
backlink.title = 'Back to top';
backlink.onclick = function(e) {
window.scrollTo(0, 0);
return false;
};
item.parentElement.appendChild(backlink);
}
modules.length++;
}
}
cache.modules = modules;
return modules;
}
function getModule (name) {
name = name.toLowerCase();
var modules = getModulesList();
return modules[name] ? modules[name] : false;
}
function gotoModule(name) {
var module = getModule(name);
var top = module.check;
window.scrollTo(0, getTop(top) - 100);
}
/**
* Helper function to get the total offset top of an element
*/
function getTop(el) {
var top = 0;
while(el && el.offsetParent) {
el = el.offsetParent;
top += el.offsetTop;
}
return top;
}
/**
* Create a simple widget for suggesting matching modules
*/
var suggest = (function() {
var aliases = {
'ctools' : 'chaos tools',
'vntf' : 'views node taxonomy filter',
'ga stats' : 'google analytics statistics',
'gastats' : 'google analytics statistics',
'ds' : 'display suite'
};
//helper functions
function findMatches (search) {
if (!search.length) { return false; }
var ml = getModulesList(),
search = search.toLowerCase(),
alt = search.replace(/ /g, '_'),
results = new Array();
var added = {};
for (var m in ml) {
if (!added[m] && (m.indexOf(search) === 0 || m.indexOf(alt) === 0)) {
added[m] = true;
results.push(ml[m]);
} else {
for (var al in aliases) {
if(!added[aliases[al]] && (al.indexOf(search) === 0 || al.indexOf(alt) === 0)) {
added[aliases[al]] = true;
results.push( ml[aliases[al]] );
}
}
}
}
return results;
}
function makeResults (matches) {
var list = document.createElement('ul');
for (var i = 0; i < matches.length; i++) {
var m = matches[i];
var item = document.createElement('li');
item.className = 'search-suggestion';
var link = document.createElement('a');
link.href = "#" + m.machine;
link.innerText = m.title;
link.rmodule = m.name;
item.appendChild(link);
list.appendChild(item);
link.onclick = function(e) {
gotoModule(this.rmodule);
return false;
}
};
return list;
};
function showResults (matches) {
var el = document.createElement('div');
el.appendChild(makeResults(matches));
searchbar.appendChild(el);
return el;
}
function hideResults (el) {
if(el) {
searchbar.removeChild(el);
}
}
//main execution
return function (search) {
var status = cache.suggestStatus;
if (!status) { status = {}; }
var matches = findMatches(search);
if (matches) {
if (status.shown) {
hideResults(status.el);
status.el = showResults(matches);
} else {
status.el = showResults(matches);
}
status.shown = true;
} else {
hideResults(status.el);
delete(status.el);
status.shown = false;
}
cache.suggestStatus = status;
};
})(); //end suggest definition
function linkDescriptions() {
var reqs = document.getElementsByClassName('admin-requirements');
var l = reqs.length;
while(l--) {
var cur = reqs[l];
var items = cur.innerHTML.match(/ (([^:\(]+?) \(.+?\)),?/g);
var j = items ? items.length : 0;
while(j--) {
var dep = items[j];
var match = dep.match(/ (.+) \(.*\),?/);
if(match) {
dep = match[1];
var link = '<a href="javascript:" class="gotomodule">' + dep + '</a>';
cur.innerHTML = cur.innerHTML.replace(dep, link);
}
}
var links = cur.getElementsByClassName('gotomodule');
var j = links.length;
while(j--) {
var link = links[j];
link.onclick = function(e) {
gotoModule(this.innerText.toLowerCase());
return false;
}
}
}
}
/**
* Create elements and attach handlers
*/
var searchbar = document.createElement('div');
searchbar.id = 'drupalhelper-module-search-bar';
var input = document.createElement('input');
input.type = 'text';
input.autocomplete = 'off';
input.name = 'drupalhelper-search';
input.className = 'drupalhelper-input';
input.onkeyup = function(e) {
if(e.keyCode == 13) {
gotoModule(this.value);
return false;
} else {
suggest(this.value);
}
};
var instr = document.createElement('div');
instr.innerText = ' Search for a module.';
instr.className = 'instructions';
searchbar.appendChild(instr);
searchbar.appendChild(input);
var actions = document.getElementsByClassName('form-actions');
for (var i = actions.length - 1; i >= 0; i--) {
var backlink = document.createElement('a');
backlink.href='#'
backlink.innerText = '[top]';
backlink.title = 'Back to top';
backlink.onclick = function(e) {
window.scrollTo(0, 0);
return false;
};
actions[i].appendChild(backlink);
};
var ldeps = document.createElement('a');
ldeps.href = '#';
ldeps.className = 'link-deps';
ldeps.innerText = '[Link Dependencies]';
ldeps.title = 'Link each dependecy so clicking it will scroll to that module. This can take some time.';
//maintain whether we have already linked these things
var linked = false;
ldeps.onclick = function() {
if (!linked) {
linked = true;
linkDescriptions();
}
}
instr.appendChild(ldeps);
//create a link to send the use all the way to the bottom of the page.
var bottom = document.createElement('a');
bottom.href = '#';
bottom.className = 'goto-bottom';
bottom.title = 'Go to the bottom of the page.';
bottom.innerText = '[bottom]';
bottom.onclick = function(e) {
window.scrollTo(0, document.body.scrollHeight);
//for some reason it doesn't go all the way down in rubik if we don't run it twice
setTimeout(function() {
window.scrollTo(0, document.body.scrollHeight);
}, 10);
return false;
};
var sidebar = document.getElementsByClassName('column-side');
var top = document.getElementById('console');
if (sidebar && sidebar[0]) {
sidebar = sidebar[0].getElementsByClassName('column-wrapper');
//put the bottom link in the sidebar if its there
var actions = sidebar[0].getElementsByClassName('form-actions');
if(actions) {
actions[0].appendChild(bottom);
bottom.added = true;
}
sidebar[0].appendChild(searchbar);
} else if (top) {
top.appendChild(searchbar);
} else {
var content = document.getElementsByClassName('region-content');
if (content) {
content = content[0];
content.insertBefore(searchbar, content.firstElementChild);
} else {
console.log('Could not find a region to attach to.');
}
}
if (!bottom.added) {
//otherwise put the bottom link in the searchbar.
instr.appendChild(bottom);
}
getModulesList();
}
ModuleList();