jQuery collection.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.getSaveDialogHtml = function ( callback ) {
var viewPage = this,
$wrap = $( '
' ).html( this.constructor.saveDialogTemplate );
// Based on EditPage::getCheckboxes and EditPage::initialiseForm
// TODO: Remove saveDialog-diffButton if this is a page creation.
mw.user.getRights( function ( rights ) {
// MediaWiki only allows usage of minor flag when editing an existing page
// and the user has the right to use the feature.
// If either is not the case, remove it from the form.
if ( !viewPage.pageExists || ve.indexOf( 'minoredit', rights ) === -1 ) {
$wrap
.find( '.ve-init-mw-viewPageTarget-saveDialog-minorEdit-label, #ve-init-mw-viewPageTarget-saveDialog-minorEdit' )
.remove();
}
if ( mw.user.isAnon() ) {
$wrap
.find( '.ve-init-mw-viewPageTarget-saveDialog-watchList-label, #ve-init-mw-viewPageTarget-saveDialog-watchList' )
.remove();
} else if (
mw.user.options.get( 'watchdefault' ) ||
( mw.user.options.get( 'watchcreations' ) && !viewPage.pageExists ) ||
mw.config.get( 'wgVisualEditor' ).isPageWatched
) {
$wrap
.find( '#ve-init-mw-viewPageTarget-saveDialog-watchList' )
.prop( 'checked', true );
}
callback( $wrap );
} );
};
/**
* Adds content and event bindings to the save dialog.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.setupSaveDialog = function () {
var viewPage = this;
viewPage.getSaveDialogHtml( function ( $wrap ) {
viewPage.$saveDialog
// Must not use replaceWith because that can't be used on fragement roots,
// plus, we want to preserve the reference and class names of the wrapper.
.empty().append( $wrap.contents() )
.find( '.ve-init-mw-viewPageTarget-saveDialog-closeButton' )
.click( ve.bind( viewPage.onSaveDialogCloseButtonClick, viewPage ) )
.end()
.find( '.ve-init-mw-viewPageTarget-saveDialog-prevButton' )
.click( ve.bind( viewPage.onSaveDialogPrevButtonClick, viewPage ) )
.end()
.find( '#ve-init-mw-viewPageTarget-saveDialog-editSummary' )
.attr( {
'placeholder': ve.msg( 'visualeditor-editsummary' )
} )
.placeholder()
.byteLimit( viewPage.editSummaryByteLimit )
.on( {
'focus': function () {
$(this).parent().addClass(
've-init-mw-viewPageTarget-saveDialog-summary-focused'
);
},
'blur': function () {
$(this).parent().removeClass(
've-init-mw-viewPageTarget-saveDialog-summary-focused'
);
},
'keydown mouseup cut paste change focus blur': function () {
var $textarea = $(this),
$editSummaryCount = $textarea
.closest( '#ve-init-mw-viewPageTarget-saveDialog-slide-main' )
.find( '.ve-init-mw-viewPageTarget-saveDialog-editSummaryCount' );
// TODO: This looks a bit weird, there is no unit in the UI, just numbers
// Users likely assume characters but then it seems to count down quicker
// than expected. Facing users with the word "byte" is bad? (bug 40035)
setTimeout( function () {
$editSummaryCount.text(
viewPage.editSummaryByteLimit - $.byteLength( $textarea.val() )
);
}, 0 );
}
} )
.end()
.find( '.ve-init-mw-viewPageTarget-saveDialog-editSummaryCount' )
.text( viewPage.editSummaryByteLimit )
.end()
.find( '.ve-init-mw-viewPageTarget-saveDialog-minorEdit-label' )
.html( ve.init.platform.getParsedMessage( 'minoredit' ) )
.find( 'a' )
.attr( 'target', '_blank' )
.end()
.end()
.find( '.ve-init-mw-viewPageTarget-saveDialog-watchList-label' )
.html( ve.init.platform.getParsedMessage( 'watchthis' ) )
.end()
.find( '.ve-init-mw-viewPageTarget-saveDialog-saveButton' )
.on( {
'mousedown': function ( e ) {
$(this).addClass( 've-init-mw-viewPageTarget-button-down' );
e.preventDefault();
},
'mouseleave mouseup': function ( e ) {
$(this).removeClass( 've-init-mw-viewPageTarget-button-down' );
e.preventDefault();
},
'click': ve.bind( viewPage.onSaveDialogSaveButtonClick, viewPage )
} )
.end()
.find( '.ve-init-mw-viewPageTarget-saveDialog-saveButton > span' )
.text( ve.msg( viewPage.restoring ? 'visualeditor-restore-page' : 'savearticle' ) )
.end()
.find( '.ve-init-mw-viewPageTarget-saveDialog-diffButton > span' )
.text( ve.msg( 'showdiff' ) )
.end()
.find( '.ve-init-mw-viewPageTarget-saveDialog-diffButton' )
.on( {
'mousedown': function ( e ) {
$(this).addClass( 've-init-mw-viewPageTarget-button-down' );
e.preventDefault();
},
'mouseleave mouseup': function ( e ) {
$(this).removeClass( 've-init-mw-viewPageTarget-button-down' );
e.preventDefault();
},
'click': ve.bind( viewPage.onSaveDialogDiffButtonClick, viewPage )
} )
.end()
.find( '.ve-init-mw-viewPageTarget-saveDialog-license' )
.html( ve.init.platform.getParsedMessage( 'copyrightwarning' ) );
viewPage.$saveDialogSaveButton = viewPage.$saveDialog
.find( '.ve-init-mw-viewPageTarget-saveDialog-saveButton' );
viewPage.$saveDialogDiffButton = viewPage.$saveDialog
.find( '.ve-init-mw-viewPageTarget-saveDialog-diffButton' );
viewPage.$saveDialogLoadingIcon = viewPage.$saveDialog
.find( '.ve-init-mw-viewPageTarget-saveDialog-working' );
} );
// Hook onto the 'watch' event on by mediawiki.page.watch.ajax.js
// Triggered when mw.page.watch.updateWatchLink(link, action) is called
$( '#ca-watch, #ca-unwatch' )
.on(
'watchpage.mw',
function ( e, action ) {
viewPage.$saveDialog
.find( '#ve-init-mw-viewPageTarget-saveDialog-watchList' )
.prop( 'checked', ( action === 'watch' ) );
}
);
};
/**
* Adds the save dialog to the user interface.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.attachSaveDialog = function () {
this.$toolbarWrapper.find( '.ve-ui-toolbar' ).append( this.$saveDialog );
};
/**
* Removes the save dialog from the user interface.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.detachSaveDialog = function () {
this.$saveDialog.detach();
};
/**
* Remembers the window's scroll position.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.saveScrollPosition = function () {
this.scrollTop = $( window ).scrollTop();
};
/**
* Restores the window's scroll position.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.restoreScrollPosition = function () {
if ( this.scrollTop ) {
$( window ).scrollTop( this.scrollTop );
this.scrollTop = null;
}
};
/**
* Shows the loading spinner.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.showSpinner = function () {
$( '#firstHeading' ).prepend( this.$spinner );
};
/**
* Hides the loading spinner.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.hideSpinner = function () {
this.$spinner.detach();
};
/**
* Shows the page content.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.showPageContent = function () {
$( '#bodyContent > .ve-init-mw-viewPageTarget-content' )
.removeClass( 've-init-mw-viewPageTarget-content' )
.show()
.fadeTo( 0, 1 );
};
/**
* Mutes the page content.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.mutePageContent = function () {
$( '#bodyContent > :visible:not(#siteSub, #contentSub)' )
.addClass( 've-init-mw-viewPageTarget-content' )
.fadeTo( 'fast', 0.6 );
};
/**
* Hides the page content.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.hidePageContent = function () {
$( '#bodyContent > :visible:not(#siteSub, #contentSub)' )
.addClass( 've-init-mw-viewPageTarget-content' )
.hide();
};
/**
* Shows the table of contents in the view mode.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.showTableOfContents = function () {
var $toc = $( '#toc' ),
$wrap = $toc.parent();
if ( $wrap.data( 've.hideTableOfContents' ) ) {
$wrap.slideDown(function () {
$toc.unwrap();
});
}
};
/**
* Hides the table of contents in the view mode.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.hideTableOfContents = function () {
$( '#toc' )
.wrap( '
' )
.parent()
.data( 've.hideTableOfContents', true )
.slideUp();
};
/**
* Shows the save dialog.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.showSaveDialog = function () {
var viewPage = this;
viewPage.$toolbarEditNotices.fadeOut( 'fast' );
viewPage.unlockSaveDialogSaveButton();
viewPage.$saveDialogLoadingIcon.hide();
viewPage.$saveDialog
// Reset width
.css( 'width', '' )
// Reset title
.find( '.ve-init-mw-viewPageTarget-saveDialog-title' )
.text( ve.msg( 'visualeditor-save-title' ) )
.end()
// Reset slide to main
.find( '#ve-init-mw-viewPageTarget-saveDialog-slide-main' )
.show()
.end()
.find( '.ve-init-mw-viewPageTarget-saveDialog-slide:not(#ve-init-mw-viewPageTarget-saveDialog-slide-main)' )
.hide()
.end()
// Hide back button
.find ( '.ve-init-mw-viewPageTarget-saveDialog-prevButton' )
.hide()
.end()
.fadeIn( 'fast', function () {
// Initial size
viewPage.onResizeSaveDialog();
});
$( document ).on( 'keydown.ve-savedialog', function ( e ) {
// Escape
if ( e.which === 27 ) {
viewPage.onSaveDialogCloseButtonClick();
}
});
$( window ).on( 'resize.ve-savedialog', ve.bind( viewPage.onResizeSaveDialog, viewPage ) );
// Change focus
setTimeout( function() {
viewPage.$saveDialog.find( 'textarea:first' ).focus();
}, 0 );
};
/**
* Update window-size related aspects of the save dialog
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.onResizeSaveDialog = function () {
var $d = this.$saveDialog, $w = $( window );
// Available space for css-height is window height,
// without the space between the dialog and the window top,
// without the space above/below between css-height and outerHeight.
$d.css( 'max-height',
$w.height() -
( $d.offset().top - $w.scrollTop() ) -
( $d.outerHeight( true ) - $d.height() ) -
20 // shadow
);
};
/**
* Hides the save dialog
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.hideSaveDialog = function () {
this.$saveDialog.fadeOut( 'fast' );
this.$document.focus();
$( document ).off( 'keydown.ve-savedialog' );
$( window ).off( 'resize', this.onResizeSaveDialog );
};
/**
* Resets the fields of the save dialog
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.resetSaveDialog = function () {
this.$saveDialog
.find( '#ve-init-mw-viewPageTarget-saveDialog-editSummary' )
.val( '' )
.end()
.find( '#ve-init-mw-viewPageTarget-saveDialog-minorEdit' )
.prop( 'checked', false );
};
/**
* Enables the toolbar save button.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.enableToolbarSaveButton = function () {
this.$toolbarSaveButton
.removeClass( 've-init-mw-viewPageTarget-button-disabled' );
};
/**
* Disables the toolbar save button.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.disableToolbarSaveButton = function () {
this.$toolbarSaveButton
.addClass( 've-init-mw-viewPageTarget-button-disabled' );
};
/**
* Enables the save dialog save button.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.unlockSaveDialogSaveButton = function () {
this.$saveDialogSaveButton
.removeClass( 've-init-mw-viewPageTarget-button-disabled' );
};
/**
* Disables the save dialog save button.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.lockSaveDialogSaveButton = function () {
this.$saveDialogSaveButton
.addClass( 've-init-mw-viewPageTarget-button-disabled' );
};
/**
* Enables the save dialog diff button.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.unlockSaveDialogDiffButton = function () {
this.$saveDialogDiffButton
.removeClass( 've-init-mw-viewPageTarget-button-disabled' );
};
/**
* Disables the save dialog diff button.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.lockSaveDialogDiffButton = function () {
this.$saveDialogDiffButton
.addClass( 've-init-mw-viewPageTarget-button-disabled' );
};
/**
* Shows the toolbar.
*
* This also transplants the toolbar to a new location.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.attachToolbar = function () {
this.$toolbarWrapper = $( '.ve-ui-toolbar-wrapper' )
.insertBefore( $( '#firstHeading' ) )
.find( '.ve-ui-toolbar' )
.slideDown( 'fast', ve.bind( function() {
this.surface.getContext().update();
}, this ) )
.end();
};
/**
* Hides the toolbar.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.detachToolbar = function () {
$( '.ve-ui-toolbar' ).slideUp( 'fast', function () {
$(this).parent().remove();
} );
};
/**
* Enables the toolbar save button.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.transformPageTitle = function () {
$( '#firstHeading' ).addClass( 've-init-mw-viewPageTarget-pageTitle' );
};
/**
* Enables the toolbar save button.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.mutePageTitle = function () {
$( '#firstHeading, #siteSub:visible, #contentSub:visible' ).fadeTo( 'fast', 0.6 );
};
/**
* Disables the toolbar save button.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.restorePageTitle = function () {
$( '#firstHeading, #siteSub:visible, #contentSub:visible' ).fadeTo( 'fast', 1 );
setTimeout( function () {
$( '#firstHeading' ).removeClass( 've-init-mw-viewPageTarget-pageTitle' );
}, 1000 );
};
/**
* Modifies page tabs to show that editing is taking place.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.transformSkinTabs = function () {
$( $( '#p-views' ).length ? '#p-views' : '#p-cactions' )
.find( 'li.selected' ).removeClass( 'selected' );
$( this.tabLayout === 'add' ? '#ca-ve-edit' : '#ca-edit' )
.addClass( 'selected' );
};
/**
* Modifies page tabs to show that viewing is taking place.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.restoreSkinTabs = function () {
$( $( '#p-views' ).length ? '#p-views' : '#p-cactions' )
.find( 'li.selected' ).removeClass( 'selected' );
$( '#ca-view' ).addClass( 'selected' );
};
/**
* Hides site notice on page if present.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.hideSiteNotice = function () {
$( '#siteNotice:visible' )
.addClass( 've-hide' )
.slideUp( 'fast' );
};
/**
* Show site notice on page if present.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.restoreSiteNotice = function () {
$(' #siteNotice.ve-hide' )
.slideDown( 'fast' );
};
/**
* Replaces the page content with new HTML.
*
* @method
* @param {HTMLElement} html Rendered HTML from server
*/
ve.init.mw.ViewPageTarget.prototype.replacePageContent = function ( html ) {
$( '#mw-content-text' ).html( html );
};
/**
* Gets the numeric index of a section in the page.
*
* @method
* @param {HTMLElement} heading Heading element of section
*/
ve.init.mw.ViewPageTarget.prototype.getEditSection = function ( heading ) {
var $page = $( '#mw-content-text' ),
section = 0;
$page.find( 'h1, h2, h3, h4, h5, h6' ).not( '#toc h2' ).each( function () {
section++;
if ( this === heading ) {
return false;
}
} );
return section;
};
/**
* Gets the numeric index of a section in the page.
*
* @method
* @param {HTMLElement} heading Heading element of section
*/
ve.init.mw.ViewPageTarget.prototype.saveEditSection = function ( heading ) {
this.section = this.getEditSection( heading );
};
/**
* Moves the cursor in the editor to a given section.
*
* @method
* @param {Number} section Section to move cursor to
*/
ve.init.mw.ViewPageTarget.prototype.restoreEditSection = function () {
if ( this.section !== null ) {
var offset,
surfaceView = this.surface.getView(),
surfaceModel = surfaceView.getModel();
this.$document.find( 'h1, h2, h3, h4, h5, h6' ).eq( this.section - 1 ).each( function () {
var headingNode = $(this).data( 'node' );
if ( headingNode ) {
offset = surfaceModel.getDocument().getNearestContentOffset(
headingNode.getModel().getOffset()
);
surfaceModel.change( null, new ve.Range( offset, offset ) );
surfaceView.showSelection( surfaceModel.getSelection() );
}
} );
this.section = null;
}
};
/**
* Adds onbeforunload handler.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.setupBeforeUnloadHandler = function () {
// Remember any already set on before unload handler
this.onBeforeUnloadFallback = window.onbeforeunload;
// Attach before unload handler
window.onbeforeunload = this.proxiedOnBeforeUnload = ve.bind( this.onBeforeUnload, this );
// Attach page show handlers
if ( window.addEventListener ) {
window.addEventListener( 'pageshow', ve.bind( this.onPageShow, this ), false );
} else if ( window.attachEvent ) {
window.attachEvent( 'pageshow', ve.bind( this.onPageShow, this ) );
}
};
/**
* Removes onbeforunload handler.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.tearDownBeforeUnloadHandler = function () {
// Restore whatever previous onbeforeload hook existed
window.onbeforeunload = this.onBeforeUnloadFallback;
};
/**
* Responds to page show event.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.onPageShow = function () {
// Re-add onbeforeunload handler
window.onbeforeunload = this.proxiedOnBeforeUnload;
};
/**
* Responds to before unload event.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.onBeforeUnload = function () {
var fallbackResult,
message,
proxiedOnBeforeUnload = this.proxiedOnBeforeUnload;
// Check if someone already set on onbeforeunload hook
if ( this.onBeforeUnloadFallback ) {
// Get the result of their onbeforeunload hook
fallbackResult = this.onBeforeUnloadFallback();
}
// Check if their onbeforeunload hook returned something
if ( fallbackResult !== undefined ) {
// Exit here, returning their message
message = fallbackResult;
} else {
// Override if submitting
if ( this.submitting ) {
return null;
}
// Check if there's been an edit
if ( this.surface && this.surface.getModel().getHistory().length ) {
// Return our message
message = ve.msg( 'visualeditor-viewpage-savewarning' );
}
}
// Unset the onbeforeunload handler so we don't break page caching in Firefox
window.onbeforeunload = null;
if ( message !== undefined ) {
// ...but if the user chooses not to leave the page, we need to rebind it
setTimeout( function () {
window.onbeforeunload = proxiedOnBeforeUnload;
}, 1 );
return message;
}
};
/* Initialization */
ve.init.mw.targets.push( new ve.init.mw.ViewPageTarget() );