﻿var ListView = function()
{
    this.store = new Ext.data.Store({
        reader: new Ext.data.XmlReader(
            { record: "Subject" },
            [
                "No",
                "Name",
                "Dat",
                "ResCnt",
                "CreateDate",
                "Speed"
            ])
    });

    var height = Math.floor($(window).height() * 0.4);

    ListView.superclass.constructor.call(this, {
        store: this.store,
        title: "スレッド一覧",
        region: "north",
        columns:
            [
                { header: "No.", width: 60, dataIndex: "No", sortable: false },
                { header: "タイトル", width: 20, dataIndex: "Name", sortable: false, id: "TitleName" },
                { header: "レス数", width: 60, dataIndex: "ResCnt", sortable: false },
                { header: "スレ立上げ日", width: 140, dataIndex: "CreateDate", sortable: true },
                { header: "スレスピード", width: 100, dataIndex: "Speed", sortable: true },
                { header: "Dat", hidden: true, dataIndex: "Dat", sortable: false }
            ],
        split: true,
        height: height,
        minSize: 200,
        collapsible: true,
        layout: "fit",
        margins: "0 0 0 0",
        autoExpandColumn: "TitleName",
        autoExpandMin: 100,
        loadMask: true,
        tools:[{
            id: "refresh",
            qtip: "スレッド一覧更新",
            scope: this,
            handler: function(event, toolEl, panel)
            {
                panel.reloadView();
            }
            },
            {
            id: "help",
            qtip: "HikkyWebについて",
            scope: this,
            handler: function(event, toolEl, panel)
            {
                panel.onAbout();
            }
        }],
        tbar: new Ext.Toolbar({
            id: "listView_Toolbar",
            items:
                    [
                    //"->",
                    {
                        id: "listView_searchFieldTitle",
                        xtype: "tbtext",
                        text: "スレタイトル絞り込み:"
                    },
                    {
                        id: "listView_searchField",
                        xtype: "textfield",
                        width: 300,
                        enableKeyEvents: true
                    }
                    ]
        })
    });

    var items = this.getTopToolbar().items;
    items.get("listView_searchField").on("keyup", this.onChangeSearchField, this);

    this.boardSubjectURL = null;
};

Ext.extend(ListView, Ext.grid.GridPanel,
{
    updateView: function(boardName, boardSubjectURL)
    {
        // タイトルの設定
        this.setTitle(boardName);

        // urlの保持
        this.boardSubjectURL = boardSubjectURL;

        // 絞り込みフィールドのクリア
        var items = this.getTopToolbar().items;
        items.get("listView_searchField").setValue("");

        var requestURL = "GetSubjectListXML.aspx";
        var queryString = "";

        if (boardName != null)
            queryString += ("BoardName=" + boardName);

        if (boardSubjectURL != null)
            queryString += ("&URL=" + boardSubjectURL);

        if (queryString.length > 0)
            requestURL += ("?" + queryString);

        requestURL = encodeURI(requestURL);

        if (this.store.proxy == null)
            this.store.proxy = new Ext.data.HttpProxy({});
        this.store.proxy.setUrl(requestURL, true);

        this.store.proxy.on("exception", this.onLoadException, this);
        this.store.load();
    },

    reloadView: function()
    {
        var boardName = this.title;
        var boardSubjectURL = this.boardSubjectURL;
        this.updateView(boardName, boardSubjectURL);
    },

    onLoadException: function(dataProxy, type, action, options, response, arg)
    {
        Ext.Msg.alert("ロードエラー", "スレッド一覧の取得に失敗しました。");
    },

    onChangeSearchField: function(searchField, evt)
    {
        if (Ext.isEmpty(searchField))
            return;

        if (this.store.getTotalCount() <= 0)
            return;

        var value = searchField.getValue();
        this.store.filter("Name", value, true, false);
    },

    onAbout: function()
    {
        var win = new AboutHikkyWeb();
        win.showAbout();
    }
});


