001/* Attribute for inserting script content (e.g. Javascript) into a page
002 * generated by a web exporter.
003
004 Copyright (c) 2011-2014 The Regents of the University of California.
005 All rights reserved.
006 Permission is hereby granted, without written agreement and without
007 license or royalty fees, to use, copy, modify, and distribute this
008 software and its documentation for any purpose, provided that the above
009 copyright notice and the following two paragraphs appear in all copies
010 of this software.
011
012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
016 SUCH DAMAGE.
017
018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023 ENHANCEMENTS, OR MODIFICATIONS.
024
025 PT_COPYRIGHT_VERSION_2
026 COPYRIGHTENDKEY
027
028 */
029
030package ptolemy.vergil.basic.export.web;
031
032import ptolemy.actor.gui.style.TextStyle;
033import ptolemy.data.expr.Parameter;
034import ptolemy.data.expr.StringParameter;
035import ptolemy.data.type.BaseType;
036import ptolemy.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.NameDuplicationException;
038import ptolemy.kernel.util.NamedObj;
039
040///////////////////////////////////////////////////////////////////
041//// Script
042/**
043 * A parameter for associating a script (such as Javascript) with an object in
044 * a model.
045 *
046 * @author Edward A. Lee, Elizabeth Latronico
047 * @version $Id$
048 * @since Ptolemy II 10.0
049 * @Pt.ProposedRating Red (cxh)
050 * @Pt.AcceptedRating Red (cxh)
051 */
052public abstract class Script extends WebContent implements WebExportable {
053
054    /** Create an instance of this parameter.
055     *  @param container The container.
056     *  @param name The name.
057     *  @exception IllegalActionException If the superclass throws it.
058     *  @exception NameDuplicationException If the superclass throws it.
059     */
060    public Script(NamedObj container, String name)
061            throws IllegalActionException, NameDuplicationException {
062        super(container, name);
063
064        _icon.setIconText("S");
065        displayText.setExpression("Web page script to run on containers icon.");
066
067        eventType = new AreaEventType(this, "eventType");
068
069        script = new StringParameter(this, "script");
070        TextStyle style = new TextStyle(script, "style");
071        style.height.setExpression("5");
072
073        evaluateScript = new Parameter(this, "evaluateScript");
074        evaluateScript.setTypeEquals(BaseType.BOOLEAN);
075        evaluateScript.setExpression("true");
076    }
077
078    ///////////////////////////////////////////////////////////////////
079    ////                         parameters                        ////
080
081    /** Parameter indicating whether the script text's expression should be
082     *  evaluated or not.  Ptolemy interprets the dollar sign to indicate that
083     *  the value of a parameter should be inserted; for example, $pi and
084     *  a variable pi=3.14 would evaluate to 3.14.  However, many scripting
085     *  languages such as jQuery use a dollar sign as part of the script itself
086     *  (to refer to jQuery variables).  In this case, we want to insert the
087     *  exact plain text into the web page, not the evaluated text.
088     */
089    public Parameter evaluateScript;
090
091    /** Event type to respond to by executing the command given by
092     *  the value of this Script parameter.
093     *  The script will be run when the icon corresponding to the
094     *  container of this parameter gets one of the following events:
095     *  <ul>
096     *  <li><b>onblur</b>: Command to be run when an element loses focus.
097     *  <li><b>onclick</b>: Command to be run on a mouse click.
098     *  <li><b>ondblclick</b>: Command to be run on a mouse double-click.
099     *  <li><b>onfocus</b>: Command to be run when an element gets focus.
100     *  <li><b>onmousedown</b>: Command to be run when mouse button is pressed.
101     *  <li><b>onmousemove</b>: Command to be run when mouse pointer moves.
102     *  <li><b>onmouseout</b>: Command to be run when mouse pointer moves out of an element.
103     *  <li><b>onmouseover</b>: Command to be run when mouse pointer moves over an element.
104     *  <li><b>onmouseup</b>: Command to be run when mouse button is released.
105     *  <li><b>onkeydown</b>: Command to be run when a key is pressed.
106     *  <li><b>onkeypress</b>: Command to be run when a key is pressed and released.
107     *  <li><b>onkeyup</b>: Command to be run when a key is released.
108     *  </ul>
109     *  These are the events supported by the HTML area tag.
110     *  The default is "onmouseover".
111     */
112    public AreaEventType eventType;
113
114    /** Script to insert in the head section of the
115     *  web page. This will normally define a JavaScript function that
116     *  will be invoked when the UI event specified by <i>eventType</i>
117     *  occurs. By default, this is blank. For example, if the value
118     *  of this parameter is the string
119    <pre>
120    function writeText(text) {
121    document.getElementById("xyz").innerHTML = text;
122    };
123    </pre>
124     * and the value of this parameter is "writeText('hello world')",
125     * then the HTML element with ID xyz will be populated with the
126     * string 'hello world' when the UI action <i>eventType</i> occurs.
127     */
128    public StringParameter script;
129
130    ///////////////////////////////////////////////////////////////////
131    ////                         public methods                    ////
132
133    /** Script is of type text/javascript for backwards compatibility.
134     *  return string text/javascript
135     */
136    // FIXME:  Any other types of scripts?
137    // FIXME:  Difference between these mime types?
138    // Some info here:  http://stackoverflow.com/questions/6122905/whats-is-difference-between-text-javascript-and-application-javascript
139    // .js     application/x-javascript
140    // .js     application/javascript
141    // .js     application/ecmascript
142    // .js     text/javascript
143    // .js     text/ecmascript
144    @Override
145    public String getMimeType() {
146        return "text/javascript";
147    }
148
149    /** Return true, since new scripts and method calls should overwrite old.
150     *
151     * @return True, since new scripts and method calls should overwrite old
152     */
153    @Override
154    public boolean isOverwriteable() {
155        return true;
156    }
157}