summaryrefslogtreecommitdiff
path: root/wikipedia/widgets/category_selector_view.js
blob: 72eff07ab4cfbb9a08c0162072ac7cab555e3734 (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
const GObject = imports.gi.GObject;
const Gtk = imports.gi.Gtk;
const Lang = imports.lang;

const CategoryButton = imports.wikipedia.widgets.category_button;
const CategoryLayoutManager = imports.wikipedia.widgets.category_layout_manager;

const CATEGORY_COLUMN_SPACING = 10;  // pixels
const CATEGORY_ROW_SPACING = 10;  // pixels

const CategorySelectorView = new Lang.Class({
    Name: 'CategorySelectorView',
    Extends: CategoryLayoutManager.CategoryLayoutManager,
    Signals: {
        'category-chosen': {
            param_types: [GObject.TYPE_STRING]
        }
    },

    _init: function(props) {
        props = props || {};
        props.column_spacing = CATEGORY_COLUMN_SPACING;
        props.row_spacing = CATEGORY_ROW_SPACING;
        this.parent(props);
    },

    /**
     * Method: setCategories
     * Create buttons in this view for a list of categories to display
     *
     * Parameters:
     *   categories - An array of <CategoryModels>
     *
     */
    setCategories: function(categories) {
        categories.forEach(function (category) {
            let button = new CategoryButton.CategoryButton({
                category_title: category.title,
                image_uri: category.image_thumbnail_uri,
                clickable_category: category.has_articles,
                is_main_category: category.is_main_category,
            });
            button.id = category.id; // ID to return to when clicked
            //if the category has no articles, you shouldn't be able to click on it.
            if (category.has_articles) {
                button.connect('clicked', Lang.bind(this, this._onButtonClicked));              
            }

            this.add(button);
        }, this);
    },

    _onButtonClicked: function(button) {
        this.emit('category-chosen', button.id);
    }
});