Class NativeJS

Object
NativeJS

public class NativeJS extends Object
Collection of native javascript methods.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Private default constructor to ensure non-instantiability.
  • Method Summary

    Modifier and Type
    Method
    Description
    static final void
    JSNI method: Add a fullscreen change handler.
    static void
    JSNI method: Display javascript alert panel - use sparingly.
    static String
    JSNI method: decoding of base-64 encoded string.
    static String
    JSNI method: base-64 encoding of string.
    createSVGContext(int width, int height)
    JSNI method: Create a SVG context for exporting the view.
    static final boolean
    JSNI method: test if ePub reader supports feature.
    static void
    JSNI method: Exit fullscreen mode.
    static void
    export(String dataURL, String filename)
    JSNI method: encourage browser to download dataURL as a file named filename.
    static void
    JSNI method: Export the SVG context.
    static final void
    focusOn(Element element)
    JSNI method: focus on element.
    static String
    JSNI method: Determine name of the fullscreen change event in current web browser.
    static final int
    JSNI method: return the pixel ratio of the current device.
    static final String
    JSNI method: Retrieve string identifying the ePub reading system: web browser: null iBooks: iBooks, Apple Books Adobe Digital Editions 4.5.8 (macOS): RMSDK Readium: no JS (and no display equations!) TEA Ebook 1.5.0 (macOS): no JS (and issues with MathML) calibre: calibre-desktop (supports JS and MathML but appears unstable) Note: nonlinear content in Apple Books on iOS does not report as an ePub reading system (at least on the iPad).
    static final Element
    JSNI method: Gets fullscreen element if in fullscreen mode or null if not in fullscreen or fullscreen not supported by web browser.
    static final void
    JSNI method: the HTML5 File API enables reading of files.
    static final boolean
    JSNI method: Check if document has focus.
    static final boolean
    JSNI method: Check if execution environment supports keyboard events.
    static final boolean
    JSNI method: Check if execution environment supports mouse events.
    static final boolean
    JSNI method: Check if execution environment supports touch events.
    static final boolean
    JSNI method: check if element is active.
    static final boolean
    JSNI method: Check if graph is displayed in an ePub reading system.
    static final boolean
    JSNI method: Check if the document is in fullscreen mode.
    static boolean
    JSNI method: Check if fullscreen mode is supported.
    static final boolean
    JSNI method: check whether a single plist files was dropped.
    static boolean
    JSNI method: check whether WebGL and hence 3D graphics are supported.
    static final boolean
    JSNI method: Test whether loaded from an XHTML document.
    static final NodeList<Element>
    JSNI method: expose convenient javascript method to obtain a list of elements in the DOM that match the selection criterion selector.
    static final void
    JSNI method: Remove the fullscreen change handler.
    static void
    JSNI method: Request fullscreen mode for the element ele.
    static final void
    JSNI method: opens javascript file chooser and attempts to restore state from selected file

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • NativeJS

      private NativeJS()
      Private default constructor to ensure non-instantiability.
  • Method Details

    • alert

      public static void alert(String msg)
      JSNI method: Display javascript alert panel - use sparingly.
      Parameters:
      msg - the message to display
    • b64encode

      public static String b64encode(String b)
      JSNI method: base-64 encoding of string.
      Parameters:
      b - string to encode
      Returns:
      encoded string
    • b64decode

      public static String b64decode(String a)
      JSNI method: decoding of base-64 encoded string.
      Parameters:
      a - string to decode
      Returns:
      decoded string
    • querySelectorAll

      public static final NodeList<Element> querySelectorAll(String selector)
      JSNI method: expose convenient javascript method to obtain a list of elements in the DOM that match the selection criterion selector.
      Parameters:
      selector - the criterion for selecting elements in DOM
      Returns:
      list of elements that match selector
    • isElementActive

      public static final boolean isElementActive(Element element)
      JSNI method: check if element is active.
      Parameters:
      element - the element to check
      Returns:
      true if element is active
    • focusOn

      public static final void focusOn(Element element)
      JSNI method: focus on element.
      Parameters:
      element - the element to focus on
    • hasFocus

      public static final boolean hasFocus()
      JSNI method: Check if document has focus. This is important to avoid showing tooltips when the browser window is not in focus.
      Returns:
      true if document has focus
    • getDevicePixelRatio

      public static final int getDevicePixelRatio()
      JSNI method: return the pixel ratio of the current device. This is intended to prevent distortions on the canvas objects of the data views.
      Returns:
      the pixel ratio of device
    • isFullscreenSupported

      public static boolean isFullscreenSupported()
      JSNI method: Check if fullscreen mode is supported.
      Returns:
      true if fullscreen supported
    • requestFullscreen

      public static void requestFullscreen(Element ele)
      JSNI method: Request fullscreen mode for the element ele.
      Parameters:
      ele - the element to request fullscreen mode for
    • isFullscreen

      public static final boolean isFullscreen()
      JSNI method: Check if the document is in fullscreen mode.
      Returns:
      true if the document is in fullscreen mode
    • getFullscreenElement

      public static final Element getFullscreenElement()
      JSNI method: Gets fullscreen element if in fullscreen mode or null if not in fullscreen or fullscreen not supported by web browser.
      Returns:
      fullscreen element or null
    • exitFullscreen

      public static void exitFullscreen()
      JSNI method: Exit fullscreen mode.
    • fullscreenChangeEventName

      public static String fullscreenChangeEventName()
      JSNI method: Determine name of the fullscreen change event in current web browser.

      Note: Chrome implements both fullscreenchange and webkitfullscreenchange but with slightly different behaviour (neither identical to Safari). fullscreenchange at least works for a single graph and hence give it precedence. For Firefox scaling/resizing issues remain as well as for Chrome with multiple graphs.

      Returns:
      web browser specific fullscreen change event name or null if Fullscreen API not implemented.
    • addFullscreenChangeHandler

      public static final void addFullscreenChangeHandler(String eventname, FullscreenChangeHandler handler)
      JSNI method: Add a fullscreen change handler.
      Parameters:
      eventname - the name of the fullscreen change event
      handler - the handler to add
      Implementation Notes:
      The JSNI routine works reasonably well with Safari but not with all other browsers because aspects of the fullscreen API are interpreted differently, see fullscreenChangeEventName().
    • removeFullscreenChangeHandler

      public static final void removeFullscreenChangeHandler(String eventname, FullscreenChangeHandler handler)
      JSNI method: Remove the fullscreen change handler.
      Parameters:
      eventname - the name of the fullscreen change event
      handler - the handler to add
      Implementation Notes:
      The handler function needs to be specified again when removing the listener... Because we don't know how to store the handler returned by the JSNI method _addFullscreenChangeHandler(String, FullscreenChangeHandler) it must be exact copy of handler specification there.
    • isWebGLSupported

      public static boolean isWebGLSupported()
      JSNI method: check whether WebGL and hence 3D graphics are supported.

      Note: asssumes that canvas is supported

      Returns:
      true if WebGL is supported
    • isValidDnD

      public static final boolean isValidDnD(JavaScriptObject dataTransfer)
      JSNI method: check whether a single plist files was dropped.

      Note: This check needs to be done in native javascript because the DataTransfer object returned by the onDrop handler cannot be read using GWT routines.

      Parameters:
      dataTransfer - list of dropped file(s)
      Returns:
      true if the dropped file looks promising and ok for further processing
    • handleDnD

      public static final void handleDnD(JavaScriptObject dataTransfer, EvoLudoWeb gui)
      JSNI method: the HTML5 File API enables reading of files. Take advantage of functionality to read contents of dropped file.

      Note: isValidDnD(JavaScriptObject) should be called first to ensure that only a single 'plist' file was dropped.

      Parameters:
      dataTransfer - the list of dropped file(s)
      gui - the user interface that processes dropped file
    • restoreFromFile

      public static final void restoreFromFile(EvoLudoWeb gui)
      JSNI method: opens javascript file chooser and attempts to restore state from selected file
      Parameters:
      gui - the user interface that processes the contents of selected file
    • createSVGContext

      public static AbstractGraph.MyContext2d createSVGContext(int width, int height)
      JSNI method: Create a SVG context for exporting the view.

      Note: Requires that the Canvas2SVG.js script has been injected, e.g. using something like ScriptInjector.fromString(Resources.INSTANCE.canvas2SVG().getText()).inject();

      Parameters:
      width - the width of the context
      height - the height of the context
      Returns:
      the SVG context
    • exportSVG

      public static void exportSVG(Context2d ctx)
      JSNI method: Export the SVG context.
      Parameters:
      ctx - the SVG context to export
    • export

      public static void export(String dataURL, String filename)
      JSNI method: encourage browser to download dataURL as a file named filename.

      Note: # characters cause trouble in data URL. Escape them with %23.

      Parameters:
      dataURL - the file content encoded as data URL
      filename - the name of the downloaded file
    • isXML

      public static final boolean isXML()
      JSNI method: Test whether loaded from an XHTML document.

      Note: GWT interferes here and both Document.get().createElement("div").getTagName()=="DIV" as well as $doc.createElement("div").tagName == "DIV" falsely (always?) return false.

      Returns:
      true if XML document
    • isEPub

      public static final boolean isEPub()
      JSNI method: Check if graph is displayed in an ePub reading system.
      Returns:
      true if ePub reading system.
    • getEPubReader

      public static final String getEPubReader()
      JSNI method: Retrieve string identifying the ePub reading system:
      • web browser: null
      • iBooks: iBooks, Apple Books
      • Adobe Digital Editions 4.5.8 (macOS): RMSDK
      • Readium: no JS (and no display equations!)
      • TEA Ebook 1.5.0 (macOS): no JS (and issues with MathML)
      • calibre: calibre-desktop (supports JS and MathML but appears unstable)
      Note: nonlinear content in Apple Books on iOS does not report as an ePub reading system (at least on the iPad). Apple Books on macOS behaves as expected.
      Returns:
      identification string of ePub reading system or null if no reading system or reading system unknown
    • ePubReaderHasFeature

      public static final boolean ePubReaderHasFeature(String feature)
      JSNI method: test if ePub reader supports feature. in ePub 3 possible features are:
      dom-manipulation
      Scripts MAY make structural changes to the document’s DOM (applies to spine-level scripting only).
      layout-changes
      Scripts MAY modify attributes and CSS styles that affect content layout (applies to spine-level scripting only).
      touch-events
      The device supports touch events and the Reading System passes touch events to the content.
      mouse-events
      The device supports mouse events and the Reading System passes mouse events to the content.
      keyboard-events
      The device supports keyboard events and the Reading System passes keyboard events to the content.
      spine-scripting
      Indicates whether the Reading System supports spine-level scripting (e.g., so a container-constrained script can determine whether any actions that depend on scripting support in a Top-level Content Document have any chance of success before attempting them).
      Parameters:
      feature - the ePub feature to test
      Returns:
      true if feature supported
      See Also:
    • hasKeys

      public static final boolean hasKeys()
      JSNI method: Check if execution environment supports keyboard events.
      Returns:
      true if keyboard events are supported
    • hasMouse

      public static final boolean hasMouse()
      JSNI method: Check if execution environment supports mouse events.
      Returns:
      true if mouse events are supported
    • hasTouch

      public static final boolean hasTouch()
      JSNI method: Check if execution environment supports touch events.
      Returns:
      true if touch events are supported