woocommerce/apigen/theme-woocommerce/js/main.js

309 lines
8.6 KiB
JavaScript
Raw Normal View History

2015-01-16 16:06:58 +00:00
$(window).load(function() {
var $document = $(document);
2015-11-20 10:05:33 +00:00
var $navigation = $('#navigation');
var navigationHeight = $navigation.height();
2015-01-16 16:06:58 +00:00
var $left = $('#left');
var $right = $('#right');
var $rightInner = $('#rightInner');
var $splitter = $('#splitter');
var $groups = $('#groups');
var $content = $('#content');
// Menu
// Hide deep packages and namespaces
$('ul span', $groups).click(function(event) {
event.preventDefault();
event.stopPropagation();
$(this)
.toggleClass('collapsed')
.parent()
.next('ul')
.toggleClass('collapsed');
}).click();
$active = $('ul li.active', $groups);
if ($active.length > 0) {
// Open active
$('> a > span', $active).click();
} else {
$main = $('> ul > li.main', $groups);
if ($main.length > 0) {
// Open first level of the main project
$('> a > span', $main).click();
} else {
// Open first level of all
$('> ul > li > a > span', $groups).click();
}
}
// Content
// Search autocompletion
var autocompleteFound = false;
var autocompleteFiles = {'c': 'class', 'co': 'constant', 'f': 'function', 'm': 'class', 'mm': 'class', 'p': 'class', 'mp': 'class', 'cc': 'class'};
2015-11-20 10:05:33 +00:00
var $search = $('#search input[name=q]');
2015-01-16 16:06:58 +00:00
$search
.autocomplete(ApiGen.elements, {
matchContains: true,
scrollHeight: 200,
max: 20,
2015-11-20 10:05:33 +00:00
width: 300,
2015-01-16 16:06:58 +00:00
noRecord: '',
highlight: function(value, term) {
var term = term.toUpperCase().replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1").replace(/[A-Z0-9]/g, function(m, offset) {
return offset === 0 ? '(?:' + m + '|^' + m.toLowerCase() + ')' : '(?:(?:[^<>]|<[^<>]*>)*' + m + '|' + m.toLowerCase() + ')';
});
return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term + ")(?![^<>]*>)(?![^&;]+;)"), "<strong>$1</strong>");
},
formatItem: function(data) {
return data.length > 1 ? data[1].replace(/^(.+\\)(.+)$/, '<span><small>$1</small>$2</span>') : data[0];
},
formatMatch: function(data) {
return data[1];
},
formatResult: function(data) {
return data[1];
},
show: function($list) {
var $items = $('li span', $list);
var maxWidth = Math.max.apply(null, $items.map(function() {
return $(this).width();
}));
// 10px padding
2015-11-20 10:05:33 +00:00
$list.width(Math.max(maxWidth + 10, $search.innerWidth()));
2015-01-16 16:06:58 +00:00
}
}).result(function(event, data) {
autocompleteFound = true;
var location = window.location.href.split('/');
location.pop();
var parts = data[1].split(/::|$/);
var file = $.sprintf(ApiGen.config.templates[autocompleteFiles[data[0]]].filename, parts[0].replace(/\(\)/, '').replace(/[^\w]/g, '.'));
if (parts[1]) {
file += '#' + ('mm' === data[0] || 'mp' === data[0] ? 'm' : '') + parts[1].replace(/([\w]+)\(\)/, '_$1');
}
location.push(file);
window.location = location.join('/');
// Workaround for Opera bug
$(this).closest('form').attr('action', location.join('/'));
}).closest('form')
.submit(function() {
var query = $search.val();
if ('' === query) {
return false;
}
2015-11-20 10:05:33 +00:00
return !autocompleteFound && '' !== $('#search input[name=cx]').val();
2015-01-16 16:06:58 +00:00
});
// Save natural order
$('table.summary tr[data-order]', $content).each(function(index) {
do {
index = '0' + index;
} while (index.length < 3);
$(this).attr('data-order-natural', index);
});
// Switch between natural and alphabetical order
var $caption = $('table.summary', $content)
.filter(':has(tr[data-order])')
2015-11-20 10:05:33 +00:00
.prev('h2');
2015-01-16 16:06:58 +00:00
$caption
.click(function() {
var $this = $(this);
var order = $this.data('order') || 'natural';
order = 'natural' === order ? 'alphabetical' : 'natural';
$this.data('order', order);
$.cookie('order', order, {expires: 365});
var attr = 'alphabetical' === order ? 'data-order' : 'data-order-natural';
$this
2015-11-20 10:05:33 +00:00
.next('table')
2015-01-16 16:06:58 +00:00
.find('tr').sortElements(function(a, b) {
return $(a).attr(attr) > $(b).attr(attr) ? 1 : -1;
});
return false;
})
.addClass('switchable')
.attr('title', 'Switch between natural and alphabetical order');
if ((null === $.cookie('order') && 'alphabetical' === ApiGen.config.options.elementsOrder) || 'alphabetical' === $.cookie('order')) {
$caption.click();
}
// Open details
if (ApiGen.config.options.elementDetailsCollapsed) {
2015-11-20 10:05:33 +00:00
$(document.body).on('click', 'tr', function(ev) {
var short = this.querySelector('.short')
, detailed = this.querySelector('.detailed')
if (!short || !detailed) return
$(short).toggleClass('hidden')
$(detailed).toggleClass('hidden')
})
2015-01-16 16:06:58 +00:00
}
// Splitter
var splitterWidth = $splitter.width();
var splitterPosition = $.cookie('splitter') ? parseInt($.cookie('splitter')) : null;
var splitterPositionBackup = $.cookie('splitterBackup') ? parseInt($.cookie('splitterBackup')) : null;
function setSplitterPosition(position)
{
splitterPosition = position;
$left.width(position);
$right.css('margin-left', position + splitterWidth);
$splitter.css('left', position);
}
2015-11-20 10:05:33 +00:00
function setNavigationPosition()
{
var height = $(window).height() - navigationHeight;
$left.height(height);
$splitter.height(height);
$right.height(height);
}
2015-01-16 16:06:58 +00:00
function setContentWidth()
{
var width = $rightInner.width();
$rightInner
.toggleClass('medium', width <= 960)
.toggleClass('small', width <= 650);
}
$splitter.mousedown(function() {
$splitter.addClass('active');
$document.mousemove(function(event) {
if (event.pageX >= 230 && $document.width() - event.pageX >= 600 + splitterWidth) {
setSplitterPosition(event.pageX);
setContentWidth();
}
});
$()
.add($splitter)
.add($document)
.mouseup(function() {
$splitter
.removeClass('active')
.unbind('mouseup');
$document
.unbind('mousemove')
.unbind('mouseup');
$.cookie('splitter', splitterPosition, {expires: 365});
});
return false;
});
$splitter.dblclick(function() {
if (splitterPosition) {
splitterPositionBackup = $left.width();
setSplitterPosition(0);
} else {
setSplitterPosition(splitterPositionBackup);
splitterPositionBackup = null;
}
setContentWidth();
$.cookie('splitter', splitterPosition, {expires: 365});
$.cookie('splitterBackup', splitterPositionBackup, {expires: 365});
});
if (null !== splitterPosition) {
setSplitterPosition(splitterPosition);
}
2015-11-20 10:05:33 +00:00
setNavigationPosition();
2015-01-16 16:06:58 +00:00
setContentWidth();
2015-11-20 10:05:33 +00:00
$(window)
.resize(setNavigationPosition)
.resize(setContentWidth);
2015-01-16 16:06:58 +00:00
// Select selected lines
var matches = window.location.hash.substr(1).match(/^\d+(?:-\d+)?(?:,\d+(?:-\d+)?)*$/);
if (null !== matches) {
var lists = matches[0].split(',');
for (var i = 0; i < lists.length; i++) {
var lines = lists[i].split('-');
lines[0] = parseInt(lines[0]);
lines[1] = parseInt(lines[1] || lines[0]);
for (var j = lines[0]; j <= lines[1]; j++) {
$('#' + j).addClass('selected');
}
}
var $firstLine = $('#' + parseInt(matches[0]));
if ($firstLine.length > 0) {
2015-11-20 10:05:33 +00:00
$right.scrollTop($firstLine.position().top);
2015-01-16 16:06:58 +00:00
}
}
// Save selected lines
var lastLine;
$('.l a').click(function(event) {
event.preventDefault();
2015-11-20 10:05:33 +00:00
var selectedLine = $(this).parent().index() + 1;
var $selectedLine = $('pre.code .l').eq(selectedLine - 1);
2015-01-16 16:06:58 +00:00
if (event.shiftKey) {
if (lastLine) {
for (var i = Math.min(selectedLine, lastLine); i <= Math.max(selectedLine, lastLine); i++) {
$('#' + i).addClass('selected');
}
} else {
$selectedLine.addClass('selected');
}
} else if (event.ctrlKey) {
$selectedLine.toggleClass('selected');
} else {
var $selected = $('.l.selected')
.not($selectedLine)
.removeClass('selected');
if ($selected.length > 0) {
$selectedLine.addClass('selected');
} else {
$selectedLine.toggleClass('selected');
}
}
lastLine = $selectedLine.hasClass('selected') ? selectedLine : null;
// Update hash
var lines = $('.l.selected')
.map(function() {
return parseInt($(this).attr('id'));
})
.get()
.sort(function(a, b) {
return a - b;
});
var hash = [];
var list = [];
for (var j = 0; j < lines.length; j++) {
if (0 === j && j + 1 === lines.length) {
hash.push(lines[j]);
} else if (0 === j) {
list[0] = lines[j];
} else if (lines[j - 1] + 1 !== lines[j] && j + 1 === lines.length) {
hash.push(list.join('-'));
hash.push(lines[j]);
} else if (lines[j - 1] + 1 !== lines[j]) {
hash.push(list.join('-'));
list = [lines[j]];
} else if (j + 1 === lines.length) {
list[1] = lines[j];
hash.push(list.join('-'));
} else {
list[1] = lines[j];
}
}
hash = hash.join(',');
$backup = $('#' + hash).removeAttr('id');
window.location.hash = hash;
$backup.attr('id', hash);
});
});