Debian Codegraph @

extensions/MultimediaViewer/resources/mmv.bootstrap/mmv.Config.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 api = new mw.Api();
19
20 /**
21 * Contains/retrieves configuration/environment information for MediaViewer.
22 */
23 class Config {
24
25 /**
26 * The media route prefix
27 *
28 * @return {string}
29 */
30 static get ROUTE() {
31 return 'media';
32 }
33
34 /**
35 * RegExp representing the media route
36 *
37 * @return {RegExp}
38 */
39 static get ROUTE_REGEXP() {
40 return /^\/media\/(.+)$/;
41 }
42
43 /**
44 * RegExp representing the media position as in "File:foo.jpg/3"
45 *
46 * @return {RegExp}
47 */
48 static get POSITION_REGEXP() {
49 return /\/(\d+)$/;
50 }
51
52 /**
53 * Regular expression representing the legacy media route
54 *
55 * @return {RegExp}
56 */
57 static get LEGACY_ROUTE_REGEXP() {
58 return /^mediaviewer\/(.+)$/;
59 }
60
61 /**
62 * Returns true if MediaViewer should handle thumbnail clicks.
63 *
64 * @return {boolean}
65 */
66 static isMediaViewerEnabledOnClick() {
67 return mw.config.get( 'wgMediaViewer' ) && // global opt-out switch, can be set in user JS
68 mw.config.get( 'wgMediaViewerOnClick' ) && // thumbnail opt-out, can be set in preferences
69 ( mw.user.isNamed() || !mw.storage.get( 'wgMediaViewerOnClick' ) || mw.storage.get( 'wgMediaViewerOnClick' ) === '1' ); // thumbnail opt-out for anons
70 }
71
72 /**
73 * Returns the location hash (route string) for the given file title.
74 *
75 * @param {string} imageFileTitle the file title
76 * @param {number} [position] the relative position of this image to others with same file
77 * @return {string} the location hash
78 * @member mw.mmv
79 */
80 static getMediaHash( imageFileTitle, position ) {
81 return position > 1 ?
82 `#/${ this.ROUTE }/${ encodeURI( imageFileTitle ) }/${ position }` :
83 `#/${ this.ROUTE }/${ encodeURI( imageFileTitle ) }`;
84 }
85
86 /**
87 * (Semi-)permanently stores the setting whether MediaViewer should handle thumbnail clicks.
88 * - for logged-in users, we use preferences
89 * - for anons, we use localStorage
90 * - for anons with old browsers, we don't do anything
91 *
92 * @param {boolean} enabled
93 * @return {jQuery.Promise} a deferred which resolves/rejects on success/failure respectively
94 */
95 static setMediaViewerEnabledOnClick( enabled ) {
96 const defaultPrefValue = mw.config.get( 'wgMediaViewerEnabledByDefault' );
97 let deferred;
98 let newPrefValue;
99 let success = true;
100
101 if ( !mw.user.isNamed() ) {
102 if ( !enabled ) {
103 success = mw.storage.set( 'wgMediaViewerOnClick', '0' ); // localStorage stringifies everything, best use strings in the first place
104 } else {
105 success = mw.storage.remove( 'wgMediaViewerOnClick' );
106 }
107 if ( success ) {
108 deferred = $.Deferred().resolve();
109 } else {
110 deferred = $.Deferred().reject();
111 }
112 } else {
113 // Simulate changing the option in Special:Preferences. Turns out this is quite hard (bug 69942):
114 // we need to delete the user_properties row if the new setting is the same as the default,
115 // otherwise set '1' for enabled, '' for disabled. In theory the pref API will delete the row
116 // if the new value equals the default, but this does not always work.
117 if ( defaultPrefValue === true ) {
118 newPrefValue = enabled ? '1' : '';
119 } else {
120 // undefined will cause the API call to omit the optionvalue parameter
121 // which in turn will cause the options API to delete the row and revert the pref to default
122 newPrefValue = enabled ? '1' : undefined;
123 }
124 deferred = api.saveOption( 'multimediaviewer-enable', newPrefValue );
125 }
126
127 return deferred.done( () => {
128 // make the change work without a reload
129 mw.config.set( 'wgMediaViewerOnClick', enabled );
130 if ( !enabled ) {
131 // set flag for showing a popup if this was a first-time disable
132 Config.maybeEnableStatusInfo();
133 }
134 } );
135 }
136
137 /**
138 * True if info about enable/disable status should be displayed (mingle #719).
139 *
140 * @return {boolean}
141 */
142 static shouldShowStatusInfo() {
143 return !this.isMediaViewerEnabledOnClick() && mw.storage.get( 'mmv-showStatusInfo' ) === '1';
144 }
145
146 /**
147 * Called when MediaViewer is disabled. If status info was never displayed before, future
148 * shouldShowStatusInfo() calls will return true.
149 *
150 * @private
151 */
152 static maybeEnableStatusInfo() {
153 const currentShowStatusInfo = mw.storage.get( 'mmv-showStatusInfo' );
154 if ( currentShowStatusInfo === null ) {
155 mw.storage.set( 'mmv-showStatusInfo', '1' );
156 }
157 }
158
159 /**
160 * Called when status info is displayed. Future shouldShowStatusInfo() calls will return false.
161 */
162 static disableStatusInfo() {
163 mw.storage.set( 'mmv-showStatusInfo', '0' );
164 }
165
166 /**
167 * Returns UI language
168 *
169 * @return {string} Language code
170 */
171 static language() {
172 return mw.config.get( 'wgUserLanguage', false ) || mw.config.get( 'wgContentLanguage', 'en' );
173 }
174 }
175
176 mw.mmv = Config;
177 module.exports = Config;