Skip to content
This repository was archived by the owner on Jun 20, 2019. It is now read-only.

Commit

Permalink
Added a history test to improve for IE8
Browse files Browse the repository at this point in the history
  • Loading branch information
bigtimebuddy committed Aug 13, 2014
1 parent 2543e56 commit c86f623
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 48 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "canteen",
"version": "1.1.5",
"version": "1.1.6",
"main": "lib/canteen.min.js",
"dependencies": {
"cloudkid-debug": "*",
Expand Down
2 changes: 1 addition & 1 deletion build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ git.master=master

compressor=uglifyjs
js.validator=jshint
version=1.1.5
version=1.1.6

output.name=canteen
output.min=${build.dir}/${output.name}.min.js
Expand Down
64 changes: 36 additions & 28 deletions lib/canteen.debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@
};
return this.each(function() {
var uri, link = $(this), refresh = link.data("refresh"), href = link.attr("href");
if ("undefined" != typeof refresh) return void link.untouch().touch(function(e) {
return checkConfirm(link) ? "soft" == refresh ? (e.preventDefault(), void site.refresh()) : void (href ? document.location.href = href : site.refresh(!1)) : void 0;
});
if ("undefined" != typeof refresh) return link.untouch().touch(function(e) {
return checkConfirm(link) ? "soft" == refresh ? (e.preventDefault(), site.refresh(),
void 0) : (href ? document.location.href = href : site.refresh(!1), void 0) : void 0;
}), void 0;
if (href && 0 === href.indexOf(basePath)) {
uri = href.substr(basePath.length), link.untouch().touch(function(e) {
e.preventDefault(), checkConfirm(link) && site.redirect(uri);
Expand Down Expand Up @@ -130,11 +131,13 @@
for (var params = {}, i = 0, len = formData.length; len > i; i++) params[formData[i].name] = formData[i].value;
return params.form ? !0 : (site.refresh(!0, params), !1);
}, showResponse = function(response, statusText, xhr, form) {
if (-1 == response.search(/^{.*}$/)) return void Debug.error(response);
if (response = JSON.parse(response), "fatalError" == response.type) return void Debug.fatalError(response);
if (-1 == response.search(/^{.*}$/)) return Debug.error(response), void 0;
if (response = JSON.parse(response), "fatalError" == response.type) return Debug.fatalError(response),
void 0;
var refresh = form.find('input[name="refresh"]'), asyncRefresh = refresh.data("async");
return response.redirect !== undefined ? void site.redirect(response.redirect, !1, !0, asyncRefresh) : (Debug.log(response.messages.join("\n")),
!response.ifError && refresh.length && "true" == refresh.val() ? void site.refresh(asyncRefresh) : void Forms.formFeedback(form, response.messages, response.ifError));
return response.redirect !== undefined ? (site.redirect(response.redirect, !1, !0, asyncRefresh),
void 0) : (Debug.log(response.messages.join("\n")), !response.ifError && refresh.length && "true" == refresh.val() ? (site.refresh(asyncRefresh),
void 0) : (Forms.formFeedback(form, response.messages, response.ifError), void 0));
}, options = {
data: {
async: !0
Expand Down Expand Up @@ -175,10 +178,12 @@
var url = this.gatewayUrl + service + "/" + method;
parameters !== undefined && (url += "/" + ($.isArray(parameters) ? parameters.join("/") : String(parameters))),
Debug.log(url), $.get(url, function(response) {
if (!response || "null" == response) return void callback(null);
if (-1 == response.search(/^{.*}$/)) return Debug.error(response), void callback(null);
if (!response || "null" == response) return callback(null), void 0;
if (-1 == response.search(/^{.*}$/)) return Debug.error(response), callback(null),
void 0;
var data = JSON.parse(response);
return "fatalError" == data.type ? (Debug.fatalError(data), void callback(null)) : void callback(data);
return "fatalError" == data.type ? (Debug.fatalError(data), callback(null), void 0) : (callback(data),
void 0);
});
}, p.destroy = function() {
p = null;
Expand All @@ -194,7 +199,7 @@
"use strict";
var EventDispatcher = Canteen.EventDispatcher, LocationUtils = Canteen.LocationUtils, Gateway = Canteen.Gateway, Forms = Canteen.Forms, Page = Canteen.Page, Site = function() {
this.initialize();
}, p = Site.prototype = new EventDispatcher(), _instance = null, _lastRequest = null, _currentId = null, _currentState = null, _pages = [], _currentPage = null, _defaultOptions = {
}, p = Site.prototype = new EventDispatcher(), _instance = null, _lastRequest = null, _currentId = null, _currentState = null, _historyEnabled = !0, _pages = [], _currentPage = null, _defaultOptions = {
contentId: "#content",
pageTitleId: "h1",
pageLoadingId: "article",
Expand All @@ -203,33 +208,36 @@
};
p.currentState = null, p.currentPage = null, p.gateway = null, p.parameters = null,
p.options = {}, Site.ENTER = "enter", Site.EXIT = "exit", Site.READY = "ready",
Site.LOADING = "loading", Site.LOADING_DONE = "loadingDone", Site.VERSION = "1.1.5",
Site.LOADING = "loading", Site.LOADING_DONE = "loadingDone", Site.VERSION = "1.1.6",
Site.instance = null, p.initialize = function() {
if (_instance) throw "Site has already been created. Use Canteen.Site.instance";
Site.instance = _instance = this;
var opts = this.options = _defaultOptions;
(Canteen.settings.clientEnabled === undefined || Canteen.settings.clientEnabled) && ($(opts.siteLoadingId).addClass(opts.loadingClass),
this.parameters = LocationUtils.getParameters(), Debug.enabled = Canteen.settings.debug,
_currentId = 1, this.currentState = _currentState = Canteen.settings.uriRequest,
this._enableHistory(!0), this._fixInternalLinks(), this.gateway = new Gateway(Canteen.settings.gatewayPath, this._gatewayReady.bind(this)));
this._enableHistory(!(!global.history || !history.pushState)), _historyEnabled && this._fixInternalLinks(),
this.gateway = new Gateway(Canteen.settings.gatewayPath, this._gatewayReady.bind(this)));
}, p._enableHistory = function(enable) {
$(global).off("statechange"), enable && $(global).on("statechange", this._onStateChange.bind(this));
_historyEnabled = enable, $(global).off("statechange"), enable && $(global).on("statechange", this._onStateChange.bind(this));
}, p.redirect = function(uri, replaceInHistory, allowRefresh, asyncRefresh) {
var state = Canteen.settings.basePath, siteTitle = $("<div />").html($("title").html()).text();
return replaceInHistory = replaceInHistory === undefined ? !1 : replaceInHistory,
allowRefresh = allowRefresh === undefined ? !1 : allowRefresh, asyncRefresh = asyncRefresh === undefined ? !0 : asyncRefresh,
Canteen.settings.siteIndex != uri && (state += uri), _currentState == uri ? void (allowRefresh ? this.refresh(asyncRefresh) : Debug.log("Already on this page! (current: " + _currentState + ", request: " + uri + ")")) : void (replaceInHistory ? History.replaceState({
Canteen.settings.siteIndex != uri && (state += uri), _currentState == uri ? (allowRefresh ? this.refresh(asyncRefresh) : Debug.log("Already on this page! (current: " + _currentState + ", request: " + uri + ")"),
void 0) : (_historyEnabled ? replaceInHistory ? History.replaceState({
state: _currentId
}, siteTitle, state) : (_currentId++, History.pushState({
state: _currentId
}, siteTitle, state)));
}, siteTitle, state)) : document.location.href = state, void 0);
}, p.refresh = function(async, params) {
async = async === undefined ? !0 : async, async ? this._updatePageContent(_currentState, params) : document.location.reload(!0);
async = async === undefined ? !0 : async, async && _historyEnabled ? this._updatePageContent(_currentState, params) : document.location.reload(!0);
}, p._fixInternalLinks = function() {
$("a").internalLink(this), Forms.setup(this, "true" === this.parameters.debugForms);
}, p._onStateChange = function() {
var state = History.getState(), uri = state.url.split(Canteen.settings.baseUrl)[1];
return _currentState == uri ? void Debug.log("Already on this page! (" + uri + ")") : void this._updatePageContent(uri);
return _currentState == uri ? (Debug.log("Already on this page! (" + uri + ")"),
void 0) : (this._updatePageContent(uri), void 0);
}, p._gatewayReady = function(success) {
success ? Debug.log("Canteen is ready!") : Debug.error("Unable to setup gateway.");
var options = this.options;
Expand All @@ -254,24 +262,24 @@
}, p._updatePageContent = function(uri, params) {
var site = this, options = this.options, page = this.getPageByUri(uri), url = Canteen.settings.baseUrl + uri;
return this.trigger(Site.EXIT, _currentState), this.currentState = _currentState = uri,
page && page.content === _currentPage && !page.content.refresh(uri) ? void (page.content.uri = uri) : (_currentPage && (_currentPage.exit(),
_currentPage.active = !1, this.currentPage = _currentPage = null), Forms.destroy(),
_lastRequest = uri, params = params === undefined ? {} : params, params.async = !0,
this.trigger(Site.LOADING), $(options.pageLoadingId).removeClass(options.loadingClass).addClass(options.loadingClass),
Debug.log("Update Page Content : " + uri), void $.post(url, params, function(response) {
page && page.content === _currentPage && !page.content.refresh(uri) ? (page.content.uri = uri,
void 0) : (_currentPage && (_currentPage.exit(), _currentPage.active = !1, this.currentPage = _currentPage = null),
Forms.destroy(), _lastRequest = uri, params = params === undefined ? {} : params,
params.async = !0, this.trigger(Site.LOADING), $(options.pageLoadingId).removeClass(options.loadingClass).addClass(options.loadingClass),
Debug.log("Update Page Content : " + uri), $.post(url, params, function(response) {
if (uri == _lastRequest) {
if (_lastRequest = null, site.trigger(Site.LOADING_DONE), $(options.pageLoadingId).removeClass(options.loadingClass),
!response || "null" == response) return void Debug.error("No data for " + uri);
if (-1 == response.search(/^{.*}$/)) return void Debug.error(response);
!response || "null" == response) return Debug.error("No data for " + uri), void 0;
if (-1 == response.search(/^{.*}$/)) return Debug.error(response), void 0;
var data = JSON.parse(response);
if ("fatalError" == data.type) return void Debug.fatalError(data);
if (data.redirect !== undefined) return void site.redirect(data.redirect, !0);
if ("fatalError" == data.type) return Debug.fatalError(data), void 0;
if (data.redirect !== undefined) return site.redirect(data.redirect, !0), void 0;
$("body").attr("id", data.pageId), $(options.pageTitleId).html(data.title), $(options.contentId).html(data.content),
document.title = $("<div />").html(data.fullTitle).text(), $("meta[name='keywords']").attr("content", data.keywords),
$("meta[name='description']").attr("content", data.description), site._fixInternalLinks(),
site._enterPage();
}
}));
}), void 0);
}, namespace("Canteen").Site = Site, $(function() {
new Site();
});
Expand Down
37 changes: 28 additions & 9 deletions lib/canteen.js
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,13 @@
* @private
*/
_currentState = null,

/**
* If we should enable the HTML5 history
* @property {Boolean} _historyEnabled
* @private
*/
_historyEnabled = true,

/**
* An array of the pages
Expand Down Expand Up @@ -1267,7 +1274,7 @@
* @property {String} VERSION
* @readOnly
*/
Site.VERSION = "1.1.5";
Site.VERSION = "1.1.6";

/**
* Get the singleton instance of the site
Expand Down Expand Up @@ -1306,8 +1313,12 @@
// Setup history
_currentId = 1;
this.currentState = _currentState = Canteen.settings.uriRequest;
this._enableHistory(true);
this._fixInternalLinks();
this._enableHistory(!!(global.history && history.pushState));

if (_historyEnabled)
{
this._fixInternalLinks();
}

// Initialize the gateway
this.gateway = new Gateway(
Expand All @@ -1324,6 +1335,7 @@
*/
p._enableHistory = function(enable)
{
_historyEnabled = enable;
$(global).off('statechange');
if (enable)
$(global).on('statechange', this._onStateChange.bind(this));
Expand Down Expand Up @@ -1372,15 +1384,22 @@
return;
}

// Change the history state
if (replaceInHistory)
if (_historyEnabled)
{
History.replaceState({state:_currentId}, siteTitle, state);
// Change the history state
if (replaceInHistory)
{
History.replaceState({state:_currentId}, siteTitle, state);
}
else
{
_currentId++;
History.pushState({state:_currentId}, siteTitle, state);
}
}
else
{
_currentId++;
History.pushState({state:_currentId}, siteTitle, state);
document.location.href = state;
}
};

Expand All @@ -1393,7 +1412,7 @@
p.refresh = function(async, params)
{
async = async === undefined ? true : async;
if (async)
if (async && _historyEnabled)
this._updatePageContent(_currentState, params);
else
document.location.reload(true);
Expand Down
Loading

0 comments on commit c86f623

Please sign in to comment.