משתמש:היהודי/common.js: הבדלים בין גרסאות בדף

מתוך אוצר הספרים היהודי השיתופי
קפיצה לניווט קפיצה לחיפוש
אין תקציר עריכה
אין תקציר עריכה
שורה 1: שורה 1:
/* תוספות לסרגל כלים רגיל
mw.loader.using( ['mediawiki.api', 'mediawiki.user', 'mediawiki.util'] , function() {
*
 
*/
const storageKeyName = 'watchlistwatcher-newestclicked';
var api = new mw.Api( { cache: false } ),
function ExEditToolbar(){
titles,
/* הרחבת סרגל הכלים */
items,
var customButtons;
newest = 0,
mw.loader.using(['mediawiki.toolbar'],function(){
watchButton,  
function addBtn( location, imageFile, tagOpen, sampleText, tagClose, speedTip ) {
watchwButtonAnchor,
    mw.toolbar.addButton( ((location == 1) ? "//wiki.jewishbooks.org.il/" : "//upload.wikimedia.org/" ) + imageFile,speedTip,tagOpen, tagClose,sampleText);
timer,
}
storage = window.localStorage;
$(function() {
var toolbar = document.getElementById('toolbar');
if (!toolbar) return;
// Group 1
mw.toolbar.addButton("//upload.wikimedia.org/wikipedia/commons/1/1d/Button_italic.png", "טקסט נטוי (לא מומלץ בעברית)", "\'\'" ,"\'\'","טקסט נטוי");
addBtn(0, "wikipedia/commons/8/89/Button_bigger.png", '<big>', "טקסט מוגדל", "</big>", "טקסט מוגדל");
addBtn(0, "wikipedia/commons/0/0d/Button_smaller.png", '<small>', "טקסט מוקטן", "</small>", "טקסט מוקטן");
addBtn(0, "wikipedia/he/c/c9/Button_strike.png", "<s>", "טקסט המסומן כמחוק", "</s>", "טקסט מחוק");
addBtn(0, "wikipedia/he/1/1e/Button_font_color.png", '<span style="color: ColorName;">', "טקסט צבוע", "</span>", "טקסט עם צבע");
addBtn(0, "wikipedia/commons/6/6a/Button_sup_letter.png", "<sup>", "כתב עילי", "</sup>", "כתב עילי");
addBtn(0, "wikipedia/commons/a/aa/Button_sub_letter.png", "<sub>", "כתב תחתי", "</sub>", "כתב תחתי");
addBtn(0, "wikipedia/he/2/23/Button_code.png", '<syntaxhighlight lang="text">\n', "טקסט", "\n<\/syntaxhighlight>\n", "עיצוב קוד");
// ground 4
addBtn(0, "wikipedia/he/1/12/Button_gallery.png", "<gallery>\n", "קובץ:PictureFileName.jpg|כיתוב תמונה\nקובץ:PictureFileName.jpg|כיתוב תמונה", "\n</gallery>", "יצירת גלריית תמונות");
addBtn(0, "wikipedia/he/1/13/Button_enter.png", "{}}", "", "", "ירידת שורה");
// group 5:
addBtn(0, "wikipedia/he/d/d3/Button_definition_list.png", "\n; ", "פריט", " : ", "רשימת הגדרות");
addBtn(0, "wikipedia/he/5/5f/Button_center.png", '<div style="text-align: center;">\n', "טקסט ממורכז", "\n</div>", "מירכוז טקסט");
addBtn(0, "wikipedia/he/e/ea/Button_align_left.png", '<div class="mw-content-ltr">\n', "טקסט מיושר לשמאל", "\n</div>", "יישור טקסט לשמאל");
addBtn(0, "wikipedia/commons/a/ac/Button_ref.PNG", "{{הערה|", "הקלידו הערת שוליים כאן", "}}", "הערת שוליים");
addBtn(0, "wikipedia/he/3/34/Button_hide_comment.png", "<!-- ", "הערה מוסתרת", " -->", "הערה מוסתרת");
addBtn(0, "wikipedia/commons/c/c8/Button_redirect.png", "#הפניה [[", "יעד", "]]", "הפניה");
});
});
function addToWikiEditor(){
function i18n( key ) {
/* Math editor  */
switch ( mw.user.options.get( 'language' ) ) {
/* Math editor  */
case 'he':
function mathDialog(tag, boxtitle, help){
switch ( key ) {
// override user preference, otherwise mathjax enabler will just skip init
case 'title' : return 'רשימת המעקב שלך';
mw.loader.using(['jquery.ui.dialog'],function(){
var mathEditor=$('<div>');
var mathInput=$('<textarea>', { dir:'ltr', rows: 8, cols: 40 }).css('width', '100%').appendTo(mathEditor);
var displayOptions=$('<div>הצגה ממורכזת</div>').appendTo(mathEditor);
var mathPreviewDiv=$('<div>').appendTo(mathEditor);
var mathPreviewSpan=$('<span>', { dir: "ltr", 'class' : "tex" }).appendTo(mathPreviewDiv);
var blockDisplay=$('<input type="checkbox" />').prependTo(displayOptions);
mathEditor.dialog( {
title: boxtitle,
height: 0.5 * $('body').height(),
width: 0.5 * $('body').width(),
buttons: {
'הוספה': function(){
$("#wpTextbox1").textSelection('encapsulateSelection', {replace: true, peri: '<' + tag +
(blockDisplay.prop('checked')? ' display="block"' : '')+'>'+mathInput.val()+'</' + tag + '>'});
$(this).dialog('close');
},
'עזרה':function(){window.open(mw.util.getUrl(help));
},
'ביטול': function(){ $(this).dialog('close');}
} // buttons
});
function updatePreview() {
api.get( {
action: 'parse',
prop: 'text',
text: '<' + tag + '>' + mathInput.val() + '</' + tag + '>',
contentmodel: 'wikitext'
} )
.done( function(d) {
mathPreviewSpan.html(d.parse.text['*']);
$( 'p', mathPreviewSpan ).css( { overflow: 'auto' } );
} );
}
}
mathInput
break;
.on( 'keyup', updatePreview )
case 'en':
.on( 'paste', function() { setTimeout( updatePreview, 0 ) } ); // we need to do it after the event has been processed: we get the trigger b4 the content is actually updated.
switch ( key ) {
case 'title': return 'Your watchlist';
var api=new mw.Api();
var selection = $("#wpTextbox1").textSelection( 'getSelection' );
//insert the selected text into the equation editor
var mathRgx=new RegExp('<' + tag + '>(.*)<\/' + tag + '>');
if(selection && mathRgx.test(selection)){
mathInput.val(mathRgx.exec(selection)[1]);
mathInput.keyup();
}
}
});
break;
}
}
return key;
}
function updateStorageAndRefresh() {
storage.setItem( storageKeyName, newest ); // remember the click: only items newer than latest click will trigger coloring the bubble.
triggerQuery( 0 ); // initiate click, so when it returns we can color the icon accordingly
}
function showWatchlistDialog( e ) {
e.stopImmediatePropagation();
e.preventDefault();
updateStorageAndRefresh();
var $this = $( this ),
width60 = $( 'body' ).width() * 0.6,
height60 = $( 'body' ).height() * 0.6;
$('#wpTextbox1').on('wikiEditor-dialogs-loaded-insert-file',function(){
$.ajax( { url: mw.util.getUrl( 'Special:Watchlist' ) } )
$( '#wikieditor-toolbar-file-dialog' )
.done( function( data ) {
.dialog( {
mw.loader.using( 'jquery.ui' ).done ( function() {
open: function() {
var content = $( data ).find( '.mw-changeslist' ),
$( '#wikieditor-toolbar-file-format' ).val( 'ממוזער' );
cl = '';
}
} );
content
});
.parents()
.each( function() {  
// קבוצה חדשה בתפריט מתקדם:
if ( $( this ).length )  
$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
cl += $this.attr( 'class' ) + ' ';  
'section': 'advanced',
} );
'groups': {
'more': {
$( '<div>' )
'label': ""
.addClass( 'mw-changeslist ' + cl )
}
.dialog( {
}
position: {
my: "center top",
at: "center bottom",
of: $this
},
width: width60,
height: height60,
close: function() { $( this ).remove(); }
} )
.css( { overflow: 'auto' } )
.append( content );
} );
} );
} );
}
// קו חוצה
function announce() {
$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
const new_id = 'pt-notifications-watchlist';
'section': 'main',
var count = titles.length;
'group': 'format',
if ( ! watchButton ) {
'tools': {
var url,
'strikethrough': {
notif = $( '#pt-notifications-notice, #pt-notifications-alert' ).eq(-1);
label: 'קו חוצה',
type: 'button',
if ( ! notif.length )
filters: [ 'body.ns-4, body.ns-talk' ],
return;
icon: '//upload.wikimedia.org/wikipedia/he/8/8d/NewInlineBtn.png',
 
action: {
watchButton = notif.clone()
type: 'encapsulate',
.attr( { id: new_id } )
options: {
.insertAfter( notif );
pre: '{'+'{כתב מחוק|',
watchwButtonAnchor = watchButton.find( 'a' )
peri: "טקסט מחוק",
.removeClass('oo-ui-icon-tray')
post: '}'+'}'
.click( showWatchlistDialog )
}
.attr( { href: mw.util.getUrl( 'Special:Watchlist' ), title: i18n( 'title' ) } );
}
}
}
var lastClicked = parseInt( storage.getItem( storageKeyName ) ) || 0,
}
toColor = newest > lastClicked;
} );
watchwButtonAnchor
.toggleClass( 'mw-echo-notifications-badge-unseen', toColor )
.toggleClass( 'mw-echo-notifications-badge-all-read', count === 0 )
.attr( { 'data-counter-text': count || '', 'data-counter-num': count } );
}


