Skip to content

Commit

Permalink
Bring client side logic up to speed with da7e677 (#5)
Browse files Browse the repository at this point in the history
- Clean up active state logic
- Replace existing elements by ID, otherwise append to head and body respectively
  • Loading branch information
Fredrick Galoso committed May 30, 2012
1 parent ae356b8 commit 05c37d9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
59 changes: 38 additions & 21 deletions rp.js
Expand Up @@ -7,6 +7,7 @@
(function($){
$(document).ready(function() {
if (Modernizr.history) {
var Not = Crackle.not;
/**
* Load PJAX
*/
Expand Down Expand Up @@ -37,11 +38,12 @@
'current-menu-ancestor',
'current_page_item',
'current_page_parent',
'current_page_ancestor',
'active active',
'active'
'current_page_ancestor'
].join(' ');
$('.active').removeClass(activeClasses);
while($('.active').length > 0) {
$('.active').removeClass('active');
}
$(event.relatedTarget).parent().addClass('active');
return true;
} else {
Expand Down Expand Up @@ -83,30 +85,45 @@
}
// Head
var head = {
children: $.makeArray($('head').children())
.map(function(child) { return child.outerHTML.replace(/'/g, '"'); }),
nodes: request.getResponseHeader('X-WordPress-Head')
.split('|')
.map(function(child) { return child.replace(/'/g, '"'); })
.filter(function(child) { return child !== ''; })
before: Crackle.stringify($.makeArray($('head').children())).map(function(e) {
return e.replace(/(\r\n|\n|\r|\t)/gm,"");
}),
after: Crackle.stringify($.makeArray($(JSON.parse(request.getResponseHeader('X-WordPress-Head'))
.replace(/(\r\n|\n|\r|\t)/gm,""))))
};
for (var i = 0; i < head.nodes.length; i++) {
if (head.children.indexOf(head.nodes[i]) === -1) {
$('head').append(head.nodes[i]);
head.diff = Not(head.after).in(head.before);
for (var i = 0; i < head.diff.length; i++) {
var id = '#' + $(head.diff[i]).attr('id');
if ($(id).length > 0) {
$(id).remove();
$('head').append(head.diff[i]);
} else {
$('head').append(footer.diff[i]);
}
}
// Footer
var footer = {
children: $.makeArray($('body').children('link, script'))
.map(function(child) { return child.outerHTML.replace(/'/g, '"'); }),
nodes: request.getResponseHeader('X-WordPress-Footer')
.split('|')
.map(function(child) { return child.replace(/'/g, '"'); })
.filter(function(child) { return child !== ''; })
before: Crackle.stringify($.makeArray($('footer').nextAll())).map(function(e) {
return e.replace(/(\r\n|\n|\r|\t)/gm,"");
}),
after: Crackle.stringify($.makeArray($(JSON.parse(request.getResponseHeader('X-WordPress-Footer'))
.replace(/(\r\n|\n|\r|\t)/gm,""))))
};
for (var i = 0; i < footer.nodes.length; i++) {
if (footer.children.indexOf(footer.nodes[i]) === -1) {
$('body').append(footer.nodes[i]);
footer.diff = Not(footer.after).in(footer.before).map(function(e) {
if ($(e).attr('id') === 'wpadminbar') {
return Crackle.stringify($(e).removeClass());
}
else {
return e;
}
});
for (var i = 0; i < footer.diff.length; i++) {
var id = '#' + $(footer.diff[i]).attr('id');
if ($(id).length > 0) {
$(id).remove();
$('body').append(footer.diff[i]);
} else {
$('body').append(footer.diff[i]);
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion rp.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 05c37d9

Please sign in to comment.