summaryrefslogtreecommitdiff
path: root/test/smoke-tests/webhelper/webview2.js
blob: f50c42d0eabdb88552e8a0e9a64a0eaedf1ecd24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2015 Endless Mobile, Inc.

const Endless = imports.gi.Endless;
const Gettext = imports.gettext;
const Gtk = imports.gi.Gtk;
const Lang = imports.lang;
const WebHelper2 = imports.webhelper2;
const WebKit2 = imports.gi.WebKit2;

const TEST_APPLICATION_ID = 'com.endlessm.example.test-webview2';
const TEST_HTML = '\
<html> \
<head> \
<title>First page</title> \
<style> \
p, form { \
    width: 50%; \
    padding: 1em; \
    background: #FFFFFF; \
} \
body { \
    background: #EEEEEE; \
} \
</style> \
</head> \
\
<body> \
<script type="text/javascript"> \
    window.ngettextButton = (function () { \
        var times = 0; \
        return function () { \
            times++; \
            var message = ngettext("You have clicked me __ time", "You have clicked me __ times", times); \
            alert(message.replace("__", times.toString())); \
        }; \
    })(); \
</script> \
<h1>First page</h1> \
\
<p><a href="webhelper://moveToPage?name=page2">Move to page 2</a></p> \
\
<p><a \
href="webhelper://showMessageFromParameter?msg=This%20is%20a%20message%20from%20the%20URL%20parameter">Show \
message from parameter in this URL</a></p> \
\
<form action="webhelper://showMessageFromParameter"> \
<input name="msg" value="I am in a form!"/> \
<input type="submit" value="Show message using a form"/> \
</form> \
\
<p><a href="http://wikipedia.org">Regular link to a Web site</a></p> \
\
<p>This is text that will be italicized: <span name="translatable">Hello, \
world!</span></p> \
\
<p> \
    <button onclick="alert(gettext(\'I came from gettext\'));"> \
        Click me to use gettext \
    </button> \
    <button onclick="ngettextButton();">Click me to use ngettext</button> \
</p> \
\
</body> \
</html>';

const TestApplication = new Lang.Class({
    Name: 'TestApplication',
    Extends: Endless.Application,

    vfunc_dbus_register: function (connection, object_path) {
        this._webhelper = new WebHelper2.WebHelper({
            well_known_name: this.application_id,
            connection: connection,
        });
        return this.parent(connection, object_path);
    },

    vfunc_startup: function () {
        this.parent();

        this._webhelper.set_gettext((s) => s.italics());
        this._webhelper.set_ngettext((s, p, n) =>
            '** ' + (n == 1 ? s : p) + ' **');
        this._webhelper.define_web_actions({
            moveToPage: this.moveToPage.bind(this),
            showMessageFromParameter: this.showMessageFromParameter.bind(this),
        });

        this._webview = new WebKit2.WebView();
        this._webview.connect('load-changed', (webview, event) => {
            if (event === WebKit2.LoadEvent.FINISHED)
                this._webhelper.translate_html(webview, null, (obj, res) => {
                    this._webhelper.translate_html_finish(res);
                });
        });
        this._webview.load_html(TEST_HTML, null);

        this._page2 = new Gtk.Grid();
        let back_button = new Gtk.Button({ label: 'Go back to page 1' });
        back_button.connect('clicked', () => {
            this._pm.visible_child_name = 'page1';
        });
        this._page2.add(back_button);

        this._window = new Endless.Window({
            application: this,
            border_width: 16,
        });

        this._pm = this._window.page_manager;
        this._pm.set_transition_type(Gtk.StackTransitionType.CROSSFADE);
        this._pm.add(this._webview, { name: 'page1' });
        this._pm.add(this._page2, { name: 'page2' });
        this._pm.visible_child_name = 'page1';

        this._window.show_all();
    },

    vfunc_dbus_unregister: function (connection, object_path) {
        this.parent(connection, object_path);
        this._webhelper.unregister();
    },

    // WEB ACTIONS

    // dict['name'] is the name of the page to move to
    moveToPage: function (dict) {
        this._pm.visible_child_name = dict['name'];
    },

    // dict['msg'] is the message to display
    showMessageFromParameter: function (dict) {
        let dialog = new Gtk.MessageDialog({
            buttons: Gtk.ButtonsType.CLOSE,
            message_type: Gtk.MessageType.INFO,
            text: dict['msg'],
            transient_for: this._window,
        });
        dialog.run();
        dialog.destroy();
    },
});

let app = new TestApplication({
    application_id: TEST_APPLICATION_ID,
});
app.run(ARGV);