// נוסחאות מתמטיות
function calcParams() {
// נוסחאות כימיות
var
// הערה מוסתרת
opts = {
// טקסט ממורכז
/* watchlisthideown: we do not care about this one, since "unread" will never have own edits anyway */
// טקסט מיושר לשמאל
watchlisthideanons: '!anon',
// תגי syntaxhighlight
watchlisthidebots: '!bot',
// תו כיווניות
watchlisthideliu: 'anon', /* this strangely named option means "hide regitered users", IOW, show anons */
$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
watchlisthideminor: '!minor',
'section': 'advanced',
watchlisthidepatrolled: '!patrolled'
'group': 'more',
},
'tools': {
show = Object.keys( opts )
'formula': {
.filter( function( k ) {return mw.user.options.get( k ); } )
label: 'נוסחה מתמטית (LaTeX)',
.map( function( k ) { return opts[k]; } )
type: 'button',
.concat( 'unread' )
icon: '//upload.wikimedia.org/wikipedia/commons/thumb/3/3e/Nuvola_apps_edu_mathematics_blue-p.svg/22px-Nuvola_apps_edu_mathematics_blue-p.svg.png',
.join( '|' );
action: {type: 'callback', execute: (function () {return mathDialog('math', 'הוספת משוואה', 'עזרה:נוסחאות'); }) }
return {  
},
list: 'watchlist',  
'hiddennote': {
wlprop: 'ids|user|title|timestamp|notificationtimestamp',  
label: 'הערה מוסתרת',
wlshow: show,  
type: 'button',
wltype: mw.user.options.get( 'watchlisthidecategorization' ) ? 'edit|new|log' : 'edit|new|log|categorize',
icon: '//upload.wikimedia.org/wikipedia/commons/thumb/b/b5/Gnome-document-new.svg/22px-Gnome-document-new.svg.png',
wllimit: window.script88limit || 50
action: {
};
type: 'encapsulate',
options: {
pre: '<!-- ',
peri: "הערה מוסתרת",
post: " -->"
}
}
},
'leftalign': {
label: 'טקסט מיושר לשמאל',
type: 'button',
icon: '//upload.wikimedia.org/wikipedia/commons/thumb/c/ce/Gnome-format-justify-left.svg/22px-Gnome-format-justify-left.svg.png',
action: {
type: 'encapsulate',
options: {
pre: '<div class="mw-content-ltr">',
peri: "טקסט מיושר לשמאל",
post: "</div>"
}
}
},
'sourcetags': {
label: 'עיצוב קוד מקור',
type: 'button',
icon: '//upload.wikimedia.org/wikipedia/commons/thumb/6/66/Utilities-terminal.svg/22px-Utilities-terminal.svg.png',
action: {
type: 'encapsulate',
options: {
pre: '<syntaxhighlight lang="text">\n',
peri: "טקסט",
post: "\n</syntaxhighlight>\n"
}
}
},
'rlm': {
label: 'תו כיווניות',
type: 'button',
icon: '//upload.wikimedia.org/wikipedia/commons/thumb/a/a6/Gnome-format-text-direction-rtl.svg/22px-Gnome-format-text-direction-rtl.svg.png',
action: {
type: 'encapsulate',
options: {
pre: '{'+'{כ}'+'}'
}
}
}
}
} );
}
}
/* תוספות לסרגל כלים משופר
*
*/
if ( mw.user.options.get('usebetatoolbar')) {
mw.loader.using(['ext.wikiEditor'],function(){
if ($('#wikiEditor-ui-toolbar').length === 1) addToWikiEditor();//in case it loaded after toolbar initaliztion
else $( '#wpTextbox1' ).on( 'wikiEditor-toolbar-doneInitialSections', addToWikiEditor);
} );
function triggerQuery( timeout ) {
clearTimeout( timer );
if ( typeof(timeout) != 'number')
timeout = 1000;
timer = setTimeout( queryAndUpdate, timeout );
}
}
}
if ( ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) + 1 ) &&
function queryAndUpdate() {
!$( '#wpTextbox1' ).prop( 'readonly' )
api.get( calcParams() )
) {
.done( function(data) {
$(document).ready(ExEditToolbar);
var counts = {};
}
if ( data && data.query && data.query.watchlist ) {
items = data.query.watchlist;
items.forEach( function( item ) { counts[item.title] = ( counts[item.title] || 0 ) + 1; } );
newest = items.map( function( item ) { return new Date( item.timestamp ).getTime(); } ).sort().pop();
titles = Object.keys( counts );
announce();
triggerQuery( 60000 );
}
} ); // done
} // queryandupdate
 
