/* VisualEditor Core Module for MediaWiki.org integration */ (function( mw, $ ){ veCore = function(){ var _this = this, pageName = mw.config.get( 'wgPageName' ), validNamespace = mw.config.get('wgCanonicalNamespace') === 'VisualEditor' ? true: false; this.mainEditor = null; // On VisualEditor namespace ? if ( validNamespace ) { this.$content = $('#content'); this.setupTabs(); // modify / stash content styles this.prepareContentStyles(); $('#ca-edit > span > a').click( function( e ){ // hijack the edit link e.preventDefault(); // Load Editor if not already loaded if( _this.mainEditor === null ) { _this.selectTab( 'ca-edit' ); _this.showSpinner(); // async init mw.loader.using( 'ext.visualEditor.ve', function(){ _this.init(); }); _this.getParsoidHTML( pageName, function( content ){ _this.init( content ); }); } }); } //valid namespace }; // called once for each asnyc callback. // once ve is loaded and page has been parsed into html, continue init veCore.prototype.init = function( html ) { var _this = this, isMobileDevice = ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch || false; // store the html if (typeof html !== 'undefined') { this.html = html; } // if html and ve are loaded if( typeof ve !== 'undefined' && typeof this.html !== 'undefined' ){ $html = $("
") .html( this.html ); // release this.html delete this.html; var options = { toolbars: { top: { // If mobile device, float false 'float': !isMobileDevice, // Toolbar modes 'modes': ['wikitext'] } } }; this.$editor = $(''); this.$spinner.hide(); this.$content.css({ 'padding':'0px 0px 0px 1px' }).append( this.$editor ); this.mainEditor = new ve.Surface( '#ve-editor', $html[0], options ); this.$editor.find('.es-panes') .css('padding', this.contentStyles.padding ); // Save BTN this.$editor.find('.es-modes') .append( $('') .attr('class', 've-action-button es-inspector-savebutton') .text('Save') .click(function(){ // show/hide dialog _this.$dialog.toggle(); }) ).append( $('') .attr('class', 've-action-button ve-closeBtn') .click(function(){ // back to read mode _this.mainEditor = null; _this.cleanup(); }) ); this.initSaveDialog(); } }; veCore.prototype.setupTabs = function(){ //Non admins shouldn't have an edit button, only ca-viewsource var $viewsource; // If no edit button, user not sysop if ( $('#ca-edit').length === 0 ) { // Add Edit button mw.util.addPortletLink( 'p-views', '#', mw.msg('edit'), 'ca-edit', mw.msg('tooltip-ca-edit'), mw.msg('accesskey-ca-edit'), '#ca-history' ); // Create 'View Source' link in sub menu from original. // Remove original $viewsource = $('#ca-viewsource'); if ($viewsource.length > 0) { // if admin, move this to the p-cactions link mw.util.addPortletLink( 'p-cactions', $viewsource.find('span > a').attr('href'), $viewsource.find('span > a').text(), $viewsource.attr('id') ); $viewsource.remove(); } } else { // admin user, create an edit source link mw.util.addPortletLink( 'p-cactions', mw.util.wikiGetlink() + '?action=edit', 'Edit Source', // TODO: i18n 'ca-editsource' ); } }; // Reset tabs, select tab with tabID // #ca-view, #ca-edit, #ca-history veCore.prototype.selectTab = function( tabID ){ // unselects all tabs, selects sel $('#p-views') .find('ul') .children() .each(function(){ if( $(this).attr('id') === tabID ) { $(this).addClass('selected'); } else { $(this).removeClass('selected'); } }); }; veCore.prototype.save = function(){ var _this = this; _this.showSpinner(); // Save _this.getParsoidWikitextAndSave( function( content ){ // cleanup _this.$dialog.slideUp(); // load saved page _this.$content .find('#mw-content-text').html( content ); _this.cleanup(); }); }; veCore.prototype.initSaveDialog = function(){ var _this = this; this.$dialog = $('') .attr({ 'id': 've-saveDialog', 'class': 'es-inspector' }).append( $('') .attr('class', 'es-inspector-title') .text( mw.msg('tooltip-save') ) ).append( $('') .attr('class', 'es-inspector-button ve-saveBtn') .click(function(){ _this.$dialog.toggle(); }) ).append( $('') .attr('class', 've-dialog-divider') .append( $('') .text("Describe what you changed") //.text( mw.msg('summary') ) ).append( $('') .attr({ 'id': 'txtEditSummary', 'type':'text' }) ).append( $('Test edit made with Visual Editor
" + (new Date()).getTime() + "
", summary = $('#txtEditSummary').val(), minor = $('#chkMinorEdit').prop('checked'), watch = $('#chkWatchlist').prop('checked'); $.ajax({ url: mw.util.wikiScript( 'api' ), data: { 'action': 've-parsoid', 'paction': 'save', 'page': mw.config.get( 'wgPageName' ), 'html': html, 'token': mw.user.tokens.get('editToken'), 'summary': summary, 'minor': minor, 'watch': watch, 'format': 'json' }, dataType: 'json', type: 'POST', success: function( data ) { if ( data && data['ve-parsoid'] && data['ve-parsoid'].result && data['ve-parsoid'].result === 'success' && data['ve-parsoid'].content ) { //saved callback( data['ve-parsoid'].content ); } }, error: function( error ) {} }); }; /* Gets HTML for a page from MW API action parse */ veCore.prototype.getParsedPage = function( title, callback ) { //currently using mw api to get $.ajax({ url: mw.util.wikiScript( 'api' ), data: { 'action': 'parse', 'format': 'json', 'page': title }, dataType: 'json', type: 'GET', cache: 'false', success: function( data ) { if ( data && data.parse && data.parse.text ) { // return pages to callback if( typeof callback === 'function') { callback( data.parse.text['*'] ); } } }, error: function() { if( typeof callback === 'function') { callback(); } } }); }; var core = new veCore(); })( window.mw, jQuery );