Skip to content

Commit de4a533

Browse files
committed
Good bye jQuery
Re-implement the same of jQuery's logic by ECMAScript and CSS
1 parent 40d081b commit de4a533

File tree

8 files changed

+91
-182
lines changed

8 files changed

+91
-182
lines changed

lib/rdoc/generator/template/darkfish/_head.rhtml

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
var index_rel_prefix = "<%= rel_prefix %>/";
88
</script>
99

10-
<script src="<%= asset_rel_prefix %>/js/jquery.js"></script>
11-
<script src="<%= asset_rel_prefix %>/js/darkfish.js"></script>
10+
<script src="<%= asset_rel_prefix %>/js/navigation.js" defer></script>
11+
<script src="<%= asset_rel_prefix %>/js/search.js" defer></script>
12+
<script src="<%= asset_rel_prefix %>/js/search_index.js" defer></script>
13+
<script src="<%= asset_rel_prefix %>/js/searcher.js" defer></script>
14+
<script src="<%= asset_rel_prefix %>/js/darkfish.js" defer></script>
1215

1316
<link href="<%= asset_rel_prefix %>/css/fonts.css" rel="stylesheet">
1417
<link href="<%= asset_rel_prefix %>/css/rdoc.css" rel="stylesheet">

lib/rdoc/generator/template/darkfish/css/rdoc.css

+22-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
/* vim: ft=css et sw=2 ts=2 sts=2 */
1010
/* Base Green is: #6C8C22 */
1111

12+
.hide { display: none !important; }
13+
1214
* { padding: 0; margin: 0; }
1315

