summaryrefslogtreecommitdiff
path: root/wikipedia/PrebuiltCategoryPage.js
blob: bc5fe28f039d6563675ccbf953efe1c99d5fa6d9 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const Endless = imports.gi.Endless;
const GObject = imports.gi.GObject;
const Gtk = imports.gi.Gtk;
const Lang = imports.lang;

const CategoryBackButton = imports.wikipedia.widgets.category_back_button;
const EndlessWikipedia = imports.wikipedia.EndlessWikipedia;
const FixedSizeTextView = imports.wikipedia.widgets.FixedSizeTextView;

GObject.ParamFlags.READWRITE = GObject.ParamFlags.READABLE | GObject.ParamFlags.WRITABLE;

const PrebuiltCategoryPage = new Lang.Class({
    Name: 'PrebuiltCategoryPage',
    Extends: Gtk.Frame,
    Properties: {
        'title': GObject.ParamSpec.string('title',
            'Name of category',
            'Name of the category to be displayed at the top of the category page',
            GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT,
            ''),
        'description': GObject.ParamSpec.string('description',
            'Description',
            'Description of the category (excerpt from Wiki text)',
            GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT,
            ''),
        // resource URI for the category's accompanying image
        'image-uri': GObject.ParamSpec.string('image-uri',
            'Image URI',
            'Resource URI for the image file accompanying the category',
            GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT,
            '')
    },

    Signals: {
        'go-back-home':{}
    },

    _get_info_box: function(){
        this._title_label = new Gtk.Label({
            name: 'category-title',
            expand: false,
            halign: Gtk.Align.START,
            xalign: 0.0, // deprecated Gtk.Misc property; necessary because
            wrap: true,  // "wrap" doesn't respect "halign"
            width_chars: 15,
            max_width_chars: 18
        });

        let description_separator = new Gtk.Frame();
        description_separator.get_style_context().add_class(
            EndlessWikipedia.STYLE_CLASS_CATEGORY_HORIZONTAL_SEPARATOR);

        this._description_text_view = new FixedSizeTextView.FixedSizeTextView({
            name: 'category-description',
            sensitive: false,
            editable: false,
            cursor_visible: false,
            pixels_inside_wrap: 10,
            wrap_mode: Gtk.WrapMode.WORD
        });

        let description_scrolled_window = new Gtk.ScrolledWindow({
            name: 'category-scrolled-window',
            halign: Gtk.Align.FILL,
            vexpand: true,
            hscrollbar_policy: Gtk.PolicyType.NEVER,
            vscrollbar_policy: Gtk.PolicyType.AUTOMATIC
        });
        description_scrolled_window.add(this._description_text_view);

        // Would rather have a more lightweight widget here like alignment,
        // but alignment doesn't seem to respect padding and margin
        let description_alignment = new Gtk.Frame({
            name: 'category-description-align'
        });
        description_alignment.add(description_scrolled_window);

        let vertical_separator = new Gtk.Frame();
        vertical_separator.get_style_context().add_class(
            EndlessWikipedia.STYLE_CLASS_CATEGORY_VERTICAL_SEPARATOR);

        let grid = new Gtk.Grid();
        grid.attach(this._title_label, 0, 0, 1, 1);
        grid.attach(description_separator, 0, 1, 1, 1);
        grid.attach(description_alignment, 0, 2, 1, 1);
        grid.attach(vertical_separator, 1, 0, 1, 3);

        let frame = new Gtk.Frame({
            name: 'category-info',
            vexpand: true
        });
        frame.add(grid);
        return frame;
    },

    _init: function(props) {
        this._title = null;
        this._description = null;
        this._category_provider = new Gtk.CssProvider();
        this.parent(props);

        let info_box = this._get_info_box();

        let back_button = new CategoryBackButton.CategoryBackButton({
            name: 'category-back-button',
            expand: true,
            halign: Gtk.Align.START,
            valign: Gtk.Align.FILL
        });
        back_button.connect('clicked', Lang.bind(this, function() {
            this.emit('go-back-home');
        }));
        
        let vertical_separator = new Gtk.Frame();
        vertical_separator.get_style_context().add_class(
            EndlessWikipedia.STYLE_CLASS_CATEGORY_VERTICAL_SEPARATOR);

        let grid = new Gtk.Grid({
            orientation: Gtk.Orientation.HORIZONTAL
        });
        grid.add(back_button);
        grid.add(vertical_separator);
        grid.add(info_box);
        this.add(grid);

        // Add style contexts for CSS
        let context = this.get_style_context();
        context.add_class(EndlessWikipedia.STYLE_CLASS_CATEGORY_PAGE);
    },

    get title() {
        return this._title;
    },

    set title(value) {
        this._title = value;
        if(this._title_label)
            this._title_label.label = value.toUpperCase();
    },

    get description() {
        return this._description;
    },

    set description(value) {
        this._description = value;
        if(this._description_text_view)
            this._description_text_view.buffer.set_text(value, -1);
    },

    get image_uri(){
        return this._image_uri;
    },

    set image_uri(value){
        if (value) {
            let frame_css = "#category_frame{background-image: url('" + value + "');background-repeat:no-repeat;background-size:cover;}";
            this._category_provider.load_from_data(frame_css);
            let context = this.get_style_context();
            context.add_provider(this._category_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
        }
    }
});