Commit 47d87fc8 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Refactor Tags and Targets lists.

Extract TopList base class.

Define Tag and Target models using the new "comments" proxy which
helps us with prepending comments-specific stuff to URL.
parent a8ce8f3e
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
/**
 * A custom JsonP proxy that prepends Comments backend specific
 * prefixes to the configured URL.
 *
 * For example when configuring with:
 *
 *     url: "/tags"
 *
 * The actual URL will be something like:
 *
 *     url: "http://projects.sencha.com/auth/ext-js/4/tags"
 *
 */
Ext.define('Docs.CommentsProxy', {
    extend: 'Ext.data.proxy.JsonP',
    alias: "proxy.comments",
    requires: [
        "Docs.Comments"
    ],

    constructor: function(cfg) {
        cfg.url = Docs.Comments.buildRequestUrl(cfg.url);
        this.callParent([cfg]);
    }

});
+19 −0
Original line number Diff line number Diff line
/**
 * A tag model.
 */
Ext.define('Docs.model.Tag', {
    extend: 'Ext.data.Model',
    requires: ['Docs.CommentsProxy'],
    fields: [
        "tagname",
        "score"
    ],
    proxy: {
        type: "comments",
        url: "/tags",
        reader: {
            type: "json",
            root: "data"
        }
    }
});
+32 −0
Original line number Diff line number Diff line
/**
 * A target model.
 */
Ext.define('Docs.model.Target', {
    extend: 'Ext.data.Model',
    requires: ['Docs.CommentsProxy'],
    fields: [
        "id",
        "type",
        "cls",
        "member",
        "score",
        {name: "text", convert: function(v, rec) {
            var data = rec.data;
            if (data.type === "class") {
                return data.cls + (data.member ? "#"+data.member.replace(/^.*-/, "") : "");
            }
            else {
                return data.type + " " + data.cls;
            }
        }}
    ],

    proxy: {
        type: "comments",
        url: "/targets",
        reader: {
            type: "json",
            root: "data"
        }
    }
});
+4 −10
Original line number Diff line number Diff line
@@ -3,7 +3,9 @@
 */
Ext.define("Docs.view.comments.TagEditor", {
    extend: "Ext.container.Container",
    requires: ["Docs.Comments"],
    requires: [
        "Docs.model.Tag"
    ],
    floating: true,
    hidden: true,
    componentCls: "comments-tageditor",
@@ -13,15 +15,7 @@ Ext.define("Docs.view.comments.TagEditor", {
        getStore: function() {
            if (!this.cachedStore) {
                this.cachedStore = Ext.create('Ext.data.Store', {
                    fields: ['tagname'],
                    proxy: {
                        type: "ajax",
                        url: Docs.Comments.buildRequestUrl("/tags"),
                        reader: {
                            type: "json",
                            root: "data"
                        }
                    },
                    model: "Docs.model.Tag",
                    listeners: {
                        load: function() {
                            this.cachedStore.sort("tagname", "ASC");
+5 −112
Original line number Diff line number Diff line
@@ -2,118 +2,11 @@
 * View for showing tags.
 */
Ext.define('Docs.view.comments.Tags', {
    extend: 'Docs.view.comments.TopList',
    alias: "widget.commentsTags",
    extend: 'Ext.panel.Panel',
    componentCls: "comments-tags",
    requires: [
        "Docs.Comments",
        "Docs.view.SimpleSelectBehavior",
        "Docs.view.comments.FilterField"
    ],
    requires: ["Docs.model.Tag"],

    layout: "border",

    /**
     * @event select
     * Fired when tag is selected from list.
     * @param {Ext.data.Model} tag  The selected tag
     * or undefined when no tag selected.
     */

    initComponent: function() {
        this.items = [
            this.tabpanel = Ext.widget("tabpanel", {
                plain: true,
                region: "north",
                height: 50,
                items: [
                    {
                        title: "By comment count"
                    }
                ],
                dockedItems: [
                    {
                        dock: "bottom",
                        items: [{
                            xtype: "commentsFilterField",
                            emptyText: "Filter tags by name...",
                            width: 320,
                            height: 20,
                            listeners: {
                                filter: this.onFilter,
                                scope: this
                            }
                        }]
                    }
                ]
            }),
            this.list = Ext.widget("dataview", {
                region: "center",
                cls: "iScroll targets-list",
                autoScroll: true,
                store: Ext.create('Ext.data.Store', {
                    fields: ["tagname", "score"]
                }),
                allowDeselect: true,
                tpl: [
                    '<ul>',
                    '<tpl for=".">',
                        '<li>',
                            '<span class="score">{score}</span>',
                            '<span class="target">{tagname}</span>',
                        '</li>',
                    '</tpl>',
                    '</ul>'
                ],
                itemSelector: "li"
            })
        ];

        new Docs.view.SimpleSelectBehavior(this.list, {
            select: this.onSelect,
            deselect: this.onDeselect,
            scope: this
        });

        this.callParent(arguments);
    },

    afterRender: function() {
        this.callParent(arguments);
        this.fetchTags();
    },

    onFilter: function(pattern) {
        this.list.getSelectionModel().deselectAll();
        this.list.getStore().clearFilter(true);
        this.list.getStore().filter({property: "tagname", value: pattern, anyMatch: true});
    },

    /**
     * Clears the selection.
     */
    deselectAll: function() {
        this.list.getSelectionModel().deselectAll();
    },

    onSelect: function(tag) {
        this.fireEvent("select", tag);
    },

    onDeselect: function() {
        this.fireEvent("select", undefined);
    },

    fetchTags: function(sortBy) {
        Docs.Comments.request("jsonp", {
            url: '/tags',
            method: 'GET',
            success: this.loadTags,
            scope: this
        });
    },

    loadTags: function(tags) {
        this.list.getStore().loadData(tags.data);
    }
    model: "Docs.model.Tag",
    displayField: "tagname",
    filterEmptyText: "Filter tags by name..."
});
Loading