2012-06-11 06:54:41 +00:00
|
|
|
/**
|
|
|
|
* Edit page target.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @extends {ve.init.Target}
|
|
|
|
* @param {String} title Page title of target
|
|
|
|
*/
|
2012-06-11 23:06:35 +00:00
|
|
|
ve.init.ViewPageTarget = function() {
|
2012-06-11 06:54:41 +00:00
|
|
|
// Inheritance
|
|
|
|
ve.init.Target.call( this, mw.config.get( 'wgPageName' ) );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.$content = $( '#content' );
|
|
|
|
this.$view = this.$content.find( '#mw-content-text, #bodyContent' );
|
|
|
|
this.$heading = $( '#firstHeading' );
|
|
|
|
this.$surface = $( '<div class="ve-surface"></div>' );
|
|
|
|
this.$toolbar = null;
|
|
|
|
this.surface = null;
|
|
|
|
this.active = false;
|
2012-06-12 17:32:10 +00:00
|
|
|
this.edited = false;
|
2012-06-14 01:26:21 +00:00
|
|
|
this.activating = false;
|
2012-06-12 17:32:10 +00:00
|
|
|
this.proxiedOnSurfaceModelTransact = ve.proxy( this.onSurfaceModelTransact, this );
|
2012-06-11 06:54:41 +00:00
|
|
|
this.surfaceOptions = {
|
|
|
|
'toolbars': {
|
|
|
|
'top': {
|
|
|
|
// If mobile device, float false
|
|
|
|
'float': !this.isMobileDevice,
|
|
|
|
// Toolbar modes
|
|
|
|
'modes': ['wikitext']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Initialization
|
|
|
|
if ( mw.config.get('wgCanonicalNamespace') === 'VisualEditor' ) {
|
|
|
|
// Clicking the edit tab is the only way any other code gets run, and this sets that up
|
|
|
|
this.setupTabs();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Static Members */
|
|
|
|
|
|
|
|
/*jshint multistr: true*/
|
2012-06-11 23:06:35 +00:00
|
|
|
ve.init.ViewPageTarget.saveDialogTemplate = '\
|
|
|
|
<div class="es-inspector ve-init-viewPageTarget-saveDialog">\
|
|
|
|
<div class="es-inspector-title ve-init-viewPageTarget-saveDialog-title"></div>\
|
|
|
|
<div class="es-inspector-button ve-init-viewPageTarget-saveDialog-closeButton"></div>\
|
|
|
|
<div class="ve-init-viewPageTarget-saveDialog-body">\
|
|
|
|
<div class="ve-init-viewPageTarget-saveDialog-editSummary-label"></div>\
|
|
|
|
<input name="editSummary" id="ve-init-viewPageTarget-saveDialog-editSummary" type="text">\
|
2012-06-11 06:54:41 +00:00
|
|
|
<div class="clear:both"></div>\
|
2012-06-11 23:06:35 +00:00
|
|
|
<div class="ve-init-viewPageTarget-saveDialog-options">\
|
2012-06-11 06:54:41 +00:00
|
|
|
<input type="checkbox" name="minorEdit" \
|
2012-06-11 23:06:35 +00:00
|
|
|
id="ve-init-viewPageTarget-saveDialog-minorEdit">\
|
|
|
|
<label class="ve-init-viewPageTarget-saveDialog-minorEdit-label" \
|
|
|
|
for="ve-init-viewPageTarget-saveDialog-minorEdit">This is a minor edit</label>\
|
2012-06-11 06:54:41 +00:00
|
|
|
<div style="clear:both"></div>\
|
|
|
|
<input type="checkbox" name="watchList" \
|
2012-06-11 23:06:35 +00:00
|
|
|
id="ve-init-viewPageTarget-saveDialog-watchList">\
|
|
|
|
<label class="ve-init-viewPageTarget-saveDialog-watchList-label" \
|
|
|
|
for="ve-init-viewPageTarget-saveDialog-watchList">Watch this page</label>\
|
2012-06-11 06:54:41 +00:00
|
|
|
</div>\
|
2012-06-11 23:06:35 +00:00
|
|
|
<div class="ve-init-viewPageTarget-button ve-init-viewPageTarget-saveDialog-saveButton">\
|
|
|
|
<span class="ve-init-viewPageTarget-saveDialog-saveButton-label"></span>\
|
|
|
|
<div class="ve-init-viewPageTarget-saveDialog-saveButton-icon"></div>\
|
2012-06-11 06:54:41 +00:00
|
|
|
</div>\
|
|
|
|
<div style="clear:both"></div>\
|
|
|
|
</div>\
|
2012-06-11 23:06:35 +00:00
|
|
|
<div class="ve-init-viewPageTarget-saveDialog-foot">\
|
|
|
|
<p class="ve-init-viewPageTarget-saveDialog-license"></p>\
|
2012-06-11 06:54:41 +00:00
|
|
|
</div>\
|
|
|
|
</div>';
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
2012-06-11 23:06:35 +00:00
|
|
|
ve.init.ViewPageTarget.prototype.onEditTabClick = function( e ) {
|
2012-06-11 06:54:41 +00:00
|
|
|
// Ignore multiple clicks while editor is active
|
2012-06-14 01:26:21 +00:00
|
|
|
if ( !this.active && !this.activating ) {
|
|
|
|
this.activating = true;
|
2012-06-11 06:54:41 +00:00
|
|
|
// UI updates
|
|
|
|
this.setSelectedTab( 'ca-edit' );
|
|
|
|
this.showSpinner();
|
|
|
|
// Asynchronous initialization - load ve modules at the same time as the content
|
|
|
|
this.load( ve.proxy( this.onLoad, this ) );
|
|
|
|
}
|
|
|
|
// Prevent the edit tab's normal behavior
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2012-06-12 01:21:31 +00:00
|
|
|
/**
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.init.ViewPageTarget.prototype.onViewTabClick = function( e ) {
|
|
|
|
// Don't do anything special unless we are in editing mode
|
|
|
|
if ( this.active ) {
|
2012-06-12 17:32:10 +00:00
|
|
|
if (
|
|
|
|
!this.surface.getModel().getHistory().length ||
|
|
|
|
confirm( 'Are you sure you want to go back to view mode without saving first?' )
|
|
|
|
) {
|
|
|
|
this.tearDownSurface();
|
|
|
|
}
|
2012-06-12 01:21:31 +00:00
|
|
|
// Prevent the edit tab's normal behavior
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-11 06:54:41 +00:00
|
|
|
/**
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
2012-06-11 23:06:35 +00:00
|
|
|
ve.init.ViewPageTarget.prototype.onSaveDialogSaveButtonClick = function( e ) {
|
2012-06-11 06:54:41 +00:00
|
|
|
this.showSpinner();
|
|
|
|
// Save
|
|
|
|
this.save(
|
|
|
|
ve.dm.converter.getDomFromData( this.surface.getDocumentModel().getData() ),
|
|
|
|
{
|
2012-06-11 23:06:35 +00:00
|
|
|
'summary': $( '#ve-init-viewPageTarget-saveDialog-editSummary' ).val(),
|
|
|
|
'minor': $( '#ve-init-viewPageTarget-saveDialog-minorEdit' ).prop( 'checked' ),
|
|
|
|
'watch': $( '#ve-init-viewPageTarget-saveDialog-watchList' ).prop( 'checked' )
|
2012-06-11 06:54:41 +00:00
|
|
|
},
|
|
|
|
ve.proxy( this.onSave, this )
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2012-06-12 17:32:10 +00:00
|
|
|
/**
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.init.ViewPageTarget.prototype.onSurfaceModelTransact = function() {
|
|
|
|
if ( !this.edited ) {
|
|
|
|
this.edited = true;
|
|
|
|
this.$toolbar.find( '.ve-init-viewPageTarget-saveButton ' )
|
|
|
|
.removeClass( 've-init-viewPageTarget-button-disabled' );
|
|
|
|
this.surface.getModel().removeListener( 'transact', this.proxiedOnSurfaceModelTransact );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.init.ViewPageTarget.prototype.onSaveButtonClick = function( e ) {
|
|
|
|
if ( this.edited ) {
|
|
|
|
this.$dialog.fadeIn( 'fast' );
|
|
|
|
this.$dialog.find( 'input:first' ).focus();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.init.ViewPageTarget.prototype.onSaveDialogCloseButtonClick = function( e ) {
|
|
|
|
this.$dialog.fadeOut( 'fast' );
|
|
|
|
this.$surface.find( '.ve-ce-documentNode' ).focus();
|
|
|
|
};
|
|
|
|
|
2012-06-11 06:54:41 +00:00
|
|
|
/**
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
2012-06-11 23:06:35 +00:00
|
|
|
ve.init.ViewPageTarget.prototype.onLoad = function( error, dom ) {
|
2012-06-14 01:26:21 +00:00
|
|
|
this.activating = false;
|
2012-06-11 06:54:41 +00:00
|
|
|
if ( error ) {
|
|
|
|
// TODO: Error handling in the UI
|
|
|
|
} else {
|
2012-06-12 17:32:10 +00:00
|
|
|
this.edited = false;
|
2012-06-11 06:54:41 +00:00
|
|
|
this.setUpSurface( dom );
|
2012-06-11 20:24:20 +00:00
|
|
|
this.$surface.find( '.ve-ce-documentNode' ).focus();
|
2012-06-11 06:54:41 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
2012-06-11 23:06:35 +00:00
|
|
|
ve.init.ViewPageTarget.prototype.onSave = function( error, content ) {
|
2012-06-11 06:54:41 +00:00
|
|
|
if ( error ) {
|
|
|
|
// TODO: Handle error in UI
|
|
|
|
} else {
|
|
|
|
// Hide the save dialog
|
2012-06-11 19:16:17 +00:00
|
|
|
this.$dialog.fadeOut();
|
2012-06-11 06:54:41 +00:00
|
|
|
// Refresh page with changed content
|
|
|
|
this.$content.find( '#mw-content-text' ).html( content );
|
|
|
|
// Restore the page to how it used to be
|
|
|
|
this.tearDownSurface();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
2012-06-11 23:06:35 +00:00
|
|
|
ve.init.ViewPageTarget.prototype.setUpSurface = function( dom ) {
|
2012-06-11 06:54:41 +00:00
|
|
|
// Initialize surface
|
|
|
|
this.$surface.appendTo( this.$content );
|
|
|
|
this.surface = new ve.Surface( this.$surface, dom, this.surfaceOptions );
|
2012-06-12 17:32:10 +00:00
|
|
|
this.surface.getModel().on( 'transact', this.proxiedOnSurfaceModelTransact );
|
2012-06-11 06:54:41 +00:00
|
|
|
// Transplant the toolbar
|
2012-06-12 01:21:31 +00:00
|
|
|
this.$toolbar = this.$surface.find( '.es-toolbar-wrapper' );
|
2012-06-14 01:26:21 +00:00
|
|
|
this.$toolbar.find( '.es-toolbar' ).slideDown( 'fast' );
|
2012-06-12 01:21:31 +00:00
|
|
|
this.$heading
|
|
|
|
.before( this.$toolbar )
|
|
|
|
.addClass( 've-init-viewPageTarget-pageTitle' )
|
2012-06-14 01:26:21 +00:00
|
|
|
.fadeTo( 'fast', 0.5 );
|
2012-06-11 06:54:41 +00:00
|
|
|
// Update UI
|
|
|
|
this.$view.hide();
|
2012-06-12 01:21:31 +00:00
|
|
|
this.$spinner.remove();
|
2012-06-11 23:06:35 +00:00
|
|
|
this.$dialog = $( ve.init.ViewPageTarget.saveDialogTemplate );
|
2012-06-11 06:54:41 +00:00
|
|
|
// Add save and close buttons
|
|
|
|
this.$toolbar
|
|
|
|
.find( '.es-modes' )
|
|
|
|
.append(
|
|
|
|
$( '<div></div>' )
|
2012-06-12 17:32:10 +00:00
|
|
|
.addClass(
|
|
|
|
've-init-viewPageTarget-button ' +
|
|
|
|
've-init-viewPageTarget-button-disabled ' +
|
|
|
|
've-init-viewPageTarget-saveButton'
|
|
|
|
)
|
2012-06-11 06:54:41 +00:00
|
|
|
.append(
|
2012-06-12 01:21:31 +00:00
|
|
|
$( '<span class="ve-init-viewPageTarget-saveButton-label"></span>' )
|
2012-06-11 06:54:41 +00:00
|
|
|
.text( mw.msg( 'savearticle' ) )
|
|
|
|
)
|
2012-06-12 01:21:31 +00:00
|
|
|
.append( $( '<span class="ve-init-viewPageTarget-saveButton-icon"></span>' ) )
|
2012-06-12 17:32:10 +00:00
|
|
|
.mousedown( function( e ) {
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
} )
|
|
|
|
.click( ve.proxy( this.onSaveButtonClick, this ) )
|
2012-06-11 06:54:41 +00:00
|
|
|
);
|
|
|
|
// Set up save dialog
|
|
|
|
this.$dialog
|
2012-06-11 23:06:35 +00:00
|
|
|
.find( '.ve-init-viewPageTarget-saveDialog-title' )
|
2012-06-11 06:54:41 +00:00
|
|
|
.text( mw.msg( 'tooltip-save' ) )
|
|
|
|
.end()
|
2012-06-11 23:06:35 +00:00
|
|
|
.find( '.ve-init-viewPageTarget-saveDialog-closeButton' )
|
2012-06-12 17:32:10 +00:00
|
|
|
.click( ve.proxy( this.onSaveDialogCloseButtonClick, this ) )
|
2012-06-11 06:54:41 +00:00
|
|
|
.end()
|
2012-06-11 23:06:35 +00:00
|
|
|
.find( '.ve-init-viewPageTarget-saveDialog-editSummary-label' )
|
2012-06-11 06:54:41 +00:00
|
|
|
.text( mw.msg( 'summary' ) )
|
|
|
|
.end()
|
2012-06-11 23:06:35 +00:00
|
|
|
.find( '.ve-init-viewPageTarget-saveDialog-minorEdit-label' )
|
2012-06-11 06:54:41 +00:00
|
|
|
.text( mw.msg( 'minoredit' ) )
|
|
|
|
.end()
|
2012-06-11 23:06:35 +00:00
|
|
|
.find( '.ve-init-viewPageTarget-saveDialog-watchList' )
|
2012-06-11 06:54:41 +00:00
|
|
|
.prop( 'checked', ve.config.isPageWatched )
|
|
|
|
.end()
|
2012-06-11 23:06:35 +00:00
|
|
|
.find( '.ve-init-viewPageTarget-saveDialog-watchList-label' )
|
2012-06-11 06:54:41 +00:00
|
|
|
.text( mw.msg( 'watchthis' ) )
|
|
|
|
.end()
|
2012-06-11 23:06:35 +00:00
|
|
|
.find( '.ve-init-viewPageTarget-saveDialog-saveButton' )
|
2012-06-11 06:54:41 +00:00
|
|
|
.click( ve.proxy( this.onSaveDialogSaveButtonClick, this ) )
|
|
|
|
.end()
|
2012-06-11 23:06:35 +00:00
|
|
|
.find( '.ve-init-viewPageTarget-saveDialog-saveButton-label' )
|
2012-06-11 06:54:41 +00:00
|
|
|
.text( mw.msg( 'savearticle' ) )
|
|
|
|
.end()
|
2012-06-11 23:06:35 +00:00
|
|
|
.find( '.ve-init-viewPageTarget-saveDialog-license' )
|
2012-06-11 06:54:41 +00:00
|
|
|
.html(
|
|
|
|
"By editing this page, you agree to irrevocably release your \
|
|
|
|
contributions under the CC-By-SA 3.0 License. If you don't want your \
|
|
|
|
writing to be editied mercilessly and redistrubuted at will, then \
|
|
|
|
don't submit it here.<br /><br />You are also confirming that you \
|
|
|
|
wrote this yourself, or copied it from a public domain or similar free \
|
|
|
|
resource. See Project:Copyright for full details of the licenses \
|
|
|
|
used on this site.\
|
|
|
|
<b>DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!</b>"
|
|
|
|
)
|
|
|
|
.end()
|
2012-06-11 23:06:35 +00:00
|
|
|
.insertAfter( this.$toolbar.find( '.ve-init-viewPageTarget-saveButton' ) );
|
2012-06-12 01:21:31 +00:00
|
|
|
this.active = true;
|
2012-06-11 06:54:41 +00:00
|
|
|
};
|
|
|
|
|
2012-06-11 23:06:35 +00:00
|
|
|
ve.init.ViewPageTarget.prototype.tearDownSurface = function( content ) {
|
2012-06-11 06:54:41 +00:00
|
|
|
// Reset tabs
|
|
|
|
this.setSelectedTab( 'ca-view' );
|
|
|
|
// Update UI
|
2012-06-12 01:21:31 +00:00
|
|
|
this.$surface.empty().detach();
|
2012-06-14 01:26:21 +00:00
|
|
|
this.$toolbar.find( '.es-toolbar' ).slideUp( 'fast', function() {
|
|
|
|
$(this).parent().remove();
|
|
|
|
} );
|
2012-06-11 06:54:41 +00:00
|
|
|
this.$spinner.remove();
|
2012-06-13 22:00:13 +00:00
|
|
|
$( '.es-contextView' ).remove();
|
2012-06-12 01:21:31 +00:00
|
|
|
this.$view.show().fadeTo( 'fast', 1 );
|
2012-06-14 01:26:21 +00:00
|
|
|
this.$heading.fadeTo( 'fast', 1 );
|
2012-06-12 01:21:31 +00:00
|
|
|
setTimeout( ve.proxy( function() {
|
|
|
|
$(this).removeClass( 've-init-viewPageTarget-pageTitle' );
|
|
|
|
}, this.$heading ), 1000 );
|
2012-06-11 06:54:41 +00:00
|
|
|
// Destroy editor
|
|
|
|
this.surface = null;
|
2012-06-12 01:21:31 +00:00
|
|
|
this.active = false;
|
2012-06-11 06:54:41 +00:00
|
|
|
};
|
|
|
|
|
2012-06-11 23:06:35 +00:00
|
|
|
ve.init.ViewPageTarget.prototype.setupTabs = function(){
|
2012-06-11 06:54:41 +00:00
|
|
|
// Only sysop users will have an edit tab in this namespace, so we might need to add one
|
|
|
|
if ( $( '#ca-edit' ).length === 0 ) {
|
|
|
|
// Add edit tab
|
|
|
|
mw.util.addPortletLink(
|
|
|
|
'p-views',
|
|
|
|
'#',
|
|
|
|
mw.msg( 'edit' ),
|
|
|
|
'ca-edit',
|
|
|
|
mw.msg( 'tooltip-ca-edit' ),
|
|
|
|
mw.msg( 'accesskey-ca-edit' ),
|
|
|
|
'#ca-history'
|
|
|
|
);
|
|
|
|
// If there isn't an edit tab, there's a view source tab we need to replace with edit source
|
|
|
|
var $viewSource = $( '#ca-viewsource' );
|
|
|
|
if ( $viewSource.length > 0 ) {
|
|
|
|
// "Move" the view source link to p-actions
|
|
|
|
mw.util.addPortletLink(
|
|
|
|
'p-cactions',
|
|
|
|
$viewSource.find( 'a' ).attr( 'href' ),
|
|
|
|
$viewSource.find( 'a' ).text(),
|
|
|
|
$viewSource.attr( 'id' )
|
|
|
|
);
|
|
|
|
// Remove the view original source link
|
|
|
|
$viewSource.remove();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Sysop users will need a new edit source tab since we are highjacking the edit tab
|
|
|
|
mw.util.addPortletLink(
|
|
|
|
'p-cactions',
|
|
|
|
mw.util.wikiGetlink() + '?action=edit',
|
|
|
|
'Edit Source', // TODO: i18n
|
|
|
|
'ca-editsource'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$( '#ca-edit > span > a' ).click( ve.proxy( this.onEditTabClick, this ) );
|
2012-06-12 01:21:31 +00:00
|
|
|
$( '#ca-view > span > a' ).click( ve.proxy( this.onViewTabClick, this ) );
|
2012-06-11 06:54:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows a loading spinner.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
2012-06-11 23:06:35 +00:00
|
|
|
ve.init.ViewPageTarget.prototype.showSpinner = function() {
|
2012-06-11 06:54:41 +00:00
|
|
|
this.$spinner = $( '<div></div>' )
|
2012-06-11 23:06:35 +00:00
|
|
|
.addClass( 've-init-viewPageTarget-loadingSpinner' )
|
2012-06-11 20:24:20 +00:00
|
|
|
.prependTo( this.$heading );
|
2012-06-11 06:54:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets all tabs in the UI and selects a specific one.
|
|
|
|
*
|
|
|
|
* If no ID is given, or no ID matches the given ID, all tabs will be unselected.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {String} id HTML ID of tab to select
|
|
|
|
*/
|
2012-06-11 23:06:35 +00:00
|
|
|
ve.init.ViewPageTarget.prototype.setSelectedTab = function( id ) {
|
2012-06-11 06:54:41 +00:00
|
|
|
$( '#p-views' ).find( 'li.selected' ).removeClass( 'selected' );
|
|
|
|
$( '#' + id ).addClass( 'selected' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2012-06-11 23:06:35 +00:00
|
|
|
ve.extendClass( ve.init.ViewPageTarget, ve.init.Target );
|
2012-06-11 06:54:41 +00:00
|
|
|
|
|
|
|
/* Initialization */
|
|
|
|
|
|
|
|
// TODO: Clean this stuff up
|
|
|
|
|
|
|
|
ve.config = mw.config.get( 'wgVisualEditor' );
|
2012-06-11 23:06:35 +00:00
|
|
|
ve.init.current = new ve.init.ViewPageTarget();
|