﻿app.title = '题目列表';
app.links = [];

component.data = function () {
    return {
        tags: [
            {
                text: '按解法',
                data: [
                    { text: '搜索', data: ['暴力', '二分法', 'DFS', 'BFS', '双向BFS', '剪枝'] },
                    { text: '动态规划', data: ['线性', '树形', '背包问题'] }
                ]
            },
            { text: '按年份', data: ['2017', '2016', '2015', '2014'] }
        ],
        selected: [],
        paging: {
            current: 1,
            count: 0
        },
        request: {
            tag: null,
            title: null,
            page: 1
        },
        result: []
    };
}

component.watch = {
    selected: function (value) {
        console.log(value);
        value = value.filter(x => !value.some(y => x != y && y.indexOf(x) >= 0));
        this.request.tag = value.join(', ');
    },
    request: {
        handler: function (value) {
            console.log(value);
            var self = this;
            qv.createView('/api/problem/all', this.request).fetch(x => {
                self.paging.count = x.data.count;
                self.paging.current = x.data.current;
                self.result = x.data.result;
            });
        },
        deep: true
    }
};

component.methods = {
    triggerTag: function (tag) {
        if (this.selected.some(x => x == tag)) {
            var subs = this.selected.filter(x => x.indexOf(tag) >= 0);
            for (var i = 0; i < subs.length; i++) {
                this.selected.remove(subs[i]);
            }
        }
        else {
            this.selected.push(tag);
            if (tag.indexOf(':') >= 0 && tag.lastIndexOf(':') >= 0 && tag.indexOf(':') != tag.lastIndexOf(':')) {
                var parent = tag.substr(0, tag.lastIndexOf(':'));
                if (!this.selected.some(x => x == parent)) {
                    this.selected.push(parent);
                }
            }
        }
    },
    setSearchTitle: function () {
        this.request.title = $('#txtSearchProblemTitle').val();
    },
    toPage: function (p) {
        this.paging.current = p;
        this.request.page = p;
    }
};

component.created = function () {
    qv.createView('/api/problem/all', this.request).fetch(x => {
        this.paging.count = x.data.count;
        this.paging.current = x.data.current;
        this.result = x.data.result;
    });
};

@@ -94,336 +94,8 @@
 s: %5B
-%0A            %7B%0A                text: '%E6%8C%89%E8%A7%A3%E6%B3%95',%0A                data: %5B%0A                    %7B text: '%E6%90%9C%E7%B4%A2', data: %5B'%E6%9A%B4%E5%8A%9B', '%E4%BA%8C%E5%88%86%E6%B3%95', 'DFS', 'BFS', '%E5%8F%8C%E5%90%91BFS', '%E5%89%AA%E6%9E%9D'%5D %7D,%0A                    %7B text: '%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92', data: %5B'%E7%BA%BF%E6%80%A7', '%E6%A0%91%E5%BD%A2', '%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%98'%5D %7D%0A                %5D%0A            %7D,%0A            %7B text: '%E6%8C%89%E5%B9%B4%E4%BB%BD', data: %5B'2017', '2016', '2015', '2014'%5D %7D%0A        
 %5D,%0A 
@@ -2046,12 +2046,132 @@
 %0A    %7D);
+%0A%0A    qv.createView('/api/configuration/problemtags').fetch(x =%3E %7B%0A        this.tags = JSON.parse(x.data.value);%0A    %7D);
 %0A%7D;%0A

Move tags into configuration table