001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ade.galleries.client;
029
030import org.opencms.ade.galleries.shared.CmsGalleryTabConfiguration;
031import org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration;
032import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants;
033import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants.GalleryMode;
034import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants.GalleryTabId;
035import org.opencms.gwt.client.util.CmsDomUtil;
036import org.opencms.gwt.shared.CmsGwtConstants;
037import org.opencms.util.CmsStringUtil;
038
039import java.util.Arrays;
040import java.util.List;
041
042import com.google.gwt.core.client.JavaScriptObject;
043
044import elemental2.dom.DomGlobal;
045import elemental2.dom.Window;
046import jsinterop.base.Js;
047import jsinterop.base.JsPropertyMap;
048
049/**
050 * Gallery configuration java-script overlay object.<p>
051 */
052public final class CmsGalleryConfigurationJSO extends JavaScriptObject implements I_CmsGalleryConfiguration {
053
054    /**
055     * Hiding constructor.<p>
056     */
057    protected CmsGalleryConfigurationJSO() {
058
059        // nothing to do
060    }
061
062    /**
063     * Parses the given JSON configuration string.<p>
064     *
065     * @param conf the JSON configuration string
066     *
067     * @return the gallery configuration object
068     */
069    public static CmsGalleryConfigurationJSO parseConfiguration(String conf) {
070
071        if (CmsStringUtil.isEmptyOrWhitespaceOnly(conf)) {
072            conf = "{}";
073        }
074        CmsGalleryConfigurationJSO result = (CmsGalleryConfigurationJSO)CmsDomUtil.parseJSON(conf);
075        if (result.getLocale() == null) {
076            Window window = DomGlobal.window;
077            Window prevWindow = null;
078            while ((window != null) && (window != prevWindow)) {
079                JsPropertyMap<Object> windowProps = Js.cast(window);
080                String locale = (String)windowProps.get(CmsGwtConstants.ATTR_CONTENT_EDITOR_LOCALE);
081                if (locale != null) {
082                    result.setLocale(locale);
083                    break;
084                }
085                prevWindow = window;
086                window = window.parent;
087            }
088        }
089        return result;
090    }
091
092    /**
093     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#getCurrentElement()
094     */
095    public String getCurrentElement() {
096
097        return getString(I_CmsGalleryProviderConstants.CONFIG_CURRENT_ELEMENT);
098    }
099
100    /**
101     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#getGalleryMode()
102     */
103    public GalleryMode getGalleryMode() {
104
105        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(internalGetGalleryMode())) {
106            return Enum.valueOf(GalleryMode.class, internalGetGalleryMode());
107        }
108        return null;
109    }
110
111    /**
112     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#getGalleryPath()
113     */
114    public String getGalleryPath() {
115
116        return getString(I_CmsGalleryProviderConstants.CONFIG_GALLERY_PATH);
117    }
118
119    /**
120     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#getGalleryStoragePrefix()
121     */
122    public String getGalleryStoragePrefix() {
123
124        return getString(I_CmsGalleryProviderConstants.CONFIG_GALLERY_STORAGE_PREFIX);
125    }
126
127    /**
128     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#getGalleryTypeName()
129     */
130    public String getGalleryTypeName() {
131
132        return getString(I_CmsGalleryProviderConstants.CONFIG_GALLERY_NAME);
133    }
134
135    /**
136     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#getGalleryTypes()
137     */
138    public String[] getGalleryTypes() {
139
140        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(internalGetGalleryTypes())) {
141            return internalGetGalleryTypes().split(",");
142        }
143        return null;
144    }
145
146    /**
147     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#getImageFormatNames()
148     */
149    public native String getImageFormatNames()/*-{
150        if (typeof this[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::CONFIG_IMAGE_FORMAT_NAMES] != 'undefined') {
151            return this[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::CONFIG_IMAGE_FORMAT_NAMES]
152                    .toString();
153        }
154        return null;
155    }-*/;
156
157    /**
158     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#getImageFormats()
159     */
160    public native String getImageFormats()/*-{
161        if (typeof this[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::CONFIG_IMAGE_FORMATS] != 'undefined') {
162            return this[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::CONFIG_IMAGE_FORMATS]
163                    .toString();
164        }
165        return null;
166    }-*/;
167
168    /**
169     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#getLocale()
170     */
171    public String getLocale() {
172
173        return getString(I_CmsGalleryProviderConstants.CONFIG_LOCALE);
174    }
175
176    /**
177     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#getPageId()
178     */
179    public String getPageId() {
180
181        return getString(I_CmsGalleryProviderConstants.CONFIG_PAGE_ID);
182    }
183
184    /**
185     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#getReferencePath()
186     */
187    public String getReferencePath() {
188
189        return getString(I_CmsGalleryProviderConstants.CONFIG_REFERENCE_PATH);
190    }
191
192    /**
193     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#getResourceTypes()
194     */
195    public List<String> getResourceTypes() {
196
197        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(internalGetResourceTypes())) {
198            return Arrays.asList(internalGetResourceTypes().split(","));
199        }
200        return null;
201    }
202
203    /**
204     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#getSearchTypes()
205     */
206    public List<String> getSearchTypes() {
207
208        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(internalGetSearchTypes())) {
209            return Arrays.asList(internalGetSearchTypes().split(","));
210        }
211        return null;
212    }
213
214    /**
215     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#getStartFolder()
216     */
217    public String getStartFolder() {
218
219        return getString(I_CmsGalleryProviderConstants.CONFIG_START_FOLDER);
220    }
221
222    /**
223     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#getStartSite()
224     */
225    public native String getStartSite()/*-{
226        if (typeof this[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::CONFIG_START_SITE] != 'undefined') {
227            return this[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::CONFIG_START_SITE];
228        }
229        return null;
230    }-*/;
231
232    /**
233     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#getTabConfiguration()
234     */
235    public CmsGalleryTabConfiguration getTabConfiguration() {
236
237        return CmsGalleryTabConfiguration.resolve(getTabConfigString());
238
239    }
240
241    /**
242     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#getTreeToken()
243     */
244    public String getTreeToken() {
245
246        return getString(I_CmsGalleryProviderConstants.CONFIG_TREE_TOKEN);
247    }
248
249    /**
250     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#getUploadFolder()
251     */
252    public String getUploadFolder() {
253
254        return getString(I_CmsGalleryProviderConstants.CONFIG_UPLOAD_FOLDER);
255    }
256
257    /**
258     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#isGalleriesSelectable()
259     */
260    public boolean isGalleriesSelectable() {
261
262        return Boolean.valueOf(getString(I_CmsGalleryProviderConstants.CONFIG_GALLERIES_SELECTABLE)).booleanValue();
263    }
264
265    /**
266     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#isIncludeFiles()
267     */
268    public boolean isIncludeFiles() {
269
270        return getTabConfiguration().getTabs().contains(GalleryTabId.cms_tab_results);
271    }
272
273    /**
274     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#isResultsSelectable()
275     */
276    public boolean isResultsSelectable() {
277
278        String resultsSelectable = getString(I_CmsGalleryProviderConstants.CONFIG_RESULTS_SELECTABLE);
279        if (resultsSelectable == null) {
280            return true;
281        }
282        return Boolean.valueOf(resultsSelectable).booleanValue();
283    }
284
285    /**
286     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#isShowSiteSelector()
287     */
288    public native boolean isShowSiteSelector()/*-{
289        // defaults to true
290        return 'false' != ''
291                + this[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::CONFIG_SHOW_SITE_SELECTOR]
292    }-*/;
293
294    /**
295     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#isUseFormats()
296     */
297    public native boolean isUseFormats()/*-{
298        // defaults to false
299        return 'true' == ''
300                + this[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::CONFIG_USE_FORMATS]
301    }-*/;
302
303    /**
304     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#setCurrentElement(java.lang.String)
305     */
306    public native void setCurrentElement(String currentElement)/*-{
307        this[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::CONFIG_CURRENT_ELEMENT] = currentElement;
308    }-*/;
309
310    /**
311     * @see org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration#setStartFolder(java.lang.String)
312     */
313    public native void setStartFolder(String startFolder)/*-{
314
315        this[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::CONFIG_START_FOLDER] = startFolder;
316    }-*/;
317
318    /**
319     * Gets a string-valued attribute.<p>
320     *
321     * @param key the name of the attribute
322     *
323     * @return the value of the attribute
324     */
325    private native String getString(String key) /*-{
326        if (typeof this[key] != 'undefined') {
327            return this[key];
328        }
329        return null;
330    }-*/;
331
332    /**
333     * Gets the tab configuration string.<p>
334     *
335     * @return the tab configuration string
336     */
337    private String getTabConfigString() {
338
339        return getString(I_CmsGalleryProviderConstants.CONFIG_TAB_CONFIG);
340    }
341
342    /**
343     * Returns the gallery mode name.<p>
344     *
345     * @return the gallery mode name
346     */
347    private String internalGetGalleryMode() {
348
349        return getString(I_CmsGalleryProviderConstants.CONFIG_GALLERY_MODE);
350    }
351
352    /**
353     * Returns the gallery types in a comma separated list.<p>
354     *
355     * @return the gallery types in a comma separated list
356     */
357    private String internalGetGalleryTypes() {
358
359        return getString(I_CmsGalleryProviderConstants.CONFIG_GALLERY_TYPES);
360    }
361
362    /**
363     * Returns the resource types in a comma separated list.<p>
364     *
365     * @return the resource types in a comma separated list
366     */
367    private String internalGetResourceTypes() {
368
369        return getString(I_CmsGalleryProviderConstants.CONFIG_RESOURCE_TYPES);
370    }
371
372    /**
373     * Returns the search types in a comma separated list.<p>
374     *
375     * @return the search types in a comma separated list
376     */
377    private String internalGetSearchTypes() {
378
379        return getString(I_CmsGalleryProviderConstants.CONFIG_SEARCH_TYPES);
380    }
381
382    private void setLocale(String locale) {
383
384        JsPropertyMap<Object> props = Js.cast(this);
385        props.set(I_CmsGalleryProviderConstants.CONFIG_LOCALE, locale);
386    }
387
388}