Difference between revisions of "MediaWiki:Common.js"
BrianFreud (talk | contribs) m |
BrianFreud (talk | contribs) m |
||
Line 61: | Line 61: | ||
wiki: { | wiki: { | ||
api: new mw.Api(), | api: new mw.Api(), | ||
+ | /* Retrieves the wiki markup for a page; defaults to the current page. */ | ||
+ | getMarkup: function (page) { | ||
+ | var getPage = function (callback, page) { | ||
+ | $.get('http://stratics.com/w/api.php?action=query&prop=revisions&rvprop=content&format=json&formatversion=2&titles=' + page, function (data) { | ||
+ | callback.apply(data); | ||
+ | }); | ||
+ | }; | ||
+ | getPage(function() { | ||
+ | stratics.wiki.markup = this.query.pages[0].revisions[0].content; | ||
+ | $('body').trigger('markupArrived'); | ||
+ | }, page || mw.config.get("wgPageName")); | ||
+ | }, | ||
+ | /* Submits a markup edit to the server */ | ||
+ | submitMarkup: function (summary, content) { | ||
+ | stratics.f.wiki.api.postWithToken("edit", { | ||
+ | action: "edit", | ||
+ | title: mw.config.get("wgPageName"), | ||
+ | summary: summary, | ||
+ | text: content | ||
+ | }).done(function (result, jqXHR) { | ||
+ | mw.log("Saved successfully"); | ||
+ | $('#instructionBox').html('Edits saved. Reloading the page.'); | ||
+ | stratics.f.util.reloadPage(); | ||
+ | }).fail(function (code, result) { | ||
+ | if (code === "http") { | ||
+ | mw.log("HTTP error: " + result.textStatus); // result.xhr contains the jqXHR object | ||
+ | } else if (code === "ok-but-empty") { | ||
+ | mw.log("Got an empty response from the server"); | ||
+ | } else { | ||
+ | mw.log("API error: " + code); | ||
+ | } | ||
+ | $('#instructionBox').html('An error occurred.<br>Please try again.'); | ||
+ | }); | ||
+ | } | ||
} | } | ||
}, | }, |
Revision as of 16:03, 30 March 2017
// <nowiki>
/* globals $, mw, uix, XenForo */
(function() {
"use strict";
var namespace = window.namespace = mw.config.get('wgCanonicalNamespace');
var action = window.action = mw.config.values.wgAction;
/* Add a class to allow per-theme styling of the wiki. */
var theme;
if (typeof uix === 'undefined') {
theme = $("dt:contains('Style')").next().find('a').text();
} else {
theme = uix.user.themeName;
}
$('#mw-content-text').addClass('w' + theme.replace(/\s/g,''));
/* Add a class to allow per-namespace styling of the wiki. */
$('#mw-content-text').addClass('wNamespace' + namespace);
/* Fix broken mail and alert popups while on the wiki */
XenForo.ExtLoader = function (f) {
return function () {
try {
f.apply(this, arguments);
} catch (e) {
var mailreq = 'http://stratics.com/conversations/popup?_xfRequestUri=%2F&_xfNoRedirect=1&_xfResponseType=json&_xfToken=' + $('input[name=_xfToken]:first').attr('value');
$.getJSON(mailreq, function (data) {
$('#ConversationsMenu .listPlaceholder').empty().html(data.templateHtml);
});
var alertreq = 'http://stratics.com/conversations/alerts-popup?_xfRequestUri=%2F&_xfNoRedirect=1&_xfResponseType=json&_xfToken=' + $('input[name=_xfToken]:first').attr('value');
$.getJSON(alertreq, function (data) {
$('#AlertsMenu .listPlaceholder').empty().html(data.templateHtml);
});
}
};
}(XenForo.ExtLoader);
/* Create a common namespace for stratics wiki js */
(function () {
var temp = {
data: {},
f: {
atlas: {
ui: {}
},
util: {
makehtml: function (type, obj, text) {
var str = "<" + type, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
str += ' ' + key + '="' + obj[key] + '"';
}
}
return str + ('input' === type ? '' : '>') + ("input" === type ? '/>' + (undefined === text ? '' : text)
: (undefined === text ? '' : text) + '</' + type + '>');
},
reloadPage: function () { location.reload(); }
},
wiki: {
api: new mw.Api(),
/* Retrieves the wiki markup for a page; defaults to the current page. */
getMarkup: function (page) {
var getPage = function (callback, page) {
$.get('http://stratics.com/w/api.php?action=query&prop=revisions&rvprop=content&format=json&formatversion=2&titles=' + page, function (data) {
callback.apply(data);
});
};
getPage(function() {
stratics.wiki.markup = this.query.pages[0].revisions[0].content;
$('body').trigger('markupArrived');
}, page || mw.config.get("wgPageName"));
},
/* Submits a markup edit to the server */
submitMarkup: function (summary, content) {
stratics.f.wiki.api.postWithToken("edit", {
action: "edit",
title: mw.config.get("wgPageName"),
summary: summary,
text: content
}).done(function (result, jqXHR) {
mw.log("Saved successfully");
$('#instructionBox').html('Edits saved. Reloading the page.');
stratics.f.util.reloadPage();
}).fail(function (code, result) {
if (code === "http") {
mw.log("HTTP error: " + result.textStatus); // result.xhr contains the jqXHR object
} else if (code === "ok-but-empty") {
mw.log("Got an empty response from the server");
} else {
mw.log("API error: " + code);
}
$('#instructionBox').html('An error occurred.<br>Please try again.');
});
}
}
},
keycodes: {
27: 'ESC',
33: 'NE',
34: 'SE',
35: 'SW',
36: 'NW',
37: 'W',
38: 'N',
39: 'E',
40: 'S'
},
wiki: {
markup: '',
openEdits: []
}
};
window.stratics = $.extend(true, (undefined === window.stratics) ? {} : window.stratics ,temp); // Avoid clobbering any existing window.stratics object.
})(window);
/* Tests if an element is visible on the screen. */
$.fn.isOnScreen = function() {
var element = this.get(0);
var bounds = element.getBoundingClientRect();
return bounds.top < window.innerHeight && bounds.bottom > 0;
};
/* Returns the scale transform value for an element. */
$.fn.getScale = function() {
var vals = this.css('transform');
return (vals == 'none') ? 1 : vals.split('(')[1].split(')')[0].split(',')[0];
};
/* Returns an array of the 4 inset values for an element. */
$.fn.getInsets = function() {
var vals = this.css('clip-path');
if (vals == 'none') {
vals = this.css('-webkit-clip-path');
}
return vals.split('(')[1].split(')')[0].split(/px\s?/);
};
/* Sort the options within a select alphabetically */
$.fn.sortOptions = function () {
var optionList = this.find('option')
.sort(function (a, b) {
return (a.text > b.text ? 1 : (a.text < b.text ? -1 : 0));
});
return this.empty()
.append(optionList);
};
/* Add visibility equivalents to show/hide/toggle */
$.fn.extend({
vis: function () {
return this.each(function () {
$(this).css("visibility", "visible");
});
},
invis: function () {
return this.each(function () {
$(this).css("visibility", "hidden");
});
},
toggleVis: function () {
return this.each(function () {
$(this).css("visibility", function (b, a) {
return "visible" == a ? "hidden" : "visible";
});
});
}
});
/* HTML construction helper function */
var makehtml = window.makehtml = function (type, o, text) {
var i = '<' + type;
for (var key in o) {
if (o.hasOwnProperty(key)) {
i += ' ' + key + '="' + o[key] + '"';
}
}
i += (type === 'input') ? '' : '>';
i += (type === 'input') ? '/>' + (text || '') : (text || '') + '</' + type + '>';
return i;
};
/* Add jQuery UI css */
var addjQueryCSS = function () {
$('head').append(makehtml('link', {
rel: 'stylesheet',
type: 'text/css',
href: 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/ui-darkness/jquery-ui.min.css'
}));
/* Workaround for jQuery UI/Xenforo/Mediawiki css conflict */
$('head').append(makehtml('style', {}, 'body .ui-dialog .ui-widget-header,body .ui-button {background:#333!important}'));
/* Add jQuery UI styling for input[type=number] */
$('head').append(makehtml('style', {}, 'input[type=number] {font-size: 13px;font-family: \'Open Sans\',Arial,sans-serif;color: rgb(207, 207, 207);background-color: #101010;padding: 5px 10px;margin-bottom: 2px;border: 1px solid #383838;-webkit-border-radius: 2px;-moz-border-radius: 2px;-khtml-border-radius: 2px;border-radius: 2px;outline: 0;-webkit-box-sizing: border-box;-moz-box-sizing: border-box;-ms-box-sizing: border-box;box-sizing: border-box;min-height: 30}'));
};
/* Start: collapsible navboxes */
$('.navbox-inner:gt(0)').addClass('mw-collapsed');
$('.navbox-inner').makeCollapsible();
/* End: collapsible navboxes */
/* Start Item Editor */
if (action === 'edit' && (namespace === 'UO' || namespace === 'User' )) {
addjQueryCSS();
/* Load properties JSON */
$.getScript('/w/index.php?title=MediaWiki:PropertyBuilder.js&action=raw&ctype=text/javascript').complete(function () {
/* Load jQuery UI */
mw.loader.using(['jquery.ui.button', 'jquery.ui.tabs', 'jquery.ui.dialog'], function () {
/* Load Editor */
$.getScript('/w/index.php?title=MediaWiki:PropertyEditor.js&action=raw&ctype=text/javascript', function () {
loadEditor();
});
});
});
}
/* End Item Editor */
/* Start Atlas Tools */
if (/^UO\:Atlas/.test(mw.config.values.wgPageName) && namespace === 'UO' && $('.map-holder').length > 0) { /* Only run on UO Atlas pages which have a map present. */
addjQueryCSS();
/* Load jQuery UI */
mw.loader.using(['jquery.ui.button', 'jquery.ui.tabs', 'jquery.ui.dialog'], function () {
/* Load Editor */
$.getScript('/w/index.php?title=MediaWiki:AtlasTools.js&action=raw&ctype=text/javascript');
});
}
/* End Atlas Tools */
}());
// </nowiki>