Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / docs / app / History.js
1 /**
2  * Browser history management using Ext.util.History.
3  */
4 Ext.define("Docs.History", {
5     extend: 'Docs.LocalStore',
6     storeName: 'History',
7     singleton: true,
8
9     // Maximum number of items to keep in history store
10     maxHistoryLength: 25,
11
12     /**
13      * Initializes history management.
14      */
15     init: function() {
16         Ext.util.History.init(function() {
17             this.navigate(Ext.util.History.getToken());
18         }, this);
19         Ext.util.History.on("change", this.navigate, this);
20         this.callParent();
21     },
22
23     // Parses current URL and navigates to the page
24     navigate: function(token) {
25         if (this.ignoreChange) {
26             this.ignoreChange = false;
27             return;
28         }
29
30         var url = this.parseToken(token);
31         if (url.type === "api") {
32             Docs.App.getController('Classes').loadClass(url.key, true);
33         }
34         else if (url.type === "guide") {
35             Docs.App.getController('Classes').showGuide(url.key, true);
36         }
37         else {
38             Ext.getCmp('container').layout.setActiveItem(0);
39         }
40     },
41
42     // Parses current browser location
43     parseToken: function(token) {
44         var matches = token && token.match(/\/(api|guide)\/(.*)/);
45         return matches ? {type: matches[1], key: matches[2]} : {};
46     },
47
48     // Extracts class name from history token
49     // Returns false when it's not class-related token.
50     parseClassName: function(token) {
51         var url = this.parseToken(token);
52         if (url.type === "api") {
53             return url.key.replace(/-.*$/, '');
54         }
55         else {
56             return false;
57         }
58     },
59
60     /**
61      * Adds URL to history
62      *
63      * @param {String} token  the part of URL after #
64      */
65     push: function(token) {
66         this.ignoreChange = true;
67         Ext.util.History.add(token);
68
69         // Add class name to history store if it's not there already
70         var cls = this.parseClassName(token);
71         if (cls) {
72             // When class already in history remove it and add again.
73             // This way the most recently visited items will always be at the top.
74             var oldIndex = this.store.findExact('cls', cls);
75             if (oldIndex > -1) {
76                 this.store.removeAt(oldIndex);
77             }
78
79             // Add new item at the beginning
80             this.store.insert(0, {cls: cls});
81
82             // Remove items from the end of history if there are too many
83             while (this.store.getAt(this.maxHistoryLength)) {
84                 this.store.removeAt(this.maxHistoryLength);
85             }
86             this.syncStore();
87         }
88     },
89
90     /**
91      * Removes class from History store
92      *
93      * @param {String} cls
94      */
95     removeClass: function(cls) {
96         var index = this.store.findExact('cls', cls);
97         this.store.removeAt(index);
98         this.syncStore();
99     }
100 });