User:Mooeypoo/articlesandbox.js

var ARTICLE_REPO = 'User:Mooeypoo/test/articlesandbox';

$.when( mw.loader.using( [ 'oojs-ui', 'mediawiki.util', 'mediawiki.api' ] ), $.ready).then( function {	// Dialog settings; this must be inside the loader.using scope	// since it requires OOUI	var ArticleDialog = function ArticleDialog( config ) {		ArticleDialog.parent.call( this, config );	};	OO.inheritClass( ArticleDialog, OO.ui.Dialog );	ArticleDialog.static.title = 'Article helper dialog';	ArticleDialog.prototype.initialize = function  {		var closeButton,			dialog = this;		ArticleDialog.parent.prototype.initialize.apply( this, arguments );		this.content = new OO.ui.PanelLayout( { padded: true, expanded: false } );		this.content.$element.append( ' Dialog content ' );		closeButton = new OO.ui.ButtonWidget( { label: OO.ui.msg( 'ooui-dialog-process-dismiss' ) } );		closeButton.on( 'click', function { dialog.close; } );		this.content.$element.append( closeButton.$element );		this.$body.append( this.content.$element );	};	ArticleDialog.prototype.getBodyHeight = function {		return this.content.$element.outerHeight( true );	};

var mainDialog = new ArticleDialog, windowManager = new OO.ui.WindowManager, mainButton = new OO.ui.ButtonWidget( {			label: 'Article helper'		} );

windowManager.addWindows( [ mainDialog ] );

// Attach to DOM $( '#right-navigation' ).append( mainButton.$element ); $( 'body' ).append( windowManager.$element );

// Attach events mainButton.on( 'click', function {		windowManager.openWindow( mainDialog );	} );

// Get the data buildArticleStructure .then( function ( articles ) {			console.log( 'final', articles );		} );

} );

/** * Build the article structure from the API */ function buildArticleStructure { var path, flatPath, articles = {};

return ( new mw.Api ).get( {		action: 'query',		format: 'json',		list: 'prefixsearch',		pssearch: ARTICLE_REPO	} ) .then(		// Success		function ( queryData ) {			var articlesArr = queryData.query.prefixsearch;			articlesArr.forEach( function ( articleData ) { var pieces = [], title = articleData.title;

if ( title.indexOf( ARTICLE_REPO ) !== 0 ) { return; }				pieces = title.replace( ARTICLE_REPO, '' ).split( '/' ); pieces = pieces.filter( function ( piece ) {					return !!piece; // Get rid of empty strings				} );

// Build article structure path = articles; flatPath = ''; pieces.forEach( function ( piece ) {					flatPath += '/' + piece;

path[ piece ] = path[ piece ] || { _path: ARTICLE_REPO + flatPath, _articles: {} };					// Shift reference path = path[piece]._articles; } );			} );			return articles; },		// Convert failure to empty success function { return $.Deferred.resolve( {} ); }	); }

function createDialog { }