@zayesh/stay
Advanced tools
Comparing version 0.0.7 to 0.0.8
/** | ||
* stay build 08.07.2015 | ||
* stay build 09.07.2015 | ||
* | ||
@@ -183,3 +183,26 @@ * Copyright 2015 Raoul van Rueschen | ||
this.xhr = new XMLHttpRequest(); | ||
if(XMLHttpRequest) | ||
{ | ||
this.xhr = new XMLHttpRequest(); | ||
} | ||
else if(window.ActiveXObject) | ||
{ | ||
try | ||
{ | ||
this.xhr = new window.ActiveXObject("Msxml2.XMLHTTP"); | ||
} | ||
catch(e) | ||
{ | ||
try | ||
{ | ||
this.xhr = new window.ActiveXObject("Microsoft.XMLHTTP"); | ||
} | ||
catch(e) | ||
{ | ||
throw new Error("XMLHttpRequest functionality not available."); | ||
} | ||
} | ||
} | ||
this.xhr.responseType = "json"; | ||
this.xhr.addEventListener("readystatechange", function(event) { self._handleResponse(this, event); }); | ||
@@ -322,3 +345,3 @@ this.xhr.addEventListener("timeout", function() | ||
this.absolutePath.slice(0, this.absolutePath.length - 1) + this.infix + parts.pathname : | ||
this.absolutePath.replace(parts.pathname, this.infix + parts.pathname); | ||
this.absolutePath.replace(new RegExp(parts.pathname), this.infix + parts.pathname); | ||
@@ -490,11 +513,18 @@ this.eventNavigate.method = post ? "POST" : "GET"; | ||
{ | ||
try | ||
if(xhr.response !== undefined) | ||
{ | ||
response = JSON.parse(xhr.responseText); | ||
response = xhr.response; | ||
} | ||
catch(e) | ||
else | ||
{ | ||
response.title = "Parse Error"; | ||
response[this.responseFields[0]] = Stay.Error.UNPARSABLE; | ||
console.log(e); | ||
try | ||
{ | ||
response = JSON.parse(xhr.responseText); | ||
} | ||
catch(e) | ||
{ | ||
response.title = "Parse Error"; | ||
response[this.responseFields[0]] = Stay.Error.UNPARSABLE; | ||
console.log(e); | ||
} | ||
} | ||
@@ -501,0 +531,0 @@ |
/** | ||
* stay build 08.07.2015 | ||
* stay build 09.07.2015 | ||
* | ||
@@ -18,2 +18,2 @@ * Copyright 2015 Raoul van Rueschen | ||
*/ | ||
!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,b.Stay=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){"use strict";function d(){this._listeners={}}d.prototype.addEventListener=function(a,b){void 0===this._listeners[a]&&(this._listeners[a]=[]),-1===this._listeners[a].indexOf(b)&&this._listeners[a].push(b)},d.prototype.hasEventListener=function(a,b){return void 0!==this._listeners[a]&&-1!==this._listeners[a].indexOf(b)},d.prototype.removeEventListener=function(a,b){var c,d=this._listeners,e=d[a];void 0!==e&&(c=e.indexOf(b),-1!==c&&e.splice(c,1))},d.prototype.dispatchEvent=function(a){var b,c,d=this._listeners,e=d[a.type];if(void 0!==e)for(a.target=this,b=0,c=e.length;c>b;++b)e[b].call(this,a)},b.exports=d},{}],2:[function(a,b,c){"use strict";function d(a){var b=document.createElement("a");return b.href=a,b}function Stay(a){var b=this;e.call(this),this.responseFields=["main","navigation","footer"],this.infix="/json",this.timeoutPost=6e4,this.timeoutGet=5e3,this.autoUpdate=!0,void 0!==a&&(void 0!==a.responseFields&&(this.responseFields=a.responseFields),void 0!==a.infix&&(this.infix=a.infix),void 0!==a.timeoutPost&&(this.timeoutPost=a.timeoutPost),void 0!==a.timeoutGet&&(this.timeoutGet=a.timeoutGet),void 0!==a.autoUpdate&&(this.autoUpdate=a.autoUpdate)),this.locked=!1,this.backForward=!1,this.absolutePath=null,this.containers=[],this.intermediateContainer=null,this.navigationListeners=[],this.eventNavigate={type:"navigate"},this.eventReceive={type:"receive",response:null},this.eventLoad={type:"load"},this.xhr=new XMLHttpRequest,this.xhr.addEventListener("readystatechange",function(a){b._handleResponse(this,a)}),this.xhr.addEventListener("timeout",function(){var a={};b.responseFields.length&&(a.title="Timeout Error",a[b.responseFields[0]]=Stay.Error.TIMEOUT,b.locked=!0,b.update(a))}),window.addEventListener("popstate",function(a){b.locked||null===a.state||(b.backForward=!0,b._navigate({href:a.state.url}))}),this._switchPage=function(a){var c=void 0!==a.preventDefault,d=!1;return"submit"===a.type?d=!0:a.metaKey||a.shiftKey||a.altKey||a.ctrlKey||(void 0!==a.which?d=1===a.which:void 0!==a.button&&(d=0===a.button)),d&&(c&&a.preventDefault(),b.locked||b._navigate(this)),!(d&&!c)},this._updateListeners()}var e=a("@zayesh/eventdispatcher"),f="/";Stay.prototype=Object.create(e.prototype),Stay.prototype.constructor=Stay,Stay.prototype.addResponseField=function(a){-1===this.responseFields.indexOf(a)&&this.responseFields.push(a)},Stay.prototype.removeResponseField=function(a){var b=this.responseFields.indexOf(a);-1!==b&&this.responseFields.splice(b,1)},Stay.prototype._navigate=function(a){var b,c,e,g=!1;a.action?(this.absolutePath=a.action,b=new FormData(a),g=!0):this.absolutePath=a.href,c=d(this.absolutePath),this.locked=!0,e=c.pathname===f?this.absolutePath.slice(0,this.absolutePath.length-1)+this.infix+c.pathname:this.absolutePath.replace(c.pathname,this.infix+c.pathname),this.eventNavigate.method=g?"POST":"GET",this.dispatchEvent(this.eventNavigate),g?(this.xhr.open("POST",e,!0),this.xhr.timeout=this.timeoutPost,this.xhr.send(b)):(this.xhr.open("GET",e,!0),this.xhr.timeout=this.timeoutGet,this.xhr.send())},Stay.prototype._updateView=function(a){var b,c,d,e,f=!1;if(null===this.intermediateContainer)this.intermediateContainer=document.createElement("div");else for(;this.intermediateContainer.children.length>0;)this.intermediateContainer.removeChild(this.intermediateContainer.children[0]);for(b=0,c=this.responseFields.length;c>b;++b)if(d=this.containers[this.responseFields[b]],d||(d=this.containers[this.responseFields[b]]=document.getElementById(this.responseFields[b])),e=a[this.responseFields[b]]){for(;d.children.length>0;)d.removeChild(d.children[0]);for(this.intermediateContainer.innerHTML=e;this.intermediateContainer.children.length>0;)d.appendChild(this.intermediateContainer.children[0]);f=!0}f&&this._updateListeners()},Stay.prototype._updateListeners=function(){var a,b,c=this,d=document.getElementsByTagName("a"),e=document.getElementsByTagName("form");for(a=0,b=this.navigationListeners.length;b>a;++a)this.navigationListeners[a][0].removeEventListener(this.navigationListeners[a][1],c._switchPage);for(a=0,b=d.length;b>a;++a)d[a].addEventListener("click",c._switchPage),this.navigationListeners.push([d[a],"click"]);for(a=0,b=e.length;b>a;++a)e[a].addEventListener("submit",c._switchPage),this.navigationListeners.push([e[a],"submit"])},Stay.prototype.update=function(a){this._updateView(a),document.title=a.title,a.url&&(this.absolutePath=a.url.replace(this.infix,"")),this.backForward?this.backForward=!1:history.pushState({url:this.absolutePath},a.title,this.absolutePath),this.eventReceive.response=null,this.dispatchEvent(this.eventLoad),this.locked=!1},Stay.prototype._handleResponse=function(a){var b={};if(0===this.responseFields.length)b.title="Setup Error",b[this.responseFields[0]]=Stay.Error.NO_RESPONSE_FIELDS;else if(4===a.readyState){try{b=JSON.parse(a.responseText)}catch(c){b.title="Parse Error",b[this.responseFields[0]]=Stay.Error.UNPARSABLE,console.log(c)}b.url=a.responseURL,this.eventReceive.response=b,this.dispatchEvent(this.eventReceive),this.autoUpdate&&this.update(b)}},Stay.Error=Object.freeze({TIMEOUT:"<p>The server didn't respond in time. Please try again later!</p>",UNPARSABLE:"<p>The received content could not be parsed.</p>",NO_RESPONSE_FIELDS:"<p>No response fields have been specified.</p>"}),b.exports=Stay},{"@zayesh/eventdispatcher":1}]},{},[2])(2)}); | ||
!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,b.Stay=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){"use strict";function d(){this._listeners={}}d.prototype.addEventListener=function(a,b){void 0===this._listeners[a]&&(this._listeners[a]=[]),-1===this._listeners[a].indexOf(b)&&this._listeners[a].push(b)},d.prototype.hasEventListener=function(a,b){return void 0!==this._listeners[a]&&-1!==this._listeners[a].indexOf(b)},d.prototype.removeEventListener=function(a,b){var c,d=this._listeners,e=d[a];void 0!==e&&(c=e.indexOf(b),-1!==c&&e.splice(c,1))},d.prototype.dispatchEvent=function(a){var b,c,d=this._listeners,e=d[a.type];if(void 0!==e)for(a.target=this,b=0,c=e.length;c>b;++b)e[b].call(this,a)},b.exports=d},{}],2:[function(a,b,c){"use strict";function d(a){var b=document.createElement("a");return b.href=a,b}function Stay(a){var b=this;if(e.call(this),this.responseFields=["main","navigation","footer"],this.infix="/json",this.timeoutPost=6e4,this.timeoutGet=5e3,this.autoUpdate=!0,void 0!==a&&(void 0!==a.responseFields&&(this.responseFields=a.responseFields),void 0!==a.infix&&(this.infix=a.infix),void 0!==a.timeoutPost&&(this.timeoutPost=a.timeoutPost),void 0!==a.timeoutGet&&(this.timeoutGet=a.timeoutGet),void 0!==a.autoUpdate&&(this.autoUpdate=a.autoUpdate)),this.locked=!1,this.backForward=!1,this.absolutePath=null,this.containers=[],this.intermediateContainer=null,this.navigationListeners=[],this.eventNavigate={type:"navigate"},this.eventReceive={type:"receive",response:null},this.eventLoad={type:"load"},XMLHttpRequest)this.xhr=new XMLHttpRequest;else if(window.ActiveXObject)try{this.xhr=new window.ActiveXObject("Msxml2.XMLHTTP")}catch(c){try{this.xhr=new window.ActiveXObject("Microsoft.XMLHTTP")}catch(c){throw new Error("XMLHttpRequest functionality not available.")}}this.xhr.responseType="json",this.xhr.addEventListener("readystatechange",function(a){b._handleResponse(this,a)}),this.xhr.addEventListener("timeout",function(){var a={};b.responseFields.length&&(a.title="Timeout Error",a[b.responseFields[0]]=Stay.Error.TIMEOUT,b.locked=!0,b.update(a))}),window.addEventListener("popstate",function(a){b.locked||null===a.state||(b.backForward=!0,b._navigate({href:a.state.url}))}),this._switchPage=function(a){var c=void 0!==a.preventDefault,d=!1;return"submit"===a.type?d=!0:a.metaKey||a.shiftKey||a.altKey||a.ctrlKey||(void 0!==a.which?d=1===a.which:void 0!==a.button&&(d=0===a.button)),d&&(c&&a.preventDefault(),b.locked||b._navigate(this)),!(d&&!c)},this._updateListeners()}var e=a("@zayesh/eventdispatcher"),f="/";Stay.prototype=Object.create(e.prototype),Stay.prototype.constructor=Stay,Stay.prototype.addResponseField=function(a){-1===this.responseFields.indexOf(a)&&this.responseFields.push(a)},Stay.prototype.removeResponseField=function(a){var b=this.responseFields.indexOf(a);-1!==b&&this.responseFields.splice(b,1)},Stay.prototype._navigate=function(a){var b,c,e,g=!1;a.action?(this.absolutePath=a.action,b=new FormData(a),g=!0):this.absolutePath=a.href,c=d(this.absolutePath),this.locked=!0,e=c.pathname===f?this.absolutePath.slice(0,this.absolutePath.length-1)+this.infix+c.pathname:this.absolutePath.replace(new RegExp(c.pathname),this.infix+c.pathname),this.eventNavigate.method=g?"POST":"GET",this.dispatchEvent(this.eventNavigate),g?(this.xhr.open("POST",e,!0),this.xhr.timeout=this.timeoutPost,this.xhr.send(b)):(this.xhr.open("GET",e,!0),this.xhr.timeout=this.timeoutGet,this.xhr.send())},Stay.prototype._updateView=function(a){var b,c,d,e,f=!1;if(null===this.intermediateContainer)this.intermediateContainer=document.createElement("div");else for(;this.intermediateContainer.children.length>0;)this.intermediateContainer.removeChild(this.intermediateContainer.children[0]);for(b=0,c=this.responseFields.length;c>b;++b)if(d=this.containers[this.responseFields[b]],d||(d=this.containers[this.responseFields[b]]=document.getElementById(this.responseFields[b])),e=a[this.responseFields[b]]){for(;d.children.length>0;)d.removeChild(d.children[0]);for(this.intermediateContainer.innerHTML=e;this.intermediateContainer.children.length>0;)d.appendChild(this.intermediateContainer.children[0]);f=!0}f&&this._updateListeners()},Stay.prototype._updateListeners=function(){var a,b,c=this,d=document.getElementsByTagName("a"),e=document.getElementsByTagName("form");for(a=0,b=this.navigationListeners.length;b>a;++a)this.navigationListeners[a][0].removeEventListener(this.navigationListeners[a][1],c._switchPage);for(a=0,b=d.length;b>a;++a)d[a].addEventListener("click",c._switchPage),this.navigationListeners.push([d[a],"click"]);for(a=0,b=e.length;b>a;++a)e[a].addEventListener("submit",c._switchPage),this.navigationListeners.push([e[a],"submit"])},Stay.prototype.update=function(a){this._updateView(a),document.title=a.title,a.url&&(this.absolutePath=a.url.replace(this.infix,"")),this.backForward?this.backForward=!1:history.pushState({url:this.absolutePath},a.title,this.absolutePath),this.eventReceive.response=null,this.dispatchEvent(this.eventLoad),this.locked=!1},Stay.prototype._handleResponse=function(a){var b={};if(0===this.responseFields.length)b.title="Setup Error",b[this.responseFields[0]]=Stay.Error.NO_RESPONSE_FIELDS;else if(4===a.readyState){if(void 0!==a.response)b=a.response;else try{b=JSON.parse(a.responseText)}catch(c){b.title="Parse Error",b[this.responseFields[0]]=Stay.Error.UNPARSABLE,console.log(c)}b.url=a.responseURL,this.eventReceive.response=b,this.dispatchEvent(this.eventReceive),this.autoUpdate&&this.update(b)}},Stay.Error=Object.freeze({TIMEOUT:"<p>The server didn't respond in time. Please try again later!</p>",UNPARSABLE:"<p>The received content could not be parsed.</p>",NO_RESPONSE_FIELDS:"<p>No response fields have been specified.</p>"}),b.exports=Stay},{"@zayesh/eventdispatcher":1}]},{},[2])(2)}); |
@@ -5,3 +5,3 @@ { | ||
"keywords": ["xhr", "xmlhttprequest", "ajax", "web", "application"], | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"homepage": "https://github.com/vanruesc/stay", | ||
@@ -8,0 +8,0 @@ "author": { |
@@ -70,3 +70,26 @@ "use strict"; | ||
this.xhr = new XMLHttpRequest(); | ||
if(XMLHttpRequest) | ||
{ | ||
this.xhr = new XMLHttpRequest(); | ||
} | ||
else if(window.ActiveXObject) | ||
{ | ||
try | ||
{ | ||
this.xhr = new window.ActiveXObject("Msxml2.XMLHTTP"); | ||
} | ||
catch(e) | ||
{ | ||
try | ||
{ | ||
this.xhr = new window.ActiveXObject("Microsoft.XMLHTTP"); | ||
} | ||
catch(e) | ||
{ | ||
throw new Error("XMLHttpRequest functionality not available."); | ||
} | ||
} | ||
} | ||
this.xhr.responseType = "json"; | ||
this.xhr.addEventListener("readystatechange", function(event) { self._handleResponse(this, event); }); | ||
@@ -209,3 +232,3 @@ this.xhr.addEventListener("timeout", function() | ||
this.absolutePath.slice(0, this.absolutePath.length - 1) + this.infix + parts.pathname : | ||
this.absolutePath.replace(parts.pathname, this.infix + parts.pathname); | ||
this.absolutePath.replace(new RegExp(parts.pathname), this.infix + parts.pathname); | ||
@@ -377,11 +400,18 @@ this.eventNavigate.method = post ? "POST" : "GET"; | ||
{ | ||
try | ||
if(xhr.response !== undefined) | ||
{ | ||
response = JSON.parse(xhr.responseText); | ||
response = xhr.response; | ||
} | ||
catch(e) | ||
else | ||
{ | ||
response.title = "Parse Error"; | ||
response[this.responseFields[0]] = Stay.Error.UNPARSABLE; | ||
console.log(e); | ||
try | ||
{ | ||
response = JSON.parse(xhr.responseText); | ||
} | ||
catch(e) | ||
{ | ||
response.title = "Parse Error"; | ||
response[this.responseFields[0]] = Stay.Error.UNPARSABLE; | ||
console.log(e); | ||
} | ||
} | ||
@@ -388,0 +418,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40965
974