Debian Codegraph @

extensions/MultimediaViewer/resources/mmv/ui/mmv.ui.permission.js

raw symbols SCIP
1 /*
2 * This file is part of the MediaWiki extension MediaViewer.
3 *
4 * MediaViewer is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * MediaViewer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with MediaViewer. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 const HtmlUtils = require( '../mmv.HtmlUtils.js' );
19 const UiElement = require( './mmv.ui.js' );
20 const MetadataPanelScroller = require( './mmv.ui.metadataPanelScroller.js' );
21
22 /**
23 * A box to display additional terms or remarks from the image author.
24 * (Typically comes from the Permission field of the {{Information}} template.)
25 * It has two states: when closed, it just shows some text, when open, it shows the HTML
26 * block supplied by the author in its full beauty.
27 */
28 class Permission extends UiElement {
29 /**
30 * @param {jQuery} $container
31 * @param {MetadataPanelScroller} scroller
32 */
33 constructor( $container, scroller ) {
34 super( $container );
35
36 /**
37 * Contains everything else.
38 *
39 * @property {jQuery}
40 */
41 this.$box = $( '<div>' )
42 .addClass( 'mw-mmv-permission-box mw-mmv-info-box empty' )
43 .appendTo( this.$container );
44
45 /**
46 * Box title
47 *
48 * @property {jQuery}
49 */
50 this.$title = $( '<h3>' )
51 .text( mw.msg( 'multimediaviewer-permission-title' ) )
52 .appendTo( this.$box );
53
54 /**
55 * Plain-text version of the author's message
56 * This is just the text parsed out from the original markup, it might not make much sense
57 * (e.g. if the original is a HTML table)
58 *
59 * @property {jQuery}
60 */
61 this.$text = $( '<div>' )
62 .addClass( 'mw-mmv-permission-text' )
63 .appendTo( this.$box )
64 .on( 'click', '.mw-mmv-permission-text-viewmore', ( e ) => {
65 e.preventDefault();
66 this.grow();
67 this.scroller.toggle( 'up' );
68 } );
69
70 /**
71 * A helper element to fade off text
72 *
73 * @property {jQuery}
74 */
75 this.$fader = $( '<div>' )
76 .addClass( 'mw-mmv-permission-text-fader' )
77 .append(
78 $( '<a>' )
79 .addClass( 'mw-mmv-permission-text-viewmore' )
80 .prop( 'href', '#' )
81 .text( mw.msg( 'multimediaviewer-permission-viewmore' ) )
82 );
83
84 /**
85 * Original (HTML) version of the author's message
86 * This can be scary sometimes (huge tables, black text on dark purple background etc).
87 *
88 * @property {jQuery}
89 */
90 this.$html = $( '<div>' )
91 .addClass( 'mw-mmv-permission-html' )
92 .appendTo( this.$box );
93
94 /**
95 * "Close" button (does not actually close the box, just makes it smaller).
96 *
97 * @property {jQuery}
98 */
99 this.$close = $( '<button>' )
100 .addClass( 'mw-mmv-permission-close' )
101 .on( 'click', () => {
102 this.shrink();
103 } )
104 .appendTo( this.$box );
105
106 /**
107 * Panel scroller from the metadata panel object.
108 *
109 * @property {MetadataPanelScroller}
110 */
111 this.scroller = scroller;
112 }
113
114 /**
115 * Clear everything
116 */
117 empty() {
118 this.$box.addClass( 'empty' );
119 this.$text.empty();
120 this.$html.empty();
121 }
122
123 /**
124 * Set permission text/html
125 *
126 * @param {string} permission the text or HTML code written by the image author
127 */
128 set( permission ) {
129 this.$box.removeClass( 'empty' );
130
131 this.$text.html( HtmlUtils.htmlToTextWithLinks( permission ) );
132 this.$text.append( this.$fader );
133
134 this.$html.html( permission );
135 }
136
137 /**
138 * @event Permission#mmv-permission-grow
139 */
140
141 /**
142 * Enlarge the box, show HTML instead of text.
143 *
144 * @fires Permission#mmv-permission-grow
145 */
146 grow() {
147 this.$box.addClass( 'full-size' );
148 this.$container.trigger( 'mmv-permission-grow' );
149 }
150
151 /**
152 * @event Permission#mmv-permission-shrink
153 */
154
155 /**
156 * Limit the size of the box, show text only.
157 *
158 * @fires Permission#mmv-permission-shrink
159 */
160 shrink() {
161 this.$box.removeClass( 'full-size' );
162 this.$container.trigger( 'mmv-permission-shrink' );
163 }
164
165 /**
166 * Returns whether the box is full-size.
167 *
168 * @return {boolean}
169 */
170 isFullSize() {
171 return this.$box.hasClass( 'full-size' );
172 }
173 }
174
175 module.exports = Permission;