User:Ch1902/common.js
Jump to navigation
Jump to search
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
(function ($) {
$.fn.extend({
/**
* Keep an element position: fixed until window scrolls beyond its top position
*/
fixedUntilScroll: function () {
return this.each(function () {
var el = $(this), style = el.css('position'), pos = el.offset(), win = $(window);
if (style !== 'fixed') return;
win.on('scroll', function (e) {
var scrollY = win.scrollTop(), scrollX = win.scrollLeft();
el.css({top: (scrollY >= pos.top ? 0 : pos.top - scrollY), left: (scrollX >= pos.left ? 0 : pos.left - scrollX)});
});
});
},
/**
* Get background color of nearest parent if not explicitly set on
* element. Doesn't deal with positioned elements, simple cases only!
*/
bgColorInherited: function () {
var color = $(this).css('background-color');
if (color !== 'transparent' && color !== 'rgba(0, 0, 0, 0)')
return color;
$(this[0]).parents().each(function () {
var css = $(this).css('background-color');
if (css !== 'transparent' && css !== 'rgba(0, 0, 0, 0)') {
color = css;
return false;
}
});
return color;
}
});
$.extend({
lcfirst: function (str) { return str.charAt(0).toLowerCase() + str.substr(1); },
ucfirst: function (str) { return str.charAt(0).toUpperCase() + str.substr(1); },
/**
* Add a page to watchlist via ajax
*/
watchPage: function (page) {
return $.ajax({
url: '//www.wikidata.org/w/api.php',
type: 'POST',
dataType: 'json',
data: {action: 'watch', format: 'json', title: page, token: mw.user.tokens.get('watchToken')}
});
},
/**
* Add a mw-spinner icon and overlay it on top of an element
* to prevent clicks on it
*/
overlaySpinner: function (el, id, size) {
var bg = $('<div/>').attr('id', 'mw-spinner-cover-' + id), el = $(el), ew = el.outerWidth(),
eh = el.outerHeight(), pos = $(el).offset(), ex = pos.left, ey = pos.top, spinner = $.createSpinner({size:size,type:'inline',id:id}),
sw = spinner.outerWidth(), sh = spinner.outerHeight(), sx = ex + (ew / 2) - (sw / 2), sy = ey + (eh / 2) - (sh / 2);
el.addClass('mw-spinner-element-' + id).css({opacity: .2});
bg.css({position:'absolute',top:ey,left:ex,width:ew,height:eh}).appendTo(document.body);
spinner.css({position:'absolute',top:sy,left:sx}).appendTo(document.body);
},
/**
* Remove mw-spinner icon
*/
removeOverlaySpinner: function (id) {
$('.mw-spinner-element-' + id).css({opacity: 1}).removeClass('.mw-spinner-element-' + id);
$('#mw-spinner-cover-' + id).remove();
return $.removeSpinner(id);
},
/**
* Add a new link to the page action dropdown menu
*/
addActionMenuItem: function (text, href, id, how) {
return $('<li/>').attr({id: id || ''}).append(
$('<a/>').attr('href', href).text(text)
)[(how || 'append') + 'To']('#p-cactions ul');
},
/**
* Add a new link to the page view menu
*/
addViewMenuItem: function (text, href, id, how) {
return $('<li/>').attr({id: id || ''}).append(
$('<span/>').append(
$('<a/>').attr('href', href).text(text)
)
)[(how || 'prepend') + 'To']('#p-views ul');
}
});
})(jQuery);