Commit 5ebc0278 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Re-implemented history management.

To make refreshing of a page work, added .htaccess file that
directs all api-URL-s to index.html.

Currently using URL in form: some-path/api:Ext.ClassName
I would like to use: some-path/api/Ext.ClassName

But I didn't figure out how to make them work, especially so that
it could be used from a local filesystem.  When the basedir of
the app changes, you can't use relative URL-s any more.
parent 94fd99fa
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -221,8 +221,12 @@ module JsDuck
    def link_template(template_dir, dir)
      puts "Linking template files to #{dir}..." if @verbose
      FileUtils.mkdir(dir)
      Dir.glob(template_dir + "/*").each do |file|
        File.symlink(File.expand_path(file), dir+"/"+File.basename(file))
      # Use File::FNM_DOTMATCH to include .htaccess
      Dir.glob(template_dir + "/*", File::FNM_DOTMATCH).each do |file|
        basename = File.basename(file)
        unless basename == "." || basename == ".."
          File.symlink(File.expand_path(file), dir+"/"+basename)
        end
      end
      init_output_dirs(dir)
    end

template/.htaccess

0 → 100644
+2 −0
Original line number Diff line number Diff line
RewriteEngine on
RewriteRule ^api:(.*)$ index.html [QSA]
+2 −0
Original line number Diff line number Diff line
@@ -67,6 +67,8 @@

  <script type="text/javascript" src="js/functions.js"></script>

  <script type="text/javascript" src="js/App.js"></script>
  <script type="text/javascript" src="js/History.js"></script>
  <script type="text/javascript" src="js/ClassTree.js"></script>
  <script type="text/javascript" src="js/OverviewPanel.js"></script>
  <script type="text/javascript" src="js/OverviewToolbar.js"></script>

template/js/App.js

0 → 100644
+14 −0
Original line number Diff line number Diff line
/**
 * Global functions that I don't know where else to put.
 */
Ext.define("Docs.App", {
    singleton: true,
    /**
     * Returns base URL used for making AJAX requests.
     * @return {String} URL
     */
    getBaseUrl: function() {
        var url = document.location.href;
        return url.replace(/\/api:.*/, "").replace(/\/$/, "");
    }
});

template/js/History.js

0 → 100644
+36 −0
Original line number Diff line number Diff line
/**
 * Browser history management with history.pushState for compliant
 * browsers.
 */
Ext.define("Docs.History", {
    singleton: true,
    compliant: window.history && window.history.pushState,

    init: function() {
        if (!this.compliant) {
            return;
        }

        window.addEventListener('popstate', function(e) {
            e.preventDefault();
            if (e.state && e.state.docClass) {
                getDocClass(e.state.docClass, true);
            }
            return false;
        }, false);

        var matches = document.location.href.match(/\/api:(.*)/);
        if (matches) {
            var className = matches[1];
            getDocClass(className, true);
        }
    },

    push: function(className) {
        if (!this.compliant) {
            return;
        }
        var fullUrl = Docs.App.getBaseUrl() + "/api:" + className;
        window.history.pushState({docClass: className}, '', fullUrl);
    }
});
Loading