Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / examples / app / feed-viewer / app / controller / Articles.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 Ext.define('FV.controller.Articles', {
16     extend: 'Ext.app.Controller',
17
18     stores: ['Articles'],
19
20     models: ['Article'],
21
22     views: ['article.Grid', 'article.Preview'],
23
24     refs: [{
25         ref: 'feedShow',
26         selector: 'feedshow'
27     }, {
28         ref: 'viewer',
29         selector: 'viewer'
30     }, {
31         ref: 'articlePreview',
32         selector: 'articlepreview'
33     }, {
34         ref: 'articleTab',
35         xtype: 'articlepreview',
36         closable: true,
37         forceCreate: true,
38         selector: 'articlepreview'
39     }],
40
41     init: function() {
42         this.control({
43             'articlegrid': {
44                 selectionchange: this.previewArticle
45             },
46             'articlegrid > tableview': {
47                 itemdblclick: this.loadArticle,
48                 refresh: this.selectArticle
49             },
50             'articlegrid button[action=openall]': {
51                 click: this.openAllArticles
52             },
53             'articlepreview button[action=viewintab]': {
54                 click: this.viewArticle
55             },
56             'articlepreview button[action=gotopost]': {
57                 click: this.openArticle
58             }
59         });
60     },
61
62     selectArticle: function(view) {
63         var first = this.getArticlesStore().getAt(0);
64         if (first) {
65             view.getSelectionModel().select(first);
66         }
67     },
68
69     /**
70      * Loads the given article into the preview panel
71      * @param {FV.model.Article} article The article to load
72      */
73     previewArticle: function(grid, articles) {
74         var article = articles[0],
75             articlePreview = this.getArticlePreview();
76
77         if (article) {
78             articlePreview.article = article;
79                 articlePreview.update(article.data);
80         }
81     },
82
83     openArticle: function(btn) {
84         window.open(btn.up('articlepreview').article.get('link'));
85     },
86     
87     openAllArticles: function() {
88         var articles = [],
89             viewer = this.getViewer();
90             
91         this.getArticlesStore().each(function(article) {
92             articles.push(this.loadArticle(null, article, true));
93         }, this);
94         
95         viewer.add(articles);
96         viewer.setActiveTab(articles[articles.length-1]);
97     },
98
99     viewArticle: function(btn) {
100         this.loadArticle(null, btn.up('articlepreview').article);
101     },
102
103     /**
104      * Loads the given article into a new tab
105      * @param {FV.model.Article} article The article to load into a new tab
106      */
107     loadArticle: function(view, article, preventAdd) {
108         var viewer = this.getViewer(),
109             title = article.get('title'),
110             articleId = article.id;
111             
112         tab = viewer.down('[articleId=' + articleId + ']');
113         if (!tab) {
114             tab = this.getArticleTab();
115             tab.down('button[action=viewintab]').destroy();
116         }
117
118         tab.setTitle(title);
119         tab.article = article;
120         tab.articleId = articleId;
121         tab.update(article.data);
122
123         if (preventAdd !== true) {
124             viewer.add(tab);
125             viewer.setActiveTab(tab);            
126         }
127         
128         return tab;
129     }
130 });
131