Difference between revisions of "MediaWiki:Common.js"
From cm2.liecourt.com
(Fill database) |
(Fill database) |
||
Line 124: | Line 124: | ||
}); | }); | ||
/* End of WSForm: Select2 */ | /* End of WSForm: Select2 */ | ||
+ | |||
+ | (function($) { | ||
+ | $.fn.autogrow = function() { | ||
+ | return this.each(function() { | ||
+ | var textarea = this; | ||
+ | $.fn.autogrow.resize(textarea); | ||
+ | $(textarea).focus(function() { | ||
+ | textarea.interval = setInterval(function() { | ||
+ | $.fn.autogrow.resize(textarea); | ||
+ | }, 500); | ||
+ | }).blur(function() { | ||
+ | clearInterval(textarea.interval); | ||
+ | }); | ||
+ | }); | ||
+ | }; | ||
+ | $.fn.autogrow.resize = function(textarea) { | ||
+ | var lineHeight = parseInt($(textarea).css('line-height'), 10); | ||
+ | var lines = textarea.value.split('\n'); | ||
+ | var columns = textarea.cols; | ||
+ | var lineCount = 0; | ||
+ | $.each(lines, function() { | ||
+ | lineCount += Math.ceil(this.length / columns) || 1; | ||
+ | }); | ||
+ | var height = lineHeight * (lineCount + 1); | ||
+ | $(textarea).css('height', height); | ||
+ | }; | ||
+ | })(jQuery); | ||
+ | |||
+ | $('textarea.form-control').autogrow(); |
Revision as of 11:29, 20 May 2020
/* Any JavaScript here will be loaded for all users on every page load. */ $( document ).ready(function() { $('body.action-edit #wpTextbox1, body.action-submit #wpTextbox1').keydown(function(event) { var x = event.keyCode; if (x == 13 ) { if(!event.shiftKey){ var insert = document.execCommand('insertText', false, '<!-- \n -->'); if(insert){ event.preventDefault(); $('#wpTextbox1').focus() insert } } } }) // Start of esc save $('body.action-edit, body.action-submit').keydown(function() { var x = event.keyCode; if (x == 27) { $.ajax({ url :'/api.php?action=query&meta=tokens&format=json', type: 'GET', dataType: 'json', success : function(result){ var token = result.query.tokens.csrftoken; $("input[name='wpEditToken']").attr("value", token ); saveEdit(); } }); }else{ mw.confirmCloseWindow(); $('#wpSave, #wpPreview, #wpDiff').on('click', function(){ $( window ).off( 'beforeunload' ); }) } }) if($('body').hasClass('action-submit')){ $('#top').prepend('<button class="btn btn-success enable-live-mode">Enable live mode</button>'); $(document).on('click','.enable-live-mode', function(){ if($(this).hasClass('btn-success')){ liveMode(); $(this).removeClass('btn-success'); $(this).text('Live mode is on'); }else{ $(this).addClass('btn-success'); $('body.action-submit textarea').off("change keyup paste"); $(this).text('Enable live mode'); } }); } }); window.saveEdit = function() { $.ajax({ url : $('form.mw-editform').attr('action'), type : 'POST', data :$('form.mw-editform').serialize(), success : function(html) { $( window ).off( 'beforeunload' ); mw.notify( 'Saved' ); if($('body').hasClass('action-submit')){ var parser = new DOMParser(); var doc = parser.parseFromString(html, "text/html"); var elem = doc.querySelectorAll('.mw-content-ltr')[0]; $('.mw-content-ltr').html(elem); } } }) }; window.liveMode = function() { var oldVal = ""; $('body.action-submit textarea').on("change keyup paste", function() { var currentVal = $(this).val(); if(currentVal == oldVal) { return; //check to prevent multiple simultaneous triggers } oldVal = currentVal; var text = encodeURIComponent($('textarea').val()); $.ajax({ url: '/api.php?action=parse&format=json&formatversion=2&title=New&text='+text+'&pst=&prop=text%7Cmodules%7Cjsconfigvars&preview=true&disableeditsection=true&uselang=en', dataType: 'json', success: function (x) { $('#wikiPreview').html(x.parse.text); } }); }); }; /* End of Esc Save */ /***** WSForm: loading Select2 ****/ $( document ).ready(function() { if ($('select[data-inputtype="ws-select2"]')[0]) { mw.loader.load('/extensions/WSForm/select2.min.css', 'text/css'); $.getScript('/extensions/WSForm/select2.min.js').done(function() { $('select[data-inputtype="ws-select2"]').each(function() { var selectid = $(this).attr('id'); var selectoptionsid = 'select2options-' + selectid; var select2config = $("input#" + selectoptionsid).val(); var F = new Function(select2config); return (F()); }); }); } }); /* End of WSForm: Select2 */ (function($) { $.fn.autogrow = function() { return this.each(function() { var textarea = this; $.fn.autogrow.resize(textarea); $(textarea).focus(function() { textarea.interval = setInterval(function() { $.fn.autogrow.resize(textarea); }, 500); }).blur(function() { clearInterval(textarea.interval); }); }); }; $.fn.autogrow.resize = function(textarea) { var lineHeight = parseInt($(textarea).css('line-height'), 10); var lines = textarea.value.split('\n'); var columns = textarea.cols; var lineCount = 0; $.each(lines, function() { lineCount += Math.ceil(this.length / columns) || 1; }); var height = lineHeight * (lineCount + 1); $(textarea).css('height', height); }; })(jQuery); $('textarea.form-control').autogrow();