001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://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: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://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.preview;
029
030import org.opencms.gwt.client.util.CmsJSONMap;
031import org.opencms.util.CmsStringUtil;
032
033import java.util.Map;
034import java.util.Map.Entry;
035
036import com.google.gwt.core.client.JsArrayString;
037
038/**
039 * Utility class for resource preview.<p>
040 *
041 * @since 8.0.0
042 */
043public final class CmsPreviewUtil {
044
045    /** The close dialog function key. */
046    static final String KEY_DIALOG_CLOSE_FUNCTION = "closeDialog";
047
048    /** The dialog OK function key. */
049    static final String KEY_DIALOG_OK_FUNCTION = "dialogOk";
050
051    /** The enable dialog OK function key. */
052    static final String KEY_ENABLE_DIALOG_OK_FUNCTION = "enableDialogOk";
053
054    /** The get image info function key. */
055    static final String KEY_GET_IMAGE_INFO_FUNCTION = "getImageInfo";
056
057    /** The has enhanced image options function key. */
058    static final String KEY_HAS_ENHANCED_IMAGE_OPTIONS = "hasEnhancedImageOptions";
059
060    /** The close gallery dialog function key. */
061    static final String KEY_SET_DATA_IN_EDITOR_FUNCTION = "setDataInEditor";
062
063    /** The set image function key. */
064    static final String KEY_SET_IMAGE_FUNCTION = "setImage";
065
066    /** The set image link function key. */
067    static final String KEY_SET_IMAGE_LINK_FUNCTION = "setImageLink";
068
069    /** The set link function key. */
070    static final String KEY_SET_LINK_FUNCTION = "setLink";
071
072    /**
073     * Constructor.<p>
074     */
075    private CmsPreviewUtil() {
076
077        // hiding the constructor
078    }
079
080    /**
081     * Triggers the dialog OK action.<p>
082     */
083    public static native void closeDialog() /*-{
084        $wnd[@org.opencms.ade.galleries.client.preview.CmsPreviewUtil::KEY_DIALOG_CLOSE_FUNCTION]
085                ();
086    }-*/;
087
088    /**
089     * Enables the dialog OK button within the rich text editor (FCKEditor, CKEditor, ...).<p>
090     *
091     * @param enabled <code>true</code> to enable the button
092     */
093    public static native void enableEditorOk(boolean enabled)/*-{
094        $wnd[@org.opencms.ade.galleries.client.preview.CmsPreviewUtil::KEY_ENABLE_DIALOG_OK_FUNCTION]
095                (enabled);
096    }-*/;
097
098    /**
099     * Exports the functions of {@link org.opencms.ade.galleries.client.preview.I_CmsResourcePreview}
100     * to the window object for use via JSNI.<p>
101     *
102     * @param previewName the preview name
103     * @param preview the preview
104     */
105    public static native void exportFunctions(String previewName, I_CmsResourcePreview<?> preview) /*-{
106        $wnd[@org.opencms.ade.galleries.client.preview.CmsPreviewUtil::KEY_SET_DATA_IN_EDITOR_FUNCTION] = function() {
107            return preview.@org.opencms.ade.galleries.client.preview.I_CmsResourcePreview::setDataInEditor()();
108        };
109    }-*/;
110
111    /**
112     * Returns the xml-content field id.<p>
113     *
114     * @return the field id
115     */
116    public static native String getFieldId() /*-{
117        return $wnd[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::KEY_FIELD_ID];
118    }-*/;
119
120    /**
121     * Returns the available image format names for gallery widget mode.<p>
122     *
123     * @return the available image format names
124     */
125    public static String[] getFormatNames() {
126
127        JsArrayString formatNames = nativeGetFormatNames();
128        if ((formatNames == null) || (formatNames.length() == 0)) {
129            return null;
130        }
131        String[] result = new String[formatNames.length()];
132        for (int i = 0; i < formatNames.length(); i++) {
133            result[i] = formatNames.get(i);
134        }
135        return result;
136    }
137
138    /**
139     * Returns the available image formats for gallery widget mode.<p>
140     *
141     * @return the available image formats
142     */
143    public static String[] getFormats() {
144
145        JsArrayString tempArr = nativeGetFormats();
146        if ((tempArr == null) || (tempArr.length() == 0)) {
147            return null;
148        }
149        String[] result = new String[tempArr.length()];
150        for (int i = 0; i < tempArr.length(); i++) {
151            result[i] = tempArr.get(i);
152        }
153        return result;
154    }
155
156    /**
157     * Returns all available information of the selected image tag, or null, if no image is selected.<p>
158     *
159     * @return a map with the following keys:<p>
160     *          alt, class, height, hspace, linkPath, linkTarget, longDesc, style, title, vspace, width<p>
161     *
162     *          all keys represent a tag attribute by the same name, only linkPath and linkTarget contain
163     *          information on an surrounding link tag
164     */
165    public static native CmsJSONMap getImageAttributes() /*-{
166        return $wnd[@org.opencms.ade.galleries.client.preview.CmsPreviewUtil::KEY_GET_IMAGE_INFO_FUNCTION]
167                ();
168    }-*/;
169
170    /**
171     * Returns the availability of enhanced image options.<p>
172     *
173     * @return <code>true</code> if enhanced image options are available
174     */
175    public static native boolean hasEnhancedImageOptions() /*-{
176        return $wnd[@org.opencms.ade.galleries.client.preview.CmsPreviewUtil::KEY_HAS_ENHANCED_IMAGE_OPTIONS]
177                () ? true : false;
178    }-*/;
179
180    /**
181     * Returns if the gallery widget is used in advanced mode.<p>
182     *
183     * @return <code>true</code> if format selector should be shown
184     */
185    public static native boolean isAdvancedWidget()/*-{
186        var id = $wnd[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::KEY_HASH_ID];
187        var additional = $wnd.parent['cms_additional_' + id];
188        if (additional && additional['isAdvancedWidget']) {
189            return true;
190        }
191        return false;
192    }-*/;
193
194    /**
195     * Returns if the image format selector should be shown within gallery widget mode.<p>
196     *
197     * @return <code>true</code> if format selector should be shown
198     */
199    public static native boolean isShowFormats()/*-{
200        var id = $wnd[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::KEY_HASH_ID];
201        var additional = $wnd.parent['cms_additional_' + id];
202        if (additional) {
203            return additional[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::CONFIG_USE_FORMATS];
204        }
205        return false;
206    }-*/;
207
208    /**
209     * Triggers the dialog OK action.<p>
210     */
211    public static native void setDataAndCloseDialog() /*-{
212        $wnd[@org.opencms.ade.galleries.client.preview.CmsPreviewUtil::KEY_DIALOG_OK_FUNCTION]
213                ();
214    }-*/;
215
216    /**
217     * Sets the image tag within the rich text editor (FCKEditor, CKEditor, ...).<p>
218     *
219     * @param path the image path
220     * @param attributes the image tag attributes
221     */
222    public static void setImage(String path, Map<String, String> attributes) {
223
224        CmsJSONMap attributesJS = CmsJSONMap.createJSONMap();
225        for (Entry<String, String> entry : attributes.entrySet()) {
226            attributesJS.put(entry.getKey(), entry.getValue());
227        }
228        nativeSetImage(path, attributesJS);
229    }
230
231    /**
232     * Sets the image link within the rich text editor (FCKEditor, CKEditor, ...).<p>
233     *
234     * @param path the image path
235     * @param attributes the image tag attributes
236     * @param linkPath the link path
237     * @param target the link target attribute
238     */
239    public static void setImageLink(String path, Map<String, String> attributes, String linkPath, String target) {
240
241        CmsJSONMap attributesJS = CmsJSONMap.createJSONMap();
242        for (Entry<String, String> entry : attributes.entrySet()) {
243            attributesJS.put(entry.getKey(), entry.getValue());
244        }
245        nativeSetImageLink(path, attributesJS, linkPath, target);
246    }
247
248    /**
249     * Sets the resource link within the rich text editor (FCKEditor, CKEditor, ...).<p>
250     *
251     * @param path the link path
252     * @param title the link title
253     * @param target the link target attribute
254     */
255    public static void setLink(String path, String title, String target) {
256
257        nativeSetLink(path, CmsStringUtil.escapeHtml(title), target);
258    }
259
260    /**
261     * Sets the path of the selected resource in the input field of the xmlcontent.<p>
262     *
263     * Widget mode: Use this function inside the xmlcontent.
264     *
265     * @param path the path to the selected resource
266     */
267    public static native void setResourcePath(String path) /*-{
268        //the id of the input field in the xml content
269        var fieldId = $wnd[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::KEY_FIELD_ID];
270
271        if (fieldId != null && fieldId != "") {
272            var inputField = $wnd.parent.document.getElementById(fieldId);
273            inputField.setAttribute('value', path);
274            inputField.value = path;
275            try {
276                // toggle preview icon if possible
277                $wnd.parent.checkPreview(fieldId);
278            } catch (e) {
279            }
280        }
281
282        $wnd.setTimeout(function() {
283            $wnd.parent.cmsCloseDialog(fieldId);
284        }, 10);
285    }-*/;
286
287    /**
288     * Sets the path of the selected resource in the input field of the xmlcontent.<p>
289     *
290     * Widget mode: Use this function inside the xmlcontent.
291     *
292     * @param path the path to the selected resource
293     * @param scale the scale parameter
294     * @param formatName the selected format name
295     * @param ratio the image ratio (width/height)
296     */
297    public static native void setVfsImage(String path, String scale, String formatName, String ratio) /*-{
298        //the id of the input field in the xml content
299        var fieldId = $wnd[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::KEY_FIELD_ID];
300
301        if (fieldId != null && fieldId != "") {
302            var inputField = $wnd.parent.document.getElementById("img."
303                    + fieldId);
304            inputField.setAttribute("value", path);
305            inputField.value = path;
306            if (formatName != null) {
307                var formatBox = $wnd.parent.document.getElementById("format."
308                        + fieldId);
309                if (formatBox != null && formatBox.options != null) {
310                    var selectIndex = -1;
311                    for (var i = 0; i < formatBox.options.length; i++) {
312                        if (formatBox.options[i].value == formatName) {
313                            formatBox.selectedIndex = i;
314                            break;
315                        }
316                    }
317
318                    if (selectIndex != -1) {
319                        formatBox.selectedIndex = selectIndex;
320                        $wnd.parent
321                                .setImageFormat(
322                                        fieldId,
323                                        $wnd[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::KEY_HASH_ID]);
324                    }
325                }
326                var formatField = $wnd.parent.document.getElementById("fmtval."
327                        + fieldId);
328                formatField.setAttribute("value", formatName);
329                formatField.value = formatName;
330            }
331            var ratioField = $wnd.parent.document.getElementById("imgrat."
332                    + fieldId);
333            ratioField.setAttribute("value", ratio);
334            ratioField.value = ratio;
335            var scaleField = $wnd.parent.document.getElementById("scale."
336                    + fieldId);
337            scaleField.setAttribute("value", scale);
338            scaleField.value = scale;
339            try {
340                // toggle preview icon if possible
341                $wnd.parent.checkVfsImagePreview(fieldId);
342            } catch (e) {
343            }
344        }
345        $wnd.setTimeout(function() {
346            $wnd.parent.cmsCloseDialog(fieldId);
347        }, 10);
348    }-*/;
349
350    /**
351     * Returns if the select button should be shown.<p>
352     *
353     * @return <code>true</code> if the select button should be shown
354     */
355    public static native boolean shouldShowSelectButton()/*-{
356        return "true" == $wnd[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::KEY_SHOW_SELECT];
357    }-*/;
358
359    /**
360     * Returns the image format names.<p>
361     *
362     * @return the image format names
363     */
364    private static native JsArrayString nativeGetFormatNames()/*-{
365        var id = $wnd[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::KEY_HASH_ID];
366        var additional = $wnd.parent['cms_additional_' + id];
367        if (additional) {
368            return additional[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::CONFIG_IMAGE_FORMAT_NAMES];
369        }
370        return null;
371    }-*/;
372
373    /**
374     * Returns the available image formats for gallery widget mode.<p>
375     *
376     * @return the available image formats
377     */
378    private static native JsArrayString nativeGetFormats()/*-{
379        var id = $wnd[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::KEY_HASH_ID];
380        var additional = $wnd.parent['cms_additional_' + id];
381        if (additional) {
382            return additional[@org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants::CONFIG_IMAGE_FORMATS];
383        }
384        return null;
385    }-*/;
386
387    /**
388    * Calls the integrator's set image function to set the image tag within the rich text editor (FCKEditor, CKEditor, ...).<p>
389    *
390    * @param path the image path
391    * @param attributes the image tag attributes
392    */
393    private static native void nativeSetImage(String path, CmsJSONMap attributes)/*-{
394        if ($wnd[@org.opencms.ade.galleries.client.preview.CmsPreviewUtil::KEY_SET_IMAGE_FUNCTION] != null) {
395            $wnd[@org.opencms.ade.galleries.client.preview.CmsPreviewUtil::KEY_SET_IMAGE_FUNCTION]
396                    (path, attributes);
397        }
398
399    }-*/;
400
401    /**
402     * Calls the integrator's set image link function to set the image link within the rich text editor (FCKEditor, CKEditor, ...).<p>
403     *
404     * @param path the image path
405     * @param attributes the image tag attributes
406     * @param linkPath the link path
407     * @param target the link target attribute
408     */
409    private static native void nativeSetImageLink(
410        String path,
411        CmsJSONMap attributes,
412        String linkPath,
413        String target)/*-{
414        $wnd[@org.opencms.ade.galleries.client.preview.CmsPreviewUtil::KEY_SET_IMAGE_LINK_FUNCTION]
415                (path, attributes, linkPath, target);
416    }-*/;
417
418    /**
419     * Calls the integrator's set link function to set the resource link within the rich text editor (FCKEditor, CKEditor, ...).<p>
420     *
421     * @param path the link path
422     * @param title the link title
423     * @param target the link target attribute
424     */
425    private static native void nativeSetLink(String path, String title, String target)/*-{
426        $wnd[@org.opencms.ade.galleries.client.preview.CmsPreviewUtil::KEY_SET_LINK_FUNCTION]
427                (path, title, target);
428    }-*/;
429}