From 2543e5629b585ba34b2f995cc03d132af5f26654 Mon Sep 17 00:00:00 2001 From: Matt Karl Date: Tue, 12 Aug 2014 16:20:29 -0400 Subject: [PATCH] IE8 Compatibility, removed defineProperty --- LICENSE | 2 +- bower.json | 3 +- build.properties | 2 +- lib/canteen.debug.js | 40 +++++++++--------------- lib/canteen.js | 73 ++++++++++++++++++-------------------------- lib/canteen.min.js | 2 +- src/Canteen/Site.js | 71 +++++++++++++++++------------------------- 7 files changed, 77 insertions(+), 116 deletions(-) diff --git a/LICENSE b/LICENSE index cea2704..674736d 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2013 Matt Karl +Copyright (c) 2014 Matt Karl Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/bower.json b/bower.json index 5f69f95..2908dd8 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "canteen", - "version": "1.1.4", + "version": "1.1.5", "main": "lib/canteen.min.js", "dependencies": { "cloudkid-debug": "*", @@ -12,6 +12,7 @@ }, "ignore": [ ".gitignore", + ".gitattributes", "build.xml", "build.properties", "docs.json", diff --git a/build.properties b/build.properties index 3997ba0..68b7dd7 100644 --- a/build.properties +++ b/build.properties @@ -18,7 +18,7 @@ git.master=master compressor=uglifyjs js.validator=jshint -version=1.1.4 +version=1.1.5 output.name=canteen output.min=${build.dir}/${output.name}.min.js diff --git a/lib/canteen.debug.js b/lib/canteen.debug.js index c1984e6..8bcdfbc 100644 --- a/lib/canteen.debug.js +++ b/lib/canteen.debug.js @@ -201,16 +201,17 @@ siteLoadingId: "body", loadingClass: "loading" }; - 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.4", p.initialize = function() { + 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.instance = null, p.initialize = function() { if (_instance) throw "Site has already been created. Use Canteen.Site.instance"; - _instance = this; + 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, _currentState = Canteen.settings.uriRequest, this._enableHistory(!0), - this._fixInternalLinks(), this.gateway = new Gateway(Canteen.settings.gatewayPath, this._gatewayReady.bind(this))); + _currentId = 1, this.currentState = _currentState = Canteen.settings.uriRequest, + this._enableHistory(!0), 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)); }, p.redirect = function(uri, replaceInHistory, allowRefresh, asyncRefresh) { @@ -222,19 +223,7 @@ }, siteTitle, state) : (_currentId++, History.pushState({ state: _currentId }, siteTitle, state))); - }, Object.defineProperty(Site, "instance", { - get: function() { - return _instance; - } - }), Object.defineProperty(p, "currentState", { - get: function() { - return _currentState; - } - }), Object.defineProperty(p, "currentPage", { - get: function() { - return _currentPage; - } - }), p.refresh = function(async, params) { + }, p.refresh = function(async, params) { async = async === undefined ? !0 : async, async ? this._updatePageContent(_currentState, params) : document.location.reload(!0); }, p._fixInternalLinks = function() { $("a").internalLink(this), Forms.setup(this, "true" === this.parameters.debugForms); @@ -255,8 +244,8 @@ })) : Debug.warn("The page your trying to add needs to extend Canteen.Page"); }, p._enterPage = function() { var page = this.getPageByUri(_currentState); - page && (_currentPage = page.content, _currentPage.uri = _currentState, _currentPage.active = !0, - _currentPage.enter(), this.resize()), this.trigger(Site.ENTER, _currentState); + page && (this.currentPage = _currentPage = page.content, _currentPage.uri = _currentState, + _currentPage.active = !0, _currentPage.enter(), this.resize()), this.trigger(Site.ENTER, _currentState); }, p.getPageByUri = function(uri) { var len = _pages.length, i = 0, pg = null; if (0 === len) return null; @@ -264,10 +253,11 @@ return null; }, 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), _currentState = uri, page && page.content === _currentPage && !page.content.refresh(uri) ? void (page.content.uri = uri) : (_currentPage && (_currentPage.exit(), - _currentPage.active = !1, _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), + 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) { if (uri == _lastRequest) { if (_lastRequest = null, site.trigger(Site.LOADING_DONE), $(options.pageLoadingId).removeClass(options.loadingClass), diff --git a/lib/canteen.js b/lib/canteen.js index 244b3b3..c9c9ab6 100644 --- a/lib/canteen.js +++ b/lib/canteen.js @@ -1197,7 +1197,21 @@ */ loadingClass : 'loading' }; - + + /** + * The current state URI + * @property {String} currentState + * @readOnly + */ + p.currentState = null; + + /** + * Get the current page object + * @property {Canteen.Page} currentPage + * @readOnly + */ + p.currentPage = null; + /** * The instancee of the gateway * @property {Gateway} gateway @@ -1253,7 +1267,15 @@ * @property {String} VERSION * @readOnly */ - Site.VERSION = "1.1.4"; + Site.VERSION = "1.1.5"; + + /** + * Get the singleton instance of the site + * @property {Site} instance + * @static + * @readOnly + */ + Site.instance = null; /** * Constructor for the site @@ -1265,7 +1287,7 @@ throw "Site has already been created. Use Canteen.Site.instance"; } - _instance = this; + Site.instance = _instance = this; var opts = this.options = _defaultOptions; @@ -1283,7 +1305,7 @@ // Setup history _currentId = 1; - _currentState = Canteen.settings.uriRequest; + this.currentState = _currentState = Canteen.settings.uriRequest; this._enableHistory(true); this._fixInternalLinks(); @@ -1362,43 +1384,6 @@ } }; - /** - * Get the singleton instance of the site - * @property {Site} instance - * @static - * @readOnly - */ - Object.defineProperty(Site, "instance", { - get: function() - { - return _instance; - } - }); - - /** - * The current state URI - * @property {String} currentState - * @readOnly - */ - Object.defineProperty(p, "currentState", { - get: function() - { - return _currentState; - } - }); - - /** - * Get the current page object - * @property {Canteen.Page} currentPage - * @readOnly - */ - Object.defineProperty(p, "currentPage", { - get: function() - { - return _currentPage; - } - }); - /** * Do a refresh of the page content * @method refresh @@ -1520,7 +1505,7 @@ var page = this.getPageByUri(_currentState); if (page) { - _currentPage = page.content; + this.currentPage = _currentPage = page.content; _currentPage.uri = _currentState; _currentPage.active = true; _currentPage.enter(); @@ -1576,7 +1561,7 @@ this.trigger(Site.EXIT, _currentState); // Update the current uri - _currentState = uri; + this.currentState = _currentState = uri; // Special-case if we're already on the current page // but only the content needs to update @@ -1591,7 +1576,7 @@ { _currentPage.exit(); _currentPage.active = false; - _currentPage = null; + this.currentPage = _currentPage = null; } // Form cleanup diff --git a/lib/canteen.min.js b/lib/canteen.min.js index 9ab2f73..7d8cc27 100644 --- a/lib/canteen.min.js +++ b/lib/canteen.min.js @@ -1 +1 @@ -!function(){"use strict";Function.prototype.bind||(Function.prototype.bind=function(t){var e,n,r=this;if("function"!=typeof r)throw new TypeError;return e=Array.prototype.slice.call(arguments,1),n=function(){if(this instanceof n){var i,a,o;return i=function(){},i.prototype=r.prototype,a=new i,o=r.apply(a,e.concat(Array.prototype.slice.call(arguments))),Object(o)===o?o:a}return r.apply(t,e.concat(Array.prototype.slice.call(arguments)))}})}(),function(){Debug.fatalError=function(t){var e=t.message+"\non "+t.file+" (code: "+t.code+")\n";if(t.stackTrace)for(var n=0;no;o++)a=i[o],this._listeners[a]=this._listeners[a]||[],-1===this._callbackIndex(a,e)&&this._listeners[a].push(e);else if("array"===n(e))for(var u=0,c=e.length;c>u;u++)this.on(t,e[u]);return this},i.off=function(t,r){if(t===e)this._listeners=[];else if("array"===n(r))for(var i=0,a=r.length;a>i;i++)this.off(t,r[i]);else for(var o=t.split(" "),s=null,u=0,c=o.length;c>u;u++)if(s=o[u],this._listeners[s]=this._listeners[s]||[],r===e)this._listeners[s].length=0;else{var l=this._callbackIndex(s,r);-1!==l&&this._listeners[t].splice(l,1)}return this},i._callbackIndex=function(t,e){for(var n=0,r=this._listeners[t].length;r>n;n++)if(this._listeners[t][n]===e)return n;return-1},namespace("Canteen").EventDispatcher=r}(window),$.fn.touch=function(t){return this.on("click",!1).on("touchclick",t)},$.fn.untouch=function(){return this.off("touchclick")},$.fn.internalLink=function(t){var e=Canteen.settings.basePath,n=Canteen.settings.siteIndex,r=t.currentState,i=function(t){if(t.hasClass("confirm")){var e=t.data("confirm")||"Are you sure you wish to continue?",n=confirm(e);return n}return!0};return this.each(function(){var a,o=$(this),s=o.data("refresh"),u=o.attr("href");if("undefined"!=typeof s)return void o.untouch().touch(function(e){return i(o)?"soft"==s?(e.preventDefault(),void t.refresh()):void(u?document.location.href=u:t.refresh(!1)):void 0});if(u&&0===u.indexOf(e)){a=u.substr(e.length),o.untouch().touch(function(e){e.preventDefault(),i(o)&&t.redirect(a)}).removeClass("selected");var c=new RegExp("^"+a.replace("/","/")+"(/.*)?$");(a==r||""!==r&&c.test(r)||a==n&&""===r)&&o.addClass("selected")}})},function(){"use strict";var t=function(){},e=864e5;t.NEVER_EXPIRE="neverExpire",t.clear=function(e){t.write(e,"",-1)},t.write=function(n,r,i){var a,o;i?(i==t.NEVER_EXPIRE?o=new Date(2147483646e3):(o=new Date,o.setTime(o.getTime()+i*e)),a="; expires="+o.toGMTString()):a="",document.cookie=n+"="+r+a+"; path=/"},t.read=function(t){var e,n=t+"=",r=document.cookie.split(";"),i=0;for(i=0;in?"":e.substr(n+1),i=r.indexOf("#"),r=0>i?r:r.substring(0,i),a=r.split("&");for(s in a)o=a[s].split("="),t[o[0]]=o[1];return t},namespace("Canteen").LocationUtils=t}(),function(t){"use strict";var e=function(){};e.ERROR="error",e.DISABLED="disabled",e.SELECTED="selected",e.REQUIRED="required",e.CONFIRM="confirm",e.FEEDBACK_ERROR="formError",e.FEEDBACK_SUCCESS="formSuccess",e.destroy=function(){$("input."+e.CONFIRM+", button."+e.CONFIRM).untouch(),$("form").resetForm().off("submit"),$(":submit").untouch()},e.setup=function(n,r){for(var i=new Array("text","button","reset","submit","checkbox","image","radio","file","password","date"),a=function(t){var e=$(t.target).closest("form"),n=e.find(":submit").eq(0);13==t.keyCode&&n&&(t.stopImmediatePropagation(),t.preventDefault(),n.trigger("touchclick"))},o=0,s=i.length;s>o;o++)$("input[type='"+i[o]+"']").removeClass(i[o]).addClass(i[o]).keyup(a);$("input."+e.CONFIRM+", button."+e.CONFIRM).untouch().touch(function(t){var n=$(this).data(e.CONFIRM)||"Are you sure you wish to continue?",r=confirm(n);return r?void 0:(t.stopImmediatePropagation(),!1)}),$('input[name="formSession"]').remove();var u=function(t,r){var i=r.find("."+e.REQUIRED).removeClass(e.ERROR),a=i.filter(function(){return!this.value}).addClass(e.ERROR);if(a.length>0){var o=function(){$(this).removeClass(e.ERROR).off("keydown")};return a.untouch().on("touchclick focus keydown",o),!1}for(var s={},u=0,c=t.length;c>u;u++)s[t[u].name]=t[u].value;return s.form?!0:(n.refresh(!0,s),!1)},c=function(r,i,a,o){if(-1==r.search(/^{.*}$/))return void Debug.error(r);if(r=JSON.parse(r),"fatalError"==r.type)return void Debug.fatalError(r);var s=o.find('input[name="refresh"]'),u=s.data("async");return r.redirect!==t?void n.redirect(r.redirect,!1,!0,u):!r.ifError&&s.length&&"true"==s.val()?void n.refresh(u):void e.formFeedback(o,r.messages,r.ifError)},l={data:{async:!0},beforeSubmit:u,success:c};$(":submit").touch(function(t){this.name&&this.value&&(l.data={async:!0},l.data[this.name]=this.value);var e=$(this).closest("form");e.length>0&&(t.preventDefault(),e.ajaxSubmit(l))}),$("form").on("submit",function(t){t.preventDefault(),$(this).ajaxSubmit(l)})},e.formFeedback=function(t,n,r){$("ul."+e.FEEDBACK_ERROR+", ul."+e.FEEDBACK_SUCCESS).remove();for(var i=r?e.FEEDBACK_ERROR:e.FEEDBACK_SUCCESS,a='
    ',o=0,s=n.length;s>o;o++)a+="
  • "+n[o]+"
  • ";a+="
