001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2017-2018 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.portal.base.events;
020
021import java.io.IOException;
022import java.io.Writer;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.Map;
026
027/**
028 * Causes a notification to be display on the top of the portal page.
029 * 
030 * The event triggers the creation of a notification widget in the
031 * portal page. 
032 */
033public class DisplayNotification extends PortalCommand {
034
035    private String content;
036    private Map<String, Object> options;
037
038    /**
039     * Creates a new event. The content must be valid HTML, i.e. it
040     * must start with a tag (usually a "`<span>`"). See the JavaScript 
041     * documentation of the
042     * <a href="../../../../jsdoc/index.html#notificationplugin">
043     * notification plugin</a> for details about the available options.
044     * 
045     * @param content the content (valid HTML)
046     * @param options the options (must be serializable as JSON)
047     */
048    public DisplayNotification(String content, Map<String, Object> options) {
049        this.content = content;
050        this.options = options;
051    }
052
053    /**
054     * Creates a new event without any options.
055     * The content must be valid HTML, i.e. it
056     * must start with a tag (usually a "`<span>`").
057     * 
058     * @param content the content (valid HTML)
059     */
060    public DisplayNotification(String content) {
061        this(content, null);
062    }
063
064    /**
065     * Adds an option to the event. See the JavaScript documentation of the
066     * <a href="../../../../jsdoc/index.html#notificationplugin">
067     * notification plugin</a> for details about the available options.
068     * 
069     * @param name the option name
070     * @param value the option value (must be serializable as JSON)
071     * @return the event for easy chaining
072     */
073    public DisplayNotification addOption(String name, Object value) {
074        if (options == null) {
075            options = new HashMap<String, Object>();
076        }
077        options.put(name, value);
078        return this;
079    }
080
081    /**
082     * Returns the content.
083     * 
084     * @return the content
085     */
086    public String content() {
087        return content;
088    }
089
090    /**
091     * Return the options.
092     * 
093     * @return the options
094     */
095    public Map<String, Object> options() {
096        return options == null ? Collections.emptyMap() : options;
097    }
098
099    @Override
100    public void toJson(Writer writer) throws IOException {
101        Map<String, Object> options = options();
102        options.put("destroyOnClose", true);
103        toJson(writer, "displayNotification", content(), options);
104    }
105
106}