Sphere Engine Compilers Widget integration

Before the widget can be integrated, it is necessary to create and configure it. Please follow this quickstart tutorial to create and configure your Sphere Engine Compilers Widget.

Important: To successfully integrate Sphere Engine Compilers Widget you need to have a working Sphere Engine account. Register for a Sphere Engine account by filling in the sign-up form.

Standalone page

To embed the widget on a separate website located in the sphere-engine.com domain use the feature of presenting it as a "standalone page".

A link to a standalone page is generated in the Sphere Engine client panel when creating the widget: Menu > Compilers > Widgets > WIDGET HASH > Widget integration. It has the following structure:

https://compilers.widgets.sphere-engine.com/lp?hash=<hash>

JavaScript integration

When using this method, you need to insert two snippets of HTML code responsible for the initialization and display of the widget.

Initializing the widget

The code responsible for widget initialization should be inserted only once (regardless of the number of widgets you want to display on the page), immediately after the <body> element.

A customized and ready-to-use widget initialization code can be found in Sphere Engine client panel: Menu > Compilers > Widgets > WIDGET HASH > Widget integration:

<script>SEC_HTTPS = true;
SEC_BASE = "<customized_link>"; 
(function(d, s, id){ SEC = window.SEC || (window.SEC = []);
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; 
  js.src = (SEC_HTTPS ? "https" : "http") + "://" + SEC_BASE + "/static/sdk/sdk.js";
  fjs.parentNode.insertBefore(js, fjs);   
}(document, "script", "sphere-engine-compilers-jssdk"));
</script>

Displaying the widget

The following code should be inserted on the page in the spot where you want the widget to be displayed:

<div class="sec-widget" data-widget="<hash>"></div>

The hash parameter which is the value of the data-widget attribute is the unique ID of the widget in the Sphere Engine system. It can be found under the Unique hash field in Sphere Engine client panel: Menu > Compilers > Widgets > WIDGET HASH > Widget settings.

Displaying multiple widgets

Sphere Engine Compilers Widget allows you to embed multiple widgets on a single page.

If you need to manipulate widget's behavior using JavaScript (see JavaScript SDK) it is necessary to identify it, which is done by means of a special identifier (marked as <id>) denoted by the data-id attribute of the div element used to embed the widget. For example:

<div id="idInHTML" data-id="<id>" class="sec-widget" data-widget="<hash>"></div>

Note: The widget is initialized automatically after the page loads in the browser, even if it is not yet visible on the page. This makes performing operations on the widget possible (e.g. configuration) before displaying it.

