File size: 4,217 Bytes
e1d589b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | ( function ( M, $ ) {
var
Overlay = M.require( 'Overlay' ),
popup = M.require( 'toast' ),
api = M.require( 'api' ),
user = M.require( 'user' ),
Page = M.require( 'Page' ),
pageApi = M.require( 'pageApi' ),
TalkSectionOverlay;
/**
* Overlay for showing talk page section
* @class TalkSectionOverlay
* @extends Overlay
* @uses Api
* @uses Page
* @uses Toast
*/
TalkSectionOverlay = Overlay.extend( {
templatePartials: {
header: mw.template.get( 'mobile.talk.overlays', 'Section/header.hogan' ),
content: mw.template.get( 'mobile.talk.overlays', 'Section/content.hogan' )
},
/**
* @inheritdoc
* @cfg {Object} defaults Default options hash.
* @cfg {String} defaults.title Title.
* @cfg {Section} defaults.section that is currently being viewed in overlay.
* @cfg {String} defaults.reply Reply heading.
* @cfg {String} defaults.info Message that informs the user their talk reply will be
* automatically signed.
*/
defaults: $.extend( {}, Overlay.prototype.defaults, {
title: undefined,
section: undefined,
reply: mw.msg( 'mobile-frontend-talk-reply' ),
info: mw.msg( 'mobile-frontend-talk-reply-info' )
} ),
events: $.extend( {}, Overlay.prototype.events, {
'focus textarea': 'onFocusTextarea',
'click .save-button': 'onSaveClick'
} ),
/**
* Fetches the talk topics of the page specified in options.title
* if options.section is not defined.
* @inheritdoc
*/
postRender: function ( options ) {
Overlay.prototype.postRender.apply( this, arguments );
this.$saveButton = $( '.save-button' );
if ( !options.section ) {
this.renderFromApi( options );
} else {
this.clearSpinner();
this._enableComments();
}
},
/**
* Enables comments on the current rendered talk topic
* @method
* @private
*/
_enableComments: function () {
this.$commentBox = this.$( '.comment' );
if ( user.isAnon() ) {
this.$commentBox.remove();
} else {
this.$textarea = this.$commentBox.find( 'textarea' );
}
},
/**
* Loads the discussion from api and add it to the Overlay
* @param {Object} options Render options
*/
renderFromApi: function ( options ) {
var self = this;
pageApi.getPage( options.title ).done( function ( pageData ) {
var page = new Page( pageData );
options.section = page.getSection( options.id );
self.render( options );
self.clearSpinner();
} );
},
/**
* Handler for focus of textarea
*/
onFocusTextarea: function () {
this.$textarea.removeClass( 'error' );
},
/**
* Handle a click on the save button
*/
onSaveClick: function () {
var val = this.$textarea.val(),
self = this;
if ( val ) {
// show a spinner
this.showSpinner();
this.$saveButton.prop( 'disabled', true );
// sign and add newline to front
val = '\n\n' + val + ' ~~~~';
api.postWithToken( 'edit', {
action: 'edit',
title: this.options.title,
section: this.options.id,
appendtext: val
} ).done( function () {
popup.show( mw.msg( 'mobile-frontend-talk-reply-success' ), 'toast' );
// invalidate the cache
pageApi.invalidatePage( self.options.title );
self.renderFromApi( self.options );
} ).fail( function ( data, response ) {
// FIXME: Code sharing with EditorOverlay?
var msg,
// When save failed with one of these error codes, the returned
// message in response.error.info will be forwarded to the user.
// FIXME: This shouldn't be needed when info texts are all localized.
whitelistedErrorInfo = [
'readonly',
'blocked',
'autoblocked'
];
if (
response.error &&
$.inArray( response.error.code, whitelistedErrorInfo ) > -1
) {
msg = response.error.info;
} else {
msg = mw.msg( 'mobile-frontend-editor-error' );
}
self.clearSpinner();
popup.show( msg, 'toast error' );
} ).always( function () {
self.$saveButton.prop( 'disabled', false );
} );
} else {
this.$textarea.addClass( 'error' );
}
}
} );
M.define( 'modules/talk/TalkSectionOverlay', TalkSectionOverlay );
}( mw.mobileFrontend, jQuery ) );
|