MediaWiki:Gadget-edit-form-ui.js: Różnice pomiędzy wersjami

Z Wikisłownika – wolnego słownika wielojęzycznego
Usunięta treść Dodana treść
m +
m $('#ed').hide()
Linia 2: Linia 2:
oldform: undefined,
oldform: undefined,
tbox: undefined,
tbox: undefined,
form: $( '<div>' ).attr( 'id', 'ed' ),
form: $( '<div>' ).attr( 'id', 'ed' ).hide(),
menu_ctrl: $( '<ul>' ).attr( 'id', 'ed_menu_controls' ),
menu_ctrl: $( '<ul>' ).attr( 'id', 'ed_menu_controls' ),
menu_sect: $( '<ul>' ).attr( 'id', 'ed_menu_sections' ),
menu_sect: $( '<ul>' ).attr( 'id', 'ed_menu_sections' ),
Linia 34: Linia 34:
window.EUi_dbg3 = EUi.form.is( ':visible' );
window.EUi_dbg3 = EUi.form.is( ':visible' );
window.EUi_dbg4 = EUi.form.css( 'display' );
window.EUi_dbg4 = EUi.form.css( 'display' );
window.EUi_test = true;
}
}

Wersja z 21:08, 22 gru 2014

EUi = {
	oldform:        undefined,
	tbox:           undefined,
	form:           $( '<div>' ).attr( 'id', 'ed' ).hide(),
	menu_ctrl:      $( '<ul>' ).attr( 'id', 'ed_menu_controls' ),
	menu_sect:      $( '<ul>' ).attr( 'id', 'ed_menu_sections' ),
	content:        $( '<div>' ).attr( 'id', 'ed_content' ),
	ajr:            $( '<div>' ).attr( 'id', 'ajax_results' ),
	usingNew:       true,
	activeLangCode: '',
	activeLangId:   '',
	formChanged:    false,

	prepareForm: function( oldform, tbox ) {
		this.oldform = oldform;
		this.tbox = tbox;
		
		EUi.form.append(
			$( '<div>' )
				.attr( 'id', 'ed_menu' )
				.append( EUi.menu_ctrl, EUi.menu_sect ),
			EUi.content,
			EUi.ajr.hide()
		);
		
		oldform.first().before( EUi.form );
		EUi.usingNew = ( mw.cookie.get( 'EditFormCurrent', null, 'new' ) === 'new' );

		if ( EUi.usingNew ) {
			oldform.hide();
			window.EUi_dbg1 = EUi.form.css( 'display' );
			window.EUi_dbg2 = EUi.form.is( ':visible' );
			EUi.form.show();
			window.EUi_dbg3 = EUi.form.is( ':visible' );
			window.EUi_dbg4 = EUi.form.css( 'display' );
			window.EUi_test = true;
		}
		
		EUi.prepareFormSections();
		EUi.addControlButtons();
		EUi.rebindFormActions();
		EKeyboard.init();
		
		if (
			mw.user.options.get( 'previewonfirst' ) &&
			mw.user.options.get( 'previewontop' )
		) {
			EUi.form[ 0 ].scrollIntoView();
		}
		
		EUi.form
			.on( 'mouseover', '.tip', $( this ).showtip )
			.on( 'mouseout',  '.tip', $( this ).hidetip )
			.on( 'input propertychange', '.keyboardable', function() {
				EUi.formChanged = true;
			} );
		
		$( window )
			.on( 'resize', EUi.resizeTextareas )
			.on( 'resize', EUi.organizeSectionMenu )
			.on( 'resize', EUi.relocateResult );
		
		$( document ).on( 'keyup', function( e ) {
			if ( e.keyCode === 27 ) {
				EUi.hideResult();
				return false;
			}
		} );
	},
	
	addControlButtons: function() {
		var newformToggle, oldformToggle;
		
		var onclickToggle = function() {
			EUi.usingNew = !EUi.usingNew;
			
			if ( EUi.usingNew ) {
				EUi.oldform.hide();
				EUi.form.show();
				Ed.resetNew();
			} else {
				EUi.oldform.show();
				EUi.form.hide();
				EUi.tbox.val( EPrinter.recalculateCode() );
			}
			
			ESpecialChars.toggle();
			
			mw.cookie.set( 'EditFormCurrent', EUi.usingNew
				? 'new'
				: 'old'
			);
			
			return false;
		};
		
		newformToggle = $( '<li>' )
			.attr( {
				'id':    'ed_menuitem_toggle',
				'class': 'tip menuitem active ed_toggle_editor'
			} )
			.appendTo( EUi.menu_ctrl )
			.on( 'click', onclickToggle )
			.data( 'tip', EStr.TOGGLE_EDITOR );
		
		oldformToggle = $( '<div>' )
			.attr( {
				'class': 'ed_toggle_editor',
				'title': EStr.TOGGLE_EDITOR
			} )
			.on( 'click', onclickToggle );
		
		if ( mw.user.options.get( 'usebetatoolbar' ) ) {
			mw.loader.using( 'ext.wikiEditor.toolbar', function() {
				oldformToggle
					.addClass( 'group group-ed-toggle' )
					.prependTo( $( '#wikiEditor-section-main' ) );
			} );
		} else {
			mw.loader.using( 'mediawiki.action.edit', function() {
				oldformToggle
				.addClass( 'mw-toolbar-editbutton' )
				.prependTo( $( '#toolbar' ) );
			} );
		}
	},

	reset: function() {
		EUi.menu_sect.html( '' );
		EUi.content.html( '' );

		EUi.prepareFormSections();
	},

	clickDefaultSection: function() {
		var firstTab;

		if ( !EUi.usingNew ) {
			return false;
		}
		
		firstTab = EUi.menu_sect
			.children( ':not(#ed_menuitem_' + EConstants.SECTION_ID_INTRO + ')' )
			.first();
		
		if ( firstTab.attr( 'id' ) !== 'ed_menuitem_new' ) {
			firstTab.trigger( 'click' );
		} else if ( Ed.content.sections[ EConstants.SECTION_ID_INTRO ] !== undefined ) {
			$( '#ed_menuitem_' + EConstants.SECTION_ID_INTRO ).trigger( 'click' );
		} else {
			$( '#ed_menuitem_new' ).trigger( 'click' );
		}
		
		return true;
	},

	prepareFormSections: function() {
		var id, addItem;

		for ( id in Ed.content.sections ) {
			if ( Ed.content.sections.hasOwnProperty( id ) ) {
				EUi.addSection( id );
				EUi.prepareFormSubsections( id );
			}
		}

		if ( !EUtil.isEditingSection() ) {
			addItem = $( '<li>' )
				.attr( {
					'id':   'ed_menuitem_new',
					'class': 'tip menuitem'
				} )
				.text( EStr.ADD )
				.appendTo( EUi.menu_sect )
				.on( 'click', function() {
					EUi.addNewSection();
					return false;
				} )
				.data( 'tip', EStr.ADD_SECTION );
		}

		EUi.organizeSectionMenu();
		EUi.clickDefaultSection();
		EUi.resizeTextareas();
		
		if (
			$( '#ed_menuitem_' + EConstants.SECTION_ID_INTRO ).length === 0 &&
			!EUtil.isEditingSection()
		) {
			EUi.addIntroAdder();
		}
	},

	addSection: function( id ) {
		var item, tip, added = false,
		sec = Ed.content.sections[ id ],
		fset = $( '<fieldset>' ).attr( {
				'id':    'ed_section_' + id,
				'class': 'ed_section'
			} )
			.appendTo( EUi.content );

		if ( id === EConstants.SECTION_ID_INTRO ) {
			sec.code = EStr.INTRO_MENU;
			sec.title = '';
		}

		item = $( '<li>' )
			.attr( {
				'id':    'ed_menuitem_' + id,
				'class': 'tip menuitem'
			} )
			.text( sec.code );
		
		tip = ( id === EConstants.SECTION_ID_INTRO )
			? EStr.INTRO_SECTION
			: EParser.insideTemplate( sec.title ) + '<br/><small>tytuł sekcji: <tt>' + sec.title + '</tt></small>';
		
		item.data( {
			section: 'ed_section_' + id,
			code:    sec.code,
			tip:     tip
		} )
		.on( 'click', function() {
			var defFocus;

			EKeyboard.hide();
			EUi.hideResult();
			
			EUi.content
				.find( '.ed_section' )
				.removeClass( 'active' );
			
			EUi.content
				.find( '#' + $( this ).data( 'section' ) )
				.addClass( 'active' );
			
			$( this )
				.addClass( 'active' )
				.siblings()
				.removeClass( 'active' );
			
			EUi.resizeTextareas();
			EUi.activeLangCode = $( this ).data( 'code' );
			EUi.activeLangId = id;
			
			defFocus = $( '#ed_content textarea.oblig_subsection:visible' );
			
			if ( defFocus.length === 0 ) {
				defFocus = $( '#ed_content fieldset.active textarea:first' );
			}
			
			window.setTimeout( function() {
				defFocus.focus();
			}, 100 ); //FIXME why?
			
			return false;
		} );

		// insert alphabetically
		EUi.menu_sect.children( 'li' ).each( function() {
			if (
				$( this ).attr( 'id' ) > item.attr( 'id' ) ||
				$( this ).attr( 'id' ) === 'ed_menuitem_new'
			) {
				item.insertBefore( $( this ) );
				added = true;
				return false;
			}
		} );
		
		if ( !added ) {
			item.appendTo( EUi.menu_sect );
		}
		
		if ( id === EConstants.SECTION_ID_INTRO ) {
			EAutomator.fillInterwiki();
		}
	},

	addNewSection: function() {
		var defaultText, message,
		defaultLang = EUtil.getSection();

		if ( !defaultLang || defaultLang === 'editform' ) {
			defaultLang = mw.cookie.get( 'EditFormLastLang' );
		}
		
		defaultText = defaultLang
			? EParser.getTitleFromCode( defaultLang )
			: mw.config.get( 'wgPageName' ) + EStr.ADD_SECTION_TEMPLATE;
		
		message = defaultLang
			? EStr.ADD_SECTION_MESSAGE_DEFAULT
			: EStr.ADD_SECTION_MESSAGE;

		$.alerts.prompt(
			message,
			defaultText,
			EStr.ADD_SECTION_TITLE,
			function( val ) {
				var sec, id;
	
				if ( !val ) {
					return;
				}
				
				sec = EParser.getSectionFromInput( val );
	
				if ( sec.code ) {
					id = sec.id;
					
					if ( Ed.content.sections[ id ] !== undefined ) {
						$.alerts.alert(
							EStr.ADD_SECTION_ALREADY,
							EStr.ADD_SECTION_ALREADY_TITLE
						);
					} else {
						Ed.content.sections[ id ] = sec;
						ESectionParser.parse( sec );
	
						EUi.addSection( id );
						EUi.prepareFormSubsections( id );
						EUi.addDefaultTexts( id, sec.code );
						
						mw.cookie.set( 'EditFormLastAdded', sec.code );
					}
					
					$( '#ed_menuitem_' + id ).trigger( 'click' );
					$( '#ed_section_' + id + ' textarea' ).reverse().autoresize();
					
					EPrinter.appendEditDescription(
						'+sekcja: ' +
						EConstants.CODE_TO_LANG[ sec.code ]
					);
				} else {
					$.alerts.alert(
						EStr.ADD_SECTION_NONEXISTENT,
						EStr.ADD_SECTION_NONEXISTENT_TITLE,
						function() {
							EUi.addNewSection();
						}
					);
				}
		} );
		
		$( '#popup_prompt' ).suggestions( 'fetch', function( input, response ) {
			var template = EParser.insideTemplate( input );
			var tmplShort = template.replace( 'język ', '' );
			
			var suggestions = $.map( EConstants.LANG_CODES, function( code, lang ) {
				if (
					tmplShort &&
					lang.indexOf( tmplShort ) === 0
				) {
					var filled = template.replace( /(język )?(.*)/, '$1' + lang );
					return input.replace( template, filled );
				} else if (
					!template &&
					lang.indexOf( input ) === 0
				) {
					return lang;
				}
			} );
			
			response( suggestions );
		} );
	},

	editSectionTitle: function( id, section ) {
		$.alerts.prompt(
			EStr.EDIT_SECTION_TITLE_MESSAGE,
			section.title,
			EStr.EDIT_SECTION_TITLE,
			function( res ) {
				var tip;
	
				if ( !res ) {
					return;
				}
				
				section.title = res;
				tip = EParser.insideTemplate( res ) + '<br/><small>tytuł sekcji: <tt>' + res + '</tt></small>';
				$( '#ed_menuitem_' + id ).data( 'tip', tip );
		} );
	},

	deleteSection: function( id, force ) {
		var del = function() {
			delete Ed.content.sections[ id ];
			
			$( '#ed_menuitem_' + id ).remove();
			$( '#ed_section_' + id ).remove();
			
			EUi.clickDefaultSection();
		};

		if ( force ) {
			del();
		} else {
			$.alerts.confirm(
				EStr.DELETE_SECTION_MESSAGE,
				EStr.DELETE_SECTION_TITLE,
				function( res ) {
					if ( res ) {
						del();
					}
			} );
		}
	},

	deleteEmptySections: function() {
		var id, sec, empty,
		setNotEmpty = function() {
			if ( $( this ).val() ) {
				empty = false;
			}
		};

		for ( id in Ed.content.sections ) {
			if ( Ed.content.sections.hasOwnProperty( id ) ) {
				sec = Ed.content.sections[ id ];
				empty = true;
				$( '#ed_section_' + id ).find( 'textarea' ).each( setNotEmpty );
				
				if ( empty ) {
					EUi.deleteSection( id, 1 );
				}
			}
		}
	},

	prepareFormSubsections: function( id ) {
		var editlink, deletelink, i,
		section = Ed.content.sections[ id ],
		fset = $( '#ed_section_' + id );

		if ( id !== EConstants.SECTION_ID_INTRO ) {
			editlink = $( '<a>' )
				.text( EStr.EDIT_SECTION_TITLE )
				.on( 'click', function() {
					EUi.editSectionTitle( id, section );
					return false;
				} );
			
			deletelink = $( '<a>' )
				.text( EStr.DELETE_SECTION )
				.on( 'click', function() {
					EUi.deleteSection( id );
					return false;
				} );
			
			$( '<p>' )
				.addClass( 'top' )
				.append( editlink, deletelink )
				.appendTo( fset );
		}

		$.each( section.subsections, function() {
			if ( this.active ) {
				fset.append( EUi.getSubsectionObj( id, section, this ) );
			}
		} );
		
		EUi.prepareSectionAutomation( id );
		EAutomator.addTransliteration( id, section.code );
	},

	getSubsectionObj: function( langid, section, subsection ) {
		var name = langid + '_' + subsection.title.replace( / /g, '_' ),
		p = $( '<p>' ).attr( 'id', 'ed_subsection_' + name ),
		caption = ( langid === EConstants.SECTION_ID_INTRO )
			? EStr.INTRO
			: EConstants.SUBSECTION_TITLE[ subsection.title ],
		label = $( '<label>' )
			.attr( {
				'class': 'newform',
				'for':   'ed_' + name
			} )
			.text( caption ),
		textarea = $( '<textarea>' )
			.attr( {
				'id':    'ed_' + name,
				'class': 'newform keyboardable',
				'name':  'ed_' + name
			} )
			.val( subsection.content ),
		extra = $( '<div>' ).attr( {
				'id':    'ed_' + name + '_extra',
				'class': 'subsection_extra'
			} );

		if ( ESectionParser.obligatorySubsection( subsection, section ) ) {
			label
				.addClass( 'oblig_subsection' )
				.append( EStr.OBLIGATORY_SUBSECTION );
			
			textarea.addClass( 'oblig_subsection' );
		} else if ( ESectionParser.botSubsection( subsection, section ) ) {
			label
				.addClass( 'bot_subsection' )
				.append( EStr.BOT_SUBSECTION );
			
			textarea.addClass( 'bot_subsection' );
		}
		
		p.append( label, textarea, extra );

		return p;
	},

	rebindFormActions: function() {
		var allowCloseWindow, handler;
		
		if ( mw.user.options.get( 'useeditwarning' ) ) {
			// mediawiki.action.edit.editWarning.js
			allowCloseWindow = mw.confirmCloseWindow( {
				test:       function() {
					return EUi.formChanged;
				},
				message:    mw.msg( 'editwarning-warning' ),
				namespace: 'ed_editwarning'
			} );
		} else {
			allowCloseWindow = function() { return true; };
		}
	
		handler = function() {
			if ( EUi.usingNew ) {
				EUi.deleteEmptySections();
				EUi.tbox.val( EPrinter.recalculateCode() );
			}
			
			return true;
		};
	
		EUi.form
			.find( 'textarea' )
			.removeAttr( 'name' );
		
		$( '#editform' )
			.on( 'submit', handler )
			.on( 'submit', allowCloseWindow );
		
		// LivePreview, mediawiki.action.edit.preview.js
		if ( mw.user.options.get( 'uselivepreview' ) ) {
			$( '#wpPreview, #wpDiff' ).on( 'click', handler );
		}
	},

	resizeTextareas: function() {
		$( '#ed_content fieldset.active' )
			.find( 'textarea' )
			.reverse()
			.autoresize();
	},
	
	organizeSectionMenu: function() {
		var menu = EUi.menu_sect.find( '.menuitem' );
		var size = menu.length - 1; // przycisk '+ dodaj'
		
		menu.removeClass( 'lastofline' );
		
		if ( menu.first().offset().top !== menu.last().offset().top ) {
			menu.eq( Math.floor( size / 2 ) - 1 ).addClass( 'lastofline' );
		}
	},

	addIntroAdder: function() {
		var addIntro = $( '<li>' )
			.attr( {
				'id':    'ed_menuitem_newintro',
				'class': 'tip menuitem'
			} )
			.text( EStr.ADD_INTRO )
			.appendTo( EUi.menu_sect )
			.on( 'click', function() {
				var sec = {
					title:       '',
					content:     '',
					id:          EConstants.SECTION_ID_INTRO,
					initcontent: ''
				};
	
				Ed.content.sections[ EConstants.SECTION_ID_INTRO ] = sec;
				ESectionParser.parse( sec );
				EUi.addSection( EConstants.SECTION_ID_INTRO );
				EUi.prepareFormSubsections( EConstants.SECTION_ID_INTRO );
				
				$( '#ed_menuitem_newintro' ).hide();
				$( '#ed_menuitem_' + EConstants.SECTION_ID_INTRO ).trigger( 'click' );
				
				return false;
			} )
			.data( 'tip', EStr.ADD_INTRO_SECTION );
	},

	addExtraButtons: function( sectionName, subsectionName, mode, buttonContent, onclick, tooltip ) {
		var extra, button;

		extra = $( '#ed_' + sectionName + '_' + subsectionName + '_extra' );
		
		button = $( '<span>' )
			.html( buttonContent )
			.on( 'click', function() {
				EApi.handleRequest( mode, onclick );
				return false;
			} )
			.data( 'tip', tooltip )
			.attr( {
				'id':    'ed_' + sectionName + '_extra_' + EConstants.API_ID[ mode ],
				'class': 'tip tipdown'
			} );
		
		extra.append( button ).addClass( 'active' );
	},

	prepareSectionAutomation: function( id ) {
		if ( id === EConstants.SECTION_ID_INTRO ) {
			EUi.addExtraButtons(
				id,
				'',
				EConstants.MODE_IW,
				EStr.ADD_INTERWIKI,
				EAutomator.fillInterwiki,
				EStr.GET_INTERWIKI
			);
		} else {
			EUi.addExtraButtons(
				id,
				'',
				EConstants.MODE_PICTURE,
				EStr.ADD_PICTURE,
				EAutomator.getPicture,
				EStr.GET_PICTURE + EStr.WILL_BE_SHOWN
			);
		}
		
		EUi.addExtraButtons(
			id,
			'wymowa',
			EConstants.MODE_IPA,
			EStr.ADD_IPA,
			EAutomator.getIPA,
			EStr.GET_IPA + EStr.WILL_BE_SHOWN
		);
		
		EUi.addExtraButtons(
			id,
			'wymowa',
			EConstants.MODE_AUDIO,
			EStr.ADD_AUDIO,
			EAutomator.getAudio,
			EStr.GET_AUDIO + EStr.WILL_BE_SHOWN
		);
		
		EUi.addExtraButtons(
			id,
			'przykłady',
			EConstants.MODE_INTERNAL_EXAMPLE,
			EStr.ADD_INTERNAL_EXAMPLE,
			EAutomator.getInternalExample,
			EStr.GET_INTERNAL_EXAMPLE + EStr.WILL_BE_SHOWN
		);
	},

	showResult: function( ajaxResult, buttonIdPart ) {
		var closelink = $( '<a>' )
			.attr( {
				'id':    'closelink',
				'class': 'tip'
			} )
			.text( '×' );

		EUi.ajr
			.empty()
			.append( ajaxResult )
			.show()
			.data( 'buttonIdPart', buttonIdPart );
		
		EUi.relocateResult();

		closelink
			.prependTo( EUi.ajr )
			.data( 'tip', EStr.ESCAPE )
			.on( 'click', function() {
				EUi.hideResult();
				return false;
			} );
	},

	relocateResult: function() {
		var nPos = {},
		button = $( '#ed_' + EUtil.getActiveLangId() + '_extra_' + EUi.ajr.data( 'buttonIdPart' ) ),
		textbox = button.parent().prev();

		if ( button.length ) {
			nPos.top  = button.position().top;
			nPos.left = textbox.position().left + 60;
			
			EUi.ajr
				.css( nPos )
				.width( textbox.outerWidth() - 120 );
		}
	},

	hideResult: function() {
		EUi.ajr.hide();
	},

	addDefaultTexts: function( langid, code ) {
		var subs, defaultText,
		arr = ( code === 'pl' )
			? EConstants.SAMPLE_SUBSECTION_CONTENTS_POLISH
			: EConstants.SAMPLE_SUBSECTION_CONTENTS_FOREIGN;

		for ( subs in arr ) {
			if ( arr.hasOwnProperty( subs ) ) {
				defaultText = arr[ subs ];
				EUi.val( langid, subs, defaultText );
			}
		}
	},

	removeDefaultTexts: function( langid ) {
		var subs, defaultText,
		arr = ( langid === '0002' )
			? EConstants.SAMPLE_SUBSECTION_CONTENTS_POLISH
			: EConstants.SAMPLE_SUBSECTION_CONTENTS_FOREIGN;

		for ( subs in arr ) {
			if ( arr.hasOwnProperty( subs ) ) {
				defaultText = arr[ subs ];
				
				if ( EUi.isDefaultText( langid, subs, 0 ) ) {
					EUi.val( langid, subs, '' );
				}
			}
		}
	},

	isDefaultText: function( langid, subsection, extendedMode ) {
		var arr = ( langid === '0002' )
			? EConstants.SAMPLE_SUBSECTION_CONTENTS_POLISH
			: EConstants.SAMPLE_SUBSECTION_CONTENTS_FOREIGN,
		
		val = EUi.val( langid, subsection );

		if ( extendedMode && val.search( /^: \(\d+\.\d+\)$/ ) !== -1 ) {
			EUi.val( langid, subsection, val + ' ' );
			return true;
		} else {
			return ( val === $.trim( arr[ subsection ] ) );
		}
	},

	val: function( langid, subsectionTitle, newValue ) {
		if ( newValue === undefined ) {
			return $.trim( $( '#ed_' + langid + '_' + subsectionTitle.replace( / /g, '_' ) ).val() );
		} else {
			$( '#ed_' + langid + '_' + subsectionTitle ).val( newValue );
			return 0;
		}
	}
};