OpenPlatform / 03. Client-side library
Updated: 12. October 2018
Author: Peter Širka

03. Client-side library

Professional Support Chat with contributors

OpenPlatform offers a small client-side library called openplatform.js. The application can obtain user/meta-data from OpenPlatform and it supports another features for OpenPlatform manipulation.

Initialization

The code below describes OpenPlatform initialization in your web application. OPENPLATFORM and OP are global variables defined in window scope.

<script src="https://cdn.totaljs.com/openplatform.min.js"></script>
<script>

    OP.init(function(err, response, redirectTimeout) {

        if (err) {
            document.body.innerHTML = '401: Unauthorized';
            return;
        }

       // response === meta-data
        OP.notify('Hello world!');
    });

</script>

Methods

  • OP.init(callback(err, response, redirectTimeout)) - initializes OpenPlatform
  • OP.loading(visible, [sleep]) - toggles preloading (animation)
  • OP.meta(callback(err, response)) - reads meta data again
  • OP.play(url) - plays a sound
  • OP.stop(url) - stops the playing sound
  • OP.maximize() - maximizes this application in OpenPlatform
  • OP.minimize() - minimizes this application in OpenPlatform
  • OP.restart() - restarts this application
  • OP.open(appid, [data]) - opens another application
  • OP.close() - closes/kills this running application
  • OP.notify([type], body, [custom_data]) - sends a push notification
  • OP.confirm(msg, buttons, callback(button_index)) shows a confirm message
  • OP.message(msg, [type], [button_label]) - shows a message box, types: success (default) and warning
  • OP.success(message, [button_label]) is a alias for OP.message()
  • OP.warning(message, [button_label]) is a alias for OP.message()
  • OP.snackbar(msg, [type], [button_label]) - shows a snackbar, types: success (default) and warning
  • OP.focus() - focuses the current app
  • OP.badge() - shows a badge in the app icon
  • OP.log(msg) - appends a log on the server-side
  • OP.email(subject, html_body) - sends an email
  • OP.share(app/appid, type, body, [callback]) shares data in another app
  • OP.progress(percentage) - shows a progress bar if the percentage > 0 and < 100
// OPENPLATFORM.notify([type], body, [url]);
// "type" {Number} 0 - info, 1 - success, 2 - error
// "body" {String}
// "url" {String} can be relative

// Examples:
OP.notify(0, 'Info');
OP.notify(1, 'Success', '/orders/101210/');
OP.notify(2, 'Error/Warning', '/reports/');

OP.confirm('Are you sure you want to remove selected order?', ['Yes', 'Cancel'], function(index) {
   // index === Button index, 0 = yes, 1 = cancel
   console.log(index); 
});

Events

OP.on('maximize', function() {
    // App is maximized
});

OP.on('minimize', function() {
    // App is minimized
});

OP.on('close', function() {
    // App is closed
});

// Is executed if the user will click on the notification
OP.on('notify', function(data) {

    // A raw data from the notification
    // {String/Number/Object}
    data;

});

// Is executed if the user will send a screenshot to this app
// IMPORTANT: app must have enabled "allowscreenshots" in "openplatform.json"
OP.on('screenshot', function(data) {

    // From which app is the screenshot?
    // {Object}
    data.app;

    // Picture .jpeg
    // {String} base64
    data.data;

});

// Is executed if the app shares some data to this app
OP.on('share', function(data) {
    // Some app wants to share some data

    // {String} sender/app ID
    data.app;

    // Type of message
    // {String/Number}
    data.type;

    // Message body/data
    // {Object}
    data.body;

    // {Date}
    data.datecreated;    

    // You can send a data back via this method:
    data.share(type, body);

});