Window ‹ Chocolat API

[Setup]

Window()

Create a new window.

.run()

Set up and display the window.

Example

var win = new Window();
win.html = "<!DOCTYPE html><h1>Test</h1>";
win.buttons = ["OK"];
win.onButtonClick = function() { win.close(); }
win.run();
win.maximize();

.buttons

Get or set an Array of button names to be shown at the bottom of the window. Buttons are shown from right-to-left.

Returns Array – the names of the buttons. Note that you cannot mutate the return value of this property, you must set it for it to be updated.

Example

var win = new Window();
win.buttons = ["OK", "Cancel"];

.htmlPath

Get or set a path to the HTML file that will be shown in the window. Can be either absolute or relative. Relative paths are relative to the mixin's directory.

Returns String – the path to the HTML file.

Example

var win = new Window();
win.htmlPath = "index.html";

.html

Set the source of HTML file that will be shown in the window. Mutually exclusive with .htmlPath.

Returns String – the path to the HTML file.

Example

var win = new Window();
win.html = "<!DOCTYPE html><h1>Test</h1>";

.url

Set the URL of the website or file that will be shown in the window. Mutually exclusive with .htmlPath and .html. Must include a protocol.

Returns String – the url

Example

var win = new Window();
win.url = "http://www.google.com";

.useDefaultCSS

If set to true, the window will inject a <link href="default.css"> element into the head element. Default is true.

Returns Boolean – whether default.css will be used.

[Basics]

.title

Get or set the title of the window.

Returns String – the title of the window.

Example

var win = new Window();
win.title = "My window";

.frame

Get or set the window's frame. Setter is equivalent to .setFrame(rect, false).

Returns Rect – the window's frame.

.setFrame(newFrame, shouldAnimate)

Set the window's frame. The frame should be an object with the x, y, width and
height properties. e.g. {x: 0, y: 0, width: 250, height: 300}

  • newFrame Rect – the new window's frame.
  • shouldAnimate Bool – optional, whether to animate the resizing or not (default: false)

.center()

Center the window on screen. Must be called after run() has been invoked.

.maximize()

Maximize the window so that it takes up the entire size of the screen.

[Visibility]

.close()

Permanently close the window. After a window is closed all resources are freed. Use .hide() to temporarily hide a window.

.show()

Show the window if it was previously hidden.

.hide()

Hide the window offscreen but don't close it.

.toggleShown()

Hides a visible window or show a hidden window.

Returns Boolean – whether the window is *now* onscreen.

.isVisible()

Is this window onscreen?

Returns Boolean – whether the window is onscreen.

.isKeyWindow()

Is this the key window? The key window receives key events.

Returns Boolean – whether the window is the key window.

.isMainWindow()

Is this the main window?

Returns Boolean – whether the window is the main window.

[Minimization]

.minimize()

Minimize the window into the dock.

.unminimize()

Unminimize the window out of the dock.

.isMinimized()

Return whether the window is minimized.

Returns Boolean – whether the window is minimized.

[Events]

.onButtonClick

Get or set a callback function that will be called when a button is clicked.

Returns Function(String) – a callback function with one argument: the name of the button that was clicked.

Example

var win = new Window();
win.buttons = ["OK", "Cancel"];
win.onButtonClick = function (buttonName) {
    console.log("Clicked " + buttonName);
}

.onMessage

Get or set a callback function that will be called when the client JS sends a message.

Returns Function(String or – Array or Object)} a callback function with two arguments: the first is the name of the message, the second is the arguments passed to it

Example

// Client code
window.sendMessage("hello", [1, 2, 3]);

// Server code
win.onMessage = function (name, arguments) {
    // name == "hello", arguments == [1, 2, 3]
}

.onLoad

Get or set a function that will be called in the client context when the page loads. Equivalent to setting an onload attribute. Useful if using default.html to build up the page exclusively in JS.

Returns Function – a function that will run in the client JS. Referencing anything in the server JS won't work.

Example

var win = new Window()
win.htmlPath = "default.html";
win.clientOnLoad = function () {
    document.body.innerHTML = "<h1>Hello World</h1>";
};
win.run();

.onLoad

Get or set a function that will be called in the node context when the window loads.

Returns Function – a function that will be run when the web view loads.

Example

var win = new Window()
win.htmlPath = "default.html";
win.onLoad = function () {
    ...
};
win.run();

.onUnload

Get or set a function that will be called in the node context when the window unloads.

Returns Function – a function that will be run when the web view unloads.

Example

var win = new Window()
win.htmlPath = "default.html";
win.onUnload = function () {
    ...
};
win.run();

[Communication]

.evalCode(code)

Eval some code on the client-side.

  • code String – some code to evaluate on the client-side.

Example

win.evalCode("document.write('Some text')");

.evalExpr(code)

Eval some code on the client-side, and return a value. The .evalExpr function must wrap your code in order to extract a value from it. So it is not suitable for defining variables, etc. Use .evalCode if you don't need a return value.

  • code String – some code to evaluate on the client-side.

Returns Value – the return value of the code. Must be a json-able type.

Example

console.log(win.evalExpr("document.body.innerHTML"));

.addFunction(code)

Add a function to the client-side JS.

  • code String – some code to evaluate on the client-side.

Example

win.eval("document.write('Some text')");

.sendMessage(msg, args)

Send a message to the window that you can catch with the window.onMessage attribute.

  • msg String – the name of the message to send.
  • args Value – an argument to pass to the callback function.

Example

win.sendMessage("I'm sending a message", [42]);

.applyFunction(f, args)

Call a named function or a function literal on the client side.

  • f String or Function – either the name of a client function, or a function literal to call in the client context.
  • args Array – a list of arguments to pass to the function.

Example

win.applyFunction("updateData", [42, 3.14, 2.71828]);

win.applyFunction(function(data) {
    document.write(data.join("<br>"));
}, [42, 3.14, 2.71828]);