User:Kondi/common.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
The accompanying .css page for this skin can be added at User:Kondi/common.css. |
var codeExtensionsDict = {
'js' : {
'urlStub' : "#editJs",
'aceMode' : "ace/mode/javascript",
'fullName' : 'javascript'
},
'css' : {
'urlStub' : "#editCss",
'aceMode' : "ace/mode/css",
'fullName' : 'css'
}
};
var themeDict = {
'Chrome': 'chrome',
'Clouds': 'clouds',
'Clouds Midnight' : 'clouds_midnight',
'Cobalt': 'cobalt',
'Crimson Editor': 'crimson_editor',
'Dawn' : 'dawn',
'Dreamweaver': 'dreamweaver',
'Eclipse': 'eclipse',
'Github': 'github',
'Idle Fingers' : 'idle_fingers',
'Monokai' : 'monokai',
'Merbivore' : 'merbivore',
'Merbivore Soft' : 'merbivore_soft',
'Mono Industrial' : 'mono_industrial',
'Pastel Dark' : 'pastel_on_dark',
'Solarized Dark' : 'solarized_dark',
'Solarized Light' : 'solarized_light',
'TextMate' : 'textmate',
'Tomorrow' : 'tomorrow',
'Tomorrow Night' : 'tomorrow_night',
'Tomorrow Night Blue' : 'tomorrow_night_blue',
'Tomorrow Night Bright' : 'tomorrow_night_bright',
'Tomorrow Night 80\'s' : 'tomorrow_night_eighties',
'Twilight' : 'twilight',
'Vibrant Ink' : 'vibrant_ink'
};
var pageType = wgPageName.split('.').pop();
var codeEditMode = false;
var editor;
if(pageType in codeExtensionsDict)
{
var editCodeLink = $('a[accesskey="e"]');
var editUrl = document.URL;
editCodeLink.attr('href','javascript:void(0)');
editCodeLink.attr('title','Click to edit your '+pageType+' code');
editCodeLink.css('pointer-events','none');
editCodeLink.css('color','grey');
editCodeLink.click(function(){
codeEditMode = true;
$('#p-views li[id^="ca-"]').removeClass("selected");
$('#p-views li[id="ca-edit"]').addClass("selected");
var codeArea = $('pre.'+codeExtensionsDict[pageType]['fullName']+'.source-'+codeExtensionsDict[pageType]['fullName']);
var codeContent = codeArea.text();
codeArea.replaceWith(''+codeContent+'');
$('#editor').css('position','relative');
$('#editor').css('width','100%');
$('#editor').css('height','500px');
editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode(codeExtensionsDict[pageType]['aceMode']);
mw.loader.using('mediawiki.api.edit', function() {
$('#toolBar').html('Editor theme : Cancel');
for(var key in themeDict)
{
$('#themeSelect').append(''+key+'');
}
$('#themeSelect').change(function(){
var themeString = $('#themeSelect').val();
editor.setTheme(themeString.toString());
});
function saveCode(action) {
var edit = new mw.Api();
var params = {
action: 'edit',
title: mw.config.get('wgPageName'),
text: editor.getSession().getValue(),
summary: $('#wpSummmary').val()
};
edit.postWithEditToken(params).done(function(data){
if(('edit' in data) && (data.edit.result == 'Success')){
mw.util.jsMessage('Your code is saved');
}
else if('warnings' in data){
mw.util.jsMessage('Code save error');
}
if(action == 'reload')
{
location.reload(true);
}
});
}
$('#wpCodeSave').click(function(){
saveCode('save');
});
$('#wpCodeSaveDone').click(function(){
saveCode('reload');
});
$('span.cancelLink').click(function(){
location.reload(true);
});
});
});
$.getScript("http://d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js", function(data, textStatus, jqxhr) {
editCodeLink.css('pointer-events','auto');
editCodeLink.css('color','');
});
}