Commit fa047a95 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Added Docs.LocalStore class.

This contains the shared functionality of History and Favorites,
which both use localStorage.
parent f45900e8
Loading
Loading
Loading
Loading
+4 −15
Original line number Diff line number Diff line
@@ -2,20 +2,10 @@
 * Favorites management.
 */
Ext.define("Docs.Favorites", {
    extend: 'Docs.LocalStore',
    storeName: 'Favorites',
    singleton: true,

    /**
     * Initializes favorites management.
     */
    init: function() {
        // Load Favorites from localStorage

        this.localStorage = ('localStorage' in window && window['localStorage'] !== null);

        this.store = Ext.getStore("Favorites");
        if (this.localStorage) this.store.load();
    },

    /**
     * Adds class to favorites
     *
@@ -24,7 +14,7 @@ Ext.define("Docs.Favorites", {
    add: function(cls) {
        if (!this.has(cls)) {
            this.store.add({cls: cls});
            if (this.localStorage) this.store.sync();
            this.syncStore();
        }
    },

@@ -36,7 +26,7 @@ Ext.define("Docs.Favorites", {
    remove: function(cls) {
        if (this.has(cls)) {
            this.store.removeAt(this.store.findExact('cls', cls));
            if (this.localStorage) this.store.sync();
            this.syncStore();
        }
    },

@@ -49,5 +39,4 @@ Ext.define("Docs.Favorites", {
    has: function(cls) {
        return this.store.findExact('cls', cls) > -1;
    }

});
+5 −7
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@
 * Browser history management using Ext.util.History.
 */
Ext.define("Docs.History", {
    extend: 'Docs.LocalStore',
    storeName: 'History',
    singleton: true,

    // Maximum number of items to keep in history store
@@ -15,11 +17,7 @@ Ext.define("Docs.History", {
            this.navigate(Ext.util.History.getToken());
        }, this);
        Ext.util.History.on("change", this.navigate, this);
        // Load History from localStorage
        this.localStorage = ('localStorage' in window && window['localStorage'] !== null);

        this.store = Ext.getStore("History");
        if (this.localStorage) this.store.load();
        this.callParent();
    },

    // Parses current URL and navigates to the page
@@ -85,7 +83,7 @@ Ext.define("Docs.History", {
            while (this.store.getAt(this.maxHistoryLength)) {
                this.store.removeAt(this.maxHistoryLength);
            }
            if (this.localStorage) this.store.sync();
            this.syncStore();
        }
    },

@@ -97,6 +95,6 @@ Ext.define("Docs.History", {
    removeClass: function(cls) {
        var index = this.store.findExact('cls', cls);
        this.store.removeAt(index);
        if (this.localStorage) this.store.sync();
        this.syncStore();
    }
});
+28 −0
Original line number Diff line number Diff line
/**
 * Provides methods dealing with localStorage- and memory-store.
 *
 * Base class for History and Favorites.
 */
Ext.define("Docs.LocalStore", {
    storeName: '',

    /**
     * Initializes store management.
     *
     * Initializes this.store variable and loads the store if
     * localStorage available.
     */
    init: function() {
        this.localStorage = ('localStorage' in window && window['localStorage'] !== null);
        this.store = Ext.getStore(this.storeName);
        this.localStorage && this.store.load();
    },

    /**
     * Syncs the store with localStorage if possible.
     */
    syncStore: function() {
        this.localStorage && this.store.sync();
    }

});
+1 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
  <script type="text/javascript" src="prettify/prettify.js"></script>

  <script type="text/javascript" src="app.js"></script>
  <script type="text/javascript" src="app/LocalStore.js"></script>
  <script type="text/javascript" src="app/Favorites.js"></script>
  <script type="text/javascript" src="app/History.js"></script>