";var u=t.find("legend");u.length?u.after(a):t.prepend(a)},namespace("Canteen").Forms=e}(),function(t){"use strict";var e=function(t,e){this.initialize(t,e)},n=e.prototype;n.gatewayUrl=null,n.initialize=function(t,e){this.gatewayUrl=t,"/"!=t.charAt(t.length-1)&&(this.gatewayUrl+="/"),this.get(function(t){var n=null===t||"error"==t.type;if(n)throw"Couldn't connect to gateway";e(!n)},"time","get-server-time")},n.get=function(e,n,r,i){var a=this.gatewayUrl+n+"/"+r;i!==t&&(a+="/"+($.isArray(i)?i.join("/"):String(i))),$.get(a,function(t){if(!t||"null"==t)return void e(null);if(-1==t.search(/^{.*}$/))return Debug.error(t),void e(null);var n=JSON.parse(t);return"fatalError"==n.type?(Debug.fatalError(n),void e(null)):void e(n)})},n.destroy=function(){n=null},namespace("Canteen").Gateway=e}(),function(){"use strict";var t=function(){},e=t.prototype;e.site=null,e.active=!1,e.uri=null,e.enter=function(){},e.exit=function(){},e.refresh=function(){return!0},e.resize=function(){},namespace("Canteen").Page=t}(),function(t,e,n){"use strict";var r=Canteen.EventDispatcher,i=Canteen.LocationUtils,a=Canteen.Gateway,o=Canteen.Forms,s=Canteen.Page,u=function(){this.initialize()},c=u.prototype=new r,l=null,f=null,d=null,h=null,g=[],p=null,v={contentId:"#content",pageTitleId:"h1",pageLoadingId:"article",siteLoadingId:"body",loadingClass:"loading"};c.gateway=null,c.parameters=null,c.options={},u.ENTER="enter",u.EXIT="exit",u.READY="ready",u.LOADING="loading",u.LOADING_DONE="loadingDone",u.VERSION="1.1.4",c.initialize=function(){if(l)throw"Site has already been created. Use Canteen.Site.instance";l=this;var t=this.options=v;(Canteen.settings.clientEnabled===n||Canteen.settings.clientEnabled)&&(e(t.siteLoadingId).addClass(t.loadingClass),this.parameters=i.getParameters(),Debug.enabled=Canteen.settings.debug,d=1,h=Canteen.settings.uriRequest,this._enableHistory(!0),this._fixInternalLinks(),this.gateway=new a(Canteen.settings.gatewayPath,this._gatewayReady.bind(this)))},c._enableHistory=function(n){e(t).off("statechange"),n&&e(t).on("statechange",this._onStateChange.bind(this))},c.redirect=function(t,r,i,a){var o=Canteen.settings.basePath,s=e("
").html(e("title").html()).text();return r=r===n?!1:r,i=i===n?!1:i,a=a===n?!0:a,Canteen.settings.siteIndex!=t&&(o+=t),h==t?void(i&&this.refresh(a)):void(r?History.replaceState({state:d},s,o):(d++,History.pushState({state:d},s,o)))},Object.defineProperty(u,"instance",{get:function(){return l}}),Object.defineProperty(c,"currentState",{get:function(){return h}}),Object.defineProperty(c,"currentPage",{get:function(){return p}}),c.refresh=function(t,e){t=t===n?!0:t,t?this._updatePageContent(h,e):document.location.reload(!0)},c._fixInternalLinks=function(){e("a").internalLink(this),o.setup(this,"true"===this.parameters.debugForms)},c._onStateChange=function(){var t=History.getState(),e=t.url.split(Canteen.settings.baseUrl)[1];h!=e&&this._updatePageContent(e)},c._gatewayReady=function(t){t||Debug.error("Unable to setup gateway.");var n=this.options;e(n.siteLoadingId).removeClass(n.loadingClass),e(window).on("resize",this.resize.bind(this)),this.resize(),this.trigger(u.READY),this._enterPage()},c.resize=function(){p&&p.active&&p.resize()},c.addPage=function(t,e){e instanceof s?(e.site=this,g.push({uri:t,content:e})):Debug.warn("The page your trying to add needs to extend Canteen.Page")},c._enterPage=function(){var t=this.getPageByUri(h);t&&(p=t.content,p.uri=h,p.active=!0,p.enter(),this.resize()),this.trigger(u.ENTER,h)},c.getPageByUri=function(t){var e=g.length,n=0,r=null;if(0===e)return null;for(n=0;e>n;n++)if(r=g[n],r.uri===t||r.uri instanceof RegExp&&r.uri.test(t))return r;return null},c._updatePageContent=function(t,r){var i=this,a=this.options,s=this.getPageByUri(t),c=Canteen.settings.baseUrl+t;return this.trigger(u.EXIT,h),h=t,s&&s.content===p&&!s.content.refresh(t)?void(s.content.uri=t):(p&&(p.exit(),p.active=!1,p=null),o.destroy(),f=t,r=r===n?{}:r,r.async=!0,this.trigger(u.LOADING),e(a.pageLoadingId).removeClass(a.loadingClass).addClass(a.loadingClass),void e.post(c,r,function(r){if(t==f){if(f=null,i.trigger(u.LOADING_DONE),e(a.pageLoadingId).removeClass(a.loadingClass),!r||"null"==r)return void Debug.error("No data for "+t);if(-1==r.search(/^{.*}$/))return void Debug.error(r);var o=JSON.parse(r);if("fatalError"==o.type)return void Debug.fatalError(o);if(o.redirect!==n)return void i.redirect(o.redirect,!0);e("body").attr("id",o.pageId),e(a.pageTitleId).html(o.title),e(a.contentId).html(o.content),document.title=e("
").html(o.fullTitle).text(),e("meta[name='keywords']").attr("content",o.keywords),e("meta[name='description']").attr("content",o.description),i._fixInternalLinks(),i._enterPage()}}))},namespace("Canteen").Site=u,e(function(){new u})}(window,jQuery); \ No newline at end of file +!function(){"use strict";Function.prototype.bind||(Function.prototype.bind=function(t){var e,n,r=this;if("function"!=typeof r)throw new TypeError;return e=Array.prototype.slice.call(arguments,1),n=function(){if(this instanceof n){var i,a,s;return i=function(){},i.prototype=r.prototype,a=new i,s=r.apply(a,e.concat(Array.prototype.slice.call(arguments))),Object(s)===s?s:a}return r.apply(t,e.concat(Array.prototype.slice.call(arguments)))}})}(),function(){Debug.fatalError=function(t){var e=t.message+"\non "+t.file+" (code: "+t.code+")\n";if(t.stackTrace)for(var n=0;ns;s++)a=i[s],this._listeners[a]=this._listeners[a]||[],-1===this._callbackIndex(a,e)&&this._listeners[a].push(e);else if("array"===n(e))for(var u=0,l=e.length;l>u;u++)this.on(t,e[u]);return this},i.off=function(t,r){if(t===e)this._listeners=[];else if("array"===n(r))for(var i=0,a=r.length;a>i;i++)this.off(t,r[i]);else for(var s=t.split(" "),o=null,u=0,l=s.length;l>u;u++)if(o=s[u],this._listeners[o]=this._listeners[o]||[],r===e)this._listeners[o].length=0;else{var c=this._callbackIndex(o,r);-1!==c&&this._listeners[t].splice(c,1)}return this},i._callbackIndex=function(t,e){for(var n=0,r=this._listeners[t].length;r>n;n++)if(this._listeners[t][n]===e)return n;return-1},namespace("Canteen").EventDispatcher=r}(window),$.fn.touch=function(t){return this.on("click",!1).on("touchclick",t)},$.fn.untouch=function(){return this.off("touchclick")},$.fn.internalLink=function(t){var e=Canteen.settings.basePath,n=Canteen.settings.siteIndex,r=t.currentState,i=function(t){if(t.hasClass("confirm")){var e=t.data("confirm")||"Are you sure you wish to continue?",n=confirm(e);return n}return!0};return this.each(function(){var a,s=$(this),o=s.data("refresh"),u=s.attr("href");if("undefined"!=typeof o)return void s.untouch().touch(function(e){return i(s)?"soft"==o?(e.preventDefault(),void t.refresh()):void(u?document.location.href=u:t.refresh(!1)):void 0});if(u&&0===u.indexOf(e)){a=u.substr(e.length),s.untouch().touch(function(e){e.preventDefault(),i(s)&&t.redirect(a)}).removeClass("selected");var l=new RegExp("^"+a.replace("/","/")+"(/.*)?$");(a==r||""!==r&&l.test(r)||a==n&&""===r)&&s.addClass("selected")}})},function(){"use strict";var t=function(){},e=864e5;t.NEVER_EXPIRE="neverExpire",t.clear=function(e){t.write(e,"",-1)},t.write=function(n,r,i){var a,s;i?(i==t.NEVER_EXPIRE?s=new Date(2147483646e3):(s=new Date,s.setTime(s.getTime()+i*e)),a="; expires="+s.toGMTString()):a="",document.cookie=n+"="+r+a+"; path=/"},t.read=function(t){var e,n=t+"=",r=document.cookie.split(";"),i=0;for(i=0;in?"":e.substr(n+1),i=r.indexOf("#"),r=0>i?r:r.substring(0,i),a=r.split("&");for(o in a)s=a[o].split("="),t[s[0]]=s[1];return t},namespace("Canteen").LocationUtils=t}(),function(t){"use strict";var e=function(){};e.ERROR="error",e.DISABLED="disabled",e.SELECTED="selected",e.REQUIRED="required",e.CONFIRM="confirm",e.FEEDBACK_ERROR="formError",e.FEEDBACK_SUCCESS="formSuccess",e.destroy=function(){$("input."+e.CONFIRM+", button."+e.CONFIRM).untouch(),$("form").resetForm().off("submit"),$(":submit").untouch()},e.setup=function(n,r){for(var i=new Array("text","button","reset","submit","checkbox","image","radio","file","password","date"),a=function(t){var e=$(t.target).closest("form"),n=e.find(":submit").eq(0);13==t.keyCode&&n&&(t.stopImmediatePropagation(),t.preventDefault(),n.trigger("touchclick"))},s=0,o=i.length;o>s;s++)$("input[type='"+i[s]+"']").removeClass(i[s]).addClass(i[s]).keyup(a);$("input."+e.CONFIRM+", button."+e.CONFIRM).untouch().touch(function(t){var n=$(this).data(e.CONFIRM)||"Are you sure you wish to continue?",r=confirm(n);return r?void 0:(t.stopImmediatePropagation(),!1)}),$('input[name="formSession"]').remove();var u=function(t,r){var i=r.find("."+e.REQUIRED).removeClass(e.ERROR),a=i.filter(function(){return!this.value}).addClass(e.ERROR);if(a.length>0){var s=function(){$(this).removeClass(e.ERROR).off("keydown")};return a.untouch().on("touchclick focus keydown",s),!1}for(var o={},u=0,l=t.length;l>u;u++)o[t[u].name]=t[u].value;return o.form?!0:(n.refresh(!0,o),!1)},l=function(r,i,a,s){if(-1==r.search(/^{.*}$/))return void Debug.error(r);if(r=JSON.parse(r),"fatalError"==r.type)return void Debug.fatalError(r);var o=s.find('input[name="refresh"]'),u=o.data("async");return r.redirect!==t?void n.redirect(r.redirect,!1,!0,u):!r.ifError&&o.length&&"true"==o.val()?void n.refresh(u):void e.formFeedback(s,r.messages,r.ifError)},c={data:{async:!0},beforeSubmit:u,success:l};$(":submit").touch(function(t){this.name&&this.value&&(c.data={async:!0},c.data[this.name]=this.value);var e=$(this).closest("form");e.length>0&&(t.preventDefault(),e.ajaxSubmit(c))}),$("form").on("submit",function(t){t.preventDefault(),$(this).ajaxSubmit(c)})},e.formFeedback=function(t,n,r){$("ul."+e.FEEDBACK_ERROR+", ul."+e.FEEDBACK_SUCCESS).remove();for(var i=r?e.FEEDBACK_ERROR:e.FEEDBACK_SUCCESS,a='
    ',s=0,o=n.length;o>s;s++)a+="
  • "+n[s]+"
  • ";a+="
