summaryrefslogtreecommitdiff
path: root/wikipedia/src/views/wikipedia_view.js
blob: 09ef6fe8813cab2639b8749c5d1faf17380af2ec (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
const Gtk = imports.gi.Gtk;
const Lang = imports.lang;
const Soup = imports.gi.Soup;
const WebKit = imports.gi.WebKit2;
const Utils = imports.utils;

const getPageURL = "http://localhost:3000/getArticleByTitle?title=";

const WikipediaView = new Lang.Class({
    Name: 'EndlessWikipediaView',
    Extends: WebKit.WebView,

    _init: function(params) {
        this._httpSession = new Soup.Session();
        this.parent(params);
        // For debugging
        // let settings = this.get_settings();
        // settings.set_enable_developer_extras(true);
        // this.set_settings(settings);
        this._is_first_time = true;
    },

    loadArticleByTitle: function(title) {
        let request = Soup.Message.new("GET", getPageURL + title);
        this._httpSession.queue_message(request, Lang.bind(this, function(_httpSession, message) {
            if(message.status_code !== 200) {
                print(message.status_code);
                return;
            }
            let articleJSON = request.response_body.data;
            let article = JSON.parse(articleJSON);
            let articleHTML = article["text"];
            let title = article['title'];
            let skeletonHTML = Utils.load_file("views/index.html");
            skeletonHTML = skeletonHTML + "<div id='wiki_content' name='"+ title +"'>" + articleHTML + "</div>"
            Utils.write_contents_to_file("views/temp.html", skeletonHTML);

            // TODO: Ask about how we can load directly from HTML. Right now, WebKit can't seem to open 
            // CSS file correctly. All characters in CSS file are in Chinese

            if(this._is_first_time) {
                this.load_uri("file:///home/endless/checkout/eos-sdk/wikipedia/src/views/temp.html", null);
                this._is_first_time = false;
            } else {
                this.reload();
            }
        }));
    }
});