(function($, mw) {
$(function() {
/** Advanced Site Notices ********
* Allow to custom dynamic site notices
* Maintainer: [[User:PhiLiP]]
*/
if (window.closeASNForever || $('#siteNotice').length < 0 || mw.config.get('wgAction') === 'edit' || mw.config.get('wgAction') === 'submit') {
return;
}
if (typeof(window.customASNInterval) === 'undefined') {
window.customASNInterval = 10;
}
var cname = 'dismissASN';
var cval = $.cookie(cname);
if (cval == '') {
cval = -1;
}
var rev = 0;
var toid = null;
var tb = $('<table id="asn-dismissable-notice" width="100%" style="background:transparent"/>');
var ct = $('<div id="advancedSiteNotices" style="height: 70px; padding: 0 10px; display: flex; justify-content: center; align-items: center; direction: rtl; word-break:break-word" class="mw-parser-output"/>');
var sd = $('<a href="#" title="دای بخە"><img src="//upload.wikimedia.org/wikipedia/commons/3/36/CloseWindow.svg" alt="دای بخە" border="0"></a>');
tb.append($('<tr/>').append($('<td/>').append(ct)).append($('<td style="vertical-align: top; width: 22px; height: 56px; paddaing: 1px;"/>').append(sd)));
var nts = null;
var styles = [];
sd.click(function() {
$.cookie(cname, rev, {
expires: 30,
path: '/',
secure: true
});
clearTimeout(toid);
tb.remove();
return false;
});
var matchCriteria = function(nt) {
var cache = nt.data('asn-cache');
if (cache !== undefined) {
return cache;
}
var criteria = nt.attr('data-asn-criteria');
if (criteria === undefined) {
criteria = nt.attr('class') ? 'false' : 'true';
if (nt.hasClass('only_sysop')) {
criteria += '||in_group("sysop")';
}
if (nt.hasClass('only_logged')) {
criteria += '||in_group("user")';
}
if (nt.hasClass('only_anon')) {
criteria += '||!in_group("user")';
}
} else {
criteria = decodeURIComponent(criteria.replace(/\+/g, '%20'));
criteria = criteria.trim();
}
if (criteria === '') {
criteria = 'true';
}
var testCriteria = function() {
var in_country = function(country) {
return window.Geo === undefined || Geo.country === country;
},
in_region = function(region) {
return window.Geo === undefined || Geo.region === region;
},
in_city = function(city) {
return window.Geo === undefined || Geo.city === city;
},
in_group = function(group) {
return $.inArray(group, mw.config.get('wgUserGroups')) > -1;
},
only_for = function(userlang) {
return userlang === mw.config.get('wgUserLanguage');
};
// FIXME: This shouldn't be using eval on data entered in wikitext. If that data is malformed it will throw an exception e.g. criteria = "(false))"
try {
return eval(criteria);
} catch (e) {
return false;
}
};
cache = testCriteria();
nt.data('asn-cache', cache);
return cache;
};
var loadNotices = function(pos) {
if (!tb.length) {
return;
}
ct.css('min-height', ct.height() + 'px');
tb.css('min-height', tb.height() + 'px');
var l = nts.length;
var nt = null;
var rt = 0;
while (rt++ < l) {
nt = $(nts[pos]);
if (matchCriteria(nt)) {
break;
}
pos = (pos + 1) % l;
}
if (rt >= l) {
return;
}
if (typeof nt.data('asn-style') == 'string') {
var style = mw.util.addCSS(decodeURIComponent(nt.data('asn-style').replace(/\+/g, '%20')));
nt.data('asn-style', null);
nt.data('asn-style-id', styles.length);
style.disabled = true;
styles.push(style);
}
if (typeof nt.data('asn-html') == 'string') {
nt.data('asn-html-raw', decodeURIComponent(nt.data('asn-html').replace(/\+/g, '%20')));
nt.data('asn-html', null);
}
var styleId = nt.data('asn-style-id');
nt = nt.data('asn-html-raw') || nt.html();
var cthtml = ct.html();
if (cthtml) {
if (cthtml !== nt) {
ct.stop().fadeOut(function() {
$.each(styles, function() {
this.disabled = true;
});
if (styles[styleId]) {
styles[styleId].disabled = false;
}
ct.html(nt);
// animation try /catched to avoid TypeError: (Animation.tweeners[prop]||[]).concat is not a function error being seen in production
try {
ct.fadeIn();
} catch (e) {}
});
}
} else if (rev == cval) {
return;
} else {
$.cookie(cname, null);
tb.appendTo($('#siteNotice'));
if (styles[styleId]) {
styles[styleId].disabled = false;
}
ct.html(nt).fadeIn();
}
toid = setTimeout(function() {
loadNotices((pos + 1) % l);
}, window.customASNInterval * 1000);
};
new mw.Api({
ajax: {
headers: {
'Api-User-Agent': 'w:ckb:MediaWiki:Gadget-AdvancedSiteNotices.js'
}
}
}).get({
page: 'Template:AdvancedSiteNotices/ajax',
variant: mw.config.get('wgUserVariant'),
prop: 'text',
action: 'parse',
format: 'json',
maxage: 3600,
smaxage: 3600
}).then(function(json) {
if (!json || !json.parse || !json.parse.text || !json.parse.text['*']) return;
json = $('<div/>').html(json.parse.text['*']).find('ul.sitents');
nts = $('li', json);
rev = json.data('asn-version');
var l = nts.length;
loadNotices(Math.floor(Math.random() * l));
}).catch(function(e) {
console.error('[AdvancedSiteNotices]: ', JSON.stringify(e));
});
});
})(jQuery, mediaWiki);