Merge "Drop zh-tw message "saveprefs""
[lhc/web/wiklou.git] / resources / src / mediawiki.messagePoster / mediawiki.messagePoster.factory.js
1 /*global OO*/
2 ( function ( mw, $ ) {
3 /**
4 * This is a factory for MessagePoster objects, which allows a pluggable to way to script leaving a
5 * talk page message.
6 *
7 * @class mw.messagePoster.factory
8 * @singleton
9 */
10 function MwMessagePosterFactory() {
11 this.api = new mw.Api();
12 this.contentModelToClass = {};
13 }
14
15 OO.initClass( MwMessagePosterFactory );
16
17 // Note: This registration scheme is currently not compatible with LQT, since that doesn't
18 // have its own content model, just islqttalkpage. LQT pages will be passed to the wikitext
19 // MessagePoster.
20 /**
21 * Registers a MessagePoster subclass for a given content model.
22 *
23 * @param {string} contentModel Content model of pages this MessagePoster can post to
24 * @param {Function} messagePosterConstructor Constructor for MessagePoster
25 */
26 MwMessagePosterFactory.prototype.register = function ( contentModel, messagePosterConstructor ) {
27 if ( this.contentModelToClass[ contentModel ] !== undefined ) {
28 throw new Error( 'The content model \'' + contentModel + '\' is already registered.' );
29 }
30
31 this.contentModelToClass[ contentModel ] = messagePosterConstructor;
32 };
33
34 /**
35 * Unregisters a given content model
36 * This is exposed for testing and should not normally be needed.
37 *
38 * @param {string} contentModel Content model to unregister
39 */
40 MwMessagePosterFactory.prototype.unregister = function ( contentModel ) {
41 delete this.contentModelToClass[ contentModel ];
42 };
43
44 /**
45 * Creates a MessagePoster, given a title. A promise for this is returned.
46 * This works by determining the content model, then loading the corresponding
47 * module (which will register the MessagePoster class), and finally constructing it.
48 *
49 * This does not require the message and should be called as soon as possible, so it does the
50 * API and ResourceLoader requests in the background.
51 *
52 * @param {mw.Title} title Title that will be posted to
53 * @return {jQuery.Promise} Promise resolving to a mw.messagePoster.MessagePoster.
54 * For failure, rejected with up to three arguments:
55 *
56 * - errorCode Error code string
57 * - error Error explanation
58 * - details Further error details
59 */
60 MwMessagePosterFactory.prototype.create = function ( title ) {
61 var pageId, page, contentModel, moduleName,
62 factory = this;
63
64 return this.api.get( {
65 action: 'query',
66 prop: 'info',
67 indexpageids: 1,
68 titles: title.getPrefixedDb()
69 } ).then( function ( result ) {
70 if ( result.query.pageids && result.query.pageids.length > 0 ) {
71 pageId = result.query.pageids[ 0 ];
72 page = result.query.pages[ pageId ];
73
74 contentModel = page.contentmodel;
75 moduleName = 'mediawiki.messagePoster.' + contentModel;
76 return mw.loader.using( moduleName ).then( function () {
77 return factory.createForContentModel(
78 contentModel,
79 title
80 );
81 }, function () {
82 return $.Deferred().reject( 'failed-to-load-module', 'Failed to load the \'' + moduleName + '\' module' );
83 } );
84 } else {
85 return $.Deferred().reject( 'unexpected-response', 'Unexpected API response' );
86 }
87 }, function ( errorCode, details ) {
88 return $.Deferred().reject( 'content-model-query-failed', errorCode, details );
89 } ).promise();
90 };
91
92 /**
93 * Creates a MessagePoster instance, given a title and content model
94 *
95 * @private
96 *
97 * @param {string} contentModel Content model of title
98 * @param {mw.Title} title Title being posted to
99 * @return {mw.messagePoster.MessagePoster}
100 *
101 */
102 MwMessagePosterFactory.prototype.createForContentModel = function ( contentModel, title ) {
103 return new this.contentModelToClass[ contentModel ]( title );
104 };
105
106 mw.messagePoster = {
107 factory: new MwMessagePosterFactory()
108 };
109 }( mediaWiki, jQuery ) );