Source: core/HistoryManager.js

// Generated by github.com/steida/coffee2closure 0.1.12
goog.provide('spark.core.HistoryManager');
goog.provide('spark.core.HistoryManager.TokenTransformer');
goog.require('spark.core.Object');
goog.require('spark.components.Input');
goog.require('goog.History');
goog.require('goog.history.Html5History');

/**
  HistoryManager class for Spark Framework to handle hashbang or push state
  navigations. This class uses classic hashbang routes but if you want to
  use HTML5 History API you can also use it. Pass `useHtml5History` option as
  true. Make sure your browser supports HTML5 History API. HistoryManager can
  be used as standalone but it's more powerful with Router. You should listen
  `Navigated` event to use HistoryManager when you use it standalone.

  @constructor
  @export
  @param   {Object=} options Class options.
  @param   {*=} data Class data
  @extends {spark.core.Object}
 */
spark.core.HistoryManager = function(options, data) {
  var isHtml5HistorySupported, useHtml5History, _ref;
  if (options == null) {
    options = {};
  }
  
/**
    Add a prefix to all routes. Only available for Html5History.
   */
  options.pathPrefix || (options.pathPrefix = options['pathPrefix'] || '');
  options.useHtml5History || (options.useHtml5History = (_ref = options['useHtml5History']) != null ? _ref : false);
  spark.core.HistoryManager.superClass_.constructor.call(this, options, data);
  isHtml5HistorySupported = goog.history.Html5History.isSupported();
  useHtml5History = options.useHtml5History;
  if (isHtml5HistorySupported && useHtml5History) {
    this.tokenTransformer = new spark.core.HistoryManager.TokenTransformer;
    this.history = new goog.history.Html5History(null, this.tokenTransformer);
    this.history.setPathPrefix(options.pathPrefix);
    this.history.setUseFragment(false);
  } else {
    this.history = new goog.History(void 0, void 0, this.getHistoryInput_());
  }
  goog.events.listen(this.history, goog.history.EventType.NAVIGATE, (function(_this) {
    return function(e) {
      return _this.emit('Navigated', e.token);
    };
  })(this));
  this.history.setEnabled(true);
}
goog.inherits(spark.core.HistoryManager, spark.core.Object);

/**
  Set token method to update history token and change the page url.

  @export
  @param {!string} token Token to change page url.
 */
spark.core.HistoryManager.prototype.setToken = function(token) {
  return this.history.setToken(token);
};

/**
  Returns a input element to give goog's History class. This prevents Firefox
  to reload the page. Also Firefox was throwing a security error on localhost
  when HistoryManager constructed. So this trick will also fix that issue too.

  @private
 */
spark.core.HistoryManager.prototype.getHistoryInput_ = function() {
  this.historyInput_ = new spark.components.Input();
  return this.historyInput_.getElement();
};

/**
  Destroys the HistoryManager and goog.history instance.

  @export
  @override
 */
spark.core.HistoryManager.prototype.destroy = function() {
  if (!this.isDestroyed()) {
    this.history.disposeInternal();
    this.history = null;
    this.historyInput_.destroy();
    this.tokenTransformer = null;
  }
  return spark.core.HistoryManager.superClass_.destroy.apply(this, arguments);
};

/**
  This class is an implementation of goog.history.Html5History.TokenTransformer.
  I needed to create a new TokenTransformer, because goog's TokenTransformer
  always appends the query string end of the created url. See http://goo.gl/8q40Cc
  That behaviour creates URLs like which has multiple `?` in it somethig like
  that `/books?sortBy=name&order=asc?query=alex?section=sci-fi`.
  I thought it's a bug and created my own TokenTransformer.

  @constructor
  @implements {goog.history.Html5History.TokenTransformer}
 */
spark.core.HistoryManager.TokenTransformer = function() {}

/**
  @override
 */
spark.core.HistoryManager.TokenTransformer.prototype.createUrl = function(token, pathPrefix, location) {
  return pathPrefix + token;
};

/**
  @override
 */
spark.core.HistoryManager.TokenTransformer.prototype.retrieveToken = function(pathPrefix, location) {
  return location.pathname.substr(pathPrefix.length);
};
Spark Framework by Fatih Acet
Copyright © 2014 - Fatih Acet
Documentation generated by JSDoc 3.2.2 on 2015-07-19T22:09:29+00:00 using the DocStrap template.