Jump to content

User:Kondi/common.js

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Kondi (talk | contribs) at 20:36, 9 January 2013 (imported code). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
 

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','');	

		});

 

}