summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/MultimediaViewer/resources/mmv/ui/mmv.ui.permission.js
blob: 74ce639aed334377e5e7094afb53e92155c6e768 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * This file is part of the MediaWiki extension MediaViewer.
 *
 * MediaViewer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * MediaViewer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MediaViewer.  If not, see <http://www.gnu.org/licenses/>.
 */

( function ( mw, $, oo ) {
	var P;

	/**
	 * A box to display additional terms or remarks from the image author.
	 * (Typically comes from the Permission field of the {{Information}} template.)
	 * It has two states: when closed, it just shows some text, when open, it shows the HTML
	 * block supplied by the author in its full beauty.
	 *
	 * @class mw.mmv.ui.Permission
	 * @extends mw.mmv.ui.Element
	 * @constructor
	 * @param {jQuery} $container
	 * @param {mw.mmv.ui.MetadataPanelScroller} scroller
	 */
	function Permission( $container, scroller ) {
		var permission = this;

		mw.mmv.ui.Element.call( this, $container );

		/** @property {mw.mmv.HtmlUtils} htmlUtils - */
		this.htmlUtils = new mw.mmv.HtmlUtils();

		/**
		 * Contains everything else.
		 * @property {jQuery}
		 */
		this.$box = $( '<div>' )
			.addClass( 'mw-mmv-permission-box mw-mmv-info-box empty' )
			.appendTo( this.$container );

		/**
		 * Box title
		 * @property {jQuery}
		 */
		this.$title = $( '<h3>' )
			.text( mw.message( 'multimediaviewer-permission-title' ).text() )
			.appendTo( this.$box );

		/**
		 * Plain-text version of the author's message
		 * This is just the text parsed out from the original markup, it might not make much sense
		 * (e.g. if the original is a HTML table)
		 * @property {jQuery}
		 */
		this.$text = $( '<div>' )
			.addClass( 'mw-mmv-permission-text' )
			.appendTo( this.$box )
			.on( 'click', '.mw-mmv-permission-text-viewmore', function ( e ) {
				e.preventDefault();
				permission.grow();
				permission.scroller.toggle( 'up' );
			} )
		;

		/**
		 * A helper element to fade off text
		 * @property {jQuery}
		 */
		this.$fader = $( '<div>' )
			.addClass( 'mw-mmv-permission-text-fader' )
			.append(
				$( '<a>' )
					.addClass( 'mw-mmv-permission-text-viewmore' )
					.prop( 'href', '#' )
					.text( mw.message( 'multimediaviewer-permission-viewmore' ).text() )
			);

		/**
		 * Original (HTML) version of the author's message
		 * This can be scary sometimes (huge tables, black text on dark purple background etc).
		 * @property {jQuery}
		 */
		this.$html = $( '<div>' )
			.addClass( 'mw-mmv-permission-html' )
			.appendTo( this.$box );

		/**
		 * "Close" button (does not actually close the box, just makes it smaller).
		 * @property {jQuery}
		 */
		this.$close = $( '<button>' )
			.addClass( 'mw-mmv-permission-close' )
			.on( 'click', function () {
				permission.shrink();
			} )
			.appendTo( this.$box );

		/**
		 * Panel scroller from the metadata panel object.
		 * @property {mw.mmv.ui.MetadataPanelScroller}
		 */
		this.scroller = scroller;
	}
	oo.inheritClass( Permission, mw.mmv.ui.Element );
	P = Permission.prototype;

	/**
	 * Clear everything
	 */
	P.empty = function () {
		this.$box.addClass( 'empty' );
		this.$text.empty();
		this.$html.empty();
	};

	/**
	 * Set permission text/html
	 *
	 * @param {string} permission the text or HTML code written by the image author
	 */
	P.set = function ( permission ) {
		this.$box.removeClass( 'empty' );

		this.$text.html( this.htmlUtils.htmlToTextWithLinks( permission ) );
		this.$text.append( this.$fader );

		this.$html.html( permission );
	};

	/**
	 * Enlarge the box, show HTML instead of text.
	 *
	 * @fires mmv-permission-grow
	 */
	P.grow = function () {
		mw.mmv.actionLogger.log( 'terms-open' );

		this.$box.addClass( 'full-size' )
			.stop( true )
			.animate( { backgroundColor: '#FFFFA0' }, 500 )
			.animate( { backgroundColor: '#FFFFFF' }, 500 );
		this.$container.trigger( 'mmv-permission-grow' );
	};

	/**
	 * Limit the size of the box, show text only.
	 *
	 * @fires mmv-permission-shrink
	 */
	P.shrink = function () {
		this.$box.removeClass( 'full-size' );
		this.$container.trigger( 'mmv-permission-shrink' );
	};

	/**
	 * Returns whether the box is full-size.
	 *
	 * @return {boolean}
	 */
	P.isFullSize = function () {
		return this.$box.hasClass( 'full-size' );
	};

	mw.mmv.ui.Permission = Permission;
}( mediaWiki, jQuery, OO ) );