//load css page. would do it locally, but javascript does not support multiline strings, and putting all the CSS locally is just too much.
 
mw.loader.load( '//wiki.jewishbooks.org.il/mediawiki/index.php?title=מדיה_ויקי:סקריפטים/88.css&action=raw&ctype=text/css', 'text/css' );
queryAndUpdate();
$('#mw-watchlist-resetbutton').submit( triggerQuery );
$( 'body ').on( 'script-88-refresh', triggerQuery );
mw.hook('wikipage.content').add( triggerQuery );
$('.oo-ui-buttonElement-button').click( triggerQuery );
$( 'body ').on( 'script-88-pretend-clicked', updateStorageAndRefresh );
} );

גרסה מ־10:15, 19 בינואר 2021

mw.loader.using( ['mediawiki.api', 'mediawiki.user', 'mediawiki.util'] , function() {

	const storageKeyName = 'watchlistwatcher-newestclicked';
	var api = new mw.Api( { cache: false } ),
		titles,
		items,
		newest = 0,
		watchButton, 
		watchwButtonAnchor,
		timer,
		storage = window.localStorage;
	
	function i18n( key ) {
		switch ( mw.user.options.get( 'language' ) ) {
			case 'he':
				switch ( key ) {
					case 'title' : return 'רשימת המעקב שלך';
				}
				break;
			case 'en':
				switch ( key ) {
					case 'title': return 'Your watchlist';
				}
				break;
		}
		return key;
	}
	
	function updateStorageAndRefresh() {
		storage.setItem( storageKeyName, newest ); // remember the click: only items newer than latest click will trigger coloring the bubble.
		triggerQuery( 0 );	// initiate click, so when it returns we can color the icon accordingly
	}
	
	function showWatchlistDialog( e ) {
		e.stopImmediatePropagation();
		e.preventDefault();

		updateStorageAndRefresh();		

		var $this = $( this ),
			width60 = $( 'body' ).width() * 0.6,
			height60 = $( 'body' ).height() * 0.6;
		
		$.ajax( { url: mw.util.getUrl( 'Special:Watchlist' ) } )
			.done( function( data ) {
				mw.loader.using( 'jquery.ui' ).done ( function() {
					var content = $( data ).find( '.mw-changeslist' ),
						cl = '';
					
					content
						.parents()
						.each( function() { 
							if ( $( this ).length ) 
								cl += $this.attr( 'class' ) + ' '; 
						} );
					
					$( '<div>' )
						.addClass( 'mw-changeslist ' + cl )
						.dialog( {
							position: {
								my: "center top",
								at: "center bottom",
								of: $this
							},
							width: width60,
							height: height60,
							close: function() { $( this ).remove(); }
						} )
						.css( { overflow: 'auto' } )
						.append( content );
				} );
			} );
	}
	
	function announce() {
		const new_id = 'pt-notifications-watchlist';
		var count = titles.length;
		if ( ! watchButton ) {
			var url,
				notif = $( '#pt-notifications-notice, #pt-notifications-alert' ).eq(-1);
				
			if ( ! notif.length )
				return;

			watchButton = notif.clone()
				.attr( { id: new_id } )
				.insertAfter( notif );
			watchwButtonAnchor = watchButton.find( 'a' )
				.removeClass('oo-ui-icon-tray')
				.click( showWatchlistDialog )
				.attr( { href: mw.util.getUrl( 'Special:Watchlist' ), title: i18n( 'title' ) } );
		}
		var lastClicked = parseInt( storage.getItem( storageKeyName ) ) || 0,
			toColor = newest > lastClicked;
			
		watchwButtonAnchor
			.toggleClass( 'mw-echo-notifications-badge-unseen', toColor )
			.toggleClass( 'mw-echo-notifications-badge-all-read', count === 0 )
			.attr( { 'data-counter-text': count || '', 'data-counter-num': count } );
	}

	function calcParams() {
		var 
			opts = {
				/* watchlisthideown: we do not care about this one, since "unread" will never have own edits anyway */
				watchlisthideanons: '!anon',
				watchlisthidebots:	'!bot',
				watchlisthideliu: 	'anon',  /* this strangely named option means "hide regitered users", IOW, show anons */
				watchlisthideminor:	'!minor',
				watchlisthidepatrolled: '!patrolled'
			},
			show = Object.keys( opts )
				.filter( function( k ) {return mw.user.options.get( k ); } )
				.map( function( k ) { return opts[k]; } )
				.concat( 'unread' )
				.join( '|' );
		return { 
			list: 'watchlist', 
			wlprop: 'ids|user|title|timestamp|notificationtimestamp', 
			wlshow: show, 
			wltype: mw.user.options.get( 'watchlisthidecategorization' ) ? 'edit|new|log' : 'edit|new|log|categorize',
			wllimit: window.script88limit || 50 
		};
	}
	
	function triggerQuery( timeout ) {
		clearTimeout( timer );
		if ( typeof(timeout) != 'number')
			timeout = 1000;
		timer = setTimeout( queryAndUpdate, timeout );		
	}
	
	function queryAndUpdate() {
		api.get( calcParams() )
			.done( function(data) {
				var counts = {};
				if ( data && data.query && data.query.watchlist ) {
					items = data.query.watchlist;
					items.forEach( function( item ) { counts[item.title] = ( counts[item.title] || 0 ) + 1; } );
					newest = items.map( function( item ) { return new Date( item.timestamp ).getTime(); } ).sort().pop();
					titles = Object.keys( counts ); 
					announce();
					triggerQuery( 60000 );
				}
			} ); // done
	} // queryandupdate

	//load css page. would do it locally, but javascript does not support multiline strings, and putting all the CSS locally is just too much.

	mw.loader.load( '//wiki.jewishbooks.org.il/mediawiki/index.php?title=מדיה_ויקי:סקריפטים/88.css&action=raw&ctype=text/css', 'text/css' );
	queryAndUpdate();
	$('#mw-watchlist-resetbutton').submit( triggerQuery );
	$( 'body ').on( 'script-88-refresh', triggerQuery );
	mw.hook('wikipage.content').add( triggerQuery );
	$('.oo-ui-buttonElement-button').click( triggerQuery );
	$( 'body ').on( 'script-88-pretend-clicked', updateStorageAndRefresh );
} );