1416
body {
@@ -48,6 +50,16 @@ h6:hover span {
4850
display: inline;
4951
}
5052

53+
h1:target,
54+
h2:target,
55+
h3:target,
56+
h4:target,
57+
h5:target,
58+
h6:target {
59+
margin-left: -10px;
60+
border-left: 10px solid #f1edba;
61+
}
62+
5163
:link,
5264
:visited {
5365
color: #6C8C22;
@@ -441,7 +453,16 @@ main header h3 {
441453
/* @group Method Details */
442454

443455
main .method-source-code {
444-
display: none;
456+
max-height: 0;
457+
overflow: hidden;
458+
transition-duration: 200ms;
459+
transition-delay: 0ms;
460+
transition-property: all;
461+
transition-timing-function: ease-in-out;
462+
}
463+
464+
main .method-source-code.active-menu {
465+
max-height: 100vh;
445466
}
446467

447468
main .method-description .method-calls-super {

lib/rdoc/generator/template/darkfish/js/darkfish.js

+21-97
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
/* Provide console simulation for firebug-less environments */
11+
/*
1112
if (!("console" in window) || !("firebug" in console)) {
1213
var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
1314
"group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
@@ -16,41 +17,35 @@ if (!("console" in window) || !("firebug" in console)) {
1617
for (var i = 0; i < names.length; ++i)
1718
window.console[names[i]] = function() {};
1819
};
19-
20-
21-
/**
22-
* Unwrap the first element that matches the given @expr@ from the targets and return them.
2320
*/
24-
$.fn.unwrap = function( expr ) {
25-
return this.each( function() {
26-
$(this).parents( expr ).eq( 0 ).after( this ).remove();
27-
});
28-
};
2921

3022

3123
function showSource( e ) {
3224
var target = e.target;
33-
var codeSections = $(target).
34-
parents('.method-detail').
35-
find('.method-source-code');
36-
37-
$(target).
38-
parents('.method-detail').
39-
find('.method-source-code').
40-
slideToggle();
25+
while (!target.classList.contains('method-detail')) {
26+
target = target.parentNode;
27+
}
28+
if (typeof target !== "undefined" && target !== null) {
29+
target = target.querySelector('.method-source-code');
30+
}
31+
if (typeof target !== "undefined" && target !== null) {
32+
target.classList.toggle('active-menu')
33+
}
4134
};
4235

4336
function hookSourceViews() {
44-
$('.method-heading').click( showSource );
37+
document.querySelectorAll('.method-heading').forEach(function (codeObject) {
38+
codeObject.addEventListener('click', showSource);
39+
});
4540
};
4641

4742
function hookSearch() {
48-
var input = $('#search-field').eq(0);
49-
var result = $('#search-results').eq(0);
50-
$(result).show();
43+
var input = document.querySelector('#search-field');
44+
var result = document.querySelector('#search-results');
45+
result.classList.remove("initially-hidden");
5146

52-
var search_section = $('#search-section').get(0);
53-
$(search_section).show();
47+
var search_section = document.querySelector('#search-section');
48+
search_section.classList.remove("initially-hidden");
5449

5550
var search = new Search(search_data, input, result);
5651

@@ -77,85 +72,14 @@ function hookSearch() {
7772
}
7873

7974
search.select = function(result) {
80-
var result_element = result.get(0);
81-
window.location.href = result_element.firstChild.firstChild.href;
75+
console.log(result);
76+
window.location.href = result.firstChild.firstChild.href;
8277
}
8378

8479
search.scrollIntoView = search.scrollInWindow;
8580
};
8681

87-
function highlightTarget( anchor ) {
88-
console.debug( "Highlighting target '%s'.", anchor );
89-
90-
$("a[name]").each( function() {
91-
if ( $(this).attr("name") == anchor ) {
92-
if ( !$(this).parent().parent().hasClass('target-section') ) {
93-
console.debug( "Wrapping the target-section" );
94-
$('div.method-detail').unwrap( 'div.target-section' );
95-
$(this).parent().wrap( '<div class="target-section"></div>' );
96-
} else {
97-
console.debug( "Already wrapped." );
98-
}
99-
}
100-
});
101-
};
102-
103-
function highlightLocationTarget() {
104-
console.debug( "Location hash: %s", window.location.hash );
105-
if ( ! window.location.hash || window.location.hash.length == 0 ) return;
106-
107-
var anchor = window.location.hash.substring(1);
108-
console.debug( "Found anchor: %s; matching %s", anchor, "a[name=" + anchor + "]" );
109-
110-
highlightTarget( anchor );
111-
};
112-
113-
function highlightClickTarget( event ) {
114-
console.debug( "Highlighting click target for event %o", event.target );
115-
try {
116-
var anchor = $(event.target).attr( 'href' ).substring(1);
117-
console.debug( "Found target anchor: %s", anchor );
118-
highlightTarget( anchor );
119-
} catch ( err ) {
120-
console.error( "Exception while highlighting: %o", err );
121-
};
122-
};
123-
124-
function loadAsync(path, success, prefix) {
125-
$.ajax({
126-
url: prefix + path,
127-
dataType: 'script',
128-
success: success,
129-
cache: true
130-
});
131-
};
132-
133-
$(document).ready( function() {
82+
document.addEventListener('DOMContentLoaded', function() {
13483
hookSourceViews();
135-
highlightLocationTarget();
136-
$('ul.link-list a').bind( "click", highlightClickTarget );
137-
138-
var search_scripts_loaded = {
139-
navigation_loaded: false,
140-
search_loaded: false,
141-
search_index_loaded: false,
142-
searcher_loaded: false,
143-
}
144-
145-
var search_success_function = function(variable) {
146-
return (function (data, status, xhr) {
147-
search_scripts_loaded[variable] = true;
148-
149-
if (search_scripts_loaded['navigation_loaded'] == true &&
150-
search_scripts_loaded['search_loaded'] == true &&
151-
search_scripts_loaded['search_index_loaded'] == true &&
152-
search_scripts_loaded['searcher_loaded'] == true)
15384
hookSearch();
15485
});
155-
}
156-
157-
loadAsync('js/navigation.js', search_success_function('navigation_loaded'), rdoc_rel_prefix);
158-
loadAsync('js/search.js', search_success_function('search_loaded'), rdoc_rel_prefix);
159-
loadAsync('js/search_index.js', search_success_function('search_index_loaded'), index_rel_prefix);
160-
loadAsync('js/searcher.js', search_success_function('searcher_loaded'), rdoc_rel_prefix);
161-
});

lib/rdoc/generator/template/darkfish/js/jquery.js

-4
This file was deleted.

lib/rdoc/generator/template/darkfish/js/search.js

+32-31
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
Search = function(data, input, result) {
22
this.data = data;
3-
this.$input = $(input);
4-
this.$result = $(result);
3+
this.input = input;
4+
this.result = result;
55

6-
this.$current = null;
7-
this.$view = this.$result.parent();
6+
this.current = null;
7+
this.view = this.result.parentNode;
88
this.searcher = new Searcher(data.index);
99
this.init();
1010
}
1111

12-
Search.prototype = $.extend({}, Navigation, new function() {
12+
Search.prototype = Object.assign({}, Navigation, new function() {
1313
var suid = 1;
1414

1515
this.init = function() {
1616
var _this = this;
1717
var observer = function(e) {
18-
switch(e.originalEvent.keyCode) {
18+
switch(e.keyCode) {
1919
case 38: // Event.KEY_UP
2020
case 40: // Event.KEY_DOWN
2121
return;
2222
}
23-
_this.search(_this.$input[0].value);
23+
_this.search(_this.input.value);
2424
};
25-
this.$input.keyup(observer);
26-
this.$input.click(observer); // mac's clear field
25+
this.input.addEventListener('keyup', observer);
26+
this.input.addEventListener('click', observer); // mac's clear field
2727

2828
this.searcher.ready(function(results, isLast) {
2929
_this.addResults(results, isLast);
@@ -34,7 +34,7 @@ Search.prototype = $.extend({}, Navigation, new function() {
3434
}
3535

3636
this.search = function(value, selectFirstMatch) {
37-
value = jQuery.trim(value).toLowerCase();
37+
value = value.trim().toLowerCase();
3838
if (value) {
3939
this.setNavigationActive(true);
4040
} else {
@@ -43,23 +43,23 @@ Search.prototype = $.extend({}, Navigation, new function() {
4343

4444
if (value == '') {
4545
this.lastQuery = value;
46-
this.$result.empty();
47-
this.$result.attr('aria-expanded', 'false');
46+
this.result.innerHTML = '';
47+
this.result.setAttribute('aria-expanded', 'false');
4848
this.setNavigationActive(false);
4949
} else if (value != this.lastQuery) {
5050
this.lastQuery = value;
51-
this.$result.attr('aria-busy', 'true');
52-
this.$result.attr('aria-expanded', 'true');
51+
this.result.setAttribute('aria-busy', 'true');
52+
this.result.setAttribute('aria-expanded', 'true');
5353
this.firstRun = true;
5454
this.searcher.find(value);
5555
}
5656
}
5757

5858
this.addResults = function(results, isLast) {
59-
var target = this.$result.get(0);
59+
var target = this.result;
6060
if (this.firstRun && (results.length > 0 || isLast)) {
61-
this.$current = null;
62-
this.$result.empty();
61+
this.current = null;
62+
this.result.innerHTML = '';
6363
}
6464

6565
for (var i=0, l = results.length; i < l; i++) {
@@ -70,25 +70,26 @@ Search.prototype = $.extend({}, Navigation, new function() {
7070

7171
if (this.firstRun && results.length > 0) {
7272
this.firstRun = false;
73-
this.$current = $(target.firstChild);
74-
this.$current.addClass('search-selected');
73+
this.current = target.firstChild;
74+
this.current.classList.add('search-selected');
7575
}
76-
if (jQuery.browser.msie) this.$element[0].className += '';
76+
//TODO: ECMAScript
77+
//if (jQuery.browser.msie) this.$element[0].className += '';
7778

78-
if (isLast) this.$result.attr('aria-busy', 'false');
79+
if (isLast) this.result.setAttribute('aria-busy', 'false');
7980
}
8081

8182
this.move = function(isDown) {
82-
if (!this.$current) return;
83-
var $next = this.$current[isDown ? 'next' : 'prev']();
84-
if ($next.length) {
85-
this.$current.removeClass('search-selected');
86-
$next.addClass('search-selected');
87-
this.$input.attr('aria-activedescendant', $next.attr('id'));
88-
this.scrollIntoView($next[0], this.$view[0]);
89-
this.$current = $next;
90-
this.$input.val($next[0].firstChild.firstChild.text);
91-
this.$input.select();
83+
if (!this.current) return;
84+
var next = isDown ? this.current.nextElementSibling : this.current.previousElementSibling;
85+
if (next) {
86+
this.current.classList.remove('search-selected');
87+
next.classList.add('search-selected');
88+
this.input.setAttribute('aria-activedescendant', next.getAttribute('id'));
89+
this.scrollIntoView(next, this.view);
90+
this.current = next;
91+
this.input.value = next.firstChild.firstChild.text;
92+
this.input.select();
9293
}
9394
return true;
9495
}

0 commit comments

Comments
 (0)