";var u=t.find("legend");u.length?u.after(a):t.prepend(a)},namespace("Canteen").Forms=e}(),function(t){"use strict";var e=function(t,e){this.initialize(t,e)},n=e.prototype;n.gatewayUrl=null,n.initialize=function(t,e){this.gatewayUrl=t,"/"!=t.charAt(t.length-1)&&(this.gatewayUrl+="/"),this.get(function(t){var n=null===t||"error"==t.type;if(n)throw"Couldn't connect to gateway";e(!n)},"time","get-server-time")},n.get=function(e,n,r,i){var a=this.gatewayUrl+n+"/"+r;i!==t&&(a+="/"+($.isArray(i)?i.join("/"):String(i))),$.get(a,function(t){if(!t||"null"==t)return void e(null);if(-1==t.search(/^{.*}$/))return Debug.error(t),void e(null);var n=JSON.parse(t);return"fatalError"==n.type?(Debug.fatalError(n),void e(null)):void e(n)})},n.destroy=function(){n=null},namespace("Canteen").Gateway=e}(),function(){"use strict";var t=function(){},e=t.prototype;e.site=null,e.active=!1,e.uri=null,e.enter=function(){},e.exit=function(){},e.refresh=function(){return!0},e.resize=function(){},namespace("Canteen").Page=t}(),function(t,e,n){"use strict";var r=Canteen.EventDispatcher,i=Canteen.LocationUtils,a=Canteen.Gateway,s=Canteen.Forms,o=Canteen.Page,u=function(){this.initialize()},l=u.prototype=new r,c=null,f=null,h=null,d=null,g=[],p=null,v={contentId:"#content",pageTitleId:"h1",pageLoadingId:"article",siteLoadingId:"body",loadingClass:"loading"};l.currentState=null,l.currentPage=null,l.gateway=null,l.parameters=null,l.options={},u.ENTER="enter",u.EXIT="exit",u.READY="ready",u.LOADING="loading",u.LOADING_DONE="loadingDone",u.VERSION="1.1.5",u.instance=null,l.initialize=function(){if(c)throw"Site has already been created. Use Canteen.Site.instance";u.instance=c=this;var t=this.options=v;(Canteen.settings.clientEnabled===n||Canteen.settings.clientEnabled)&&(e(t.siteLoadingId).addClass(t.loadingClass),this.parameters=i.getParameters(),Debug.enabled=Canteen.settings.debug,h=1,this.currentState=d=Canteen.settings.uriRequest,this._enableHistory(!0),this._fixInternalLinks(),this.gateway=new a(Canteen.settings.gatewayPath,this._gatewayReady.bind(this)))},l._enableHistory=function(n){e(t).off("statechange"),n&&e(t).on("statechange",this._onStateChange.bind(this))},l.redirect=function(t,r,i,a){var s=Canteen.settings.basePath,o=e("
").html(e("title").html()).text();return r=r===n?!1:r,i=i===n?!1:i,a=a===n?!0:a,Canteen.settings.siteIndex!=t&&(s+=t),d==t?void(i&&this.refresh(a)):void(r?History.replaceState({state:h},o,s):(h++,History.pushState({state:h},o,s)))},l.refresh=function(t,e){t=t===n?!0:t,t?this._updatePageContent(d,e):document.location.reload(!0)},l._fixInternalLinks=function(){e("a").internalLink(this),s.setup(this,"true"===this.parameters.debugForms)},l._onStateChange=function(){var t=History.getState(),e=t.url.split(Canteen.settings.baseUrl)[1];d!=e&&this._updatePageContent(e)},l._gatewayReady=function(t){t||Debug.error("Unable to setup gateway.");var n=this.options;e(n.siteLoadingId).removeClass(n.loadingClass),e(window).on("resize",this.resize.bind(this)),this.resize(),this.trigger(u.READY),this._enterPage()},l.resize=function(){p&&p.active&&p.resize()},l.addPage=function(t,e){e instanceof o?(e.site=this,g.push({uri:t,content:e})):Debug.warn("The page your trying to add needs to extend Canteen.Page")},l._enterPage=function(){var t=this.getPageByUri(d);t&&(this.currentPage=p=t.content,p.uri=d,p.active=!0,p.enter(),this.resize()),this.trigger(u.ENTER,d)},l.getPageByUri=function(t){var e=g.length,n=0,r=null;if(0===e)return null;for(n=0;e>n;n++)if(r=g[n],r.uri===t||r.uri instanceof RegExp&&r.uri.test(t))return r;return null},l._updatePageContent=function(t,r){var i=this,a=this.options,o=this.getPageByUri(t),l=Canteen.settings.baseUrl+t;return this.trigger(u.EXIT,d),this.currentState=d=t,o&&o.content===p&&!o.content.refresh(t)?void(o.content.uri=t):(p&&(p.exit(),p.active=!1,this.currentPage=p=null),s.destroy(),f=t,r=r===n?{}:r,r.async=!0,this.trigger(u.LOADING),e(a.pageLoadingId).removeClass(a.loadingClass).addClass(a.loadingClass),void e.post(l,r,function(r){if(t==f){if(f=null,i.trigger(u.LOADING_DONE),e(a.pageLoadingId).removeClass(a.loadingClass),!r||"null"==r)return void Debug.error("No data for "+t);if(-1==r.search(/^{.*}$/))return void Debug.error(r);var s=JSON.parse(r);if("fatalError"==s.type)return void Debug.fatalError(s);if(s.redirect!==n)return void i.redirect(s.redirect,!0);e("body").attr("id",s.pageId),e(a.pageTitleId).html(s.title),e(a.contentId).html(s.content),document.title=e("
").html(s.fullTitle).text(),e("meta[name='keywords']").attr("content",s.keywords),e("meta[name='description']").attr("content",s.description),i._fixInternalLinks(),i._enterPage()}}))},namespace("Canteen").Site=u,e(function(){new u})}(window,jQuery); \ No newline at end of file diff --git a/src/Canteen/Site.js b/src/Canteen/Site.js index 97dab0a..6c48d1e 100644 --- a/src/Canteen/Site.js +++ b/src/Canteen/Site.js @@ -116,7 +116,21 @@ */ loadingClass : 'loading' }; - + + /** + * The current state URI + * @property {String} currentState + * @readOnly + */ + p.currentState = null; + + /** + * Get the current page object + * @property {Canteen.Page} currentPage + * @readOnly + */ + p.currentPage = null; + /** * The instancee of the gateway * @property {Gateway} gateway @@ -173,6 +187,14 @@ * @readOnly */ Site.VERSION = "${version}"; + + /** + * Get the singleton instance of the site + * @property {Site} instance + * @static + * @readOnly + */ + Site.instance = null; /** * Constructor for the site @@ -184,7 +206,7 @@ throw "Site has already been created. Use Canteen.Site.instance"; } - _instance = this; + Site.instance = _instance = this; var opts = this.options = _defaultOptions; @@ -202,7 +224,7 @@ // Setup history _currentId = 1; - _currentState = Canteen.settings.uriRequest; + this.currentState = _currentState = Canteen.settings.uriRequest; this._enableHistory(true); this._fixInternalLinks(); @@ -281,43 +303,6 @@ } }; - /** - * Get the singleton instance of the site - * @property {Site} instance - * @static - * @readOnly - */ - Object.defineProperty(Site, "instance", { - get: function() - { - return _instance; - } - }); - - /** - * The current state URI - * @property {String} currentState - * @readOnly - */ - Object.defineProperty(p, "currentState", { - get: function() - { - return _currentState; - } - }); - - /** - * Get the current page object - * @property {Canteen.Page} currentPage - * @readOnly - */ - Object.defineProperty(p, "currentPage", { - get: function() - { - return _currentPage; - } - }); - /** * Do a refresh of the page content * @method refresh @@ -439,7 +424,7 @@ var page = this.getPageByUri(_currentState); if (page) { - _currentPage = page.content; + this.currentPage = _currentPage = page.content; _currentPage.uri = _currentState; _currentPage.active = true; _currentPage.enter(); @@ -495,7 +480,7 @@ this.trigger(Site.EXIT, _currentState); // Update the current uri - _currentState = uri; + this.currentState = _currentState = uri; // Special-case if we're already on the current page // but only the content needs to update @@ -510,7 +495,7 @@ { _currentPage.exit(); _currentPage.active = false; - _currentPage = null; + this.currentPage = _currentPage = null; } // Form cleanup