1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-05-14 11:35:33 +02:00

[ticket/11956] Move JS to reusable function

Move JS to reusable function that can be ran
for all content added with JavaScript such as
results of AJAX forms

PHPBB3-11956
This commit is contained in:
Vjacheslav Trushkin 2013-10-22 00:41:05 +03:00
parent d2d4438db5
commit 431b4acb36

View File

@ -410,46 +410,44 @@ function insert_single_user(formId, user)
}
/**
* Run onload functions
* Parse document block
*/
(function($) {
$(document).ready(function() {
// Swap .nojs and .hasjs
$('#phpbb.nojs').toggleClass('nojs hasjs');
function parse_document(container)
{
var test = document.createElement('div'),
oldBrowser = (typeof test.style.borderRadius == 'undefined');
// Focus forms
$('form[data-focus]:first').each(function() {
$('#' + this.getAttribute('data-focus')).focus();
});
delete test;
// Reset avatar dimensions when changing URL or EMAIL
$('input[data-reset-on-edit]').bind('keyup', function() {
/**
* Reset avatar dimensions when changing URL or EMAIL
*/
container.find('input[data-reset-on-edit]').bind('keyup', function() {
$(this.getAttribute('data-reset-on-edit')).val('');
});
// Pagination
$('a.pagination-trigger').click(function() {
/**
* Pagination
*/
container.find('a.pagination-trigger').click(function() {
jumpto($(this));
});
// Adjust HTML code for IE8 and older versions
var test = document.createElement('div'),
oldBrowser = (typeof test.style.borderRadius == 'undefined');
delete test;
/**
* Adjust HTML code for IE8 and older versions
*/
if (oldBrowser) {
// Fix .linklist.bulletin lists
$('ul.linklist.bulletin li:first-child, ul.linklist.bulletin li.rightside:last-child').addClass('no-bulletin');
container.find('ul.linklist.bulletin li:first-child, ul.linklist.bulletin li.rightside:last-child').addClass('no-bulletin');
// Do not run functions below for old browsers
return;
}
// Adjust topiclist lists with check boxes
$('ul.topiclist dd.mark').siblings('dt').children('.list-inner').addClass('with-mark');
// Resize navigation block to keep all links on same line
$('.navlinks').each(function() {
/**
* Resize navigation block to keep all links on same line
*/
container.find('.navlinks').each(function() {
var $this = $(this),
left = $this.children().not('.rightside'),
right = $this.children('.rightside');
@ -470,8 +468,10 @@ function insert_single_user(formId, user)
$(window).resize(resize);
});
// Responsive breadcrumbs
$('.breadcrumbs:not(.skip-responsive, .linklist.leftside .breadcrumbs)').each(function() {
/**
* Makes breadcrumbs responsive
*/
container.find('.breadcrumbs:not(.skip-responsive, .linklist.leftside .breadcrumbs)').each(function() {
var $this = $(this),
$body = $('body'),
links = $this.find('.crumb'),
@ -536,12 +536,24 @@ function insert_single_user(formId, user)
$(window).resize(check);
});
// Responsive topic lists
$('.topiclist.responsive-show-all > li > dl').each(function() {
/**
* Adjust topiclist lists with check boxes
*/
container.find('ul.topiclist dd.mark').siblings('dt').children('.list-inner').addClass('with-mark');
/**
* Appends contents of all extra columns to first column in
* .topiclist lists for mobile devices. Copies contents as is.
*
* To add that functionality to .topiclist list simply add
* responsive-show-all to list of classes
*/
container.find('.topiclist.responsive-show-all > li > dl').each(function() {
var $this = $(this),
block = $this.find('dt .responsive-show:last-child'),
first = true;
// Create block that is visible only on mobile devices
if (!block.length) {
$this.find('dt > .list-inner').append('<div class="responsive-show" style="display:none;" />');
block = $this.find('dt .responsive-show:last-child');
@ -550,6 +562,7 @@ function insert_single_user(formId, user)
first = (block.text().trim().length == 0);
}
// Copy contents of each column
$this.find('dd').not('.mark').each(function() {
var column = $(this),
children = column.children(),
@ -565,11 +578,19 @@ function insert_single_user(formId, user)
});
});
$('.topiclist.responsive-show-columns').each(function() {
/**
* Same as above, but prepends text from header to each
* column before contents of that column.
*
* To add that functionality to .topiclist list simply add
* responsive-show-columns to list of classes
*/
container.find('.topiclist.responsive-show-columns').each(function() {
var list = $(this),
headers = [],
headersLength = 0;
// Find all headers, get contents
list.prev('.topiclist').find('li.header dd').not('.mark').each(function() {
headers.push($(this).text());
headersLength ++;
@ -579,11 +600,13 @@ function insert_single_user(formId, user)
return;
}
// Parse each row
list.find('dl').each(function() {
var $this = $(this),
block = $this.find('dt .responsive-show:last-child'),
first = true;
// Create block that is visible only on mobile devices
if (!block.length) {
$this.find('dt > .list-inner').append('<div class="responsive-show" style="display:none;" />');
block = $this.find('dt .responsive-show:last-child');
@ -592,6 +615,7 @@ function insert_single_user(formId, user)
first = (block.text().trim().length == 0);
}
// Copy contents of each column
$this.find('dd').not('.mark').each(function(i) {
var column = $(this),
children = column.children(),
@ -601,6 +625,7 @@ function insert_single_user(formId, user)
html = children.html();
}
// Prepend contents of matching header before contents of column
if (i < headersLength) {
html = headers[i] + ': <strong>' + html + '</strong>';
}
@ -612,8 +637,10 @@ function insert_single_user(formId, user)
});
});
// Responsive tables
$('table.table1').not('.not-responsive').each(function() {
/**
* Responsive tables
*/
container.find('table.table1').not('.not-responsive').each(function() {
var $this = $(this),
th = $this.find('thead > tr > th'),
columns = th.length,
@ -682,8 +709,21 @@ function insert_single_user(formId, user)
});
});
// Responsive link lists
$('.linklist:not(.navlinks, .skip-responsive), .postbody ul.profile-icons:not(.skip-responsive)').each(function() {
/**
* Hide empty responsive tables
*/
container.find('table.responsive > tbody').each(function() {
var items = $(this).children('tr');
if (items.length == 0)
{
$(this).parent('table:first').addClass('responsive-hide');
}
});
/**
* Responsive link lists
*/
container.find('.linklist:not(.navlinks, .skip-responsive), .postbody ul.profile-icons:not(.skip-responsive)').each(function() {
var $this = $(this),
$body = $('body'),
links = $this.children().not('.skip-responsive'),
@ -816,8 +856,10 @@ function insert_single_user(formId, user)
$(window).resize(check);
});
// Responsive tabs
$('#tabs, #minitabs').not('.skip-responsive').each(function() {
/**
* Responsive tabs
*/
container.find('#tabs, #minitabs').not('.skip-responsive').each(function() {
var $this = $(this),
$body = $('body'),
ul = $this.children(),
@ -881,6 +923,31 @@ function insert_single_user(formId, user)
$(window).resize(check);
});
/**
* Hide UCP/MCP navigation if there is only 1 item
*/
container.find('#navigation').each(function() {
var items = $(this).children('ol, ul').children('li');
if (items.length == 1)
{
$(this).addClass('responsive-hide');
}
});
}
/**
* Run onload functions
*/
(function($) {
$(document).ready(function() {
// Swap .nojs and .hasjs
$('#phpbb.nojs').toggleClass('nojs hasjs');
// Focus forms
$('form[data-focus]:first').each(function() {
$('#' + this.getAttribute('data-focus')).focus();
});
// Hide responsive menu and tabs
$('#phpbb').click(function(e) {
var parents = $(e.target).parents();
@ -892,22 +959,6 @@ function insert_single_user(formId, user)
}
});
// Hide *CP navigation if there is only 1 item
$('#navigation').each(function() {
var items = $(this).children('ol, ul').children('li');
if (items.length == 1)
{
$(this).addClass('responsive-hide');
}
});
// Hide empty responsive tables
$('table.responsive > tbody').each(function() {
var items = $(this).children('tr');
if (items.length == 0)
{
$(this).parent('table:first').addClass('responsive-hide');
}
});
parse_document($('body'));
});
})(jQuery);