Note: The process of loading (and displaying) the widget begins only when the widget becomes visible on the screen (i.e. when the user scrolls the page to the widget's position). Thanks to this, the loading of other elements on the page is not blocked. This allows for the page to load faster, which is especially useful when embedding multiple widgets.

Dark theme

Sphere Engine Compilers Widget is also available in the Dark theme.

To change the theme, please add the data-theme attribute to the div element used to embed the widget together with the name of the theme - dark.

<div class="sec-widget" data-widget="<hash>" data-theme="dark"></div>

JavaScript SDK

The Sphere Engine JavaScript SDK library for Compilers widget (or simply JavaScript SDK) is loaded asynchronously. For this reason, before using the functions provided by the library, we must be sure that it has been initialized.

First, place the following snippet just after the Sphere Engine Compilers Widget:

<script>SEC.ready=function(e){"loading"!=document.readyState&&"interactive"!=document.readyState?e():window.addEventListener("load",e)};</script>

As a result, the initialization code should look as follows:

<script>SEC_HTTPS = true;
SEC_BASE = "<customized_link>"; 
(function(d, s, id){ SEC = window.SEC || (window.SEC = []);
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; 
  js.src = (SEC_HTTPS ? "https" : "http") + "://" + SEC_BASE + "/static/sdk/sdk.js";
  fjs.parentNode.insertBefore(js, fjs);   
}(document, "script", "sphere-engine-compilers-jssdk"));
SEC.ready=function(e){"loading"!=document.readyState&&"interactive"!=document.readyState?e():window.addEventListener("load",e)};
</script>

The above code defines the SEC.ready() function which ensures that features utilizing JavaScript SDK will be used only after all the required components have been loaded.

All operations that use JavaScript SDK should be performed through the SEC.ready() function, i.e. according to the following scheme:

SEC.ready(function() {
    // Sphere Engine JavaScript SDK usage
});

Using the SEC.ready() function ensures that the library has been loaded, the SEC object has been initialized and no errors will occur while scripts are being executed.

Access

We gain access to the widget by using the SEC.widget() function, which (basing on the given widget ID, previously defined by the data-id attribute) returns the object of SECWidget class used to manage the widget. For example:

SEC.ready(function() {
    var widget = SEC.widget("widget");
    // variable "widget" is ready to use
});

The SECWidget class provides the following options for managing the widget:

  • creating a widget,
  • dedicated events for handling actions performed by the widget.

Methods

The global SEC object provides public methods for managing widgets.

METHOD SEC.widget(id)

An object of class SECWidget, which represents the widget, is created.

Parameters
Name Type Description
id * string

widget's identifier placed in the HTML document (data-id attribute)

Returned value

An object of class SECWidget is returned.

Example
Press the button to load the widget
Load widget
<div data-id="example-widget" data-widget="{widget_hash}"></div>
<script>
    SEC.ready(function() {
        $("#btn-load-widget").on("click", function(e) {
            e.preventDefault();
            var SECWidget = SEC.widget("example-widget");
        });
    });
</script>
<a href="#" id="btn-load-widget" class="btn btn-default">Load widget</a>

METHOD SECWidget.config(config)

Setting the widget's configuration data.

Parameters
Name Type Description
config.code.compiler integer

programming language identifier (see. list of languages)

config.code.source string

program's source code

config.code.input string

input data

config.compilers.list array[int]

list of language identifiers (see. list of languages)

Example
<div data-id="example-widget" data-widget="{widget_hash}"></div>
<script>
    SEC.ready(function() {
        var SECWidget = SEC.widget("example-widget");
        SECWidget.config({
            code: {
                compiler: 28,
                source: "#!/bin/bash\n\necho test",
                input: "",
            },
            compilers: {
                list: [11, 21, 28, 33]
            }
        });
    });
</script>

METHOD SECWidget.loadSourceCode(compiler, sourceCode, input)

Loading data (source code, programming language, input data) to the running editor.

Parameters
Name Type Description
compiler integer

programming language identifier (see. list of languages)

sourceCode string

program's source code

input string

input data

Example
<div data-id="example-widget" data-widget="{widget_hash}"></div>
<script>
    SEC.ready(function() {
        var sourceCode = "<?php \n\n$hi = fopen('php://stdin', \"r\");\nfscanf($hi, \"%d\", $n);\n\
fclose($hi);\n\necho 'number: ' . $n;";
        var SECWidget = SEC.widget("example-widget");
        SECWidget.loadSourceCode(29, sourceCode, "5");
    });
</script>

METHOD SECWidget.setCompilers(compilersList)

Selection of programming languages available in the widget.

Parameters
Name Type Description
compilersList array[int]

list of language identifiers (see. list of languages)

Example
<div data-id="example-widget" data-widget="{widget_hash}"></div>
<script>
    SEC.ready(function() {
        var SECWidget = SEC.widget("example-widget");
        SECWidget.setCompilers([1,2]);
    });
</script>

METHOD SECWidget.events.subscribe(event, callback)

Assigns a function to the event (see list of events).

Parameters
Name Type Description
event * string

the name of the event to assign a function to

callback * function

function to be called on an event

Example
<div data-id="example-widget" data-widget="{widget_hash}"></div>
<script>
    SEC.ready(function() {

        var callback = function(data) {
            // Your code goes here
        };

        var SECWidget = SEC.widget("example-widget");
        SECWidget.events.subscribe('{name_of_the_event}', callback);

    });
</script>

METHOD SECWidget.events.unsubscribe(event, callback)

Removes a function from the list of functions assigned to the event (see list of events).

Parameters
Name Type Description
event * string

the name of the event to remove the function from

callback * function

function to be removed from the list of functions assigned to the event

Example
<div data-id="example-widget" data-widget="{widget_hash}"></div>
<script>
SEC.ready(function() {

    var callback = function(data) {
        // Your code goes here
    };

    var SECWidget = SEC.widget("example-widget");
    SECWidget.events.unsubscribe('{name_of_the_event}', callback);

});
</script>

Events

An object of the SECWidget class has a collection of dedicated events called at specific moments of the widget's operation.

EVENT beforeSendSubmission

The event is invoked before sending a submission to the Sphere Engine system (i.e. after pressing the Submit button but before actually sending the submission).

Data sent to the callback function

An object with the following fields is sent to the callback function

Name Type Description
submissionSource string

the source code of the program sent in the submission

submissionLanguage string

the programming language used to write the program

submissionInput string

input data sent in the submission

The value returned by the callback function

The value returned by the callback function determines whether the submission will be sent to the Sphere Engine system:

  • true - the submission is to be sent,
  • false - the submission will be stopped from being sent.
Example
Data:
Submit submission to see the result
<div data-id="example-widget" data-widget="{widget_hash}"></div>
<script>
    SEC.ready(function() {
        var beforeSendSubmission = function(data) {
            $('#result').html('')
                .append("> submissionLanguage: " + data.submissionLanguage + "<br><br>")
                .append("> submissionInput: <br>" + data.submissionInput + "<br><br>")
                .append("> submissionSource: <br>" + data.submissionSource + "<br><br>");
            return true;
        };
        var SECWidget = SEC.widget("example-widget");
        SECWidget.events.subscribe('beforeSendSubmission', beforeSendSubmission);
    });
</script>
Data:
<pre id="result" style="height: 300px;">Submit submission to see the result</pre>

EVENT afterSendSubmission

The event is invoked immediately after the submission has been sent to the Sphere Engine system.

Data sent to the callback function

An object with the following fields is sent to the callback function

Name Type Description
submissionId integer

submission identifier in the Sphere Engine system

Example
Data:
Submit submission to see the result
<div data-id="example-widget" data-widget="{widget_hash}"></div>
<script>
    SEC.ready(function() {
        var afterSendSubmission = function(data) {
            $('#result').html('')
                .append("> submissionId: " + data.submissionId + "<br><br>");
        };
        var SECWidget = SEC.widget("example-widget");
        SECWidget.events.subscribe('afterSendSubmission', afterSendSubmission);
    });
</script>
Data:
<pre id="result" style="height: 100px;">Submit submission to see the result</pre>

EVENT checkStatus

The event is invoked regularly while checking the status of the submission during execution.

Data sent to the callback function

An object with the following fields is sent to the callback function

Name Type Description
submissionId integer

submission identifier in the Sphere Engine system

submissionTime integer

execution time in seconds (available after execution)

submissionMemory integer

memory consumption in kilobytes (available after execution)

statusNumber integer

numeric value of the submission's status (see: list of statuses)

statusDescription string

description of the submission's status

Example
Data:
Submit submission to see the result
<div data-id="example-widget" data-widget="{widget_hash}"></div>
<script>
    SEC.ready(function() {
        var checkStatus = function(data) {
            $('#result')
                .append("<br><br>")
                .append("> submissionId: " + data.submissionId + "<br>")
                .append("> statusNumber: " + data.statusNumber + "<br>")
                .append("> statusDescription: " + data.statusDescription + "<br>")
                .append("> submissionMemory: " + data.submissionMemory + "<br>")
                .append("> submissionTime: " + data.submissionTime + "<br>");
            $('#result').scrollTop($('#result').prop("scrollHeight"));
        };
        var SECWidget = SEC.widget("example-widget");
        SECWidget.events.subscribe('checkStatus', checkStatus);
    });
</script>
Data:
<pre id="result" style="height: 300px;">Submit submission to see the result</pre>

EVENT enterFullScreen

The event is invoked when the widget is switched to full screen mode.

Example
<div data-id="example-widget" data-widget="{widget_hash}"></div>
<script>
    SEC.ready(function() {
        var enterFullScreen = function() {
            alert('enter fullscreen');
        };
        var SECWidget = SEC.widget("example-widget");
        SECWidget.events.subscribe('enterFullScreen', enterFullScreen);
    });
</script>

EVENT exitFullScreen

The event is invoked when leaving the full screen mode.

Example
<div data-id="example-widget" data-widget="{widget_hash}"></div>
<script>
    SEC.ready(function() {
        var exitFullScreen = function() {
            alert('exit fullscreen');
        };
        var SECWidget = SEC.widget("example-widget");
        SECWidget.events.subscribe('exitFullScreen', exitFullScreen);
    });
</script>