2 * Browser history management using Ext.util.History.
4 Ext.define("Docs.History", {
5 extend: 'Docs.LocalStore',
9 // Maximum number of items to keep in history store
13 * Initializes history management.
16 Ext.util.History.init(function() {
17 this.navigate(Ext.util.History.getToken());
19 Ext.util.History.on("change", this.navigate, this);
23 // Parses current URL and navigates to the page
24 navigate: function(token) {
25 if (this.ignoreChange) {
26 this.ignoreChange = false;
30 var url = this.parseToken(token);
31 if (url.type === "api") {
32 Docs.App.getController('Classes').loadClass(url.key, true);
34 else if (url.type === "guide") {
35 Docs.App.getController('Classes').showGuide(url.key, true);
38 Ext.getCmp('container').layout.setActiveItem(0);
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]} : {};
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(/-.*$/, '');
63 * @param {String} token the part of URL after #
65 push: function(token) {
66 this.ignoreChange = true;
67 Ext.util.History.add(token);
69 // Add class name to history store if it's not there already
70 var cls = this.parseClassName(token);
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);
76 this.store.removeAt(oldIndex);
79 // Add new item at the beginning
80 this.store.insert(0, {cls: cls});
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);
91 * Removes class from History store
95 removeClass: function(cls) {
96 var index = this.store.findExact('cls', cls);
97 this.store.removeAt(index);